setup-loop.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. void setup (USER_MODE) {
  12. // Start
  13. digitalWrite(L0_LED, true);
  14. BUTTON[0] = P0_PUSH_BUTTON;
  15. BUTTON[1] = P1_PUSH_BUTTON;
  16. BUTTON[2] = P2_PUSH_BUTTON;
  17. BUTTON[3] = P3_PUSH_BUTTON;
  18. BUTTON[4] = P4_PUSH_BUTTON;
  19. LED[0] = L0_LED;
  20. LED[1] = L1_LED;
  21. LED[2] = L2_LED;
  22. LED[3] = L3_LED;
  23. LED[4] = L4_LED;
  24. for (unsigned int i(0); i < 5; i++) {
  25. antiBlinkBuffer[i] = 0;
  26. buttonReleased[i] = digitalRead(BUTTON[i]);
  27. actionDone[i] = false;
  28. }
  29. }
  30. void loop (USER_MODE) {
  31. // Shift bits
  32. for (unsigned int i(0); i < 5; i++) {
  33. antiBlinkBuffer[i] <<= 1;
  34. if (digitalRead(BUTTON[i]))
  35. antiBlinkBuffer[i] |= 0x01;
  36. // Command leds with buttons
  37. if (antiBlinkBuffer[i] == bufferEmpty) {
  38. buttonReleased[i] = false;
  39. }
  40. else if (antiBlinkBuffer[i] == bufferFull) {
  41. buttonReleased[i] = true;
  42. actionDone[i] = false;
  43. }
  44. if (!buttonReleased[i] && !actionDone[i]) {
  45. digitalToggle(LED[i]);
  46. actionDone[i] = true;
  47. }
  48. }
  49. }