implement commands from server
This commit is contained in:
parent
2878f12250
commit
3d1fd4792e
1 changed files with 32 additions and 26 deletions
58
src/main.cpp
58
src/main.cpp
|
|
@ -4,8 +4,9 @@
|
|||
|
||||
#define SHORT_PRESS_TIME 1
|
||||
#define LONG_PRESS_TIME 10
|
||||
#define LOCATE_LED_BLINK_TIME 1000
|
||||
|
||||
//TODO: Set the correct pins
|
||||
// TODO: Set the correct pins
|
||||
#define POWER_BUTTON_PIN 3
|
||||
#define RESET_BUTTON_PIN 4
|
||||
#define LOCATE_LED_PIN 5
|
||||
|
|
@ -19,6 +20,7 @@
|
|||
#define EEPROM_LONG_DELAY 0x03
|
||||
|
||||
const uint8_t deviceID = 1;
|
||||
bool locateLedEnabled = false;
|
||||
|
||||
RS485 rs485(&Serial, SEND_PIN, deviceID);
|
||||
|
||||
|
|
@ -53,50 +55,54 @@ void loop()
|
|||
{
|
||||
if (rs485.available())
|
||||
{
|
||||
byte deviceID = rs485.read();
|
||||
int command = rs485.read();
|
||||
int value = rs485.read();
|
||||
if (deviceID != EEPROM.read(EEPROM_ADDRESS))
|
||||
return;
|
||||
if (command == 0x01)
|
||||
{
|
||||
digitalWrite(POWER_BUTTON_PIN, HIGH);
|
||||
if (value == 0x01)
|
||||
{
|
||||
digitalWrite(POWER_BUTTON_PIN, HIGH);
|
||||
delay(EEPROM.read(EEPROM_SHORT_DELAY) * 1000);
|
||||
digitalWrite(POWER_BUTTON_PIN, LOW);
|
||||
}
|
||||
else if (value == 0x02)
|
||||
else
|
||||
{
|
||||
digitalWrite(RESET_BUTTON_PIN, HIGH);
|
||||
delay(EEPROM.read(EEPROM_SHORT_DELAY) * 1000);
|
||||
digitalWrite(RESET_BUTTON_PIN, LOW);
|
||||
delay(EEPROM.read(EEPROM_LONG_DELAY) * 1000);
|
||||
}
|
||||
digitalWrite(POWER_BUTTON_PIN, LOW);
|
||||
}
|
||||
else if (command == 0x02)
|
||||
{
|
||||
digitalWrite(POWER_BUTTON_PIN, HIGH);
|
||||
if (value == 0x01)
|
||||
{
|
||||
digitalWrite(POWER_BUTTON_PIN, HIGH);
|
||||
delay(EEPROM.read(EEPROM_LONG_DELAY) * 1000);
|
||||
digitalWrite(POWER_BUTTON_PIN, LOW);
|
||||
delay(EEPROM.read(EEPROM_SHORT_DELAY) * 1000);
|
||||
}
|
||||
else if (value == 0x02)
|
||||
else
|
||||
{
|
||||
digitalWrite(RESET_BUTTON_PIN, HIGH);
|
||||
delay(EEPROM.read(EEPROM_LONG_DELAY) * 1000);
|
||||
digitalWrite(RESET_BUTTON_PIN, LOW);
|
||||
}
|
||||
digitalWrite(POWER_BUTTON_PIN, LOW);
|
||||
}
|
||||
else if (command == 0x03)
|
||||
{
|
||||
EEPROM.write(EEPROM_I2C_ADDRESS, value);
|
||||
resetFunc(); // reset mcu, to apply new i2c address
|
||||
}
|
||||
else if (command == 0x04)
|
||||
{
|
||||
EEPROM.write(EEPROM_SHORT_DELAY, value);
|
||||
}
|
||||
else if (command == 0x05)
|
||||
{
|
||||
EEPROM.write(EEPROM_LONG_DELAY, value);
|
||||
}
|
||||
else
|
||||
locateLedEnabled = value == 0x01;
|
||||
}
|
||||
else if (command == 0x04)
|
||||
{
|
||||
EEPROM.write(EEPROM_ADDRESS, value);
|
||||
resetFunc(); // reset mcu, to apply new i2c address
|
||||
}
|
||||
else if (command == 0x05)
|
||||
{
|
||||
EEPROM.write(EEPROM_SHORT_DELAY, value);
|
||||
}
|
||||
else if (command == 0x06)
|
||||
{
|
||||
EEPROM.write(EEPROM_LONG_DELAY, value);
|
||||
}
|
||||
if (locateLedEnabled)
|
||||
digitalWrite(LOCATE_LED_PIN, millis() % (LOCATE_LED_BLINK_TIME * 2) < LOCATE_LED_BLINK_TIME);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue