State machine implementation of lazer maze

Dependencies:   mbed

Revision:
0:b21aca784f5b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Nov 29 12:44:12 2018 +0000
@@ -0,0 +1,124 @@
+#include "mbed.h"
+/*
+#define TERMINATOR  '\n'
+#define SEPERATOR   ','
+
+// Analog input solar cells corresponding to digital output leds.
+DigitalOut leds[] = {1,2,3,4,5};
+AnalogIn solarCells[] = {1,2,3,4,5};
+bool solarCellsState[sizeof(solarCells)];
+uint16_t solarCellsThreshold[sizeof(solarCells)];
+
+Serial serial(PA_2, PA_3);
+
+char pcBuffer[256];
+uint8_t pcBufferIndex = 0;
+bool messageReceived = false;
+
+enum {
+    IDLE,
+    CALIBRATE,
+    START,
+    RUNNING,
+    STOP,
+    MAX_STATES
+} States;
+
+enum States state = IDLE;
+
+void callback(void) {
+    pcBuffer[pcBufferIndex] = serial.getc();
+    
+    if(pcBuffer[pcBufferIndex] == TERMINATOR) {
+        pcBufferIndex = 0;
+        messageReceived = true;
+    }
+}
+*/
+int main() {
+    /*
+    serial.printf("Hello\n");
+    
+    serial.attach(&callback);
+    
+    for(uint8_t i = 0; i < sizeof(solarCells); i++) {
+        solarCellsState[i] = false;
+        solarCellsThreshold[i] = 0;
+    }
+    
+    while(true) {
+        switch(state) {
+            case IDLE:
+                // If we have received a full message over UART.
+                if(messageReceived) {
+                    // Clear flag.
+                    messageReceived = false;
+                    
+                    // Handle command by pointing to correct state.
+                    switch(pcBuffer[0]) {
+                        case "s":
+                            state = START;
+                        break;
+                        case "c":
+                            state = CALIBRATE;
+                        default:
+                        break;
+                    }
+                }
+                break;
+            case CALIBRATE:
+                // If no arguments were given. 
+                if(pcBuffer[1] == TERMINATOR) {
+                    // Read solar cells and return values.
+                    for(uint8_t i = 0; i < sizeof(solarCells); i++) {
+                        uint16_t val = solarCells[i].read();
+                        serial.printf("%d%c", val, SEPERATOR);    
+                    }  
+                    serial.printf("%c", TERMINATOR);
+                }
+                else if(pcBuffer[1] == SEPERATOR) {
+                    // Store new thresholds.
+                    uint16_t i = 2;
+                    while(pcBuffer[i] != '\n') {
+                            
+                    }
+                }
+                else{
+                    // Error.
+                }
+            break;
+            case START:
+                serial.printf("%c", TERMINATOR);
+                state = RUNNING;
+            break;
+            case RUNNING:
+                // Update solarCellsState.
+                for(uint8_t i = 0; i < sizeof(solarCells); i++) {
+                    solarCellsState[i] = (solarCells[i].read() >= 
+                                            solarCellsThreshold[i]);
+                }
+                
+                // Update leds.
+                uint8_t activeLeds = 0;
+                for(uint8_t i = 0; i < sizeof(leds); i++) {
+                    leds[i] = solarCellsState[i];
+                    activeLeds += solarCellsState[i];
+                }
+                
+                if(activeLeds >= sizeof(leds)) {
+                    state = STOP;    
+                }
+                else {
+                    state = RUNNING;
+                } 
+            break;
+            case STOP:
+                serial.printf("%c", TERMINATOR);
+                state = IDLE;
+            break;
+            default:
+            break;
+                
+        }
+    }*/
+}