Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- // Defines
- #define SLAVE_ADDRESS 0x04
- #define MAX_EVENT_COUNT 20
- int number = 0;
- int state = 0;
- //Global Vars
- String i2cCommand;
- //Function Definitions
- void (*commands[5]) (String);
- int sum(int a, int b);
- // Command System
- struct command {
- void (*command)(String);
- String data;
- };
- int addCommand(int commandAddress, void (*command)(String));
- void processCommandEvent();
- struct command processCommand(String commandText);
- void runCommand();
- bool runCommand(struct command command);
- command lastCommand;
- void getStatus();
- String resultBuffer;
- //Event System
- void initalizeEvents();
- int registerEvent(void (*eventFunction)(), unsigned long duration);
- bool popEvent(int eventNumber);
- void runEvents();
- void setup() {
- pinMode(13, OUTPUT);
- Serial.begin(9600); // start serial for output
- // initialize i2c as slave
- Wire.begin(SLAVE_ADDRESS);
- // define callbacks for i2c communication
- Wire.onReceive(receiveData);
- Wire.onRequest(sendData);
- Serial.println("Ready!");
- // Register commands
- addCommand(01, getStatus);
- // Initialize Events
- initalizeEvents();
- blinkLed();
- }
- int incomingByte = 100;
- void loop() {
- // read the input on analog pin 0:
- //int sensorValue = analogRead(A0);
- // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
- //float voltage = sensorValue * (5.0 / 1023.0);
- // print out the value you read:
- //Serial.println(voltage);
- //(*commands[01])();
- //delay(900);
- // if (Serial.available() > 0) {
- // // read the incoming byte:
- // incomingByte = Serial.parseInt();
- // Serial.println(incomingByte);
- // }
- runEvents();
- }
- // Events
- void enableLed() {
- digitalWrite(13, HIGH); // turn the LED on
- }
- void disableLed() {
- digitalWrite(13, LOW); // turn the LED off
- }
- void blinkLed() {
- Serial.println("Blink LED");
- registerEvent(enableLed, 0);
- registerEvent(disableLed, incomingByte);
- registerEvent(blinkLed, 1000);
- }
- String commandString;
- // callback for received data
- void receiveData(int byteCount) {
- int avalibleBytes = Wire.available();
- char* commandChars = new char[avalibleBytes];
- // while(Wire.available()) {
- // number = Wire.read();
- // }
- for (int i = 0; i < avalibleBytes; i++) {
- commandChars[i] = Wire.read();
- }
- commandString = String(commandChars);
- //Serial.println("Command: " + commandString);
- registerEvent(processCommandEvent, 0);
- //Serial.println("Command Registered: " + commandString);
- // Parse Command
- //lastCommand = processCommand(commandString);
- //registerEvent(runCommand, 0);
- }
- // callback for sending data
- void sendData() {
- String tempResultBuffer = resultBuffer;
- int resultBufferLength = tempResultBuffer.length();
- if(resultBufferLength < 1) {
- tempResultBuffer = String("E");
- resultBufferLength = 1;
- }
- char sendCommandBuf[resultBufferLength];
- tempResultBuffer.toCharArray(sendCommandBuf, resultBufferLength);
- char sendBuf[resultBufferLength + 1]; //To accomidate the length
- sendBuf[0] = resultBufferLength;
- for(int i = 1; i < resultBufferLength + 1; i++) {
- sendBuf[i] = sendCommandBuf[i - 1];
- }
- Wire.write(sendBuf);
- }
- // Command System
- int addCommand(int commandAddress, void (*command)(String)) {
- commands[commandAddress] = command;
- }
- void getStatus(String data) {
- // read the input on analog pin 0:
- int sensorValue = analogRead(A0);
- // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
- float voltage = sensorValue * (5.0 / 1023.0);
- // print out the value you read:
- //Populate results buffer
- resultBuffer = String(voltage);
- }
- int sum(int a, int b) {
- return a + b;
- }
- // Event System
- struct event {
- void (*event)();
- unsigned long duration;
- unsigned long millisStamp;
- bool initalized;
- };
- int lastEventNumber = 0;
- event events[MAX_EVENT_COUNT];
- event emptyEvent = {0, 0, 0, false};
- // Initialze envents array so it can be reliablly checked.
- void initalizeEvents() {
- for ( int i = 0; i < MAX_EVENT_COUNT; i++) {
- events[i] = emptyEvent;
- }
- }
- // return the first unintialized location index or return -1
- int findFirstEmptyEvent() {
- for (int i = 0; i < MAX_EVENT_COUNT; i++) {
- event currentEvent = events[i];
- if (!currentEvent.initalized) return i;
- }
- return -1;
- }
- // Regester event
- // int registerEvent(void (*event)(), unsigned long duration)
- // Add event to event queue
- // Returns eventNumber or -1 if it could not be registered.
- int registerEvent(void (*eventFunction)(), unsigned long duration) {
- unsigned long currentMillis = millis();
- event newEvent = {eventFunction, duration, currentMillis, true};
- int newEventNumber = findFirstEmptyEvent();
- if (newEventNumber != -1)
- events[newEventNumber] = newEvent;
- return newEventNumber;
- }
- // Pop Event
- // bool popEvent(int eventNumber)
- // Remove event from event queue
- // Returns true if removal of the event worked, false if not.
- bool popEvent(int eventNumber) {
- events[eventNumber] = emptyEvent;
- return true;
- }
- // RunEvents
- // Events will use millisecond durations, which are how long
- // they will wait until they are run,
- // check against millis() to determin if the time has passes
- // Will be called in the main loop so that it is constantly
- // run.
- void runEvents() {
- // Get current millis value;
- unsigned long currentMillis = millis();
- // Loop through the entire event queue looking for things to run.
- for (int i = 0; i < MAX_EVENT_COUNT; i++) {
- event currentEvent = events[i];
- if (currentEvent.initalized) {
- if (currentMillis - currentEvent.millisStamp > currentEvent.duration) {
- (*currentEvent.event)();
- popEvent(i);
- }
- }
- }
- }
- // Command Parser
- //Take string, look for C, pull command ID off until end or Space, Add H support
- void processCommandEvent() {
- Serial.println("Process Command: " + commandString);
- lastCommand = processCommand(commandString);
- registerEvent(runCommand, 0);
- }
- struct command processCommand(String commandText) {
- command command;
- for (int i = 0; i < commandText.length(); i++) {
- if (commandText[i] == 'C') { //Found a command
- int commandId = parseValue(commandText, &i).toInt();
- command = {commands[i], {}};
- } else if (commandText[i] == 'H') {
- String data = "";
- for (int j = i; j < commandText.length(); j++) {
- data += commandText[j];
- }
- command.data = data;
- }
- }
- return command;
- // Run Command
- // lastCommand = command;
- // runCommand();
- }
- void runCommand() {
- runCommand(lastCommand);
- }
- bool runCommand(struct command command) {
- (command.command)(command.data);
- }
- String parseValue(String request, int *i) {
- String result = "";
- for (int j = *i + 1; j < request.length(); j++ ) {
- if (request[j - 1] != '\\' && request[j] == ' ') { // If a space is reached, stop running.
- *i = j;
- break;
- }
- result += request[j];
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement