Advertisement
encodedream

Arduino Code

May 29th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.05 KB | None | 0 0
  1. #include <Wire.h>
  2. // Defines
  3. #define SLAVE_ADDRESS 0x04
  4. #define MAX_EVENT_COUNT 20
  5.  
  6. int number = 0;
  7. int state = 0;
  8.  
  9. //Global Vars
  10. String i2cCommand;
  11.  
  12. //Function Definitions
  13. void (*commands[5]) (String);
  14. int sum(int a, int b);
  15.  
  16. // Command System
  17. struct command {
  18.   void (*command)(String);
  19.   String data;
  20. };
  21. int addCommand(int commandAddress, void (*command)(String));
  22. void processCommandEvent();
  23. struct command processCommand(String commandText);
  24. void runCommand();
  25. bool runCommand(struct command command);
  26. command lastCommand;
  27. void getStatus();
  28.  
  29. String resultBuffer;
  30.  
  31. //Event System
  32. void initalizeEvents();
  33.  
  34. int registerEvent(void (*eventFunction)(), unsigned long duration);
  35. bool popEvent(int eventNumber);
  36. void runEvents();
  37.  
  38.  
  39. void setup() {
  40.   pinMode(13, OUTPUT);
  41.   Serial.begin(9600); // start serial for output
  42.   // initialize i2c as slave
  43.   Wire.begin(SLAVE_ADDRESS);
  44.  
  45.   // define callbacks for i2c communication
  46.   Wire.onReceive(receiveData);
  47.   Wire.onRequest(sendData);
  48.  
  49.   Serial.println("Ready!");
  50.  
  51.   // Register commands
  52.   addCommand(01, getStatus);
  53.  
  54.   // Initialize Events
  55.   initalizeEvents();
  56.  
  57.   blinkLed();
  58. }
  59.  
  60. int incomingByte = 100;
  61.  
  62. void loop() {
  63.   // read the input on analog pin 0:
  64.   //int sensorValue = analogRead(A0);
  65.   // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  66.   //float voltage = sensorValue * (5.0 / 1023.0);
  67.   // print out the value you read:
  68.   //Serial.println(voltage);
  69.  
  70.   //(*commands[01])();
  71.   //delay(900);
  72.   //  if (Serial.available() > 0) {
  73.   //    // read the incoming byte:
  74.   //    incomingByte = Serial.parseInt();
  75.   //    Serial.println(incomingByte);
  76.   //  }
  77.  
  78.   runEvents();
  79. }
  80.  
  81. // Events
  82.  
  83. void enableLed() {
  84.   digitalWrite(13, HIGH); // turn the LED on
  85. }
  86.  
  87. void disableLed() {
  88.   digitalWrite(13, LOW); // turn the LED off
  89. }
  90.  
  91. void blinkLed() {
  92.   Serial.println("Blink LED");
  93.   registerEvent(enableLed, 0);
  94.   registerEvent(disableLed, incomingByte);
  95.   registerEvent(blinkLed, 1000);
  96. }
  97.  
  98. String commandString;
  99.  
  100. // callback for received data
  101. void receiveData(int byteCount) {
  102.  
  103.   int avalibleBytes = Wire.available();
  104.   char* commandChars = new char[avalibleBytes];
  105.  
  106. //  while(Wire.available()) {
  107. //    number = Wire.read();
  108. //  }
  109.  
  110.   for (int i = 0; i < avalibleBytes; i++) {
  111.     commandChars[i] = Wire.read();
  112.   }
  113.   commandString = String(commandChars);
  114.   //Serial.println("Command: " + commandString);
  115.  
  116.   registerEvent(processCommandEvent, 0);
  117.   //Serial.println("Command Registered: " + commandString);
  118.   // Parse Command
  119.   //lastCommand = processCommand(commandString);
  120.   //registerEvent(runCommand, 0);
  121.  
  122. }
  123.  
  124. // callback for sending data
  125. void sendData() {
  126.   String tempResultBuffer = resultBuffer;
  127.   int resultBufferLength = tempResultBuffer.length();
  128.   if(resultBufferLength < 1) {
  129.     tempResultBuffer = String("E");
  130.     resultBufferLength = 1;
  131.   }
  132.  
  133.   char sendCommandBuf[resultBufferLength];
  134.   tempResultBuffer.toCharArray(sendCommandBuf, resultBufferLength);
  135.   char sendBuf[resultBufferLength + 1]; //To accomidate the length
  136.   sendBuf[0] = resultBufferLength;
  137.   for(int i = 1; i < resultBufferLength + 1; i++) {
  138.     sendBuf[i] =  sendCommandBuf[i - 1];
  139.   }
  140.   Wire.write(sendBuf);
  141. }
  142.  
  143. // Command System
  144. int addCommand(int commandAddress, void  (*command)(String)) {
  145.   commands[commandAddress] = command;
  146. }
  147.  
  148. void getStatus(String data) {
  149.   // read the input on analog pin 0:
  150.   int sensorValue = analogRead(A0);
  151.   // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  152.   float voltage = sensorValue * (5.0 / 1023.0);
  153.   // print out the value you read:
  154.  
  155.   //Populate results buffer
  156.   resultBuffer = String(voltage);
  157. }
  158.  
  159. int sum(int a, int b) {
  160.   return a + b;
  161. }
  162.  
  163.  
  164. // Event System
  165.  
  166. struct event {
  167.  
  168.   void (*event)();
  169.   unsigned long duration;
  170.   unsigned long millisStamp;
  171.   bool initalized;
  172. };
  173.  
  174. int lastEventNumber = 0;
  175. event events[MAX_EVENT_COUNT];
  176. event emptyEvent = {0, 0, 0, false};
  177.  
  178. // Initialze envents array so it can be reliablly checked.
  179. void initalizeEvents() {
  180.   for ( int i = 0; i < MAX_EVENT_COUNT; i++) {
  181.     events[i] = emptyEvent;
  182.   }
  183. }
  184.  
  185. // return the first unintialized location index or return -1
  186. int findFirstEmptyEvent() {
  187.   for (int i = 0; i < MAX_EVENT_COUNT; i++) {
  188.     event currentEvent = events[i];
  189.     if (!currentEvent.initalized) return i;
  190.   }
  191.   return -1;
  192. }
  193.  
  194. // Regester event
  195. // int registerEvent(void (*event)(), unsigned long duration)
  196. // Add event to event queue
  197. // Returns eventNumber or -1 if it could not be registered.
  198. int registerEvent(void (*eventFunction)(), unsigned long duration) {
  199.   unsigned long currentMillis = millis();
  200.   event newEvent = {eventFunction, duration, currentMillis, true};
  201.   int newEventNumber = findFirstEmptyEvent();
  202.  
  203.   if (newEventNumber != -1)
  204.     events[newEventNumber] = newEvent;
  205.  
  206.   return newEventNumber;
  207. }
  208.  
  209. // Pop Event
  210. // bool popEvent(int eventNumber)
  211. // Remove event from event queue
  212. // Returns true if removal of the event worked, false if not.
  213. bool popEvent(int eventNumber) {
  214.   events[eventNumber] = emptyEvent;
  215.  
  216.   return true;
  217. }
  218.  
  219.  
  220. // RunEvents
  221. // Events will use millisecond durations, which are how long
  222. //  they will wait until they are run,
  223. //  check against millis() to determin if the time has passes
  224. //  Will be called in the main loop so that it is constantly
  225. //  run.
  226. void runEvents() {
  227.   // Get current millis value;
  228.   unsigned long currentMillis = millis();
  229.  
  230.   // Loop through the entire event queue looking for things to run.
  231.   for (int i = 0; i < MAX_EVENT_COUNT; i++) {
  232.     event currentEvent = events[i];
  233.     if (currentEvent.initalized) {
  234.  
  235.       if (currentMillis - currentEvent.millisStamp > currentEvent.duration) {
  236.         (*currentEvent.event)();
  237.         popEvent(i);
  238.       }
  239.     }
  240.   }
  241. }
  242.  
  243. // Command Parser
  244. //Take string, look for C, pull command ID off until end or Space, Add H support
  245.  
  246. void processCommandEvent() {
  247.   Serial.println("Process Command: " + commandString);
  248.   lastCommand = processCommand(commandString);
  249.   registerEvent(runCommand, 0);
  250. }
  251.  
  252.  
  253. struct command processCommand(String commandText)  {
  254.   command command;
  255.   for (int i = 0; i < commandText.length(); i++) {
  256.     if (commandText[i] == 'C') { //Found a command
  257.       int commandId = parseValue(commandText, &i).toInt();
  258.       command = {commands[i], {}};
  259.     } else if (commandText[i] == 'H') {
  260.       String data = "";
  261.       for (int j = i; j < commandText.length(); j++) {
  262.         data += commandText[j];
  263.       }
  264.       command.data = data;
  265.     }
  266.   }
  267.  
  268.   return command;
  269.   // Run Command
  270.   //  lastCommand = command;
  271.   //  runCommand();
  272. }
  273. void runCommand() {
  274.   runCommand(lastCommand);
  275. }
  276. bool runCommand(struct command command) {
  277.   (command.command)(command.data);
  278. }
  279.  
  280.  
  281. String parseValue(String request, int *i) {
  282.   String result = "";
  283.   for (int j = *i + 1; j < request.length(); j++ ) {
  284.     if (request[j - 1] != '\\' && request[j] == ' ') { // If a space is reached, stop running.
  285.       *i = j;
  286.       break;
  287.     }
  288.     result += request[j];
  289.   }
  290.  
  291.   return result;
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement