141 lines
3.4 KiB
C++
141 lines
3.4 KiB
C++
#include <Arduino.h>
|
|
#include <EEPROM.h>
|
|
|
|
#define REFERENCE_FREQUENCY // enable this to generate a 1khz tone on pin PA7
|
|
|
|
#define SHORT_PRESS_TIME 1
|
|
#define LONG_PRESS_TIME 10
|
|
#define LOCATE_LED_BLINK_TIME 1
|
|
|
|
// TODO: Set the correct pins
|
|
#define POWER_BUTTON_PIN PIN_PA4
|
|
#define RESET_BUTTON_PIN PIN_PA5
|
|
#define LOCATE_LED_PIN PIN_PA6
|
|
#define SEND_PIN PIN_PA3
|
|
#define FACTORY_RESET_PIN PIN_PB0
|
|
|
|
#define DEFAULT_ADDRESS 0x01
|
|
#define EEPROM_ADDRESS 0x00
|
|
#define EEPOROM_FIRST_BOOT 0x01
|
|
#define EEPROM_SHORT_DELAY 0x02
|
|
#define EEPROM_LONG_DELAY 0x03
|
|
|
|
bool locateLedEnabled = false;
|
|
|
|
void (*resetFunc)(void) = 0; // declare reset function @ address 0
|
|
|
|
void setup()
|
|
{
|
|
Serial.pins(PIN_PA1, PIN_PA2);
|
|
Serial.begin(9600);
|
|
|
|
pinMode(POWER_BUTTON_PIN, OUTPUT);
|
|
pinMode(RESET_BUTTON_PIN, OUTPUT);
|
|
pinMode(LOCATE_LED_PIN, OUTPUT);
|
|
pinMode(SEND_PIN, OUTPUT);
|
|
pinMode(FACTORY_RESET_PIN, INPUT);
|
|
|
|
if ((EEPROM.read(EEPOROM_FIRST_BOOT) != 0x01) || digitalRead(FACTORY_RESET_PIN) == LOW)
|
|
{
|
|
EEPROM.write(EEPROM_ADDRESS, DEFAULT_ADDRESS);
|
|
EEPROM.write(EEPROM_SHORT_DELAY, SHORT_PRESS_TIME);
|
|
EEPROM.write(EEPROM_LONG_DELAY, LONG_PRESS_TIME);
|
|
EEPROM.write(EEPOROM_FIRST_BOOT, 0x01);
|
|
digitalWrite(LOCATE_LED_PIN, HIGH);
|
|
delay(2000);
|
|
resetFunc();
|
|
}
|
|
#ifdef REFERENCE_FREQUENCY
|
|
Serial.println("Reference frequency enabled");
|
|
Serial.println("1khz tone on PA7");
|
|
pinMode(PIN_PA7, OUTPUT);
|
|
tone(PIN_PA7, 1000);
|
|
#endif
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
if (Serial.available())
|
|
{
|
|
delay(10);
|
|
if (Serial.available() != 3)
|
|
{
|
|
Serial.print("Invalid packet, length: ");
|
|
Serial.print(Serial.available());
|
|
Serial.print(" bytes: ");
|
|
while (Serial.available())
|
|
{
|
|
Serial.print(Serial.read(), HEX);
|
|
Serial.print(" ");
|
|
}
|
|
Serial.println();
|
|
return;
|
|
}
|
|
|
|
byte available = Serial.available();
|
|
byte deviceID = Serial.read();
|
|
byte command = Serial.read();
|
|
byte value = Serial.read();
|
|
|
|
Serial.print("Device ID: ");
|
|
Serial.print(deviceID, HEX);
|
|
Serial.print(" Command: ");
|
|
Serial.print(command, HEX);
|
|
Serial.print(" Value: ");
|
|
Serial.println(value, HEX);
|
|
|
|
if (deviceID != EEPROM.read(EEPROM_ADDRESS))
|
|
return;
|
|
if (command == 0x01)
|
|
{
|
|
digitalWrite(POWER_BUTTON_PIN, HIGH);
|
|
if (value == 0x01)
|
|
{
|
|
delay(EEPROM.read(EEPROM_SHORT_DELAY) * 1000);
|
|
}
|
|
else
|
|
{
|
|
delay(EEPROM.read(EEPROM_LONG_DELAY) * 1000);
|
|
}
|
|
digitalWrite(POWER_BUTTON_PIN, LOW);
|
|
}
|
|
else if (command == 0x02)
|
|
{
|
|
digitalWrite(RESET_BUTTON_PIN, HIGH);
|
|
if (value == 0x01)
|
|
{
|
|
delay(EEPROM.read(EEPROM_SHORT_DELAY) * 1000);
|
|
}
|
|
else
|
|
{
|
|
delay(EEPROM.read(EEPROM_LONG_DELAY) * 1000);
|
|
}
|
|
digitalWrite(RESET_BUTTON_PIN, LOW);
|
|
}
|
|
else if (command == 0x03)
|
|
{
|
|
locateLedEnabled = value == 0x01;
|
|
digitalWrite(LOCATE_LED_PIN, LOW);
|
|
}
|
|
else if (command == 0x04)
|
|
{
|
|
EEPROM.write(EEPROM_ADDRESS, value);
|
|
}
|
|
else if (command == 0x05)
|
|
{
|
|
EEPROM.write(EEPROM_SHORT_DELAY, value);
|
|
}
|
|
else if (command == 0x06)
|
|
{
|
|
EEPROM.write(EEPROM_LONG_DELAY, value);
|
|
}
|
|
else if (command == 0x07)
|
|
{
|
|
resetFunc();
|
|
}
|
|
}
|
|
if (locateLedEnabled)
|
|
{
|
|
digitalWrite(LOCATE_LED_PIN, (millis() / 1000) % (LOCATE_LED_BLINK_TIME * 2) < LOCATE_LED_BLINK_TIME);
|
|
}
|
|
}
|