Skip to content

Commit f855725

Browse files
committed
a bit of tidying
1 parent ac9d942 commit f855725

File tree

1 file changed

+74
-72
lines changed

1 file changed

+74
-72
lines changed

sixtube_lm.ino

Lines changed: 74 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,14 @@ const byte altAdjFn = 255; //if equipped
3939

4040
const byte enableSoftAlarmSwitch = 1;
4141
// 1 = yes. Use if using the integrated beeper or another non-switched device (bell solenoid, etc).
42-
// 0 = no. Use if the connected alarm device has its own switch (e.g. clock radio function switch). Alarm will be permanently on in software.
42+
// 0 = no. Use if the connected alarm device has its own switch (e.g. clock radio function switch).
43+
// Alarm will be permanently on in software.
4344
const byte alarmRadio = 0;
44-
// 0 = no. Alarm output is connected to the onboard piezoelectric beeper or similar signal device. When alarm and timer go off, it will output a beep pattern for alarmDur minutes.
45-
// 1 = yes. Alarm output is connected to a relay to switch other equipment (like a radio). When alarm goes off, output will stay on for alarmDur minutes (120 is common). When timer is running, output will stay on until timer runs down.
45+
// 0 = no. Alarm output is connected to the onboard piezoelectric beeper or similar signal device.
46+
// When alarm and timer go off, it will output a beep pattern for alarmDur minutes.
47+
// 1 = yes. Alarm output is connected to a relay to switch other equipment (like a radio).
48+
// When alarm goes off, output will stay on for alarmDur minutes (120 is common).
49+
// When timer is running, output will stay on until timer runs down.
4650
const byte alarmDur = 3;
4751

4852
// How long (in ms) are the button hold durations?
@@ -56,10 +60,6 @@ byte clockFn = 0; //currently displayed function: 0=time, 1=date, 2=alarm, 3=tim
5660
byte clockFnSet = 0; //whether this function is currently being set, and which option/page it's on
5761
byte setupOptsCt = 3; //1-index
5862
byte setupOpts[4] = {0,0,1,1}; //see ctrlEvt() switch(clockFnSet) for what these are/do
59-
//0 = nothing, just a pad to make this 1-index
60-
//1 = dim(1,0)
61-
//2 = doesn't do anything, choose a number from 1 to 7
62-
//3 = doesn't do anything, choose a number from 1 to 13
6363

6464
byte displayNext[6] = {15,15,15,15,15,15}; //Blank tubes at start. When display should change, put it here
6565
int btnPresses = 0;
@@ -73,34 +73,34 @@ int btnPresses = 0;
7373
//RTC_DS1307 RTC;
7474

7575
void setup(){
76-
Serial.begin(57600);
77-
//Wire.begin(); TODO
78-
//RTC.begin(); TODO
79-
initOutputs();
80-
initInputs();
76+
Serial.begin(57600);
77+
//Wire.begin(); TODO
78+
//RTC.begin(); TODO
79+
initOutputs();
80+
initInputs();
8181
updateDisplay(); //initial fill of data
8282
}
8383

8484
void loop(){
85-
//Things done every "clock cycle"
86-
checkInputs(); //will do things if necessary and updateDisplay
87-
cycleDisplay(); //keeps the multiplexing cycle going
85+
//Things done every "clock cycle"
86+
checkInputs(); //will do things if necessary and updateDisplay
87+
cycleDisplay(); //keeps the multiplexing cycle going
8888
}
8989

9090

9191
////////// Control inputs //////////
9292
unsigned long inputSampleLast = 0; //millis() of last time inputs were sampled
9393
int inputSampleDur = 50; //input sampling frequency (in ms) to avoid bounce
94-
//bool mainRotLast[2] = {0,0}; //last state of main rotary encoder inputs
94+
//bool mainRotLast[2] = {0,0}; //last state of main rotary encoder inputs TODO implement
9595
//bool altRotLast[2] = {0,0}; //and alt (if equipped)
9696

9797
void initInputs(){
9898
//TODO are there no "loose" pins left floating after this? per https://electronics.stackexchange.com/q/37696/151805
9999
pinMode(A0, INPUT_PULLUP);
100100
pinMode(A1, INPUT_PULLUP);
101-
pinMode(A2, INPUT_PULLUP);
101+
pinMode(A2, INPUT_PULLUP);
102102
pinMode(A3, INPUT_PULLUP);
103-
//4 and 5 used for I2C
103+
//4 and 5 used for I2C
104104
pinMode(A6, INPUT); digitalWrite(A6, HIGH);
105105
pinMode(A7, INPUT); digitalWrite(A7, HIGH);
106106
//If using rotary encoders, capture their initial state
@@ -110,8 +110,8 @@ void initInputs(){
110110

111111
void checkInputs(){
112112
// TODO can all this if/else business be defined at load instead of evaluated every sample?
113-
if(millis() >= inputSampleLast+inputSampleDur) { //time for a sample
114-
inputSampleLast = millis();
113+
if(millis() >= inputSampleLast+inputSampleDur) { //time for a sample
114+
inputSampleLast = millis();
115115
//potential issue: if user only means to rotate or push encoder but does both?
116116
checkBtn(mainSel); //main select
117117
//if(mainAdjType==2) checkRot(mainAdj,mainAdjRotLast,true); //main rotary encoder
@@ -122,7 +122,7 @@ void checkInputs(){
122122
//if(altAdjType==2) checkRot(altAdj,altAdjRotLast,true); //alt rotary encoder (if equipped)
123123
//else
124124
//if(altAdjType==1) { checkBtn(altAdj[0]); checkBtn(altAdj[1]); } //alt adj buttons
125-
} //end if time for a sample
125+
} //end if time for a sample
126126
}
127127

128128
// void checkRot(rot[],last[],triggerEvent){
@@ -139,38 +139,39 @@ void checkInputs(){
139139

140140
byte btnCur = 0; //momentary button currently in use - only one allowed at a time
141141
unsigned long btnCurStart = 0; //when the current button was pressed
142-
byte btnCurHeld = 0; //hold thresholds passed: 0=none, 1=reserved TODO, 2=short, 3=long, 4=mute further actions from this button until after it's released
142+
byte btnCurHeld = 0; //hold thresholds passed: 0=none, 1=reserved TODO, 2=short, 3=long,
143+
//4=mute further actions from this button until after it's released
143144
void checkBtn(byte btn){
144-
//Changes in momentary buttons, LOW = pressed.
145-
//When a button event has occurred, will call ctrlEvt(btn,evt)
146-
//evt: 0=pressed, 1=released, 2=held past med point, 3=held past long point
145+
//Changes in momentary buttons, LOW = pressed.
146+
//When a button event has occurred, will call ctrlEvt(btn,evt)
147+
//evt: 0=pressed, 1=released, 2=held past med point, 3=held past long point
147148
bool bnow = readInput(btn);
148-
//If the button has just been pressed, and no other buttons are in use...
149-
if(btnCur==0 && bnow==LOW) {
150-
btnCur = btn; btnCurHeld = 0; btnCurStart = millis();
149+
//If the button has just been pressed, and no other buttons are in use...
150+
if(btnCur==0 && bnow==LOW) {
151+
btnCur = btn; btnCurHeld = 0; btnCurStart = millis();
151152
log("Btn "); log(String(btn)); log(" has been pressed\n");
152-
ctrlEvt(btn,1); //hey, the button has been pressed
153-
}
154-
//If the button is being held...
155-
if(btnCur==btn && bnow==LOW) {
156-
if(millis() >= btnCurStart+btnLongHold && btnCurHeld < 3) {
157-
btnCurHeld = 3;
153+
ctrlEvt(btn,1); //hey, the button has been pressed
154+
}
155+
//If the button is being held...
156+
if(btnCur==btn && bnow==LOW) {
157+
if(millis() >= btnCurStart+btnLongHold && btnCurHeld < 3) {
158+
btnCurHeld = 3;
158159
log("Btn "); log(String(btn)); log(" has been long-held\n");
159-
ctrlEvt(btn,3); //hey, the button has been held past the long point
160-
}
161-
else if(millis() >= btnCurStart+btnShortHold && btnCurHeld < 2) {
162-
btnCurHeld = 2;
160+
ctrlEvt(btn,3); //hey, the button has been held past the long point
161+
}
162+
else if(millis() >= btnCurStart+btnShortHold && btnCurHeld < 2) {
163+
btnCurHeld = 2;
163164
log("Btn "); log(String(btn)); log(" has been short-held\n");
164-
ctrlEvt(btn,2); //hey, the button has been held past the med point
165-
}
166-
}
167-
//If the button has just been released...
168-
if(btnCur==btn && bnow==HIGH) {
169-
btnCur = 0;
165+
ctrlEvt(btn,2); //hey, the button has been held past the med point
166+
}
167+
}
168+
//If the button has just been released...
169+
if(btnCur==btn && bnow==HIGH) {
170+
btnCur = 0;
170171
log("Btn "); log(String(btn)); log(" has been released\n\n");
171172
if(btnCurHeld < 4) ctrlEvt(btn,0); //hey, the button was released
172173
btnCurHeld = 0;
173-
}
174+
}
174175
}
175176
bool readInput(byte pin){
176177
if(pin==A6 || pin==A7) return analogRead(pin)<100?0:1; //analog-only pins
@@ -179,7 +180,9 @@ bool readInput(byte pin){
179180
void btnStop(){ btnCurHeld = 4; }
180181

181182
void ctrlEvt(byte ctrl, byte evt){
182-
//In some cases, when reacting to evt 1/2/3, we may want to call btnStop() so following events 2/3/0 don't cause unintended behavior.
183+
//Handle button (for now) events.
184+
//In some cases, when reacting to evt 1/2/3, we may want to call btnStop()
185+
//so following events 2/3/0 don't cause unintended behavior.
183186
if(clockFn==255) { //SETUP menu
184187
if(ctrl==mainSel) {
185188
if(evt==1) { //mainSel press
@@ -222,7 +225,7 @@ void ctrlEvt(byte ctrl, byte evt){
222225
} else { //normal function running
223226
//short hold: enter clockFnSet
224227
//press: increment counter
225-
if(ctrl==mainSel && evt==1) { btnPresses++; log("Incrementing counter to "); log(String(btnPresses)); log("\n"); updateDisplay(); return; }
228+
if(ctrl==mainSel && evt==1) {btnPresses++; log("Incrementing counter to "); log(String(btnPresses)); log("\n"); updateDisplay(); return; }
226229
}
227230
}
228231
}
@@ -246,21 +249,21 @@ void updateDisplay(){
246249
//This function takes that data and uses it to edit displayNext[] for cycleDisplay() to pick up
247250
switch(clockFn){ //which function are we displaying?
248251
case 255: //SETUP menu
249-
log("Per SETUP, updating display to key="); log(String(clockFnSet)); log(", val="); log(String(setupOpts[clockFnSet])); log("\n");
250-
editDisplay(clockFnSet, 4, 5, false); //current option key, displayed on little tubes (4-5)
251-
editDisplay(setupOpts[clockFnSet], 0, 3, false); //current option value, on big tubes (0-3)
252+
log("Per SETUP, updating display to key="); log(String(clockFnSet)); log(", val="); log(String(setupOpts[clockFnSet])); log("\n");
253+
editDisplay(clockFnSet, 4, 5, false); //current option key, displayed on little tubes (4-5)
254+
editDisplay(setupOpts[clockFnSet], 0, 3, false); //current option value, on big tubes (0-3)
252255
case 1: //date
253-
break;
256+
break;
254257
case 2: //alarm
255-
break;
258+
break;
256259
case 3: //timer
257-
break;
260+
break;
258261
default: //clock
259-
//TODO currently we are just displaying the btnPresses count
260-
log("Per normal running mode, displaying btnPresses="); log(String(btnPresses)); log("\n");
261-
editDisplay(btnPresses, 0, 3, true); //big tubes: counter – pad with leading zeros
262-
blankDisplay(4,5); //small tubes blank
263-
break;
262+
//TODO currently we are just displaying the btnPresses count
263+
log("Per normal running mode, displaying btnPresses="); log(String(btnPresses)); log("\n");
264+
editDisplay(btnPresses, 0, 3, true); //big tubes: counter – pad with leading zeros
265+
blankDisplay(4,5); //small tubes blank
266+
break;
264267
} //end switch clockFn
265268
} //end updateDisplay()
266269
void editDisplay(int n, byte posStart, byte posEnd, bool leadingZeros){
@@ -321,10 +324,12 @@ void cycleDisplay(){
321324
if(clockFnSet>0) { //but if we're setting, dim for every other 500ms since we started setting
322325
if(setStartLast==0) setStartLast = millis();
323326
dim = 1-(((millis()-setStartLast)/500)%2);
324-
} else if(setStartLast>0) setStartLast=0;
327+
} else {
328+
if(setStartLast>0) setStartLast=0;
329+
}
325330

326331
//Anode channel 0: tubes #2 (min x10) and #5 (sec x1)
327-
setCathodes(displayLast[2],displayLast[5]); //Via d2b decoder chip, set cathodes to old digits
332+
setCathodes(displayLast[2],displayLast[5]); //Via d2b decoder chip, set cathodes to old digits
328333
digitalWrite(anodes[0], HIGH); //Turn on tubes
329334
delay(displayLastFade[0]/(dim?4:1)); //Display for fade-out cycles
330335
setCathodes(displayNext[2],displayNext[5]); //Switch cathodes to new digits
@@ -343,7 +348,7 @@ void cycleDisplay(){
343348

344349
if(dim) delay(fadeMax/1.5);
345350

346-
//Anode channel 2: tubes #0 (hour x10) and #3 (min x1)
351+
//Anode channel 2: tubes #0 (hour x10) and #3 (min x1)
347352
setCathodes(displayLast[0],displayLast[3]);
348353
digitalWrite(anodes[2], HIGH);
349354
delay(displayLastFade[2]/(dim?4:1));
@@ -354,37 +359,34 @@ void cycleDisplay(){
354359
if(dim) delay(fadeMax*0.75);
355360

356361
// Loop thru and update all the arrays, and fades.
357-
for( int i = 0 ; i < 6 ; i ++ )
358-
{
359-
if( displayNext[i] != displayLast[i] )
360-
{
362+
for( int i = 0 ; i < 6 ; i ++ ) {
363+
if( displayNext[i] != displayLast[i] ) {
361364
displayNextFade[i] += fadeStep;
362365
displayLastFade[i] -= fadeStep;
363366

364-
if( displayNextFade[i] >= fadeMax )
365-
{
367+
if( displayNextFade[i] >= fadeMax ){
366368
displayNextFade[i] = 0.0f;
367369
displayLastFade[i] = fadeMax;
368370
displayLast[i] = displayNext[i];
369371
}
370372
}
371373
}
372-
}
374+
} //end cycleDisplay()
373375
void setCathodes(int decValA, int decValB){
374376
bool binVal[4]; //4-bit binary number with values [1,2,4,8]
375377
decToBin(binVal,decValA); //have binary value of decVal set into binVal
376-
for(byte i=0; i<4; i++) digitalWrite(binOutA[i],binVal[i]); //set bin inputs of SN74141 A, which in turn set tube cathodes
378+
for(byte i=0; i<4; i++) digitalWrite(binOutA[i],binVal[i]); //set bin inputs of SN74141
377379
decToBin(binVal,decValB);
378-
for(byte i=0; i<4; i++) digitalWrite(binOutB[i],binVal[i]); //set bin inputs of SN74141 B. Actual tubes powered depends on anode channel.
379-
}
380+
for(byte i=0; i<4; i++) digitalWrite(binOutB[i],binVal[i]); //set bin inputs of SN74141
381+
} //end setCathodes()
380382
void decToBin(bool binVal[], int i){
381383
//binVal is a reference (modify in place) of a binary number bool[4] with values [1,2,4,8]
382384
if(i<0 || i>15) i=15; //default value, turns tubes off
383385
binVal[3] = int(i/8)%2;
384386
binVal[2] = int(i/4)%2;
385387
binVal[1] = int(i/2)%2;
386388
binVal[0] = i%2;
387-
}
389+
} //end decToBin()
388390

389391

390392
////////// Misc global utility functions //////////

0 commit comments

Comments
 (0)