advanceprg.ino 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #ifdef SPS_USE_DISPLAY
  2. #include <TM1637Display.h>
  3. #endif
  4. #ifndef SPS_USE_DISPLAY
  5. void advancePrg() {}
  6. #endif
  7. #ifdef SPS_USE_DISPLAY
  8. TM1637Display display(DIGIT_CLOCK, DIGIT_DATA_IO);
  9. byte displayValues[4];
  10. const byte PRGS_ADDR = 0;
  11. const byte PRGS_COM = 1;
  12. const byte PRGS_DATA = 2;
  13. Switch prgSwitch = Switch(SW_PRG);
  14. Switch selSwitch = Switch(SW_SEL);
  15. void advancePrg() {
  16. resetDisplay();
  17. showPrg();
  18. dbgOutLn("adv prg mode");
  19. while ((digitalRead(SW_PRG) == 0) || (digitalRead(SW_SEL) == 0));
  20. delay(DEBOUNCE);
  21. addr = 0;
  22. byte Eebyte = EEPROM.read(addr);
  23. boolean dirty = false;
  24. showAddress(addr, Eebyte);
  25. unsigned long saveTime = millis();
  26. byte prgState = PRGS_COM;
  27. showActualState(prgState);
  28. prgSwitch.poll();
  29. selSwitch.poll();
  30. // hier die Programmschleife
  31. do {
  32. prgSwitch.poll();
  33. selSwitch.poll();
  34. boolean lit = (((millis() - saveTime) % 500) > 250);
  35. display.setColon(lit);
  36. outputDisplay();
  37. if (prgSwitch.pushed()) {
  38. prgState += 1;
  39. prgState = prgState % 3;
  40. if (prgState == PRGS_ADDR) {
  41. if (dirty) {
  42. dbgOut("prg:");
  43. dbgOut2(addr, HEX);
  44. dbgOut(':');
  45. dbgOutLn2(Eebyte, HEX);
  46. displayValues[2] = 0x40;
  47. displayValues[3] = 0x40;
  48. outputDisplay();
  49. EEPROM.write(addr, Eebyte);
  50. delay(500);
  51. displayValues[2] = 0x00;
  52. displayValues[3] = 0x00;
  53. outputDisplay();
  54. }
  55. addr = (addr + 1) % E2END;
  56. Eebyte = EEPROM.read(addr);
  57. dirty = false;
  58. prgState = PRGS_COM;
  59. showAddress(addr, Eebyte);
  60. }
  61. showActualState(prgState);
  62. }
  63. if (selSwitch.pushed()) {
  64. switch (prgState) {
  65. case PRGS_COM:
  66. dbgOut('c');
  67. dirty = true;
  68. Eebyte = Eebyte + 0x10;
  69. showAddress(addr, Eebyte);
  70. break;
  71. case PRGS_DATA:
  72. dbgOut('d');
  73. dirty = true;
  74. byte data = Eebyte & 0x0F;
  75. data += 1;
  76. data = data & 0x0F;
  77. Eebyte = (Eebyte & 0xF0) + data;
  78. showAddress(addr, Eebyte);
  79. break;
  80. }
  81. }
  82. }
  83. while (true);
  84. }
  85. void showActualState(byte prgState) {
  86. switch (prgState) {
  87. case PRGS_ADDR:
  88. blinkAddr();
  89. delay(250);
  90. blinkAddr();
  91. break;
  92. case PRGS_COM:
  93. blinkCol(2);
  94. delay(250);
  95. blinkCol(2);
  96. break;
  97. case PRGS_DATA:
  98. blinkCol(3);
  99. delay(250);
  100. blinkCol(3);
  101. break;
  102. }
  103. }
  104. void blinkAddr() {
  105. byte saveValue1 = displayValues[0];
  106. byte saveValue2 = displayValues[1];
  107. displayValues[0] = 0;
  108. displayValues[1] = 0;
  109. outputDisplay();
  110. delay(250);
  111. displayValues[0] = saveValue1;
  112. displayValues[1] = saveValue2;
  113. outputDisplay();
  114. }
  115. void blinkCol(byte col) {
  116. byte saveValue1 = displayValues[col];
  117. displayValues[col] = 0;
  118. outputDisplay();
  119. delay(250);
  120. displayValues[col] = saveValue1;
  121. outputDisplay();
  122. }
  123. void showPrg() {
  124. displayValues[0] = 0b01110011;
  125. displayValues[1] = 0b01010000;
  126. displayValues[2] = 0b01101111;
  127. outputDisplay();
  128. }
  129. void showAddress(word addr, byte Eebyte) {
  130. displayValues[0] = display.encodeDigit(addr >> 4);
  131. displayValues[1] = display.encodeDigit(addr & 15);
  132. data = Eebyte & 15;
  133. com = Eebyte >> 4;
  134. displayValues[2] = display.encodeDigit(com);
  135. displayValues[3] = display.encodeDigit(data);
  136. outputDisplay();
  137. }
  138. // routines for the tm1637 display
  139. void outputDisplay() {
  140. display.setSegments(displayValues);
  141. }
  142. void initDisplay() {
  143. display.setBrightness(0x0f);
  144. resetDisplay();
  145. for (byte i = 0; i < 4; i++) {
  146. displayValues[i] = 0x40;
  147. delay(150);
  148. outputDisplay();
  149. }
  150. delay(250);
  151. resetDisplay();
  152. }
  153. void resetDisplay() {
  154. for (byte i = 0; i < 4; i++) {
  155. displayValues[i] = 0;
  156. }
  157. display.setSegments(displayValues);
  158. }
  159. #endif