Advertisement
Guest User

AlphaBeta

a guest
Apr 27th, 2009
1,429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.47 KB | None | 0 0
  1. /*
  2. ||
  3. || @file mlaser.pde
  4. || @version 1.0
  5. || @author Alexander Brevig
  6. || @contact alexanderbrevig@gmail.com
  7. ||
  8. || @description
  9. || | Implement an example using the library Toolkit
  10. || | Improvised from this thread: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1240828046
  11. || #
  12. ||
  13. || @contribution
  14. || | [url=http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?action=viewprofile;username=mlaser]mlaser[/url]
  15. || | [url=http://arduino.cc/playground/Profiles/AlphaBeta]AlphaBeta[/url]
  16. || #
  17. */
  18.  
  19. @@#include <Toolkit.h> //http://hosting.alexanderbrevig.com/arduino/libraries/Toolkit.zip
  20.  
  21. //constants
  22. const unsigned int TIMEOUT = 1000;
  23. const byte MAX_COMMAND_LENGTH = 12;
  24. //command syntax
  25. const char REQUEST = '+';
  26. const char REPLACE = '-';
  27. const char END_OF_COMMAND = '$';
  28.  
  29. //initialize some tools
  30. @@Tool spanner8 = Tool("spanner8");
  31. @@Tool spanner10 = Tool("spanner10");
  32. @@Tool spanner12 = Tool("spanner12");
  33. @@Tool spanner14 = Tool("spanner14");
  34. //prepare the toolkit
  35. @@Toolkit toolkit;
  36. //initialize a user
  37. @@ToolUser mlaser = ToolUser("mlaser",toolkit);   //user name is mlaser, and this user can use tools from the Toolkit named toolkit
  38.  
  39. //program variables
  40. unsigned long time = 0;                         //store the time when the serial becomes available. Is used to implement a TIMEOUT
  41. boolean toolRequest = false;                    //do user want to add or remove a tool?
  42. boolean readCommand = false;                    //are we ready to recieve a command?
  43. char command[MAX_COMMAND_LENGTH+1] = {'\0'};    //store the command
  44. byte currentCommandIndex = 0;                   //current index in the command array
  45.  
  46. /*
  47. || @description
  48. || | Setup toolkit and mlaser:ToolUser
  49. || | Print information and a description to the serial console
  50. || #
  51. */
  52. void setup() {
  53.   Serial.begin(9600);
  54.  
  55.   //description to serial monitor
  56.   Serial.println("You can add or remove tools by usnig these commands:");
  57.   Serial.println("\t+SpannerXX$");
  58.   Serial.println("\t-SpannerXX$");
  59.   Serial.println("\tWhere:");
  60.   Serial.println("\t\t+ and - indicates the wanted operation (add / remove)");
  61.   Serial.println("\t\tXX could be either 8,10,12 or 14");
  62.   Serial.println("\t\t$ indicates command end");
  63.   Serial.println("Write '+Spanner10$' in order to add spanner10");
  64.   Serial.println("Write '-Spanner10$' in order to remove spanner10");
  65.  
  66.   //setup toolkit
  67. @@  toolkit.addTool(spanner8);
  68. @@  toolkit.addTool(spanner10);
  69. @@  toolkit.addTool(spanner12);
  70. @@  toolkit.addTool(spanner14);
  71. @@  toolkit.printTools(); //make the user aware of what tools is available
  72.   //setup user programmatically
  73. @@  mlaser.request(spanner8);
  74. @@  mlaser.request(spanner12);
  75. @@  mlaser.request(spanner14);
  76. @@  mlaser.printTools(); //make user aware of what tools it already has
  77. }//end setup
  78.  
  79. void loop(){
  80.   /*
  81.     This is just an example, and not programmatically good.
  82.     Contact me for useful additions to the Toolkit and ToolUser API
  83.   */
  84.   if (Serial.available()){              //there are something available in the buffer
  85.     readCommand = false;                //reset readCommand. We will need a REQUEST or REPLACE command to set this true
  86.     currentCommandIndex = 0;
  87.     time = millis();
  88.     while (millis()<time+TIMEOUT){      //if the command is not recieved in TIMEOUT milliseconds, discard it
  89.       if(Serial.available()){
  90.         char recieved = Serial.read();
  91.         switch (recieved){
  92.           case REQUEST:                 //if we recieved a REQUEST cymbol, set toolRequest true
  93.             toolRequest = true;
  94.             readCommand = true;         //indicate that we are ready to recieve a command
  95.           break;
  96.           case REPLACE:                 //if we recieved a REPLACE cymbol, set toolRequest false
  97.             toolRequest = false;
  98.             readCommand = true;         //indicate that we are ready to recieve a command
  99.           break;
  100.           case END_OF_COMMAND:          //if we recieved a END_OF_COMMAND cymbol, process the command
  101.             if(readCommand){            //there has not been a timeout, so execute command
  102. @@// this is where all the action happens:
  103. @@// mlaser.request( spanner8 ); will ask toolkit for the spanner8 and add it to the mlaser ToolUser
  104. @@// mlaser.replace( spanner8 ); will ask toolkit to recieve the spanner8 and remove it from the mlaser ToolUser
  105.               switch (command[7]){
  106.                 case '8': toolRequest ? mlaser.request(spanner8) : mlaser.replace(spanner8); break;
  107.                 case '1':
  108.                 switch (command[8]){
  109.                   case '0': toolRequest ? mlaser.request(spanner10) : mlaser.replace(spanner10); break;
  110.                   case '2': toolRequest ? mlaser.request(spanner12) : mlaser.replace(spanner12); break;
  111.                   case '4': toolRequest ? mlaser.request(spanner14) : mlaser.replace(spanner14); break;
  112.                 }
  113.                 break;
  114.               }
  115.               command[7] = '\0';        //assure new command
  116.               command[8] = '\0';
  117.               mlaser.printTools();      //make user aware of what tools it has
  118.             }
  119.           break;//end case END_OF_COMMAND
  120.           default:
  121.             if (readCommand && currentCommandIndex+1 < MAX_COMMAND_LENGTH){ //if we are ready to recieve a command and no TIMEOUT has been met, read command
  122.               command[currentCommandIndex++] = recieved;
  123.             }
  124.           break;
  125.         }//end switch (recieved)
  126.       }//end Serial.available
  127.     }//end while (millis()<time+TIMEOUT)
  128.   }//end Serial.available
  129. }//end loop
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement