RAK4631 analogue user button on input 31
This commit is contained in:
@@ -209,40 +209,50 @@ void UITask::userLedHandler() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void UITask::buttonHandler() {
|
void UITask::buttonHandler() {
|
||||||
#ifdef PIN_USER_BTN
|
#ifdef PIN_USER_BTN || PIN_USER_BTN_ANA
|
||||||
static int prev_btn_state = !USER_BTN_PRESSED;
|
static int prev_btn_state = !USER_BTN_PRESSED;
|
||||||
static unsigned long btn_state_change_time = 0;
|
static int prev_btn_state_ana = !USER_BTN_PRESSED;
|
||||||
static unsigned long next_read = 0;
|
static unsigned long btn_state_change_time = 0;
|
||||||
int cur_time = millis();
|
static unsigned long next_read = 0;
|
||||||
if (cur_time >= next_read) {
|
int cur_time = millis();
|
||||||
int btn_state = digitalRead(PIN_USER_BTN);
|
if (cur_time >= next_read) {
|
||||||
if (btn_state != prev_btn_state) {
|
int btn_state = 0;
|
||||||
if (btn_state == USER_BTN_PRESSED) { // pressed?
|
int btn_state_ana = 0;
|
||||||
if (_display != NULL) {
|
#ifdef PIN_USER_BTN
|
||||||
if (_display->isOn()) {
|
btn_state = digitalRead(PIN_USER_BTN);
|
||||||
clearMsgPreview();
|
#endif
|
||||||
} else {
|
#ifdef PIN_USER_BTN_ANA
|
||||||
_display->turnOn();
|
btn_state_ana = (analogRead(PIN_USER_BTN_ANA) < 20); // analogRead returns a value hopefully below 20 when button is pressed.
|
||||||
_need_refresh = true;
|
#endif
|
||||||
|
//Serial.println(analogRead(PIN_USER_BTN_ANA));
|
||||||
|
if (btn_state != prev_btn_state || btn_state_ana != prev_btn_state_ana) { // check for either digital or analogue button change of state
|
||||||
|
if (btn_state == USER_BTN_PRESSED || btn_state_ana == USER_BTN_PRESSED) { // pressed?
|
||||||
|
if (_display != NULL) {
|
||||||
|
if (_display->isOn()) {
|
||||||
|
clearMsgPreview();
|
||||||
|
} else {
|
||||||
|
_display->turnOn();
|
||||||
|
_need_refresh = true;
|
||||||
|
}
|
||||||
|
_auto_off = cur_time + AUTO_OFF_MILLIS; // extend auto-off timer
|
||||||
|
}
|
||||||
|
} else { // unpressed ? check pressed time ...
|
||||||
|
if ((cur_time - btn_state_change_time) > 5000) {
|
||||||
|
#ifdef PIN_STATUS_LED
|
||||||
|
digitalWrite(PIN_STATUS_LED, LOW);
|
||||||
|
delay(10);
|
||||||
|
#endif
|
||||||
|
_board->powerOff();
|
||||||
}
|
}
|
||||||
_auto_off = cur_time + AUTO_OFF_MILLIS; // extend auto-off timer
|
|
||||||
}
|
|
||||||
} else { // unpressed ? check pressed time ...
|
|
||||||
if ((cur_time - btn_state_change_time) > 5000) {
|
|
||||||
#ifdef PIN_STATUS_LED
|
|
||||||
digitalWrite(PIN_STATUS_LED, LOW);
|
|
||||||
delay(10);
|
|
||||||
#endif
|
|
||||||
_board->powerOff();
|
|
||||||
}
|
}
|
||||||
|
btn_state_change_time = millis();
|
||||||
|
prev_btn_state = btn_state;
|
||||||
|
prev_btn_state_ana = btn_state_ana;
|
||||||
}
|
}
|
||||||
btn_state_change_time = millis();
|
next_read = millis() + 100; // 10 reads per second
|
||||||
prev_btn_state = btn_state;
|
|
||||||
}
|
}
|
||||||
next_read = millis() + 100; // 10 reads per second
|
#endif
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
void UITask::loop() {
|
void UITask::loop() {
|
||||||
buttonHandler();
|
buttonHandler();
|
||||||
|
|||||||
@@ -25,6 +25,9 @@ void RAK4631Board::begin() {
|
|||||||
#ifdef PIN_USER_BTN
|
#ifdef PIN_USER_BTN
|
||||||
pinMode(PIN_USER_BTN, INPUT_PULLUP);
|
pinMode(PIN_USER_BTN, INPUT_PULLUP);
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef PIN_USER_BTN_ANA
|
||||||
|
pinMode(PIN_USER_BTN_ANA, INPUT_PULLUP);
|
||||||
|
#endif
|
||||||
|
|
||||||
#if defined(PIN_BOARD_SDA) && defined(PIN_BOARD_SCL)
|
#if defined(PIN_BOARD_SDA) && defined(PIN_BOARD_SCL)
|
||||||
Wire.setPins(PIN_BOARD_SDA, PIN_BOARD_SCL);
|
Wire.setPins(PIN_BOARD_SDA, PIN_BOARD_SCL);
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ build_flags = ${nrf52840_base.build_flags}
|
|||||||
-I variants/rak4631
|
-I variants/rak4631
|
||||||
-D RAK_4631
|
-D RAK_4631
|
||||||
-D PIN_USER_BTN=9
|
-D PIN_USER_BTN=9
|
||||||
|
-D PIN_USER_BTN_ANA=31
|
||||||
-D PIN_BOARD_SCL=14
|
-D PIN_BOARD_SCL=14
|
||||||
-D PIN_BOARD_SDA=13
|
-D PIN_BOARD_SDA=13
|
||||||
-D PIN_OLED_RESET=-1
|
-D PIN_OLED_RESET=-1
|
||||||
|
|||||||
Reference in New Issue
Block a user