Guest User

Untitled

a guest
Apr 7th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.60 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <string.h>
  3.  
  4. #define STRBUF_SIZE 100
  5.  
  6. static char strbuf[STRBUF_SIZE];
  7. static uint8_t strbuf_i = 0;
  8. static uint8_t handshake_retry = 0;
  9.  
  10. #define HANDSHAKE_MAX_RETRY 5
  11. #define HANDSHAKE_FAILED 0
  12. #define HANDSHAKE_OK 1
  13. #define HANDSHAKE_LED 13
  14.  
  15. #define VERSION "INOST_IOT_V1"
  16.  
  17. void setup(void) {              
  18.  
  19.   pinMode(HANDSHAKE_LED, OUTPUT);
  20.   Serial.begin(9600);
  21.   if (!three_way_handshake()) {
  22.     reset_board();
  23.   }
  24.   snprintf(strbuf, STRBUF_SIZE, "HELLO %s", VERSION);
  25.   uart_write_string(strbuf);
  26. }
  27.  
  28. void loop() {
  29.  
  30.   char *buf = uart_read_string();
  31.  
  32.   if (buf != NULL && strncmp(buf, "GET ", 4) == 0) {
  33.  
  34.     int port = -1; /* Will use standard int here, because of atoi */
  35.     buf += 4; /* Skip "GET " */
  36.     port = atoi(buf);
  37.    
  38.     if (port == -1 || port > 5) {
  39.           uart_write_string("NACK");
  40.           return;
  41.     }
  42.    
  43.     dtostrf(get_sensor_read(port),5, 2, strbuf);
  44.     uart_write_string(strbuf);
  45.    
  46.   } else if (buf != NULL && strcmp(buf, "VER") == 0) {
  47.     uart_write_string(VERSION);
  48.   } else  if (buf != NULL && strcmp(buf, "RESET") == 0) {
  49.     reset_board();
  50.   } else {
  51.     uart_write_string("NACK");
  52.   }
  53. }
  54.  
  55. float get_sensor_read(int pinNumber) {
  56.   pinMode(A0 + pinNumber, INPUT);
  57.   return analogRead(A0 + pinNumber);
  58. }
  59.  
  60. uint8_t three_way_handshake() {
  61.  
  62.   handshake_retry++;
  63.   if (handshake_retry > HANDSHAKE_MAX_RETRY) {
  64.     reset_board();
  65.   }
  66.  
  67.   char *buf;
  68.   buf = uart_read_string();
  69.  
  70.   if (buf == NULL || strcmp("SYN", buf) != 0) {
  71.     uart_write_string("NACK");
  72.     return three_way_handshake();
  73.   }
  74.  
  75.   uart_write_string("ACK");
  76.   uart_write_string("SYN");
  77.  
  78.   buf = uart_read_string();
  79.  
  80.   if (buf == NULL || strcmp("ACK", buf) != 0) {
  81.     uart_write_string("NACK");
  82.     return three_way_handshake(); // recursive call, try again
  83.   }
  84.  
  85.   digitalWrite(HANDSHAKE_LED, HIGH);
  86.   return HANDSHAKE_OK;
  87. }
  88.  
  89. char *uart_read_string() {
  90.   strbuf_i = 0;
  91.   int8_t temp;
  92.   while (true) {
  93.      while (Serial.available() > 0) {
  94.         temp = Serial.read();
  95.        
  96.         if (temp == ';') {
  97.           strbuf[strbuf_i] = 0;
  98.           return strbuf;
  99.         }
  100.        
  101.         strbuf[strbuf_i] = temp;
  102.         strbuf_i++;
  103.         if (strbuf_i > STRBUF_SIZE) {
  104.           return NULL;
  105.         }
  106.      }
  107.   }
  108.   return NULL;
  109. }
  110.  
  111. void uart_write_string(const char *data) {
  112.   for (int i = 0; i < strlen(data); i++) {
  113.     Serial.write(data[i]);
  114.   }
  115.   Serial.write(';');
  116. }
  117.  
  118. void reset_board() {
  119.   digitalWrite(HANDSHAKE_LED, LOW);
  120.   uart_write_string("GOODBYE");
  121.   asm volatile("jmp 0");
  122. }
Advertisement
Add Comment
Please, Sign In to add comment