Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ||
- || @file mlaser.pde
- || @version 1.0
- || @author Alexander Brevig
- || @contact alexanderbrevig@gmail.com
- ||
- || @description
- || | Implement an example using the library Toolkit
- || | Improvised from this thread: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1240828046
- || #
- ||
- || @contribution
- || | [url=http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?action=viewprofile;username=mlaser]mlaser[/url]
- || | [url=http://arduino.cc/playground/Profiles/AlphaBeta]AlphaBeta[/url]
- || #
- */
- @@#include <Toolkit.h> //http://hosting.alexanderbrevig.com/arduino/libraries/Toolkit.zip
- //constants
- const unsigned int TIMEOUT = 1000;
- const byte MAX_COMMAND_LENGTH = 12;
- //command syntax
- const char REQUEST = '+';
- const char REPLACE = '-';
- const char END_OF_COMMAND = '$';
- //initialize some tools
- @@Tool spanner8 = Tool("spanner8");
- @@Tool spanner10 = Tool("spanner10");
- @@Tool spanner12 = Tool("spanner12");
- @@Tool spanner14 = Tool("spanner14");
- //prepare the toolkit
- @@Toolkit toolkit;
- //initialize a user
- @@ToolUser mlaser = ToolUser("mlaser",toolkit); //user name is mlaser, and this user can use tools from the Toolkit named toolkit
- //program variables
- unsigned long time = 0; //store the time when the serial becomes available. Is used to implement a TIMEOUT
- boolean toolRequest = false; //do user want to add or remove a tool?
- boolean readCommand = false; //are we ready to recieve a command?
- char command[MAX_COMMAND_LENGTH+1] = {'\0'}; //store the command
- byte currentCommandIndex = 0; //current index in the command array
- /*
- || @description
- || | Setup toolkit and mlaser:ToolUser
- || | Print information and a description to the serial console
- || #
- */
- void setup() {
- Serial.begin(9600);
- //description to serial monitor
- Serial.println("You can add or remove tools by usnig these commands:");
- Serial.println("\t+SpannerXX$");
- Serial.println("\t-SpannerXX$");
- Serial.println("\tWhere:");
- Serial.println("\t\t+ and - indicates the wanted operation (add / remove)");
- Serial.println("\t\tXX could be either 8,10,12 or 14");
- Serial.println("\t\t$ indicates command end");
- Serial.println("Write '+Spanner10$' in order to add spanner10");
- Serial.println("Write '-Spanner10$' in order to remove spanner10");
- //setup toolkit
- @@ toolkit.addTool(spanner8);
- @@ toolkit.addTool(spanner10);
- @@ toolkit.addTool(spanner12);
- @@ toolkit.addTool(spanner14);
- @@ toolkit.printTools(); //make the user aware of what tools is available
- //setup user programmatically
- @@ mlaser.request(spanner8);
- @@ mlaser.request(spanner12);
- @@ mlaser.request(spanner14);
- @@ mlaser.printTools(); //make user aware of what tools it already has
- }//end setup
- void loop(){
- /*
- This is just an example, and not programmatically good.
- Contact me for useful additions to the Toolkit and ToolUser API
- */
- if (Serial.available()){ //there are something available in the buffer
- readCommand = false; //reset readCommand. We will need a REQUEST or REPLACE command to set this true
- currentCommandIndex = 0;
- time = millis();
- while (millis()<time+TIMEOUT){ //if the command is not recieved in TIMEOUT milliseconds, discard it
- if(Serial.available()){
- char recieved = Serial.read();
- switch (recieved){
- case REQUEST: //if we recieved a REQUEST cymbol, set toolRequest true
- toolRequest = true;
- readCommand = true; //indicate that we are ready to recieve a command
- break;
- case REPLACE: //if we recieved a REPLACE cymbol, set toolRequest false
- toolRequest = false;
- readCommand = true; //indicate that we are ready to recieve a command
- break;
- case END_OF_COMMAND: //if we recieved a END_OF_COMMAND cymbol, process the command
- if(readCommand){ //there has not been a timeout, so execute command
- @@// this is where all the action happens:
- @@// mlaser.request( spanner8 ); will ask toolkit for the spanner8 and add it to the mlaser ToolUser
- @@// mlaser.replace( spanner8 ); will ask toolkit to recieve the spanner8 and remove it from the mlaser ToolUser
- switch (command[7]){
- case '8': toolRequest ? mlaser.request(spanner8) : mlaser.replace(spanner8); break;
- case '1':
- switch (command[8]){
- case '0': toolRequest ? mlaser.request(spanner10) : mlaser.replace(spanner10); break;
- case '2': toolRequest ? mlaser.request(spanner12) : mlaser.replace(spanner12); break;
- case '4': toolRequest ? mlaser.request(spanner14) : mlaser.replace(spanner14); break;
- }
- break;
- }
- command[7] = '\0'; //assure new command
- command[8] = '\0';
- mlaser.printTools(); //make user aware of what tools it has
- }
- break;//end case END_OF_COMMAND
- default:
- if (readCommand && currentCommandIndex+1 < MAX_COMMAND_LENGTH){ //if we are ready to recieve a command and no TIMEOUT has been met, read command
- command[currentCommandIndex++] = recieved;
- }
- break;
- }//end switch (recieved)
- }//end Serial.available
- }//end while (millis()<time+TIMEOUT)
- }//end Serial.available
- }//end loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement