Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
Timeout before aux display reverts to default page
  • Loading branch information
tomcourt authored and tomcourt committed Jul 30, 2017
1 parent 2404091 commit 6df5ca8
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 111 deletions.
5 changes: 4 additions & 1 deletion enguino/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ AuxDisplay auxDisplay[] = {
AUX(A,L,t, , &voltS, &voltS),
};

// number of pages shown on startup
// number of pages shown on startup, immedietly after the startup pages is the default page
#define AUX_STARTUP_PAGES 2

// seconds before aux goes back to default page (assuming no acknowleged alerts), max. value 30
#define SHOW_DEFAULT_AUX_PAGE_TIMEOUT 10

198 changes: 111 additions & 87 deletions enguino/enguino.ino
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,22 @@ EthernetServer server(80); // Port 80 is HTTP
EthernetClient client;

// #define DEBUG // checks RAM usage
// #define SIMULATE_SENSORS 3 // number of simulated sensor 'states', use serial to advace state
#define SIMULATE_SENSORS 3 // number of simulated sensor 'states', use serial to advace state

// sketches don't like typdef's so they are in in this header file instead
#include "egTypes.h"

#include "utility.h"

#define HOBBS_COUNT_INTERVAL (3600/40) // update hobbs 40 times an hour
bool leanMode;
int peakEGT[4];

bool eeUpdateDirty;
byte hobbsCount = HOBBS_COUNT_INTERVAL/2; // in order to prevent cumulative hobbs error assume half a hobbs count of engine run time was lost on last shutdown
bool engineRunning;

volatile bool tachDidPulse;
volatile int rpm[8];
bool engineRunning;

// printLED functions for the auxiliary display
#include "printLED.h"
#include "printAux.h"

// configuration of sensors and layout of the gauges
#include "config.h"
Expand All @@ -52,11 +49,12 @@ volatile int rpm[8];
// Measure thermocouple tempertures in the background
#include "tcTemp.h"


byte auxPage = 0;
signed char blinkAux[2];
bool dimAux;
bool didHoldKey;
bool didChangeDim;
byte auxPage = 0;
signed char blinkAux[2];



Expand Down Expand Up @@ -102,10 +100,11 @@ void checkForAlerts(bool warning) {
}
}

void showAuxPage(byte inx) {
AuxDisplay *a = auxDisplay+inx;
for (byte n=0; n<2; n++) {



void showAuxPage() {
AuxDisplay *a = auxDisplay + auxPage;
for (byte n=0; n<2; n++) {
if (a->alertState[n] & WARNING_ANY) {
alertStatus = STATUS_WARNING;
if (blinkAux[n] == 0)
Expand All @@ -131,32 +130,81 @@ void showAuxPage(byte inx) {
}
}

bool ackAlert(byte inx) {
if (auxDisplay[inx].warning > 0) {
auxDisplay[inx].warning = -1;
auxDisplay[inx].caution = -1;


bool ackAlert() {
if (auxDisplay[auxPage].warning > 0) {
auxDisplay[auxPage].warning = -1;
auxDisplay[auxPage].caution = -1;
return true;
}
else if (auxDisplay[inx].caution > 0) {
auxDisplay[inx].caution = -1;
else if (auxDisplay[auxPage].caution > 0) {
auxDisplay[auxPage].caution = -1;
return true;
}
return false;
}

void ackBlink() {
if (blinkAux[0] > 0)
bool ackBlink() {
bool ack = false;
if (blinkAux[0] > 0) {
blinkAux[0] = -1;
if (blinkAux[1] > 0)
ack = true;
}
if (blinkAux[1] > 0) {
blinkAux[1] = -1;
ack = true;
}
return ack;
}



// Press (acknowedge/next-page)
inline void buttonPress() {
if (ackBlink()) {
ackAlert();
}
else {
if (ackAlert())
auxPage = AUX_STARTUP_PAGES;
else {
auxPage++;
if (auxPage >= N(auxDisplay))
auxPage = AUX_STARTUP_PAGES;
}
}
}

// Hold (reshow all alerts)
inline void buttonHold() {
blinkAux[0] = blinkAux[1] = 0;
for (byte i=AUX_STARTUP_PAGES; i<N(auxDisplay); i++)
auxDisplay[i].warning = auxDisplay[i].caution = 0;
auxPage = AUX_STARTUP_PAGES;
didHoldKey = true;
}

// Long hold (toggle dim and bright)
inline void buttonLongHold() {
dimAux = !dimAux;
for (byte line=0; line<2; line++)
commandLED(line, dimAux?HT16K33_BRIGHT_MIN:HT16K33_BRIGHT_MAX);
didChangeDim = true;
didHoldKey = false;
}

///////////////////////////////////////////////////////////////////

void setup() {
Serial.begin(9600);
// while (!Serial)
// ; // wait for serial port to connect. Stops here until Serial Monitor is started. Good for debugging setup

sensorSetup();
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();

delay(1); // delay to allow LED display chip to startup

Expand All @@ -165,58 +213,14 @@ void setup() {
tcTempSetup();
printLEDSetup();
for (auxPage=0; auxPage<AUX_STARTUP_PAGES; auxPage++) {
showAuxPage(auxPage);
showAuxPage();
delay(2000);
}
switchPress = 0;

// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
switchPress = 0;
}

void loop() {
if (switchPress > 0) {
if (switchPress < 8) {
// Press (acknowedge/next-page)
// ----------------------------
if (blinkAux[0] > 0 || blinkAux[1] > 0) {
ackAlert(auxPage);
ackBlink();
}
else {
if (ackAlert(auxPage))
auxPage = AUX_STARTUP_PAGES;
else {
auxPage++;
if (auxPage >= N(auxDisplay))
auxPage = AUX_STARTUP_PAGES;
}
}
}
switchPress = 0;
didChangeDim = false;
didHoldKey = false;
}
if (switchDown >= 24 && !didChangeDim) {
// Long hold (dim,bright toggle)
dimAux = !dimAux;
for (byte line=0; line<2; line++)
commandLED(line, dimAux?HT16K33_BRIGHT_MIN:HT16K33_BRIGHT_MAX);
didChangeDim = true;
didHoldKey = false;
}
else if (switchDown >= 8 && !didHoldKey) {
// Hold (reshow all)
// -----------------
// un-ack all and reshow alerts
blinkAux[0] = blinkAux[1] = 0;
for (byte i=AUX_STARTUP_PAGES; i<N(auxDisplay); i++)
auxDisplay[i].warning = auxDisplay[i].caution = 0;
auxPage = AUX_STARTUP_PAGES;
didHoldKey = true;
}

void loop() {
#if SIMULATE_SENSORS
if (Serial.read() >= 0) {
if (++simState >= SIMULATE_SENSORS)
Expand All @@ -226,20 +230,50 @@ void loop() {

pollForHttpRequest();

if (eighthSecondTick) {
if (eighthSecondTick) {
eighthSecondTick = false;

updateADC();

if (eighthSecondCount == 4 || eighthSecondCount >= 8) {
if (eighthSecondCount >= 8) { // every second
if (switchPress > 0) {
if (switchPress < 8)
buttonPress();
switchPress = 0;
didChangeDim = false;
didHoldKey = false;
}
if (switchDown >= 24 && !didChangeDim)
buttonLongHold();
else if (switchDown >= 8 && !didHoldKey)
buttonHold();
if (switchUp > SHOW_DEFAULT_AUX_PAGE_TIMEOUT*8) {
if (auxDisplay[auxPage].warning <=0 && auxDisplay[auxPage].caution <= 0)
auxPage = AUX_STARTUP_PAGES;
}

if (eighthSecondCount == 4 || eighthSecondCount >= 8) {
// every half second
// -----------------
updateTach();
engineRunning = isEngineRunning();

if (engineRunning) {
alertStatus = STATUS_NORMAL;
updateAlerts();
checkForAlerts(false);
checkForAlerts(true);
}
else
alertStatus = STATUS_WARNING;

showAuxPage();

// every second
// ------------
if (eighthSecondCount >= 8) {
#if DEBUG
logValue(minFreeRam,"minFreeRam");
#endif

updateTach();

engineRunning = isEngineRunning();

if (engineRunning)
updateHobbs();

Expand All @@ -249,16 +283,6 @@ void loop() {
}
eighthSecondCount -= 8;
}
// every half second
if (engineRunning) {
alertStatus = STATUS_NORMAL;
updateAlerts();
checkForAlerts(false);
checkForAlerts(true);
}
else
alertStatus = STATUS_WARNING;
showAuxPage(auxPage);
}
}
}
47 changes: 24 additions & 23 deletions enguino/printLED.h → enguino/printAux.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,41 +55,41 @@
#define LED_ 0x0 // blank

#define LED_A 0x77
#define LED_a 0x77
#define LED_B 0x7c
#define LED_a 0x77 // looks like A
#define LED_B 0x7c // looks like b
#define LED_b 0x7c
#define LED_C 0x39
#define LED_c 0x58
#define LED_D 0x5e
#define LED_D 0x5e // looks like d
#define LED_d 0x5e
#define LED_E 0x79
#define LED_F 0x71
#define LED_f 0x71
#define LED_f 0x71 // same as F
#define LED_G 0x3b
#define LED_g 0x6f
#define LED_H 0x76
#define LED_h 0x74
#define LED_i 0x4
#define LED_J 0x1e
#define LED_j 0x1e
#define LED_j 0x1e // same as j
#define LED_L 0x38
#define LED_n 0x54
#define LED_O 0x3F
#define LED_O 0x3F // same as 0
#define LED_o 0x5c
#define LED_P 0x73
#define LED_r 0x50
#define LED_S 0x6d
#define LED_s 0x6d
#define LED_T 0x78
#define LED_S 0x6d // same as 5
#define LED_s 0x6d // same as S,5
#define LED_T 0x78 // looks like t
#define LED_t 0x78
#define LED_U 0x3e
#define LED_u 0x1c
#define LED_V 0x3e
#define LED_v 0x1c
#define LED_V 0x3e // same as U
#define LED_v 0x1c // same as u
#define LED_Y 0x6e
#define LED_y 0x6e
#define LED_Z 0x5b
#define LED_z 0x5b
#define LED_y 0x6e // same as Y
#define LED_Z 0x5b // same as 2
#define LED_z 0x5b // same as Z, 2

volatile byte switchDown; // use this for detecting a key held down for a period of time
volatile byte switchUp;
Expand All @@ -109,13 +109,12 @@ byte colon;
#define LED_COLON 2

void writeI2C(byte line, byte *buffer, byte len) {
if (!i2c_start((I2C_ADDRESS | (line<<1)) | I2C_WRITE))
goto stop;
while (len--) {
if (!i2c_write(*buffer++))
goto stop;
if (i2c_start((I2C_ADDRESS | (line<<1)) | I2C_WRITE)) {
while (len--) {
if (!i2c_write(*buffer++))
break;
}
}
stop:
i2c_stop();
}

Expand Down Expand Up @@ -230,16 +229,18 @@ void printLED(byte line, int number, byte decimal) {
inline void pollAuxSwitch() {
if (digitalRead(AUX_SWITCH)) {
// switch up
if (++switchUp > 2) {
switchUp = 2;
if (switchUp < 255)
switchUp++;
if (switchUp > 2) { // debounce test, button must be up for more than 1/4 second before considered a button up state
if (switchDown)
switchPress = switchDown;
switchDown = 0;
}
}
}
else {
// switch is down
++switchDown;
switchUp = 0;
}
}

Loading

0 comments on commit 6df5ca8

Please sign in to comment.