Copyright © https://mongoose-os.com

Mongoose OS Forum

frame

GPIO Interrupt Handler with custom function

hello. I am trying to make Button Switch Handler to control LED with button. I am using my own code; it looks like:

/* read( 12 ) --> The statement 'The switch is on' currently is  FALSE
 [Jun  5 16:56:37.048]   at init.js:20
 [Jun  5 16:56:37.056] MJS error: failed to call FFIed function: actual arg #2 is not a function, but undefined
*/
load('api_rpc.js');
load('api_config.js');
load('api_events.js');
load('api_gpio.js');

let pinButton = Cfg.get('pins.btn_switch');
GPIO.set_mode(pinButton, GPIO.MODE_INPUT);
GPIO.set_pull(pinButton, GPIO.PULL_DOWN); 

let pinLed = Cfg.get('pins.led');
GPIO.set_mode(pinLed, GPIO.MODE_OUTPUT); 

function switchButtonStateChangedCallback(){
  let isSwitchOn = GPIO.read(pinButton);
  GPIO.write(pinLed, isSwitchOn);
  print("read(", pinButton, ") --> The statement 'The switch is on' currently is " , isSwitchOn === 1 ? "TRUE" : "FALSE");
  return GPIO.read(pinButton) === 1 ? true : false;
}

GPIO.set_int_handler(pinButton, GPIO.INT_EDGE_ANY, switchButtonStateChangedCallback(), null);
GPIO.enable_int(pinButton);

PS - there were any build or flash errors;
So, the problem is when I want to call my function switchButtonStateChangedCallback() it doesn't work;
here is the mos console output:

[Jun  7 15:47:15.362] rBoot v1.2.1-cesanta1 - richardaburton@gmail.com
[Jun  7 15:47:15.364] Flash Size:   32 Mbit
[Jun  7 15:47:15.366] Flash Mode:   DIO
[Jun  7 15:47:15.368] Flash Speed:  40 MHz
[Jun  7 15:47:15.370] rBoot Option: Big flash
[Jun  7 15:47:15.370]
[Jun  7 15:47:15.407] Booting rom 0 (0x100000).
[Jun  7 15:47:15.464]       n   p  o|  l $ll`  c 2    2r  l n   N   l    r $     mode : softAP(de:4f:22:10:2a:3f)
[Jun  7 15:47:15.465] ad
[Jun  7 15:47:15.474] esp_mgos_init2       SMBR.Ex01.ButtonSwitchHandler 1.2 (20180607-123739/SwitchCallback@de1353dd+)
[Jun  7 15:47:15.481] esp_mgos_init2       Mongoose OS 2018060616 (20180606-162012/master@5c635a4e)
[Jun  7 15:47:15.488] esp_mgos_init2       CPU: ESP8266EX, 160 MHz, RAM: 52976 total, 50236 free
[Jun  7 15:47:15.492] esp_mgos_init2       SDK 2.2.1(1247cc5); flash: 4M
[Jun  7 15:47:15.497] esp_print_reset_info Reset cause: 6 (sys reset)
[Jun  7 15:47:15.501] mg_lwip_if_init      Mongoose 6.11, LwIP 1.4.1
[Jun  7 15:47:15.504] mg_ssl_if_init       mbed TLS 2.8.0
[Jun  7 15:47:15.509] mgos_vfs_dev_open    sysflash () -> 0x3ffefdb4
[Jun  7 15:47:15.519] mgos_vfs_mount       Mount SPIFFS @ / (dev 0x3ffefdb4, opts {"addr": 32768, "size": 262144}) -> 0x3ffefdc4
[Jun  7 15:47:15.579] mgos_vfs_mount       /: size 233681, used: 82077, free: 151604
[Jun  7 15:47:15.658] mgos_sys_config_init MAC: DE4F22102A3F
[Jun  7 15:47:15.662] mgos_sys_config_init WDT: 30 seconds
[Jun  7 15:47:15.669] mg_rpc_channel_uart  0x3fff0d24 UART0
[Jun  7 15:47:15.677] mgos_init            Init done, RAM: 52976 total, 45656 free, 45664 min free
[Jun  7 15:47:15.969] read( 12 ) --> The statement 'The switch is on' currently is  TRUE
[Jun  7 15:47:15.975]   at init.js:24
[Jun  7 15:47:15.982] MJS error: failed to call FFIed function: actual arg #2 is not a function, but null
[Jun  7 15:47:15.991] mongoose_poll        New heap free LWM: 38296

I will be very grateful.

Comments

  • tripflextripflex Orlando, FL

    It tells you exactly where the error is:

    [Jun  7 15:47:15.975]   at init.js:24
    [Jun  7 15:47:15.982] MJS error: failed to call FFIed function: actual arg #2 is not a function, but null
    

    When specifying the callback you don't want to actually call the function, you just want to tell it what function to call:

    GPIO.set_int_handler(pinButton, GPIO.INT_EDGE_ANY, switchButtonStateChangedCallback, null);
    
Sign In or Register to comment.