firmware.ino 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // libraries //
  2. #include "config.h"
  3. #include "clock.h"
  4. #if defined LIGHTBAR_ENABLED && LIGHTBAR_ENABLED == 1
  5. #include "lightbar.h"
  6. #endif // LIGHTBAR_ENABLED
  7. #include "protocol.h"
  8. #include "gesture.h"
  9. #include "touch.h"
  10. #include "battery.h"
  11. #define ISR_NUM2 2
  12. #define NUM_ISR 3
  13. uint32_t prevOutboundTime;
  14. uint32_t outboundInterval;
  15. uint8_t buttonsPressed;
  16. uint32_t buttonPressTime;
  17. // global variables //
  18. ConfigData mcuConfig;
  19. volatile byte isrFlag[NUM_ISR];
  20. /*
  21. Gesture ISR
  22. */
  23. void gestureInterruptRoutine() {
  24. isrFlag[APDS9960_ISR_NUM] = 1;
  25. }
  26. /*
  27. Touch ISR
  28. */
  29. void touchInterruptRoutine() {
  30. if (digitalRead(JG106C_INT_PIN) == 0) {
  31. // FALLING
  32. isrFlag[JG106C_ISR_NUM] = 1;
  33. }
  34. else {
  35. // RISING
  36. isrFlag[ISR_NUM2] = 1;
  37. }
  38. }
  39. /*
  40. UART ISR
  41. */
  42. char* rxBuff = new char[64];
  43. int rxIndex = 0;
  44. void serialEvent() {
  45. while (Serial.available()) {
  46. char inChar = (char)Serial.read();
  47. if (inChar == '~'){
  48. rxIndex = 0;
  49. continue;
  50. } else if (inChar == ';'){
  51. rxBuff[rxIndex] = 0;
  52. parseInput(rxBuff);
  53. continue;
  54. } else {
  55. rxBuff[rxIndex++] = inChar;
  56. }
  57. }
  58. }
  59. /*
  60. * main program setup function
  61. */
  62. void setup() {
  63. Serial.begin(9600);
  64. // Serial.println(F("SETUP FUNCTION"));
  65. prevOutboundTime = 0;
  66. outboundInterval = 15;
  67. buttonPressTime = 0;
  68. buttonsPressed = 0;
  69. loadConfig();
  70. #if defined LIGHTBAR_ENABLED && LIGHTBAR_ENABLED == 1
  71. lightBarInit();
  72. // lightBarSetColor(0x6432c8);
  73. #endif // LIGHTBAR_ENABLED
  74. #if defined TOUCH_ENABLED && TOUCH_ENABLED == 1
  75. touchInit();
  76. #endif // TOUCH_ENABLED
  77. // initialize pins
  78. pinMode(APDS9960_INT_PIN, INPUT);
  79. pinMode(JG106C_INT_PIN, INPUT);
  80. // initialize peripherals
  81. setupClock();
  82. #if defined GESTURE_ENABLED && GESTURE_ENABLED == 1
  83. setupGesture();
  84. setupAmbientLight();
  85. #endif // GESTURE_ENABLED
  86. // Initialize interrupt service routines
  87. #if GESTURE_INTERRUPT
  88. // gesture sensor
  89. attachInterrupt(0, gestureInterruptRoutine, FALLING);
  90. #endif // GESTURE_INTERRUPT
  91. // touch sensor
  92. attachInterrupt(digitalPinToInterrupt(JG106C_INT_PIN), touchInterruptRoutine, CHANGE);
  93. }
  94. // the super-loop function
  95. void loop() {
  96. // handle clock and time display
  97. handleTime();
  98. // handle touch
  99. if (isrFlag[JG106C_ISR_NUM] == 1 || isrFlag[ISR_NUM2] == 1) {
  100. detachInterrupt(digitalPinToInterrupt(JG106C_INT_PIN));
  101. #if defined TOUCH_ENABLED && TOUCH_ENABLED == 1
  102. if (isrFlag[JG106C_ISR_NUM] == 1) {
  103. buttonPressTime = millis();
  104. buttonsPressed = handleTouch();
  105. isrFlag[JG106C_ISR_NUM] = 0;
  106. }
  107. if (isrFlag[ISR_NUM2] == 1) {
  108. sendButtonCommand(buttonsPressed, millis() - buttonPressTime);
  109. // reset
  110. buttonPressTime = 0;
  111. buttonsPressed = 0;
  112. isrFlag[ISR_NUM2] = 0;
  113. }
  114. #endif // TOUCH_ENABLED
  115. attachInterrupt(digitalPinToInterrupt(JG106C_INT_PIN), touchInterruptRoutine, CHANGE);
  116. }
  117. // handle gestures
  118. #if GESTURE_INTERRUPT
  119. if (isrFlag[APDS9960_ISR_NUM] == 1) {
  120. detachInterrupt(0);
  121. #if defined GESTURE_ENABLED && GESTURE_ENABLED == 1
  122. handleGesture();
  123. #endif // GESTURE_ENABLED
  124. isrFlag[APDS9960_ISR_NUM] = 0;
  125. attachInterrupt(0, gestureInterruptRoutine, FALLING);
  126. }
  127. #elif GESTURE_ENABLED
  128. handleGesture();
  129. #endif // GESTURE_INTERRUPT
  130. // handle ambient light brightness
  131. #if defined GESTURE_ENABLED && GESTURE_ENABLED == 1
  132. handleAmbientLight();
  133. #endif // GESTURE_ENABLED
  134. // update lightbar
  135. #if defined LIGHTBAR_ENABLED && LIGHTBAR_ENABLED == 1
  136. lightBarFade();
  137. #endif // LIGHTBAR_ENABLED
  138. // outbound communication on an interval
  139. if (getEpoch() - prevOutboundTime >= outboundInterval) {
  140. // ambient light
  141. #if defined GESTURE_ENABLED && GESTURE_ENABLED == 1
  142. // onion.io: testing - removed this
  143. sendAmbientLightReading();
  144. #endif // GESTURE_ENABLED
  145. #if defined BATTERY_ENABLED && BATTERY_ENABLED == 1
  146. // onion.io: testing - removed this
  147. sendBatteryLevel();
  148. #endif // BATTERY_ENABLED
  149. // prepare for next interval
  150. prevOutboundTime = getEpoch();
  151. }
  152. }