ActuBoard.ino 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include <dummy.h>
  2. #include "doubleSerial.h"
  3. #include "cmdInterpreter.h"
  4. #include "pca9635.h"
  5. #define LED_PIN 2
  6. #define OE_PIN 27
  7. #define MAX_PWM_BOARDS 12 // limited to the fact that not more i2c addresses are available with HW rev A1
  8. doubleSerial doubleS;
  9. cmdInterpreter cmdI;
  10. pca9635 pwm_driver[MAX_PWM_BOARDS];
  11. /**
  12. * read from selected Serial if available
  13. * write it to cmdInterpreter and return if end of command is detected
  14. */
  15. boolean fillBuffer(void) {
  16. uint8_t d = 0;
  17. boolean r = doubleS.readSerial(&d);
  18. //something got read?
  19. if (r) {
  20. return cmdI.addToBuffer(d);
  21. }
  22. return false;
  23. }
  24. void setOE(boolean enable) {
  25. digitalWrite(OE_PIN, enable);
  26. }
  27. /**
  28. * setup task
  29. */
  30. void setup() {
  31. doubleS.selectSerial(false);
  32. cmdI.prepareForNextCommand();
  33. pinMode(LED_PIN, OUTPUT);
  34. digitalWrite(LED_PIN, 0);
  35. pinMode(OE_PIN, OUTPUT);
  36. digitalWrite(OE_PIN, HIGH);
  37. for (uint8_t i = 0; i < MAX_PWM_BOARDS; i++) {
  38. if (!i) {
  39. // init i2c on the first device
  40. pwm_driver[i].begin(0x04, true, true);
  41. } else {
  42. // skip i2c init on others
  43. pwm_driver[i].begin(0x04+i, false, true);
  44. }
  45. pwm_driver[i].set_led_mode(2);
  46. }
  47. }
  48. /**
  49. * task
  50. */
  51. void loop() {
  52. static uint8_t ButtonlastPinState = 1;
  53. static uint16_t ledBlink = 0;
  54. uint8_t pinState = digitalRead(0);
  55. //button handling
  56. if(!pinState && ButtonlastPinState){
  57. boolean useBT = !doubleS.isBT();
  58. digitalWrite(LED_PIN, useBT);
  59. doubleS.selectSerial(useBT);
  60. cmdI.prepareForNextCommand();
  61. }
  62. ButtonlastPinState = pinState;
  63. //command handling
  64. if (fillBuffer()) {
  65. //cmd end received, lets analyse the command
  66. uint8_t r = cmdI.interprete(pwm_driver, MAX_PWM_BOARDS, OE_PIN);
  67. cmdI.prepareForNextCommand();
  68. doubleS.writeSerial(r);
  69. }
  70. // let the led blink, if a user is connected to bluetooth serial console
  71. if (doubleS.isBT()) {
  72. if (ledBlink++ == 0) {
  73. if (doubleS.hasClient()) {
  74. digitalWrite(LED_PIN, !digitalRead(LED_PIN));
  75. } else {
  76. digitalWrite(LED_PIN, true);
  77. }
  78. }
  79. }
  80. }