![](/media/cache/profiles/7c9a78d4cbeacba4b35f3b445d01f055.50x50_q85.jpg)
Software to drive a monitor unit for a closed circuit rebreather using 3 electrogalvanic oxygen sensor cells run through an amplifier (lm324) . Uses a separate ds1307 clock IC to get timestamp values for logged data.
Dependencies: DS1307 TextOLED_custom mbed
The main electornics is housed in another pod mounted on the back of the unit. I'm using an mbed lpc11u24 to drive everything which comes with a flash drive for data logging built in. It has an external ds1307 clock chip added and a very cheapo lm324 quad op-amp to amplify the o2 sensor signals from the 10s of mV range by 30x so that ppo2=0.21 corresponds to about 0.3V. I still have to do some ADC averaging with this amplifier and do have to calibrate out the individual offsets of the chip but this works ok now that I've worked out which amp is on which adc...
Rebmon_main.cpp@4:74df6d31ee0a, 2012-08-03 (annotated)
- Committer:
- pegcjs
- Date:
- Fri Aug 03 14:19:44 2012 +0000
- Revision:
- 4:74df6d31ee0a
- Parent:
- 3:0d94a277aa8c
- Child:
- 5:35417986539a
leds done bu need to sort out logging data and the recall of saved settings and scrub time in case of swirtchoff between dives on same scrubber.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pegcjs | 1:9cff4feccbce | 1 | //lpc1124lcddemo |
pegcjs | 1:9cff4feccbce | 2 | #include "ds1307.h" |
pegcjs | 1:9cff4feccbce | 3 | #include "mbed.h" |
pegcjs | 1:9cff4feccbce | 4 | #include "TextLCD.h" |
pegcjs | 1:9cff4feccbce | 5 | |
pegcjs | 1:9cff4feccbce | 6 | |
pegcjs | 1:9cff4feccbce | 7 | #define METRE 0.02 // change in DEPin for 1m depth |
pegcjs | 1:9cff4feccbce | 8 | |
pegcjs | 1:9cff4feccbce | 9 | //pin assignments and declarations |
pegcjs | 1:9cff4feccbce | 10 | // LCD display |
pegcjs | 1:9cff4feccbce | 11 | TextLCD g_lcd(p26, p25, p24, p23, p22, p21); // RS, E, DB4, DB5, DB6, DB7 |
pegcjs | 1:9cff4feccbce | 12 | //backlight |
pegcjs | 1:9cff4feccbce | 13 | DigitalOut backlight(p29); |
pegcjs | 1:9cff4feccbce | 14 | |
pegcjs | 1:9cff4feccbce | 15 | //onboard leds |
pegcjs | 1:9cff4feccbce | 16 | DigitalOut led1(LED1); |
pegcjs | 1:9cff4feccbce | 17 | DigitalOut led2(LED2); |
pegcjs | 1:9cff4feccbce | 18 | |
pegcjs | 1:9cff4feccbce | 19 | // warning leds |
pegcjs | 1:9cff4feccbce | 20 | DigitalOut red(p34); |
pegcjs | 1:9cff4feccbce | 21 | DigitalOut green(p33); |
pegcjs | 1:9cff4feccbce | 22 | DigitalOut blue(p30); |
pegcjs | 1:9cff4feccbce | 23 | |
pegcjs | 1:9cff4feccbce | 24 | // switches and buttons - these are pulled up by resistors so are active low |
pegcjs | 1:9cff4feccbce | 25 | DigitalIn CAL(p36); |
pegcjs | 1:9cff4feccbce | 26 | DigitalIn SW1(p35); |
pegcjs | 1:9cff4feccbce | 27 | |
pegcjs | 1:9cff4feccbce | 28 | // log data storage |
pegcjs | 1:9cff4feccbce | 29 | LocalFileSystem local("local"); |
pegcjs | 1:9cff4feccbce | 30 | |
pegcjs | 1:9cff4feccbce | 31 | // adc inputs for sensors |
pegcjs | 1:9cff4feccbce | 32 | AnalogIn PRESin(p20); |
pegcjs | 1:9cff4feccbce | 33 | AnalogIn EG1(p19); |
pegcjs | 1:9cff4feccbce | 34 | AnalogIn EG2(p18); |
pegcjs | 1:9cff4feccbce | 35 | AnalogIn Vbatt(p17); |
pegcjs | 1:9cff4feccbce | 36 | |
pegcjs | 1:9cff4feccbce | 37 | // realtime clock |
pegcjs | 1:9cff4feccbce | 38 | DS1307 my1307(p28,p27); // start DS1307 class and give it pins for connections of the DS1307 device |
pegcjs | 1:9cff4feccbce | 39 | |
pegcjs | 1:9cff4feccbce | 40 | // variables for realtime clock |
pegcjs | 1:9cff4feccbce | 41 | int sec = 0; |
pegcjs | 1:9cff4feccbce | 42 | int min = 0; |
pegcjs | 1:9cff4feccbce | 43 | int hours = 0; |
pegcjs | 1:9cff4feccbce | 44 | int day = 0; |
pegcjs | 1:9cff4feccbce | 45 | int date = 0; |
pegcjs | 1:9cff4feccbce | 46 | int month = 0; |
pegcjs | 1:9cff4feccbce | 47 | int year = 0; |
pegcjs | 4:74df6d31ee0a | 48 | int seconds=0; // general number of seconds since 2000 etc timestamp variable |
pegcjs | 1:9cff4feccbce | 49 | |
pegcjs | 1:9cff4feccbce | 50 | int scrubtime=0; // these are expressed in minutes |
pegcjs | 1:9cff4feccbce | 51 | int divetime=0; |
pegcjs | 1:9cff4feccbce | 52 | |
pegcjs | 3:0d94a277aa8c | 53 | int flash=0; // variable used top control flashing icons |
pegcjs | 2:a1c26faa9103 | 54 | int state=0; // IMPORTANT - VARIABLE THAT DRIVES HNTE STATE MACHINE STATE=0 = STARTUP, STATE=1=SURFACE STATE=2= DIVING |
pegcjs | 2:a1c26faa9103 | 55 | |
pegcjs | 1:9cff4feccbce | 56 | // variables for the eg cells and pressure sensor eg1calamd eg2cal ar reading when the sensor is in 0.21bar O2 and |
pegcjs | 1:9cff4feccbce | 57 | //dcal is the reading whe the pressure sensor is at the surface |
pegcjs | 1:9cff4feccbce | 58 | float eg1cal=0.09,eg2cal=0.09,pcal=0.1136; |
pegcjs | 1:9cff4feccbce | 59 | // NB these are updated from /local/cal.dat so values not so important.... eventually |
pegcjs | 1:9cff4feccbce | 60 | |
pegcjs | 1:9cff4feccbce | 61 | float depth=0,ppo1=0,ppo2=0, Vb=0,pressure=0; // depth, 1st o2 sensor second o2 sensor battery voltage,,Pressure |
pegcjs | 1:9cff4feccbce | 62 | float fo1=0,fo2=0,mod=55; //%f values,mod |
pegcjs | 3:0d94a277aa8c | 63 | |
pegcjs | 1:9cff4feccbce | 64 | //===== sub to get time from ds1307 and create the 'seconds' which is a version of timestamp.... |
pegcjs | 1:9cff4feccbce | 65 | int getseconds() { |
pegcjs | 1:9cff4feccbce | 66 | my1307.gettime( &sec, &min, &hours, &day, &date, &month, &year); |
pegcjs | 1:9cff4feccbce | 67 | //simple timestamp = # seconds since midnight jan 1st 2000 if all months were 30 days. |
pegcjs | 4:74df6d31ee0a | 68 | int secondst=year*365*24*60*60+month*30*24*60*60+day*24*60*60+hours*60*60+min*60+sec; |
pegcjs | 1:9cff4feccbce | 69 | //simple timestamp = # seconds since midnight jan 1st 2000 if all months were 30 days.... |
pegcjs | 1:9cff4feccbce | 70 | // ie wrong but simpler than the real thing |
pegcjs | 4:74df6d31ee0a | 71 | return(secondst); |
pegcjs | 1:9cff4feccbce | 72 | } |
pegcjs | 1:9cff4feccbce | 73 | |
pegcjs | 1:9cff4feccbce | 74 | |
pegcjs | 1:9cff4feccbce | 75 | void set_custom_char() { |
pegcjs | 1:9cff4feccbce | 76 | char cgchar[64]={ |
pegcjs | 1:9cff4feccbce | 77 | 6,9,9,9,9,9,9,15, // battery empty symbol 0 |
pegcjs | 1:9cff4feccbce | 78 | 6,9,9,9,9,15,15,15, // battery 50% symbol 1 |
pegcjs | 1:9cff4feccbce | 79 | 6,9,9,15,15,15,15,15, // battery 75% symbol 2 |
pegcjs | 1:9cff4feccbce | 80 | 6,15,15,15,15,15,15,15, // battery 100% symbol 3 |
pegcjs | 1:9cff4feccbce | 81 | 31,19,21,21,21,21,19,31, // diving symbol 4 inverse D |
pegcjs | 1:9cff4feccbce | 82 | 6,6,6,6,6,0,0,6, // warning symbol 5 |
pegcjs | 1:9cff4feccbce | 83 | 31,17,23,17,29,17,31,0, // surface symbol 6 inverse S |
pegcjs | 2:a1c26faa9103 | 84 | 0,0,17,17,0,17,14,0 // happy symbol 7 |
pegcjs | 1:9cff4feccbce | 85 | }; |
pegcjs | 1:9cff4feccbce | 86 | int i=0; |
pegcjs | 1:9cff4feccbce | 87 | // do stuff here to set cstom chars |
pegcjs | 1:9cff4feccbce | 88 | g_lcd.writeCommand(0x40); // set start address for CGRAM |
pegcjs | 1:9cff4feccbce | 89 | for (i=0; i<64; i++) { |
pegcjs | 1:9cff4feccbce | 90 | g_lcd.writeData(cgchar[i]); |
pegcjs | 1:9cff4feccbce | 91 | } |
pegcjs | 1:9cff4feccbce | 92 | |
pegcjs | 1:9cff4feccbce | 93 | } |
pegcjs | 1:9cff4feccbce | 94 | |
pegcjs | 1:9cff4feccbce | 95 | |
pegcjs | 1:9cff4feccbce | 96 | // subroutine to calibreate o2 sesnors and store ca data in /local/CAL.dat |
pegcjs | 1:9cff4feccbce | 97 | void calibrate() { |
pegcjs | 1:9cff4feccbce | 98 | int count=1; |
pegcjs | 1:9cff4feccbce | 99 | float ppo1=0,ppo2=0,pres=0; |
pegcjs | 1:9cff4feccbce | 100 | // average 20 readings for noise reduction |
pegcjs | 1:9cff4feccbce | 101 | g_lcd.cls(); |
pegcjs | 1:9cff4feccbce | 102 | for (count=20; count>0; count--) { |
pegcjs | 1:9cff4feccbce | 103 | g_lcd.locate(0,0); |
pegcjs | 4:74df6d31ee0a | 104 | g_lcd.printf("Calibrate 21%% %.2d",count); |
pegcjs | 1:9cff4feccbce | 105 | ppo1=ppo1+EG1; |
pegcjs | 1:9cff4feccbce | 106 | ppo2=ppo2+EG2; |
pegcjs | 1:9cff4feccbce | 107 | pres=pres+PRESin; |
pegcjs | 1:9cff4feccbce | 108 | g_lcd.locate(0,1); |
pegcjs | 1:9cff4feccbce | 109 | g_lcd.printf("%1.2f: %1.2f: %1.2f",ppo1/(20-count+1),ppo2/(20-count+1),pres/(20-count+1)); |
pegcjs | 1:9cff4feccbce | 110 | wait(1); |
pegcjs | 1:9cff4feccbce | 111 | } |
pegcjs | 1:9cff4feccbce | 112 | //average |
pegcjs | 1:9cff4feccbce | 113 | ppo1=ppo1/20; |
pegcjs | 1:9cff4feccbce | 114 | ppo2=ppo2/20; |
pegcjs | 1:9cff4feccbce | 115 | // set calibration variables |
pegcjs | 1:9cff4feccbce | 116 | eg1cal=ppo1; |
pegcjs | 1:9cff4feccbce | 117 | eg2cal=ppo2; |
pegcjs | 1:9cff4feccbce | 118 | pcal=pres/20; // surface pressure.... |
pegcjs | 1:9cff4feccbce | 119 | scrubtime=0; // reset the scrubber timer to zero. |
pegcjs | 2:a1c26faa9103 | 120 | // write cal data NB overwites previous |
pegcjs | 2:a1c26faa9103 | 121 | FILE *fp=fopen("/local/CAL.dat","w"); |
pegcjs | 2:a1c26faa9103 | 122 | fprintf(fp,"%e\n%e\n%e\n%d",eg1cal,eg2cal,pcal,scrubtime); |
pegcjs | 2:a1c26faa9103 | 123 | fclose(fp); //NB file system locked on write so must make sure we close files in case want to reprogram etc... |
pegcjs | 1:9cff4feccbce | 124 | } |
pegcjs | 1:9cff4feccbce | 125 | |
pegcjs | 4:74df6d31ee0a | 126 | // sub to test if a variable is an even number |
pegcjs | 4:74df6d31ee0a | 127 | int iseven(int g) { |
pegcjs | 4:74df6d31ee0a | 128 | int test=0; |
pegcjs | 4:74df6d31ee0a | 129 | if(g%2 ==0) test=1; |
pegcjs | 4:74df6d31ee0a | 130 | return(test); |
pegcjs | 4:74df6d31ee0a | 131 | } |
pegcjs | 4:74df6d31ee0a | 132 | |
pegcjs | 1:9cff4feccbce | 133 | |
pegcjs | 2:a1c26faa9103 | 134 | void status() { |
pegcjs | 3:0d94a277aa8c | 135 | if (state==0) { |
pegcjs | 3:0d94a277aa8c | 136 | g_lcd.character(9,0,5); // warning icon until 1 min up |
pegcjs | 3:0d94a277aa8c | 137 | g_lcd.character(8,0,6); // surface icon |
pegcjs | 3:0d94a277aa8c | 138 | } |
pegcjs | 2:a1c26faa9103 | 139 | if (state==1) g_lcd.character(8,0,6); // surface icon |
pegcjs | 4:74df6d31ee0a | 140 | if (state==2 && iseven(seconds)==1) g_lcd.character(8,0,4); // diving icon |
pegcjs | 4:74df6d31ee0a | 141 | if (state==2 && iseven(seconds)==0) g_lcd.character(8,0,68); // diving icon |
pegcjs | 4:74df6d31ee0a | 142 | |
pegcjs | 4:74df6d31ee0a | 143 | } |
pegcjs | 3:0d94a277aa8c | 144 | |
pegcjs | 3:0d94a277aa8c | 145 | // warning and LED conditions |
pegcjs | 2:a1c26faa9103 | 146 | |
pegcjs | 2:a1c26faa9103 | 147 | void warning() { |
pegcjs | 3:0d94a277aa8c | 148 | if (depth>=mod && flash==1) g_lcd.character(13,0,5); |
pegcjs | 2:a1c26faa9103 | 149 | |
pegcjs | 2:a1c26faa9103 | 150 | } |
pegcjs | 2:a1c26faa9103 | 151 | |
pegcjs | 4:74df6d31ee0a | 152 | // pick maximum of two values |
pegcjs | 4:74df6d31ee0a | 153 | float maximum(float a,float b) { |
pegcjs | 4:74df6d31ee0a | 154 | float maximum; |
pegcjs | 4:74df6d31ee0a | 155 | if (a>b) maximum=a; |
pegcjs | 4:74df6d31ee0a | 156 | else maximum=b; |
pegcjs | 4:74df6d31ee0a | 157 | return(maximum); |
pegcjs | 4:74df6d31ee0a | 158 | } |
pegcjs | 4:74df6d31ee0a | 159 | |
pegcjs | 4:74df6d31ee0a | 160 | // pick minimum of two values |
pegcjs | 4:74df6d31ee0a | 161 | float minimum(float a,float b) { |
pegcjs | 4:74df6d31ee0a | 162 | float minim; |
pegcjs | 4:74df6d31ee0a | 163 | if (a<b) minim=a; |
pegcjs | 4:74df6d31ee0a | 164 | else minim=b; |
pegcjs | 4:74df6d31ee0a | 165 | return(minim); |
pegcjs | 4:74df6d31ee0a | 166 | } |
pegcjs | 4:74df6d31ee0a | 167 | |
pegcjs | 4:74df6d31ee0a | 168 | |
pegcjs | 4:74df6d31ee0a | 169 | |
pegcjs | 2:a1c26faa9103 | 170 | void leds() { |
pegcjs | 4:74df6d31ee0a | 171 | // first turn everything off |
pegcjs | 4:74df6d31ee0a | 172 | red=0; |
pegcjs | 4:74df6d31ee0a | 173 | green=0; |
pegcjs | 4:74df6d31ee0a | 174 | blue=0; |
pegcjs | 4:74df6d31ee0a | 175 | float ppo; |
pegcjs | 4:74df6d31ee0a | 176 | ppo=maximum(ppo1,ppo2); // use max value to compute leds... |
pegcjs | 4:74df6d31ee0a | 177 | if(ppo<0.2 && flash==1) red=1; // flashing red means very bad things - getting low on oxygen!!! |
pegcjs | 4:74df6d31ee0a | 178 | if(ppo>0.2 && ppo < 1) red=1; // non-flashing red |
pegcjs | 4:74df6d31ee0a | 179 | if(ppo>=1.0 && ppo <1.2) {red=1;green=1;} // red-green |
pegcjs | 4:74df6d31ee0a | 180 | if(ppo<1.3 && ppo >=1.2) green=1; // green - optimal range in ccr mode |
pegcjs | 4:74df6d31ee0a | 181 | if(ppo<1.4 && ppo >=1.3){green=1;blue=1;} // green-blue - high ppo2 be careful of spiking |
pegcjs | 4:74df6d31ee0a | 182 | if(ppo2<1.6 && ppo2>=1.4) blue=1; // DANGE ble high ppo2 |
pegcjs | 4:74df6d31ee0a | 183 | if(ppo2>=1.6 && flash==1) blue=1; |
pegcjs | 1:9cff4feccbce | 184 | } |
pegcjs | 1:9cff4feccbce | 185 | |
pegcjs | 4:74df6d31ee0a | 186 | |
pegcjs | 4:74df6d31ee0a | 187 | |
pegcjs | 1:9cff4feccbce | 188 | //read battery state and insert the battery symbol |
pegcjs | 1:9cff4feccbce | 189 | void battery() { |
pegcjs | 1:9cff4feccbce | 190 | int batsym=0; |
pegcjs | 1:9cff4feccbce | 191 | Vb=Vbatt; // read adc connected to battery via a 1/3 potential divider |
pegcjs | 1:9cff4feccbce | 192 | if (Vb>0.606) batsym=1; |
pegcjs | 1:9cff4feccbce | 193 | if (Vb>0.707) batsym=2; |
pegcjs | 1:9cff4feccbce | 194 | if (Vb>0.808) batsym=3; |
pegcjs | 3:0d94a277aa8c | 195 | if (batsym >0) g_lcd.character(8,1,batsym); |
pegcjs | 3:0d94a277aa8c | 196 | if (batsym ==0 && flash==1) g_lcd.character(8,1,batsym); |
pegcjs | 3:0d94a277aa8c | 197 | if (batsym ==0 && flash==0) g_lcd.character(8,1,32); |
pegcjs | 1:9cff4feccbce | 198 | } |
pegcjs | 1:9cff4feccbce | 199 | |
pegcjs | 2:a1c26faa9103 | 200 | // subroutine to write the main display data |
pegcjs | 2:a1c26faa9103 | 201 | //0123456789abcdef |
pegcjs | 2:a1c26faa9103 | 202 | |
pegcjs | 2:a1c26faa9103 | 203 | //x.xx:xx D XX xx |
pegcjs | 2:a1c26faa9103 | 204 | //x.xx:xx B XX xxx NB the warning, staus and battery icons are driven by separate subroutines. |
pegcjs | 2:a1c26faa9103 | 205 | void display() { |
pegcjs | 2:a1c26faa9103 | 206 | //1st line |
pegcjs | 2:a1c26faa9103 | 207 | g_lcd.locate(0,0); |
pegcjs | 2:a1c26faa9103 | 208 | g_lcd.printf("%1.2f:%.2d %.2d %.2d",ppo1,(int)fo1,(int)depth,(int)mod); |
pegcjs | 2:a1c26faa9103 | 209 | //2nd line |
pegcjs | 2:a1c26faa9103 | 210 | g_lcd.locate(0,1); |
pegcjs | 3:0d94a277aa8c | 211 | g_lcd.printf("%1.2f:%.2d %.2d %.3d",ppo2,(int)fo2,divetime,scrubtime); |
pegcjs | 2:a1c26faa9103 | 212 | // bung in battery icon |
pegcjs | 2:a1c26faa9103 | 213 | battery(); |
pegcjs | 2:a1c26faa9103 | 214 | status(); // this will set the diviong / suface mode icon |
pegcjs | 2:a1c26faa9103 | 215 | warning(); // this will set the warning icon assuming that max ppo2 is exceeded |
pegcjs | 2:a1c26faa9103 | 216 | |
pegcjs | 2:a1c26faa9103 | 217 | leds(); // this sets the leds according to the various warning conditions |
pegcjs | 2:a1c26faa9103 | 218 | |
pegcjs | 2:a1c26faa9103 | 219 | } |
pegcjs | 2:a1c26faa9103 | 220 | |
pegcjs | 2:a1c26faa9103 | 221 | |
pegcjs | 2:a1c26faa9103 | 222 | |
pegcjs | 1:9cff4feccbce | 223 | |
pegcjs | 2:a1c26faa9103 | 224 | |
pegcjs | 2:a1c26faa9103 | 225 | // read sensors and generate calibrated outputs NB battery is read elsewhere |
pegcjs | 1:9cff4feccbce | 226 | void readsensors() { |
pegcjs | 1:9cff4feccbce | 227 | float barometric=0,mod1,mod2; |
pegcjs | 1:9cff4feccbce | 228 | ppo1=EG1*0.21/eg1cal; // eg1cal is 0.21bar ppO2 |
pegcjs | 1:9cff4feccbce | 229 | ppo2=EG2*0.21/eg2cal; // second oxygen cell ppO2 |
pegcjs | 2:a1c26faa9103 | 230 | pressure=(PRESin*3.3-0.024)/(0.0038574); // pressure in kPa assuming standard cal for mpx5700 sensor SUSPECT |
pegcjs | 3:0d94a277aa8c | 231 | // barometric=(pcal*3.3-0.024)/(0.0038574); // sealevel in kPa assuming standard cal for mpx5700 sensor |
pegcjs | 2:a1c26faa9103 | 232 | depth=(pressure-101.325)*0.1; //100kPa=10m 1kPa=0.1m - this gives depth in m for freshwater. |
pegcjs | 1:9cff4feccbce | 233 | //with two sensors will calculate mod from the largest ppo2 reading |
pegcjs | 2:a1c26faa9103 | 234 | |
pegcjs | 2:a1c26faa9103 | 235 | fo1=100*ppo1/(pressure/100); // pressure in bar = pressure /100 and want a % so multiply by 100 as well |
pegcjs | 2:a1c26faa9103 | 236 | fo2=100*ppo2/(pressure/100); |
pegcjs | 3:0d94a277aa8c | 237 | |
pegcjs | 2:a1c26faa9103 | 238 | |
pegcjs | 2:a1c26faa9103 | 239 | mod1=(1.4/(fo1/100)-1)*10; |
pegcjs | 2:a1c26faa9103 | 240 | mod2=(1.4/(fo2/100)-1)*10; |
pegcjs | 3:0d94a277aa8c | 241 | |
pegcjs | 2:a1c26faa9103 | 242 | mod=minimum(mod1,mod2); // pick the least value |
pegcjs | 2:a1c26faa9103 | 243 | //DEBUG |
pegcjs | 3:0d94a277aa8c | 244 | |
pegcjs | 1:9cff4feccbce | 245 | } |
pegcjs | 4:74df6d31ee0a | 246 | |
pegcjs | 1:9cff4feccbce | 247 | |
pegcjs | 1:9cff4feccbce | 248 | int main() { |
pegcjs | 2:a1c26faa9103 | 249 | // first some local variables |
pegcjs | 2:a1c26faa9103 | 250 | int startuptime=getseconds(); |
pegcjs | 4:74df6d31ee0a | 251 | int startdive=0,endclock=0; // value of seconds when dive starts and counter to decide if dive complete... |
pegcjs | 1:9cff4feccbce | 252 | |
pegcjs | 4:74df6d31ee0a | 253 | int minutes=0,dt=0;; // minutes is elapsed minutes since start of prog |
pegcjs | 1:9cff4feccbce | 254 | int i=0,j=0; // general loop counting variables |
pegcjs | 1:9cff4feccbce | 255 | |
pegcjs | 1:9cff4feccbce | 256 | |
pegcjs | 1:9cff4feccbce | 257 | set_custom_char(); // does what it says on the tin really |
pegcjs | 1:9cff4feccbce | 258 | g_lcd.cls(); |
pegcjs | 1:9cff4feccbce | 259 | g_lcd.locate(0, 0); |
pegcjs | 1:9cff4feccbce | 260 | g_lcd.printf( "RebMon"); |
pegcjs | 1:9cff4feccbce | 261 | g_lcd.locate(0,1); |
pegcjs | 1:9cff4feccbce | 262 | g_lcd.printf("CAL?"); |
pegcjs | 1:9cff4feccbce | 263 | battery(); |
pegcjs | 4:74df6d31ee0a | 264 | j=0; |
pegcjs | 1:9cff4feccbce | 265 | // hang about waiting for the cal switch to be pressed in ccase it is |
pegcjs | 1:9cff4feccbce | 266 | while (seconds-startuptime<20) { |
pegcjs | 1:9cff4feccbce | 267 | seconds=getseconds(); |
pegcjs | 1:9cff4feccbce | 268 | g_lcd.locate(5,1); |
pegcjs | 1:9cff4feccbce | 269 | g_lcd.printf("%.2d",21-(seconds-startuptime)); |
pegcjs | 4:74df6d31ee0a | 270 | if (j>1) flash=1; |
pegcjs | 3:0d94a277aa8c | 271 | else flash=0; |
pegcjs | 1:9cff4feccbce | 272 | battery(); // bung in battery symbol. |
pegcjs | 1:9cff4feccbce | 273 | g_lcd.locate(7,0); |
pegcjs | 1:9cff4feccbce | 274 | g_lcd.printf( "%.2d:%.2d:%.2d", hours,min,sec); |
pegcjs | 1:9cff4feccbce | 275 | if (CAL==0) { |
pegcjs | 1:9cff4feccbce | 276 | calibrate(); |
pegcjs | 1:9cff4feccbce | 277 | |
pegcjs | 1:9cff4feccbce | 278 | } |
pegcjs | 3:0d94a277aa8c | 279 | wait(0.2); |
pegcjs | 3:0d94a277aa8c | 280 | j=(j+1) % 4; |
pegcjs | 1:9cff4feccbce | 281 | } |
pegcjs | 1:9cff4feccbce | 282 | g_lcd.cls(); |
pegcjs | 1:9cff4feccbce | 283 | backlight=1; // backlight on - this driven by bc182l and 50ohm resistor off the 5V supply to send ~ 20mA |
pegcjs | 1:9cff4feccbce | 284 | |
pegcjs | 1:9cff4feccbce | 285 | // ok there are three states in this system |
pegcjs | 1:9cff4feccbce | 286 | //MAIN LOOP ONCE STARTUP PROTOCOLS ARE COMPLETED |
pegcjs | 3:0d94a277aa8c | 287 | j=0; |
pegcjs | 1:9cff4feccbce | 288 | while (1) { |
pegcjs | 2:a1c26faa9103 | 289 | wait(0.2); //stop screen flicker |
pegcjs | 1:9cff4feccbce | 290 | readsensors(); |
pegcjs | 2:a1c26faa9103 | 291 | seconds=getseconds(); |
pegcjs | 4:74df6d31ee0a | 292 | minutes=(int)(((float)seconds-(float)startuptime)/60); |
pegcjs | 3:0d94a277aa8c | 293 | dt=seconds-startuptime; // elapsed seconds |
pegcjs | 3:0d94a277aa8c | 294 | |
pegcjs | 3:0d94a277aa8c | 295 | if (j>1) flash=1; |
pegcjs | 3:0d94a277aa8c | 296 | else flash=0; |
pegcjs | 3:0d94a277aa8c | 297 | |
pegcjs | 3:0d94a277aa8c | 298 | display(); // write the display |
pegcjs | 3:0d94a277aa8c | 299 | |
pegcjs | 3:0d94a277aa8c | 300 | // setup state variable |
pegcjs | 3:0d94a277aa8c | 301 | if (minutes<1) state=0; // startup mode - do nothing just wait to allow sensor readings to settle. |
pegcjs | 4:74df6d31ee0a | 302 | if (minutes>=1 && state==0) state=1; // surface mode - ok to go for a dive now |
pegcjs | 4:74df6d31ee0a | 303 | if (minutes>=1 && depth>0.5 && state==1) { |
pegcjs | 3:0d94a277aa8c | 304 | state=2; // enter dive mode |
pegcjs | 4:74df6d31ee0a | 305 | if (startdive==0) startdive=seconds; // set start of divetime. don't do this twice |
pegcjs | 3:0d94a277aa8c | 306 | endclock=0; // reset end of dive clock |
pegcjs | 3:0d94a277aa8c | 307 | } |
pegcjs | 3:0d94a277aa8c | 308 | if (state==2) { |
pegcjs | 4:74df6d31ee0a | 309 | divetime=(int)(((float)seconds-(float)startdive)/60); // time since start of dive in minutes. |
pegcjs | 3:0d94a277aa8c | 310 | // do deco calcs here when implemented |
pegcjs | 3:0d94a277aa8c | 311 | if (depth<0.3) { |
pegcjs | 4:74df6d31ee0a | 312 | endclock=endclock+1; |
pegcjs | 4:74df6d31ee0a | 313 | |
pegcjs | 4:74df6d31ee0a | 314 | if (endclock>150) state=1; // 30s at shallower than 0.3m and we return to surface mode. |
pegcjs | 3:0d94a277aa8c | 315 | } |
pegcjs | 4:74df6d31ee0a | 316 | scrubtime=minutes; // need to add memory of prior scrub time to this once variable input fom log file is implements |
pegcjs | 3:0d94a277aa8c | 317 | } |
pegcjs | 1:9cff4feccbce | 318 | |
pegcjs | 1:9cff4feccbce | 319 | |
pegcjs | 3:0d94a277aa8c | 320 | j=(j+1) %4; // flash control variable = used to make the warnings flash for 0.4s duty cycle |
pegcjs | 1:9cff4feccbce | 321 | } // end while |
pegcjs | 1:9cff4feccbce | 322 | } //end main |
pegcjs | 1:9cff4feccbce | 323 | |
pegcjs | 1:9cff4feccbce | 324 | |
pegcjs | 1:9cff4feccbce | 325 | |
pegcjs | 1:9cff4feccbce | 326 |