Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
396 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
|
||
#include "WebSocketToSerial.h" | ||
|
||
// These variables manages button feature | ||
btn_state_e _btn_State; // Button management state machine | ||
btn_action_e _btn_Action; // button action after press | ||
bool _btn_LongPress;// indicate a long press on button | ||
unsigned long _btn_StartTime;// when push started | ||
|
||
|
||
/* ====================================================================== | ||
Function: buttonManageState | ||
Purpose : manage button states (longpress actions/simple press/debounce) | ||
Input : pin button state (LOW or HIGH) we read just before | ||
Output : current state machine | ||
Comments: need to be called after push until state machine = BTN_WAIT_PUSH | ||
code inspired from one button library | ||
====================================================================== */ | ||
btn_state_e buttonManageState(uint8_t buttonLevel) | ||
{ | ||
// get current time in msecs | ||
unsigned long now = millis(); | ||
|
||
// Implementation of the state machine | ||
// waiting for menu pin being pressed. | ||
if (_btn_State == BTN_WAIT_PUSH) { | ||
// Putton pushed ? | ||
if (buttonLevel == BTN_PRESSED) { | ||
// now wait for release | ||
_btn_State = BTN_WAIT_RELEASE; | ||
// Set starting time | ||
_btn_StartTime = now; | ||
} | ||
|
||
// we're waiting for button released. | ||
} else if (_btn_State == BTN_WAIT_RELEASE) { | ||
// effectivly released ? | ||
if (buttonLevel == BTN_RELEASED) { | ||
// next step is to check debounce | ||
_btn_State = BTN_WAIT_DEBOUNCE; | ||
// still pressed, is it a Long press that is now starting ? | ||
} else if ((buttonLevel == BTN_PRESSED) && (now > _btn_StartTime + BTN_LONG_PUSH_DELAY)) { | ||
// Set long press state | ||
_btn_LongPress = true; | ||
|
||
// step to waiting long press release | ||
_btn_State = BTN_WAIT_LONG_RELEASE; | ||
} | ||
|
||
// waiting for being pressed timeout. | ||
} else if (_btn_State == BTN_WAIT_DEBOUNCE) { | ||
// do we got debounce time reached when released ? | ||
if (now > _btn_StartTime + BTN_DEBOUNCE_DELAY) | ||
_btn_Action = BTN_QUICK_PRESS; | ||
else | ||
_btn_Action = BTN_BAD_PRESS; | ||
|
||
// restart state machine | ||
_btn_State = BTN_WAIT_PUSH; | ||
|
||
// waiting for menu pin being release after long press. | ||
} else if (_btn_State == BTN_WAIT_LONG_RELEASE) { | ||
// are we released the long press ? | ||
if (buttonLevel == BTN_RELEASED) { | ||
// we're not anymore in a long press | ||
_btn_LongPress = false; | ||
|
||
// be sure to light off the blinking RGB led | ||
LedRGBOFF(); | ||
|
||
// We done, restart state machine | ||
_btn_State = BTN_WAIT_PUSH; | ||
} else { | ||
uint8_t sec_press; | ||
|
||
// for how long we have been pressed (in s) | ||
sec_press = ((now - _btn_StartTime)/1000L); | ||
|
||
// we're still in a long press | ||
_btn_LongPress = true; | ||
|
||
// We pressed button more than 7 sec | ||
if (sec_press >= 7 ) { | ||
// Led will be off | ||
_btn_Action = BTN_TIMEOUT; | ||
// We pressed button between 6 and 7 sec | ||
} else if (sec_press >= 6 ) { | ||
_btn_Action = BTN_PRESSED_67; | ||
// Prepare LED color | ||
LedRGBON(COLOR_RED); | ||
// We pressed button between 5 and 6 sec | ||
} else if (sec_press >= 5 ) { | ||
_btn_Action = BTN_PRESSED_56; | ||
LedRGBON(COLOR_YELLOW); | ||
// We pressed button between 4 and 5 sec | ||
} else if (sec_press >= 4 ) { | ||
_btn_Action = BTN_PRESSED_45; | ||
LedRGBON(COLOR_GREEN); | ||
// We pressed button between 3 and 4 sec | ||
} else if (sec_press >= 3 ) { | ||
_btn_Action = BTN_PRESSED_34; | ||
LedRGBON(COLOR_BLUE); | ||
// We pressed button between 2 and 3 sec | ||
} else if (sec_press >= 2 ) { | ||
_btn_Action = BTN_PRESSED_23; | ||
LedRGBON(COLOR_MAGENTA); | ||
// We pressed button between 1 and 2 sec | ||
} else if (sec_press >= 1 ) { | ||
_btn_Action = BTN_PRESSED_12; | ||
LedRGBON(COLOR_CYAN); | ||
} | ||
|
||
// manage the fast blinking | ||
// 20ms ON / 80ms OFF | ||
if (millis() % 100 < 50 ) { | ||
// Set Color | ||
#ifdef RGB_LED_PIN | ||
rgb_led.SetPixelColor(0, rgb_led_color); | ||
rgb_led.Show(); | ||
#endif | ||
} else { | ||
LedRGBOFF(); | ||
} | ||
} | ||
} | ||
|
||
// return the state machine we're in | ||
return (_btn_State); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
|
||
|
||
#ifndef _PUSHBUTTON_H | ||
#define _PUSHBUTTON_H | ||
|
||
// Switch button | ||
#define BTN_GPIO 14 | ||
#define BTN_DEBOUNCE_DELAY 100 | ||
#define BTN_LONG_PUSH_DELAY 1000 | ||
|
||
// Button pressed set pin port to LOW | ||
#define BTN_PRESSED LOW | ||
#define BTN_RELEASED HIGH | ||
|
||
// The button state machine when pressed | ||
typedef enum { | ||
BTN_WAIT_PUSH, | ||
BTN_WAIT_RELEASE, | ||
BTN_WAIT_DEBOUNCE, | ||
BTN_WAIT_LONG_RELEASE | ||
} | ||
btn_state_e; | ||
|
||
// The actions possibe with different button press | ||
typedef enum { | ||
BTN_NONE, // do nothing. | ||
BTN_BAD_PRESS, // button pressed lower than debounce time | ||
BTN_QUICK_PRESS, // button pressed with debounce OK | ||
BTN_PRESSED_12, // pressed between 1 and 2 seconds | ||
BTN_PRESSED_23, // pressed between 2 and 3 seconds | ||
BTN_PRESSED_34, // pressed between 3 and 4 seconds | ||
BTN_PRESSED_45, // pressed between 4 and 5 seconds | ||
BTN_PRESSED_56, // pressed between 5 and 6 seconds | ||
BTN_PRESSED_67, // pressed between 6 and 7 seconds | ||
BTN_TIMEOUT // Long press timeout | ||
} | ||
btn_action_e; | ||
|
||
|
||
// These variables manages button feature | ||
extern btn_state_e _btn_State; // Button management state machine | ||
extern btn_action_e _btn_Action; // button action after press | ||
extern bool _btn_LongPress;// indicate a long press on button | ||
extern unsigned long _btn_StartTime;// when push started | ||
|
||
// Exported functions | ||
btn_state_e buttonManageState(uint8_t buttonLevel); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
|
||
#include "WebSocketToSerial.h" | ||
|
||
radio_state_e _radio_state; | ||
bool radio_continuous_send = false; | ||
|
||
void radioInit(uint32_t baudrate) { | ||
/* | ||
// Close proper | ||
SERIAL_DEVICE.flush(); | ||
//SERIAL_DEVICE.end(); | ||
// enable auto baud rate (see RN2483 datasheet) | ||
SERIAL_DEVICE.begin(baudrate); | ||
SERIAL_DEVICE.write((byte) 0x00); | ||
SERIAL_DEVICE.flush(); | ||
SERIAL_DEVICE.write(0x55); | ||
SERIAL_DEVICE.flush(); | ||
SERIAL_DEVICE.write(0x0A); | ||
SERIAL_DEVICE.write(0x0D); | ||
*/ | ||
_radio_state = RADIO_IDLE; | ||
} | ||
|
||
// Send a radio command | ||
void radioExec( char * cmd) { | ||
// Set to blue immediatly | ||
LedRGBON(COLOR_BLUE, true); | ||
ws.textAll(cmd); | ||
execCommand(NULL, cmd); | ||
_radio_state = RADIO_WAIT_OK; | ||
} | ||
|
||
// Send a radio command | ||
void radioExec( PGM_P cmd) { | ||
String str = cmd; | ||
radioExec( str.c_str()); | ||
} | ||
|
||
// Send data | ||
bool radioSend(uint32_t value) { | ||
|
||
if (_radio_state != RADIO_IDLE) | ||
return false; | ||
|
||
// Set to BLUE immediatly | ||
LedRGBON(COLOR_BLUE, true); | ||
char cmd[32]; | ||
sprintf_P(cmd, PSTR("radio tx %08X"), value); | ||
ws.textAll(cmd); | ||
execCommand(NULL, cmd); | ||
_radio_state = RADIO_WAIT_OK_SEND; | ||
return true; | ||
} | ||
|
||
// Put RN2483 into listening mode | ||
void radioListen(void) { | ||
|
||
// Idle or previously listening ? | ||
if ( _radio_state==RADIO_IDLE || _radio_state==RADIO_LISTENING ) { | ||
char cmd[32]; | ||
|
||
// Set receive Watchdog to 1H | ||
//sprintf_P( cmd, PSTR("radio set wdt 3600000")); | ||
//ws.textAll(cmd); | ||
//execCommand(NULL, cmd); | ||
//delay(250); | ||
|
||
// Enable Receive mode | ||
sprintf_P( cmd, PSTR("radio rx 0")); | ||
ws.textAll(cmd); | ||
execCommand(NULL, cmd); | ||
|
||
// Wait ok listenning | ||
_radio_state = RADIO_WAIT_OK_LISTENING; | ||
} | ||
} | ||
|
||
// we received a RN2483 serial response | ||
bool radioResponse(String inputString) { | ||
// got OK | ||
if (inputString == "ok") { | ||
if (_radio_state == RADIO_WAIT_OK_LISTENING) { | ||
_radio_state = RADIO_LISTENING; | ||
} else if (_radio_state == RADIO_WAIT_OK_SEND) { | ||
_radio_state = RADIO_SENDING; | ||
} else { | ||
_radio_state = RADIO_IDLE; | ||
} | ||
|
||
// "radio_tx_ok" | ||
} else if (inputString == "radio_tx_ok") { | ||
_radio_state = RADIO_IDLE; | ||
|
||
// Set to GREEN immediatly | ||
LedRGBON(COLOR_GREEN, true); | ||
|
||
// "radio_rx data" | ||
} else if (inputString.startsWith("radio_rx ")) { | ||
// Stop Animation for being sure to start a full one | ||
LedRGBOFF(); | ||
// Set to GREEN immediatly | ||
LedRGBON(COLOR_GREEN, true); | ||
|
||
_radio_state = RADIO_RECEIVED_DATA; | ||
|
||
// got something to do | ||
return (true); | ||
|
||
// radio_err | ||
} else if (inputString == "radio_err") { | ||
// We were listening, restart listen (timed out) | ||
// not really an error | ||
if ( _radio_state == RADIO_LISTENING ) { | ||
radioListen(); | ||
} else { | ||
_radio_state = RADIO_ERROR; | ||
|
||
// Set to RED immediatly | ||
LedRGBON(COLOR_RED, true); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
// Manage radio state machine | ||
void radioManageState(btn_action_e btn) { | ||
static radio_state_e old_radio_state = _radio_state; | ||
|
||
// Action due to push button? | ||
if (btn != BTN_NONE) { | ||
if ( btn == BTN_QUICK_PRESS) { | ||
radioSend(millis()/1000); | ||
} else if ( btn == BTN_PRESSED_12) { | ||
LedRGBON(COLOR_CYAN, true); | ||
radioListen(); | ||
} else if ( btn == BTN_PRESSED_23) { | ||
if (!radio_continuous_send) { | ||
LedRGBON(COLOR_MAGENTA, true); | ||
radio_continuous_send = true; | ||
} else { | ||
LedRGBOFF(); | ||
radio_continuous_send = false; | ||
} | ||
} | ||
} | ||
|
||
// Check radio state changed ? | ||
if (_radio_state != old_radio_state) { | ||
old_radio_state = _radio_state; | ||
|
||
// Set Breathing to cyan when listening | ||
if (_radio_state == RADIO_LISTENING ) { | ||
LedRGBON(COLOR_CYAN); | ||
|
||
} else if (_radio_state == RADIO_RECEIVED_DATA) { | ||
_radio_state=RADIO_IDLE; | ||
// Listen back | ||
radioListen(); | ||
} else if (_radio_state == RADIO_SENDING) { | ||
LedRGBON(COLOR_RED, true); | ||
|
||
} else if (_radio_state == RADIO_IDLE) { | ||
//LedRGBOFF(); | ||
} | ||
} // Radio state changed | ||
} | ||
|
||
// get radio state machine | ||
radio_state_e radioState(void) { | ||
return _radio_state; | ||
} |
Oops, something went wrong.