setup-loop.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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_ "Welcome Jovian");
  34. }
  35. void loop (USER_MODE) {
  36. // Shift bits
  37. for (unsigned int i(0); i < 5; i++) {
  38. antiBlinkBuffer[i] <<= 1;
  39. if (digitalRead(BUTTON[i]))
  40. antiBlinkBuffer[i] |= 0x01;
  41. // Command leds with buttons
  42. if (antiBlinkBuffer[i] == bufferEmpty) {
  43. buttonReleased[i] = false;
  44. }
  45. else if (antiBlinkBuffer[i] == bufferFull) {
  46. buttonReleased[i] = true;
  47. actionDone[i] = false;
  48. }
  49. if (!buttonReleased[i] && !actionDone[i]) {
  50. digitalToggle(LED[i]);
  51. actionDone[i] = true;
  52. counter ++;
  53. counter %= 20;
  54. // Show action on LCD
  55. gotoLineColumn(MODE_ 1, 13 - i);
  56. printChar(MODE_ '1');
  57. gotoLineColumn(MODE_ 2, 12);
  58. printUnsigned(MODE_ counter);
  59. }
  60. }
  61. }