|
| 1 | +// NOTE: This code is not yet finished, it still needs to be made into a library. |
| 2 | + |
| 3 | +bool menuRunning=0; |
| 4 | + |
| 5 | +void loop() // with debug 170us !!! Attention: is blocked during menu operations !!! |
| 6 | +{ button.poll(); |
| 7 | + if(longPress()) selectMenu(); // has its own button.poll() |
| 8 | + else dongleLoop(); |
| 9 | +} |
| 10 | + |
| 11 | +void selectMenu() |
| 12 | +{ menuRunning = 1; // stop the ledBar updates in the yamahaISR because this disturbes the ledbar |
| 13 | + if(debug) Serial << "\nselectMenu"; |
| 14 | + ledBar.turnLedsOff(); |
| 15 | + delay(2000); |
| 16 | + int menuNo = countFlashes(5); // there are 5 menus |
| 17 | + switch(menuNo) |
| 18 | + { case 1: setSpecified_mAh(); break; |
| 19 | + case 2: showValueInTenths(specified_mAh/100); break; // show in Ah with one decimal 12500 -> 125 |
| 20 | + case 3: setFuelGaugeFull(); break; |
| 21 | + case 4: setendOffChargeVoltage(); break; |
| 22 | + case 5: showValueInTenths(endOffChargeVoltage*10); break; // show in Volt with one decimal 41.5V -> 415 |
| 23 | + } |
| 24 | + delay(2000); |
| 25 | + if(debug) Serial << "\nend"; |
| 26 | + menuRunning = 0; |
| 27 | +} |
| 28 | + |
| 29 | +long setValueInTenths() |
| 30 | +{ if(debug) Serial << "\nSet Value"; |
| 31 | + |
| 32 | + ledBar.flash(5); // must be without delay |
| 33 | + int tens = countFlashes(9); // enter tens 12.5Ah, tens=1 |
| 34 | + |
| 35 | + ledBar.flash(5); |
| 36 | + int units = countFlashes(9); // enter units 12.5Ah, units=2 |
| 37 | + delay(1000); |
| 38 | + |
| 39 | + ledBar.flash(5); |
| 40 | + int tenths = countFlashes(9); // enter decimal 12.5Ah decimal=5 |
| 41 | + delay(1000); |
| 42 | + |
| 43 | + long value = tens*100 + units*10 + tenths; |
| 44 | + if(debug) Serial << "\nValue = " << value; |
| 45 | + return value; |
| 46 | +} |
| 47 | + |
| 48 | +void showValueInTenths(long value) |
| 49 | +{ if(debug) Serial << "\nShow value: " << value; |
| 50 | + |
| 51 | + int tenths = value % 10; // operator % needs int or long |
| 52 | + |
| 53 | + value = value / 10; |
| 54 | + int units = value % 10; |
| 55 | + |
| 56 | + value = value/ 10; |
| 57 | + int tens = value % 10; |
| 58 | + |
| 59 | + delayFlashesDelay(1000, 5, 1000); // start |
| 60 | + for(int i=1; i <= tens; i++) delayFlashesDelay(0, 1, 500); // show tens |
| 61 | + |
| 62 | + delayFlashesDelay(1000, 5, 1000); // start |
| 63 | + for(int i=1; i <= units; i++) delayFlashesDelay(0, 1, 500); // show units |
| 64 | + |
| 65 | + delayFlashesDelay(1000, 5, 1000); // start |
| 66 | + for(int i=1; i <= tenths; i++) delayFlashesDelay(0, 1, 500); // show decimal |
| 67 | + |
| 68 | + delayFlashesDelay(1000, 5, 1000); // start |
| 69 | +} |
| 70 | + |
| 71 | +void delayFlashesDelay(unsigned long delay1, int flashes, unsigned long delay2) |
| 72 | +{ delay(delay1); |
| 73 | + ledBar.flash(flashes); |
| 74 | + delay(delay2); |
| 75 | +} |
| 76 | + |
| 77 | +int countFlashes(int maxCount) |
| 78 | +{ int i=0; |
| 79 | + bool pressed=0; |
| 80 | + if(clicked()) i=0; |
| 81 | + else |
| 82 | + { for(i=1; i <= maxCount; i++) // first flash = 1 |
| 83 | + { ledBar.flash(1); |
| 84 | + if(clicked()) // stop with counting |
| 85 | + { pressed = 1; |
| 86 | + break; |
| 87 | + } |
| 88 | + } |
| 89 | + if(!pressed) i=0; // for input zero: do nothing |
| 90 | + } |
| 91 | + if(debug) Serial << "\nno = " << i; |
| 92 | + return i; |
| 93 | +} |
| 94 | + |
| 95 | +bool clicked() |
| 96 | +{ bool returnVal=0; |
| 97 | + unsigned long start_ms = millis(); |
| 98 | + while(millis() < start_ms + countFlashesInterval_ms) |
| 99 | + { button.poll(); |
| 100 | + if(button.pushed()) returnVal = 1; |
| 101 | + } |
| 102 | + return returnVal; |
| 103 | +} |
0 commit comments