Copyright © https://mongoose-os.com

Mongoose OS Forum

frame

Still can not call function from set_int_handler

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

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); 

let obj = 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, obj, null);
GPIO.enable_int(pinButton);

mos console gives me only error

[Jun  9 13:59:17.937] MJS error: parse error at line 24: [.set_int_h]

I have tried to do that without object obj:

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);

In this case, I dont get any errors but I do not get any debug from my Function(even if debug.level = 3);
how can i fix this problem? should i make static class?

Comments

  • nliviunliviu Romania

    The first version has a syntax error: missing a semicolon

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

    Did you define pins.btn_switch and pins.led in the config_schema: section of mos.yml?
    Example:

      - ["pins.led", 2]
      - ["pins.btn_switch", "i", 4, {Title: "Button switch"}]
    
Sign In or Register to comment.