Newer
Older
TestStandRepository / Software / Arduino / libraries / Arduino-Libraries / CmdMessenger / Examples / ConsoleShell / ConsoleShell.ino
  1. // *** ConsoleShell ***
  2.  
  3. // This example shows how to use CmdMessenger as a shell, and communicate with it using the Serial Console
  4. // This example is different from all others:
  5. // - there is no PC counterpart
  6. // - it will only receive commands, instead of sending commands it will use Serial.Pring
  7. //
  8. // Below is an example of interacting with the sample:
  9. //
  10. // Available commands
  11. // 0; - This command list
  12. // 1,<led state>; - Set led. 0 = off, 1 = on
  13. // 2,<led brightness>; - Set led brighness. 0 - 1000
  14. // 3; - Show led state
  15. //
  16. // Command> 3;
  17. //
  18. // Led status: on
  19. // Led brightness: 500
  20. //
  21. // Command> 2,1000;
  22. //
  23. // Led status: on
  24. // Led brightness: 1000
  25. //
  26. // Command> 1,0;
  27. //
  28. // Led status: off
  29. // Led brightness: 1000
  30.  
  31.  
  32. #include <CmdMessenger.h> // CmdMessenger
  33.  
  34. // PWM timing variables
  35. unsigned long intervalOn = 0;
  36. unsigned long prevBlinkTime = 0;
  37. const unsigned long PWMinterval = 1000;
  38.  
  39. // Blinking led variables
  40. bool ledState = 1; // On/Off state of Led
  41. int ledBrightness = prevBlinkTime /2 ; // 50 % Brightness
  42. const int kBlinkLed = 13; // Pin of internal Led
  43.  
  44. // Attach a new CmdMessenger object to the default Serial port
  45. CmdMessenger cmdMessenger = CmdMessenger(Serial);
  46.  
  47. // This is the list of recognized commands.
  48. // In order to receive, attach a callback function to these events
  49. enum
  50. {
  51. kCommandList , // Command to request list of available commands
  52. kSetLed , // Command to request led to be set in specific state
  53. kSetLedBrightness , // Command to request led to be set in to specific brightness
  54. kStatus , // Command to request led status
  55. };
  56.  
  57. // Callbacks define on which received commands we take action
  58. void attachCommandCallbacks()
  59. {
  60. // Attach callback methods
  61. cmdMessenger.attach(OnUnknownCommand);
  62. cmdMessenger.attach(kCommandList, OnCommandList);
  63. cmdMessenger.attach(kSetLed, OnSetLed);
  64. cmdMessenger.attach(kSetLedBrightness, OnSetLedBrightness);
  65. cmdMessenger.attach(kStatus, OnStatus);
  66. }
  67.  
  68. // Called when a received command has no attached function
  69. void OnUnknownCommand()
  70. {
  71. Serial.println("This command is unknown!");
  72. ShowCommands();
  73. }
  74.  
  75. // Callback function that shows a list of commands
  76. void OnCommandList()
  77. {
  78. ShowCommands();
  79. }
  80.  
  81. // Callback function that sets led on or off
  82. void OnSetLed()
  83. {
  84. // Read led state argument, expects 0 or 1 and interprets as false or true
  85. ledState = cmdMessenger.readBoolArg();
  86. ShowLedState();
  87. }
  88.  
  89. // Callback function that sets led on or off
  90. void OnSetLedBrightness()
  91. {
  92. // Read led brightness argument, expects value between 0 to 255
  93. ledBrightness = cmdMessenger.readInt16Arg();
  94. // Set led brightness
  95. SetBrightness();
  96. // Show Led state
  97. ShowLedState();
  98. }
  99.  
  100. // Callback function that shows led status
  101. void OnStatus()
  102. {
  103. // Send back status that describes the led state
  104. ShowLedState();
  105. }
  106.  
  107. // Show available commands
  108. void ShowCommands()
  109. {
  110. Serial.println("Available commands");
  111. Serial.println(" 0; - This command list");
  112. Serial.println(" 1,<led state>; - Set led. 0 = off, 1 = on");
  113. Serial.print (" 2,<led brightness>; - Set led brighness. 0 - ");
  114. Serial.println(PWMinterval);
  115. Serial.println(" 3; - Show led state");
  116. }
  117.  
  118. // Show led state
  119. void ShowLedState()
  120. {
  121. Serial.print("Led status: ");
  122. Serial.println(ledState?"on":"off");
  123. Serial.print("Led brightness: ");
  124. Serial.println(ledBrightness);
  125. }
  126.  
  127. // Set led state
  128. void SetLedState()
  129. {
  130. if (ledState) {
  131. // If led is turned on, go to correct brightness using analog write
  132. analogWrite(kBlinkLed, ledBrightness);
  133. } else {
  134. // If led is turned off, use digital write to disable PWM
  135. digitalWrite(kBlinkLed, LOW);
  136. }
  137. }
  138.  
  139. // Set led brightness
  140. void SetBrightness()
  141. {
  142. // clamp value intervalOn on 0 and PWMinterval
  143. intervalOn = max(min(ledBrightness,PWMinterval),0);
  144. }
  145.  
  146. // Pulse Width Modulation to vary Led intensity
  147. // turn on until intervalOn, then turn off until PWMinterval
  148. bool blinkLed() {
  149. if ( micros() - prevBlinkTime > PWMinterval ) {
  150. // Turn led on at end of interval (if led state is on)
  151. prevBlinkTime = micros();
  152. digitalWrite(kBlinkLed, ledState?HIGH:LOW);
  153. } else if ( micros() - prevBlinkTime > intervalOn ) {
  154. // Turn led off at halfway interval
  155. digitalWrite(kBlinkLed, LOW);
  156. }
  157. }
  158.  
  159. // Setup function
  160. void setup()
  161. {
  162. // Listen on serial connection for messages from the PC
  163. Serial.begin(115200);
  164. // Adds newline to every command
  165. cmdMessenger.printLfCr();
  166.  
  167. // Attach my application's user-defined callback methods
  168. attachCommandCallbacks();
  169.  
  170. // set pin for blink LED
  171. pinMode(kBlinkLed, OUTPUT);
  172. // Show command list
  173. ShowCommands();
  174. }
  175.  
  176. // Loop function
  177. void loop()
  178. {
  179. // Process incoming serial data, and perform callbacks
  180. cmdMessenger.feedinSerialData();
  181. blinkLed();
  182. }