setup-loop.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "all-headers.h"
  2. // Bit operation references
  3. const uint32_t bufferEmpty(0);
  4. const uint32_t bufferFull(~ bufferEmpty);
  5. // Button variables
  6. uint32_t antiBlinkBuffer[5];
  7. bool buttonReleased[5];
  8. bool actionDone[5];
  9. DigitalPort LED[5];
  10. DigitalPort BUTTON[5];
  11. // Counter
  12. uint32_t counter(0);
  13. void setup (USER_MODE) {
  14. // Start
  15. digitalWrite(L0_LED, true);
  16. // Init buttons and LEDs
  17. BUTTON[0] = P0_PUSH_BUTTON;
  18. BUTTON[1] = P1_PUSH_BUTTON;
  19. BUTTON[2] = P2_PUSH_BUTTON;
  20. BUTTON[3] = P3_PUSH_BUTTON;
  21. BUTTON[4] = P4_PUSH_BUTTON;
  22. LED[0] = L0_LED;
  23. LED[1] = L1_LED;
  24. LED[2] = L2_LED;
  25. LED[3] = L3_LED;
  26. LED[4] = L4_LED;
  27. for (unsigned int i(0); i < 5; i++) {
  28. antiBlinkBuffer[i] = 0;
  29. buttonReleased[i] = digitalRead(BUTTON[i]);
  30. actionDone[i] = false;
  31. }
  32. // Init LCD
  33. printString(MODE_ "Wake up in ");
  34. printUnsigned(MODE_ millis(MODE));
  35. printString(MODE_ "ms");
  36. }
  37. void loop (USER_MODE) {
  38. // Shift bits
  39. for (unsigned int i(0); i < 5; i++) {
  40. antiBlinkBuffer[i] <<= 1;
  41. if (digitalRead(BUTTON[i]))
  42. antiBlinkBuffer[i] |= 0x01;
  43. // Command leds with buttons
  44. if (antiBlinkBuffer[i] == bufferEmpty) {
  45. buttonReleased[i] = false;
  46. }
  47. else if (antiBlinkBuffer[i] == bufferFull) {
  48. buttonReleased[i] = true;
  49. actionDone[i] = false;
  50. }
  51. if (!buttonReleased[i] && !actionDone[i]) {
  52. digitalToggle(LED[i]);
  53. actionDone[i] = true;
  54. counter ++;
  55. // Prevent persistent digit
  56. if (counter >= 20) {
  57. counter = 0;
  58. clearScreen(MODE);
  59. }
  60. // Show action on LCD
  61. gotoLineColumn(MODE_ 1, 13 - i);
  62. printChar(MODE_ '1');
  63. gotoLineColumn(MODE_ 2, 5 - counter / 10);
  64. printString(MODE_ "Counter:");
  65. printUnsigned(MODE_ counter);
  66. gotoLineColumn(MODE_ 3, 0);
  67. printString(MODE_ "Date:");
  68. printUnsigned(MODE_ millis(MODE));
  69. printString(MODE_ "ms");
  70. }
  71. }
  72. }