prgmode.ino 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. entering the programming mode
  3. */
  4. #define BLINK_DELAY 250
  5. #define SHOW_DELAY 500
  6. #define KEY_DELAY 250
  7. enum PROGRAMMING_MODE {ADDRESS, COMMAND, DATA};
  8. PROGRAMMING_MODE prgMode;
  9. void programMode() {
  10. // checking if advance programmer board connected?
  11. #ifdef SPS_ENHANCEMENT
  12. if (digitalRead(SW_SEL) == 0) {
  13. advancePrg();
  14. }
  15. else {
  16. #endif
  17. dbgOutLn("PrgMode");
  18. // light up all LEDs
  19. doPort(0x08);
  20. while (digitalRead(SW_PRG) == 0) {
  21. // waiting for PRG to release
  22. }
  23. blinkAll();
  24. prgMode = ADDRESS;
  25. addr = 0;
  26. do {
  27. blinkD1();
  28. dbgOut("Adr:");
  29. dbgOutLn(addr);
  30. // LoNibble Adresse anzeigen
  31. doDimPort(addr);
  32. blinkD2();
  33. // HiNibble Adresse anzeigen
  34. data = (addr & 0xf0) >> 4; //Adresse anzeigen
  35. doDimPort(data);
  36. byte Eebyte = EEPROM.read(addr);
  37. data = Eebyte & 15;
  38. com = Eebyte >> 4;
  39. blinkD3();
  40. prgMode = COMMAND;
  41. dbgOutLn("com");
  42. doPort(com); //show command
  43. do {
  44. if (digitalRead(SW_SEL) == 0) {
  45. delay(KEY_DELAY);
  46. com += 1;
  47. com = com & 0x0F;
  48. doPort(com);
  49. }
  50. }
  51. while (digitalRead(SW_PRG) == 1);
  52. delay(DEBOUNCE);
  53. blinkD4();
  54. prgMode = DATA;
  55. dbgOutLn("dat");
  56. doPort(data); //show data
  57. do {
  58. if (digitalRead(SW_SEL) == 0) {
  59. delay(KEY_DELAY);
  60. data += 1;
  61. data = data & 0x0F;
  62. doPort(data);
  63. }
  64. }
  65. while (digitalRead(SW_PRG) == 1); // S2 = 1
  66. delay(DEBOUNCE);
  67. byte newValue = (com << 4) + data;
  68. if (newValue != Eebyte) {
  69. EEPROM.write(addr, newValue); // Writeeeprom Eebyte , Addr
  70. blinkAll();
  71. }
  72. addr += 1;
  73. }
  74. while (true);
  75. #ifdef SPS_ENHANCEMENT
  76. }
  77. #endif
  78. }
  79. void blinkAll() {
  80. blinkNull();
  81. doPort(0x0F);
  82. delay(BLINK_DELAY);
  83. }
  84. void blinkD1() {
  85. blinkNull();
  86. doPort(0x01);
  87. delay(BLINK_DELAY);
  88. blinkNull();
  89. }
  90. void blinkD2() {
  91. blinkNull();
  92. doPort(0x02);
  93. delay(BLINK_DELAY);
  94. blinkNull();
  95. }
  96. void blinkD3() {
  97. blinkNull();
  98. doPort(0x04);
  99. delay(BLINK_DELAY);
  100. blinkNull();
  101. }
  102. void blinkD4() {
  103. blinkNull();
  104. doPort(0x08);
  105. delay(BLINK_DELAY);
  106. blinkNull();
  107. }
  108. void blinkNull() {
  109. doPort(0x00);
  110. delay(BLINK_DELAY);
  111. }
  112. void doDimPort(byte data) {
  113. long value = millis() + SHOW_DELAY;
  114. do {
  115. // on part
  116. digitalWrite(Dout_0, 1);
  117. digitalWrite(Dout_1, 1);
  118. digitalWrite(Dout_2, 1);
  119. digitalWrite(Dout_3, 1);
  120. delay(5);
  121. // off part
  122. if ((data & 0x01) == 0) {
  123. digitalWrite(Dout_0, 0);
  124. }
  125. if ((data & 0x02) == 0) {
  126. digitalWrite(Dout_1, 0);
  127. }
  128. if ((data & 0x04) == 0) {
  129. digitalWrite(Dout_2, 0);
  130. }
  131. if ((data & 0x08) == 0) {
  132. digitalWrite(Dout_3, 0);
  133. }
  134. delay(5);
  135. } while (value < millis());
  136. }