Created
          April 12, 2017 18:43 
        
      - 
      
 - 
        
Save yosemitebandit/342566d0718ec9b78da7aa1c0b0421a8 to your computer and use it in GitHub Desktop.  
    sample sketch for review
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | /* Sample sketch for review. | |
| */ | |
| #define USB Serial | |
| const int usbBaud = 57600; | |
| const int ledPin = 13; | |
| const int commandBufferLength = 80; | |
| static char commandBuffer[commandBufferLength]; | |
| char *commandMode; | |
| char *commandValue; | |
| int commandBufferPosition = 0; | |
| int readline(int incomingChar, char *buffer, int len, int &bufferPosition) { | |
| int totalLength; | |
| if (incomingChar > 0) { | |
| switch (incomingChar) { | |
| case '\n': | |
| break; | |
| case '\r': | |
| totalLength = bufferPosition; | |
| bufferPosition = 0; | |
| return totalLength; | |
| default: | |
| if (bufferPosition < len-1) { | |
| buffer[bufferPosition] = incomingChar; | |
| buffer[bufferPosition+1] = 0; | |
| bufferPosition++; | |
| } | |
| } | |
| } | |
| return -1; | |
| } | |
| void setup() { | |
| pinMode(ledPin, OUTPUT); | |
| USB.begin(usbBaud); | |
| } | |
| void loop() { | |
| if (readline(USB.read(), commandBuffer, commandBufferLength, commandBufferPosition) > 0) { | |
| commandMode = strtok(commandBuffer, " "); | |
| if (strcmp(commandMode, "led") == 0) { | |
| commandValue = strtok(NULL, " "); | |
| if (strcmp(commandValue, "on") == 0) { | |
| digitalWrite(ledPin, HIGH); | |
| } else if (strcmp(commandValue, "off") == 0) { | |
| digitalWrite(ledPin, LOW); | |
| } else { | |
| USB.println("value not understood"); | |
| } | |
| } else { | |
| USB.print("error: could not parse command '"); | |
| USB.print(commandBuffer); | |
| USB.println("'"); | |
| } | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment