Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define DELIMITER '\n'
- #define SYNC_WORD 451
- #define SYNC_REPLY "452"
- #define REQUEST_BOOLEAN "NIB="
- #define REQUEST_COMMAND "CMD="
- #define COMMAND_EXC "EXC="
- #define COMMAND_SHUTDOWN "SHT"
- #define COMMAND_MAKE_ACTIVE "ACT"
- #define GENERATOR_ACTIVE "PWR_REACTOR_ON"
- #define EMCON_ACTIVE "EMCON_MODE"
- #define TOGGLE_EMCON "TOGGLE_EMCON"
- #define TOGGLE_REACTOR "TOGGLE_REACTOR"
- #define STATE_NONE 0
- #define STATE_SYNC 1
- #define STATE_ACTIVE 2
- int currentState = STATE_NONE;
- const int generatorPin = 4;
- const int emconPin = 5;
- const int emconSwitchPin = 6;
- const int generatorSwitchPin = 7;
- const int errorPin = 13;
- char line[20];
- int currentElement = 0;
- bool buttonDown = false;
- #define BUTTON_TOGGLE true
- #define BUTTON_STATECHANGE false
- struct Button
- {
- Button()
- : lastState(HIGH)
- , pin(-1)
- , commandNumber(-1)
- , buttonType(BUTTON_TOGGLE)
- , lastDebounceTime(0)
- , altCommand(-1)
- {}
- Button(int p, int command, bool bt)
- : lastState(HIGH)
- , pin(p)
- , commandNumber(command)
- , buttonType(bt)
- , lastDebounceTime(0)
- , altCommand(-1)
- {}
- int pin;
- int lastState;
- int commandNumber;
- int altCommand;
- bool buttonType;
- long lastDebounceTime; // the last time the output pin was toggled
- };
- const long DEBOUNC_DELAY = 50;
- #define NUM_BUTTONS 2
- Button buttons[NUM_BUTTONS];
- void flashLed()
- {
- digitalWrite(generatorPin, LOW);
- digitalWrite(generatorPin, HIGH);
- delay(500);
- digitalWrite(generatorPin, LOW);
- }
- void setError(int p)
- {
- digitalWrite(errorPin, p);
- }
- void showError()
- {
- digitalWrite(errorPin, HIGH);
- }
- void disableErrorLight()
- {
- digitalWrite(errorPin, LOW);
- }
- void enableLight(int pin)
- {
- digitalWrite(pin, HIGH);
- }
- void disableLight(int pin)
- {
- digitalWrite(pin, LOW);
- }
- void configureLights()
- {
- pinMode(generatorPin, OUTPUT);
- pinMode(emconPin, OUTPUT);
- pinMode(errorPin, OUTPUT);
- }
- void configurePin(int p)
- {
- pinMode(p, INPUT_PULLUP);
- //digitalWrite(p, HIGH);
- }
- void configureInput()
- {
- configurePin(emconSwitchPin);
- buttons[0].pin = emconSwitchPin;
- buttons[0].commandNumber = 2;
- configurePin(generatorSwitchPin);
- buttons[1].pin = generatorSwitchPin;
- buttons[1].commandNumber = 3;
- }
- void sendBoolRequest(char* req, int channel)
- {
- Serial.print(REQUEST_BOOLEAN);
- Serial.print(req);
- Serial.print(",");
- Serial.print(channel);
- Serial.print(DELIMITER);
- }
- void sendCommandRequest(char* req, int channel)
- {
- Serial.print(REQUEST_COMMAND);
- Serial.print(req);
- Serial.print(",");
- Serial.print(channel);
- Serial.print(DELIMITER);
- }
- void executeCommand(int cmdNumber)
- {
- Serial.print(COMMAND_EXC);
- Serial.print(cmdNumber);
- Serial.print(DELIMITER);
- }
- void sendSimpleCommand(char* req)
- {
- Serial.print(req);
- Serial.print(DELIMITER);
- }
- void configureDataRequests()
- {
- sendBoolRequest(GENERATOR_ACTIVE, 0);
- sendBoolRequest(EMCON_ACTIVE, 1);
- sendCommandRequest(TOGGLE_EMCON, 2);
- //= Button(emconSwitchPin, 2, BUTTON_TOGGLE);
- sendCommandRequest(TOGGLE_REACTOR, 3);
- //buttons[1] = Button(-1/*generatorSwitchPin*/, 3, BUTTON_TOGGLE);
- sendSimpleCommand(COMMAND_MAKE_ACTIVE);
- currentState = STATE_ACTIVE;
- //showError();
- }
- void processElement()
- {
- if (currentState == STATE_ACTIVE) {
- if (strcmp(line, COMMAND_SHUTDOWN) == 0) {
- // Shut down
- disableErrorLight();
- currentState = STATE_NONE;
- } else if (line[0] == '0') {
- // generator
- if (line[2] == '1') {
- enableLight(generatorPin);
- } else {
- disableLight(generatorPin);
- }
- }
- if (line[0] == '1') {
- // EMCON
- if (line[2] == '1') {
- enableLight(emconPin);
- } else {
- disableLight(emconPin);
- }
- } else {
- //showError();
- }
- } else if (currentState == STATE_NONE) {
- // RTG
- if (strcmp(line, SYNC_REPLY) == 0) {
- currentState = STATE_SYNC;
- configureDataRequests();
- }
- }
- currentElement = 0;
- }
- void inputPump()
- {
- if (Serial.available() > 0) {
- // read the incoming byte:
- int incomingByte = Serial.read();
- if (incomingByte == DELIMITER) {
- line[currentElement++] = '\0';
- processElement();
- } else {
- line[currentElement++] = incomingByte;
- }
- }
- }
- void outputPump()
- {
- for (int i = 0; i < NUM_BUTTONS; i++) {
- if (buttons[i].pin == -1) {
- continue;
- }
- // Input using pull-down resister so they're inverted
- int state = digitalRead(buttons[i].pin);
- if (buttons[i].buttonType == BUTTON_TOGGLE) {
- if (state == LOW && buttons[i].lastState == HIGH) {
- // Pushed
- //flashLed();
- executeCommand(buttons[i].commandNumber);
- }
- } else {
- if (state != buttons[i].lastState) {
- if (state == LOW) {
- executeCommand(buttons[i].altCommand);
- } else if (state == HIGH) {
- executeCommand(buttons[i].commandNumber);
- }
- }
- }
- buttons[i].lastState = state;
- }
- }
- void setup() {
- // put your setup code here, to run once:
- configureLights();
- configureInput();
- Serial.begin(9600);
- }
- long int sa = 0;
- void syncAttempt()
- {
- sa--;
- // Send initialisation code
- if (sa <= 0) {
- sa = 250000;
- Serial.print(SYNC_WORD);
- Serial.print(DELIMITER);
- }
- }
- void loop() {
- if (currentState == STATE_NONE) {
- syncAttempt();
- } else {
- }
- inputPump();
- if (currentState == STATE_ACTIVE) {
- outputPump();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement