![](/media/cache/profiles/725d948ddd84c02eadcdbaaf0c8586b1.jpg.50x50_q85.jpg)
iBreathe Breathalyzer can now talk thanks to the Text to Speech Click Board
Dependencies: Hexi_KW40Z Hexi_OLED_SSD1351 text_to_speak_mbed
Fork of iBreathe_Breathalyzer by
main.cpp@5:a27fdb811237, 2016-09-27 (annotated)
- Committer:
- daveyclk
- Date:
- Tue Sep 27 09:27:06 2016 +0000
- Revision:
- 5:a27fdb811237
- Parent:
- 4:4e35f2e4b786
- Child:
- 6:7f4ba36b025f
initial version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
daveyclk | 5:a27fdb811237 | 1 | /**************************************************************************** |
daveyclk | 5:a27fdb811237 | 2 | * Title : iBreathe Breathalyzer |
daveyclk | 5:a27fdb811237 | 3 | * Filename : breathalyzer |
daveyclk | 5:a27fdb811237 | 4 | * Author : Dave Clarke |
daveyclk | 5:a27fdb811237 | 5 | * Origin Date : 27/09/2016 |
daveyclk | 5:a27fdb811237 | 6 | * Notes : Breathalyzer utilizing Hexiware, Alcohol click and Wolksense |
daveyclk | 5:a27fdb811237 | 7 | *****************************************************************************/ |
daveyclk | 5:a27fdb811237 | 8 | /**************************CHANGE LIST ************************************** |
daveyclk | 5:a27fdb811237 | 9 | * |
daveyclk | 5:a27fdb811237 | 10 | * Date Software Version Initials Description |
daveyclk | 5:a27fdb811237 | 11 | * 27/09/16 1.0.0 DC Interface Created. |
daveyclk | 5:a27fdb811237 | 12 | * |
daveyclk | 5:a27fdb811237 | 13 | *****************************************************************************/ |
daveyclk | 5:a27fdb811237 | 14 | |
daveyclk | 5:a27fdb811237 | 15 | /** |
daveyclk | 5:a27fdb811237 | 16 | * @page TEST_CFG Test Configurations |
daveyclk | 5:a27fdb811237 | 17 | * <h3> Test configuration : </h3> |
daveyclk | 5:a27fdb811237 | 18 | * @par |
daveyclk | 5:a27fdb811237 | 19 | * <ul> |
daveyclk | 5:a27fdb811237 | 20 | * <li><b> MCU </b> : MK64FN1M0XXX12 </li> |
daveyclk | 5:a27fdb811237 | 21 | * <li><b> Dev. Board </b> : HEXIWEAR </li> |
daveyclk | 5:a27fdb811237 | 22 | * <li><b> Oscillator </b> : 12 MHz external </li> |
daveyclk | 5:a27fdb811237 | 23 | * <li><b> Ext. Modules </b> : Alcohol Click on mikroBUS 1 </li> |
daveyclk | 5:a27fdb811237 | 24 | * <li><b> SW </b> : mBed OS5 </li> |
daveyclk | 5:a27fdb811237 | 25 | * </ul> |
daveyclk | 5:a27fdb811237 | 26 | */ |
daveyclk | 5:a27fdb811237 | 27 | |
daveyclk | 5:a27fdb811237 | 28 | /** |
daveyclk | 5:a27fdb811237 | 29 | * @mainpage |
daveyclk | 5:a27fdb811237 | 30 | * <h3> Breathalyser created with HEXIWEAR and mBed OS 5 </h3> |
daveyclk | 5:a27fdb811237 | 31 | * @par This will show you how much you've drunk and tell you with an Emoticon if |
daveyclk | 5:a27fdb811237 | 32 | * you're too hammered to even consider driving. Using the Hexiware app the readings |
daveyclk | 5:a27fdb811237 | 33 | * are transmitted to the cloud via bluetooth. Is it time to give up drinking yet?! |
daveyclk | 5:a27fdb811237 | 34 | |
daveyclk | 5:a27fdb811237 | 35 | * <h3> Alcohol Features </h3> |
daveyclk | 5:a27fdb811237 | 36 | * @par Alcohol click, Hexiwear docking station, Hexiware |
daveyclk | 5:a27fdb811237 | 37 | */ |
daveyclk | 5:a27fdb811237 | 38 | |
daveyclk | 5:a27fdb811237 | 39 | /****************************************************************************** |
daveyclk | 5:a27fdb811237 | 40 | * Includes |
daveyclk | 5:a27fdb811237 | 41 | *******************************************************************************/ |
daveyclk | 5:a27fdb811237 | 42 | |
khuang | 0:a1af4ae04b06 | 43 | #include "mbed.h" |
daveyclk | 5:a27fdb811237 | 44 | #include "Hexi_KW40Z.h" |
khuang | 0:a1af4ae04b06 | 45 | #include "Hexi_OLED_SSD1351.h" |
daveyclk | 5:a27fdb811237 | 46 | #include "OLED_types.h" |
daveyclk | 5:a27fdb811237 | 47 | #include "OpenSans_Font.h" |
khuang | 0:a1af4ae04b06 | 48 | #include "string.h" |
daveyclk | 5:a27fdb811237 | 49 | #include "iBreatheImages.h" |
daveyclk | 5:a27fdb811237 | 50 | |
daveyclk | 5:a27fdb811237 | 51 | /****************************************************************************** |
daveyclk | 5:a27fdb811237 | 52 | * Module Variable Definitions |
daveyclk | 5:a27fdb811237 | 53 | *******************************************************************************/ |
daveyclk | 5:a27fdb811237 | 54 | |
daveyclk | 5:a27fdb811237 | 55 | #define LED_ON 0 |
daveyclk | 5:a27fdb811237 | 56 | #define LED_OFF 1 |
daveyclk | 5:a27fdb811237 | 57 | |
daveyclk | 5:a27fdb811237 | 58 | // Pointers to: |
daveyclk | 5:a27fdb811237 | 59 | const uint8_t *welcome, // Welcome screen image |
daveyclk | 5:a27fdb811237 | 60 | *blank, // blank image |
daveyclk | 5:a27fdb811237 | 61 | *blow, // Start Blowing Image |
daveyclk | 5:a27fdb811237 | 62 | *drink, // You've been drinking image |
daveyclk | 5:a27fdb811237 | 63 | *drive, // Don't drive image |
daveyclk | 5:a27fdb811237 | 64 | *hang, // You'll have a hangover image |
daveyclk | 5:a27fdb811237 | 65 | *ini, // Initialising image |
daveyclk | 5:a27fdb811237 | 66 | *sober; // Sober as a judge image |
daveyclk | 5:a27fdb811237 | 67 | |
daveyclk | 5:a27fdb811237 | 68 | const float Vadc_3V3 = 0.00005035; // 16-Bit ADC step 3V3/65535 = 0.05035 mV |
daveyclk | 5:a27fdb811237 | 69 | |
daveyclk | 5:a27fdb811237 | 70 | float Vrl = 0, // Output voltage |
daveyclk | 5:a27fdb811237 | 71 | ambientAlc = 0, // Abmient Output voltage from sensor |
daveyclk | 5:a27fdb811237 | 72 | SensorRes = 0, // SensorRes (Ohm) - Sensor resistance |
daveyclk | 5:a27fdb811237 | 73 | SensorRes1 = 0, // SensorRes (Ohm) - Sensor resistance |
daveyclk | 5:a27fdb811237 | 74 | ppm = 0, // ppm |
daveyclk | 5:a27fdb811237 | 75 | ppm_1 = 0, // Ambient ppm variable |
daveyclk | 5:a27fdb811237 | 76 | ratio = 0; // SensorRes/LoadRes ratio |
daveyclk | 5:a27fdb811237 | 77 | |
daveyclk | 5:a27fdb811237 | 78 | unsigned short adc_rd = 0; //Initialise anologue read variable |
daveyclk | 5:a27fdb811237 | 79 | |
daveyclk | 5:a27fdb811237 | 80 | const uint8_t ppmText[] = "ppm:"; // text for PPM label |
daveyclk | 5:a27fdb811237 | 81 | |
daveyclk | 5:a27fdb811237 | 82 | char text[20], // Text array variables |
daveyclk | 5:a27fdb811237 | 83 | text2[20], |
daveyclk | 5:a27fdb811237 | 84 | text3[20]; |
daveyclk | 5:a27fdb811237 | 85 | |
daveyclk | 5:a27fdb811237 | 86 | float value[20], // initial sensor set up values |
daveyclk | 5:a27fdb811237 | 87 | value1[20]; // initial sensor set up values |
daveyclk | 5:a27fdb811237 | 88 | |
daveyclk | 5:a27fdb811237 | 89 | |
daveyclk | 5:a27fdb811237 | 90 | /****************************************************************************** |
daveyclk | 5:a27fdb811237 | 91 | * Function Prototypes |
daveyclk | 5:a27fdb811237 | 92 | *******************************************************************************/ |
daveyclk | 5:a27fdb811237 | 93 | |
daveyclk | 5:a27fdb811237 | 94 | void sysinit(void); |
daveyclk | 5:a27fdb811237 | 95 | void ReadSensor(); |
daveyclk | 5:a27fdb811237 | 96 | void CalculatePPM(int times, bool amb); |
daveyclk | 5:a27fdb811237 | 97 | void ambient(int times); |
daveyclk | 5:a27fdb811237 | 98 | void StartHaptic(void); |
daveyclk | 5:a27fdb811237 | 99 | void StopHaptic(void const *n); |
daveyclk | 5:a27fdb811237 | 100 | void ButtonUp(void); |
daveyclk | 5:a27fdb811237 | 101 | void txTask(void); |
daveyclk | 5:a27fdb811237 | 102 | |
daveyclk | 5:a27fdb811237 | 103 | /****************************************************************************** |
daveyclk | 5:a27fdb811237 | 104 | * Instance setups |
daveyclk | 5:a27fdb811237 | 105 | *******************************************************************************/ |
khuang | 0:a1af4ae04b06 | 106 | |
daveyclk | 5:a27fdb811237 | 107 | /* Instantiate the SSD1351 OLED Driver */ |
daveyclk | 5:a27fdb811237 | 108 | SSD1351 oled(PTB22,PTB21,PTC13,PTB20,PTE6, PTD15); /* (MOSI,SCLK,POWER,CS,RST,DC) */ |
daveyclk | 5:a27fdb811237 | 109 | |
daveyclk | 5:a27fdb811237 | 110 | /* Get OLED Class Default Text Properties */ |
daveyclk | 5:a27fdb811237 | 111 | oled_text_properties_t textProperties = {0}; |
daveyclk | 5:a27fdb811237 | 112 | |
daveyclk | 5:a27fdb811237 | 113 | //set up Analog read pin for alcohol sensor |
daveyclk | 5:a27fdb811237 | 114 | AnalogIn Alcohol(PTB2); |
daveyclk | 5:a27fdb811237 | 115 | |
daveyclk | 5:a27fdb811237 | 116 | /* Define timer for haptic feedback */ |
daveyclk | 5:a27fdb811237 | 117 | RtosTimer hapticTimer(StopHaptic, osTimerOnce); |
daveyclk | 5:a27fdb811237 | 118 | |
daveyclk | 5:a27fdb811237 | 119 | /* Instantiate the Hexi KW40Z Driver (UART TX, UART RX) */ |
daveyclk | 5:a27fdb811237 | 120 | KW40Z kw40z_device(PTE24, PTE25); |
daveyclk | 5:a27fdb811237 | 121 | |
daveyclk | 5:a27fdb811237 | 122 | /* LED and Haptic Set ups */ |
daveyclk | 5:a27fdb811237 | 123 | DigitalOut redLed(LED1); |
daveyclk | 5:a27fdb811237 | 124 | DigitalOut greenLed(LED2); |
daveyclk | 5:a27fdb811237 | 125 | DigitalOut blueLed(LED3); |
daveyclk | 5:a27fdb811237 | 126 | DigitalOut haptic(PTB9); |
daveyclk | 5:a27fdb811237 | 127 | |
daveyclk | 5:a27fdb811237 | 128 | /****************************************************************************** |
daveyclk | 5:a27fdb811237 | 129 | * Bluetooth button functions and passkey function |
daveyclk | 5:a27fdb811237 | 130 | *******************************************************************************/ |
daveyclk | 5:a27fdb811237 | 131 | |
daveyclk | 5:a27fdb811237 | 132 | void ButtonRight(void) |
daveyclk | 5:a27fdb811237 | 133 | { |
daveyclk | 5:a27fdb811237 | 134 | StartHaptic(); |
daveyclk | 5:a27fdb811237 | 135 | kw40z_device.ToggleAdvertisementMode(); |
daveyclk | 5:a27fdb811237 | 136 | blueLed = kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/ |
daveyclk | 5:a27fdb811237 | 137 | redLed = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/ |
daveyclk | 5:a27fdb811237 | 138 | greenLed = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/ |
daveyclk | 5:a27fdb811237 | 139 | } |
daveyclk | 5:a27fdb811237 | 140 | |
daveyclk | 5:a27fdb811237 | 141 | void ButtonLeft(void) |
daveyclk | 5:a27fdb811237 | 142 | { |
daveyclk | 5:a27fdb811237 | 143 | StartHaptic(); |
daveyclk | 5:a27fdb811237 | 144 | kw40z_device.ToggleAdvertisementMode(); |
daveyclk | 5:a27fdb811237 | 145 | blueLed = kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/ |
daveyclk | 5:a27fdb811237 | 146 | redLed = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/ |
daveyclk | 5:a27fdb811237 | 147 | greenLed = !kw40z_device.GetAdvertisementMode(); /*Indicate BLE Advertisment Mode*/ |
daveyclk | 5:a27fdb811237 | 148 | } |
daveyclk | 5:a27fdb811237 | 149 | |
daveyclk | 5:a27fdb811237 | 150 | void PassKey(void) |
daveyclk | 5:a27fdb811237 | 151 | { |
daveyclk | 5:a27fdb811237 | 152 | StartHaptic(); |
daveyclk | 5:a27fdb811237 | 153 | strcpy((char *) text,"PAIR CODE"); |
daveyclk | 5:a27fdb811237 | 154 | oled.TextBox((uint8_t *)text,0,25,95,18); |
daveyclk | 5:a27fdb811237 | 155 | |
daveyclk | 5:a27fdb811237 | 156 | /* Display Bond Pass Key in a 95px by 18px textbox at x=0,y=40 */ |
daveyclk | 5:a27fdb811237 | 157 | sprintf(text3,"%d", kw40z_device.GetPassKey()); |
daveyclk | 5:a27fdb811237 | 158 | oled.TextBox((uint8_t *)text3,0,40,95,18); |
daveyclk | 5:a27fdb811237 | 159 | } |
daveyclk | 5:a27fdb811237 | 160 | |
daveyclk | 5:a27fdb811237 | 161 | /****************************************************************************** |
daveyclk | 5:a27fdb811237 | 162 | * Main |
daveyclk | 5:a27fdb811237 | 163 | *******************************************************************************/ |
daveyclk | 5:a27fdb811237 | 164 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////// |
daveyclk | 5:a27fdb811237 | 165 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////// |
daveyclk | 5:a27fdb811237 | 166 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////// |
daveyclk | 5:a27fdb811237 | 167 | int main() |
daveyclk | 5:a27fdb811237 | 168 | { |
daveyclk | 5:a27fdb811237 | 169 | /* Set pointers to the BMPs stored in iBreatheImages.c */ |
daveyclk | 5:a27fdb811237 | 170 | welcome = iBreatheWS_bmp; // Welcome screen image |
daveyclk | 5:a27fdb811237 | 171 | blank = iBreatheBlank_bmp; // blank image |
daveyclk | 5:a27fdb811237 | 172 | blow = iBreatheBlow_bmp; // Start Blowing Image |
daveyclk | 5:a27fdb811237 | 173 | drink = iBreatheDrink_bmp; // You've been drinking image |
daveyclk | 5:a27fdb811237 | 174 | drive = iBreatheDrive_bmp; // Don't drive image |
daveyclk | 5:a27fdb811237 | 175 | hang = iBreatheHang_bmp; // You'll have a hangover image |
daveyclk | 5:a27fdb811237 | 176 | ini = iBreatheini_bmp; // Initialising image |
daveyclk | 5:a27fdb811237 | 177 | sober = iBreatheSober_bmp; // Sober as a judge image |
daveyclk | 5:a27fdb811237 | 178 | |
daveyclk | 5:a27fdb811237 | 179 | /* Set initial Values */ |
daveyclk | 5:a27fdb811237 | 180 | sysinit(); |
daveyclk | 5:a27fdb811237 | 181 | } |
daveyclk | 5:a27fdb811237 | 182 | |
daveyclk | 5:a27fdb811237 | 183 | /****************************************************************************** |
daveyclk | 5:a27fdb811237 | 184 | * Public Function Definitions |
daveyclk | 5:a27fdb811237 | 185 | *******************************************************************************/ |
daveyclk | 5:a27fdb811237 | 186 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////// |
daveyclk | 5:a27fdb811237 | 187 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////// |
daveyclk | 5:a27fdb811237 | 188 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////// |
daveyclk | 5:a27fdb811237 | 189 | /************************************************************************************************** |
daveyclk | 5:a27fdb811237 | 190 | * Function readSensor(void) |
daveyclk | 5:a27fdb811237 | 191 | * ------------------------------------------------------------------------------------------------- |
daveyclk | 5:a27fdb811237 | 192 | * Overview: Read sensor |
daveyclk | 5:a27fdb811237 | 193 | * Input: None |
daveyclk | 5:a27fdb811237 | 194 | * Output: None |
daveyclk | 5:a27fdb811237 | 195 | **************************************************************************************************/ |
daveyclk | 5:a27fdb811237 | 196 | |
daveyclk | 5:a27fdb811237 | 197 | void ReadSensor() |
daveyclk | 5:a27fdb811237 | 198 | { |
daveyclk | 5:a27fdb811237 | 199 | /* Read 16 Bit Analog value */ |
daveyclk | 5:a27fdb811237 | 200 | adc_rd = Alcohol.read_u16(); |
daveyclk | 5:a27fdb811237 | 201 | |
daveyclk | 5:a27fdb811237 | 202 | // pause 200ms |
daveyclk | 5:a27fdb811237 | 203 | Thread::wait(200); |
daveyclk | 5:a27fdb811237 | 204 | } |
daveyclk | 5:a27fdb811237 | 205 | |
daveyclk | 5:a27fdb811237 | 206 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////// |
daveyclk | 5:a27fdb811237 | 207 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////// |
daveyclk | 5:a27fdb811237 | 208 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////// |
daveyclk | 5:a27fdb811237 | 209 | /************************************************************************************************** |
daveyclk | 5:a27fdb811237 | 210 | * Function CalculatePPM(Int times, bool amb) |
daveyclk | 5:a27fdb811237 | 211 | * ------------------------------------------------------------------------------------------------- |
daveyclk | 5:a27fdb811237 | 212 | * Overview: Calculation of PPM |
daveyclk | 5:a27fdb811237 | 213 | * Input: times = Number of samples to take, amb sets either ambient reading or test reading (true for test) |
daveyclk | 5:a27fdb811237 | 214 | * Output: None |
daveyclk | 5:a27fdb811237 | 215 | **************************************************************************************************/ |
khuang | 0:a1af4ae04b06 | 216 | |
daveyclk | 5:a27fdb811237 | 217 | void CalculatePPM(int times, bool amb) |
daveyclk | 5:a27fdb811237 | 218 | { |
daveyclk | 5:a27fdb811237 | 219 | float lgPPM; |
daveyclk | 5:a27fdb811237 | 220 | |
daveyclk | 5:a27fdb811237 | 221 | /* Read values x times */ |
daveyclk | 5:a27fdb811237 | 222 | for(int x = 0; x < times; x++) |
daveyclk | 5:a27fdb811237 | 223 | { |
daveyclk | 5:a27fdb811237 | 224 | ReadSensor(); |
daveyclk | 5:a27fdb811237 | 225 | value[x] = ((float)adc_rd * Vadc_3V3); |
daveyclk | 5:a27fdb811237 | 226 | StartHaptic(); |
daveyclk | 5:a27fdb811237 | 227 | Thread::wait(50); |
daveyclk | 5:a27fdb811237 | 228 | } |
daveyclk | 5:a27fdb811237 | 229 | |
daveyclk | 5:a27fdb811237 | 230 | /* Calculate the average value for accuratcy */ |
daveyclk | 5:a27fdb811237 | 231 | for(int y = 0; y < times; y++) |
daveyclk | 5:a27fdb811237 | 232 | { |
daveyclk | 5:a27fdb811237 | 233 | Vrl += value[y]; |
daveyclk | 5:a27fdb811237 | 234 | } |
daveyclk | 5:a27fdb811237 | 235 | Vrl = Vrl / times; |
daveyclk | 5:a27fdb811237 | 236 | |
daveyclk | 5:a27fdb811237 | 237 | /* Set SensorRes reference value */ |
daveyclk | 5:a27fdb811237 | 238 | SensorRes = (Vrl / 3.3); |
daveyclk | 5:a27fdb811237 | 239 | |
daveyclk | 5:a27fdb811237 | 240 | /* Set ratio */ |
daveyclk | 5:a27fdb811237 | 241 | ratio = SensorRes1 / SensorRes; |
daveyclk | 5:a27fdb811237 | 242 | |
daveyclk | 5:a27fdb811237 | 243 | /* Convert to PPM */ |
daveyclk | 5:a27fdb811237 | 244 | lgPPM = ( log10( ratio ) * -1.5512 ) + 2.5911; |
daveyclk | 5:a27fdb811237 | 245 | |
daveyclk | 5:a27fdb811237 | 246 | /* If true create test result, flase creates reference result */ |
daveyclk | 5:a27fdb811237 | 247 | if (amb == true) |
daveyclk | 5:a27fdb811237 | 248 | { |
daveyclk | 5:a27fdb811237 | 249 | ppm = pow( 10, lgPPM ); |
daveyclk | 5:a27fdb811237 | 250 | } |
daveyclk | 5:a27fdb811237 | 251 | else |
daveyclk | 5:a27fdb811237 | 252 | { |
daveyclk | 5:a27fdb811237 | 253 | ppm_1 = pow( 10, lgPPM ); |
daveyclk | 5:a27fdb811237 | 254 | } |
daveyclk | 5:a27fdb811237 | 255 | } |
daveyclk | 5:a27fdb811237 | 256 | |
daveyclk | 5:a27fdb811237 | 257 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////// |
daveyclk | 5:a27fdb811237 | 258 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////// |
daveyclk | 5:a27fdb811237 | 259 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////// |
daveyclk | 5:a27fdb811237 | 260 | /************************************************************************************************** |
daveyclk | 5:a27fdb811237 | 261 | * Function void ambient(int times) |
daveyclk | 5:a27fdb811237 | 262 | * ------------------------------------------------------------------------------------------------- |
daveyclk | 5:a27fdb811237 | 263 | * Overview: Reading of the Ambient Voltgage of the sensor for reference |
daveyclk | 5:a27fdb811237 | 264 | * Input: times = Number of samples to take |
daveyclk | 5:a27fdb811237 | 265 | * Output: None |
daveyclk | 5:a27fdb811237 | 266 | **************************************************************************************************/ |
daveyclk | 5:a27fdb811237 | 267 | |
daveyclk | 5:a27fdb811237 | 268 | void ambient(int times) |
daveyclk | 5:a27fdb811237 | 269 | { |
daveyclk | 5:a27fdb811237 | 270 | /* Read ambient values x times flashing green led*/ |
daveyclk | 5:a27fdb811237 | 271 | for(int x = 0; x < times; x++) |
daveyclk | 5:a27fdb811237 | 272 | { |
daveyclk | 5:a27fdb811237 | 273 | redLed = LED_OFF; |
daveyclk | 5:a27fdb811237 | 274 | greenLed = LED_OFF; |
daveyclk | 5:a27fdb811237 | 275 | blueLed = LED_OFF; |
daveyclk | 5:a27fdb811237 | 276 | |
daveyclk | 5:a27fdb811237 | 277 | ReadSensor(); |
daveyclk | 5:a27fdb811237 | 278 | value1[x] = (float)adc_rd * Vadc_3V3; |
daveyclk | 5:a27fdb811237 | 279 | |
daveyclk | 5:a27fdb811237 | 280 | redLed = LED_OFF; |
daveyclk | 5:a27fdb811237 | 281 | greenLed = LED_ON; |
daveyclk | 5:a27fdb811237 | 282 | blueLed = LED_OFF; |
daveyclk | 5:a27fdb811237 | 283 | Thread::wait(48); |
daveyclk | 5:a27fdb811237 | 284 | } |
khuang | 0:a1af4ae04b06 | 285 | |
daveyclk | 5:a27fdb811237 | 286 | /* Calculate the average value for accuratcy */ |
daveyclk | 5:a27fdb811237 | 287 | for(int y = 0; y < times; y++) |
daveyclk | 5:a27fdb811237 | 288 | { |
daveyclk | 5:a27fdb811237 | 289 | ambientAlc+=value1[y]; |
daveyclk | 5:a27fdb811237 | 290 | } |
daveyclk | 5:a27fdb811237 | 291 | ambientAlc = ambientAlc / times; |
daveyclk | 5:a27fdb811237 | 292 | |
daveyclk | 5:a27fdb811237 | 293 | /* Set SensorRes1 reference value */ |
daveyclk | 5:a27fdb811237 | 294 | SensorRes1 = (ambientAlc / 3.3); |
daveyclk | 5:a27fdb811237 | 295 | } |
daveyclk | 5:a27fdb811237 | 296 | |
daveyclk | 5:a27fdb811237 | 297 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////// |
daveyclk | 5:a27fdb811237 | 298 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////// |
daveyclk | 5:a27fdb811237 | 299 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////// |
daveyclk | 5:a27fdb811237 | 300 | /************************************************************************************************** |
daveyclk | 5:a27fdb811237 | 301 | * Function void StartHaptic(void) |
daveyclk | 5:a27fdb811237 | 302 | * ------------------------------------------------------------------------------------------------- |
daveyclk | 5:a27fdb811237 | 303 | * Overview: Start Buzzing haptic motor |
daveyclk | 5:a27fdb811237 | 304 | * Input: None |
daveyclk | 5:a27fdb811237 | 305 | * Output: None |
daveyclk | 5:a27fdb811237 | 306 | **************************************************************************************************/ |
daveyclk | 5:a27fdb811237 | 307 | |
daveyclk | 5:a27fdb811237 | 308 | void StartHaptic(void) |
daveyclk | 5:a27fdb811237 | 309 | { |
daveyclk | 5:a27fdb811237 | 310 | hapticTimer.start(50); |
daveyclk | 5:a27fdb811237 | 311 | haptic = 1; |
daveyclk | 5:a27fdb811237 | 312 | } |
daveyclk | 5:a27fdb811237 | 313 | |
daveyclk | 5:a27fdb811237 | 314 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////// |
daveyclk | 5:a27fdb811237 | 315 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////// |
daveyclk | 5:a27fdb811237 | 316 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////// |
daveyclk | 5:a27fdb811237 | 317 | /************************************************************************************************** |
daveyclk | 5:a27fdb811237 | 318 | * Function void StopHaptic(void) |
daveyclk | 5:a27fdb811237 | 319 | * ------------------------------------------------------------------------------------------------- |
daveyclk | 5:a27fdb811237 | 320 | * Overview: Stop Buzzing haptic motor |
daveyclk | 5:a27fdb811237 | 321 | * Input: None |
daveyclk | 5:a27fdb811237 | 322 | * Output: None |
daveyclk | 5:a27fdb811237 | 323 | **************************************************************************************************/ |
daveyclk | 5:a27fdb811237 | 324 | |
daveyclk | 5:a27fdb811237 | 325 | void StopHaptic(void const *n) { |
daveyclk | 5:a27fdb811237 | 326 | haptic = 0; |
daveyclk | 5:a27fdb811237 | 327 | hapticTimer.stop(); |
daveyclk | 5:a27fdb811237 | 328 | } |
daveyclk | 5:a27fdb811237 | 329 | |
daveyclk | 5:a27fdb811237 | 330 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////// |
daveyclk | 5:a27fdb811237 | 331 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////// |
daveyclk | 5:a27fdb811237 | 332 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////// |
daveyclk | 5:a27fdb811237 | 333 | /************************************************************************************************** |
daveyclk | 5:a27fdb811237 | 334 | * Function void ButtonUp(void) |
daveyclk | 5:a27fdb811237 | 335 | * ------------------------------------------------------------------------------------------------- |
daveyclk | 5:a27fdb811237 | 336 | * Overview: Function called whe up button pressed, Begins Breathilyzer testing procedure |
daveyclk | 5:a27fdb811237 | 337 | * Input: None |
daveyclk | 5:a27fdb811237 | 338 | * Output: None |
daveyclk | 5:a27fdb811237 | 339 | **************************************************************************************************/ |
khuang | 0:a1af4ae04b06 | 340 | |
daveyclk | 5:a27fdb811237 | 341 | void ButtonUp(void) |
daveyclk | 5:a27fdb811237 | 342 | { |
daveyclk | 5:a27fdb811237 | 343 | StartHaptic(); |
daveyclk | 5:a27fdb811237 | 344 | |
daveyclk | 5:a27fdb811237 | 345 | /* LED set to green for test beginning*/ |
daveyclk | 5:a27fdb811237 | 346 | redLed = LED_OFF; |
daveyclk | 5:a27fdb811237 | 347 | greenLed = LED_ON; |
daveyclk | 5:a27fdb811237 | 348 | blueLed = LED_OFF; |
daveyclk | 5:a27fdb811237 | 349 | |
daveyclk | 5:a27fdb811237 | 350 | /* Fill 96px by 96px Screen with 96px by 96px Initialising Image starting at x=0,y=0 */ |
daveyclk | 5:a27fdb811237 | 351 | oled.DrawImage(ini,0,0); |
daveyclk | 5:a27fdb811237 | 352 | |
daveyclk | 5:a27fdb811237 | 353 | /*read ambient atmosphere levels with 10 samples and set flag to show it's ambient basline figure*/ |
daveyclk | 5:a27fdb811237 | 354 | ambient(10); |
daveyclk | 5:a27fdb811237 | 355 | CalculatePPM(10,false); |
daveyclk | 5:a27fdb811237 | 356 | |
daveyclk | 5:a27fdb811237 | 357 | /* Fill 96px by 96px Screen with 96px by 96px Blowing Image starting at x=0,y=0 */ |
daveyclk | 5:a27fdb811237 | 358 | oled.DrawImage(blow,0,0); |
daveyclk | 5:a27fdb811237 | 359 | |
daveyclk | 5:a27fdb811237 | 360 | /*read breathe alcohol levels with 10 samples and set flag to show it's breathilyzer test figure*/ |
daveyclk | 5:a27fdb811237 | 361 | CalculatePPM(10,true); |
daveyclk | 5:a27fdb811237 | 362 | |
daveyclk | 5:a27fdb811237 | 363 | /*Calculate the difference in Alcohol level based on Ambient and test sample*/ |
daveyclk | 5:a27fdb811237 | 364 | ppm = ppm - ppm_1; |
daveyclk | 5:a27fdb811237 | 365 | |
daveyclk | 5:a27fdb811237 | 366 | /*Throw away any values less than 0*/ |
daveyclk | 5:a27fdb811237 | 367 | if (ppm < 0) |
daveyclk | 5:a27fdb811237 | 368 | { |
daveyclk | 5:a27fdb811237 | 369 | ppm = 0; |
daveyclk | 5:a27fdb811237 | 370 | } |
daveyclk | 5:a27fdb811237 | 371 | |
daveyclk | 5:a27fdb811237 | 372 | /* Fill 96px by 96px Screen with 96px by 96px Blank Background Image starting at x=0,y=0 */ |
daveyclk | 5:a27fdb811237 | 373 | oled.DrawImage(blank,0,0); |
daveyclk | 5:a27fdb811237 | 374 | |
daveyclk | 5:a27fdb811237 | 375 | /* Show Calculated alcohol level in PPM and send data via bluetooth to the cloud */ |
daveyclk | 5:a27fdb811237 | 376 | oled.SetTextProperties(&textProperties); |
daveyclk | 5:a27fdb811237 | 377 | oled.Label(ppmText,20,36); |
daveyclk | 5:a27fdb811237 | 378 | sprintf(text,"%.2f",ppm); |
daveyclk | 5:a27fdb811237 | 379 | oled.Label((uint8_t *)text,50,36); |
daveyclk | 5:a27fdb811237 | 380 | Thread::wait(1000); |
daveyclk | 5:a27fdb811237 | 381 | |
daveyclk | 5:a27fdb811237 | 382 | /* Currently sending to the Pressure variable as a temp place holder */ |
daveyclk | 5:a27fdb811237 | 383 | kw40z_device.SendPressure(ppm * 10); |
daveyclk | 5:a27fdb811237 | 384 | |
daveyclk | 5:a27fdb811237 | 385 | /* You've got a Hangover coming!*/ |
daveyclk | 5:a27fdb811237 | 386 | if ( ppm > 200) |
daveyclk | 5:a27fdb811237 | 387 | { |
daveyclk | 5:a27fdb811237 | 388 | redLed = LED_ON; |
daveyclk | 5:a27fdb811237 | 389 | greenLed = LED_OFF; |
daveyclk | 5:a27fdb811237 | 390 | blueLed = LED_OFF; |
daveyclk | 5:a27fdb811237 | 391 | |
daveyclk | 5:a27fdb811237 | 392 | StartHaptic(); |
daveyclk | 5:a27fdb811237 | 393 | |
daveyclk | 5:a27fdb811237 | 394 | /* Fill 96px by 96px Screen with 96px by 96px Hangover Image starting at x=0,y=0 */ |
daveyclk | 5:a27fdb811237 | 395 | oled.DrawImage(hang,0,0); |
daveyclk | 5:a27fdb811237 | 396 | } |
daveyclk | 5:a27fdb811237 | 397 | |
daveyclk | 5:a27fdb811237 | 398 | /* You Shouldn't drive */ |
daveyclk | 5:a27fdb811237 | 399 | else if (ppm < 200 && ppm > 150) |
daveyclk | 5:a27fdb811237 | 400 | { |
daveyclk | 5:a27fdb811237 | 401 | redLed = LED_ON; |
daveyclk | 5:a27fdb811237 | 402 | greenLed = LED_OFF; |
daveyclk | 5:a27fdb811237 | 403 | blueLed = LED_ON; |
daveyclk | 5:a27fdb811237 | 404 | |
daveyclk | 5:a27fdb811237 | 405 | StartHaptic(); |
daveyclk | 5:a27fdb811237 | 406 | |
daveyclk | 5:a27fdb811237 | 407 | /* Fill 96px by 96px Screen with 96px by 96px Don't Drive Image starting at x=0,y=0 */ |
daveyclk | 5:a27fdb811237 | 408 | oled.DrawImage(drive,0,0); |
daveyclk | 5:a27fdb811237 | 409 | } |
daveyclk | 5:a27fdb811237 | 410 | |
daveyclk | 5:a27fdb811237 | 411 | /* You've had a drink */ |
daveyclk | 5:a27fdb811237 | 412 | else if (ppm < 150 && ppm > 50) |
daveyclk | 5:a27fdb811237 | 413 | { |
daveyclk | 5:a27fdb811237 | 414 | redLed = LED_OFF; |
daveyclk | 5:a27fdb811237 | 415 | greenLed = LED_ON; |
daveyclk | 5:a27fdb811237 | 416 | blueLed = LED_ON; |
daveyclk | 5:a27fdb811237 | 417 | |
daveyclk | 5:a27fdb811237 | 418 | StartHaptic(); |
daveyclk | 5:a27fdb811237 | 419 | |
daveyclk | 5:a27fdb811237 | 420 | /* Fill 96px by 96px Screen with 96px by 96px Had a drink Image starting at x=0,y=0 */ |
daveyclk | 5:a27fdb811237 | 421 | oled.DrawImage(drink,0,0); |
daveyclk | 5:a27fdb811237 | 422 | } |
daveyclk | 5:a27fdb811237 | 423 | |
daveyclk | 5:a27fdb811237 | 424 | /* Sober as a judge*/ |
daveyclk | 5:a27fdb811237 | 425 | else |
daveyclk | 5:a27fdb811237 | 426 | { |
daveyclk | 5:a27fdb811237 | 427 | redLed = LED_OFF; |
daveyclk | 5:a27fdb811237 | 428 | greenLed = LED_ON; |
daveyclk | 5:a27fdb811237 | 429 | blueLed = LED_OFF; |
daveyclk | 5:a27fdb811237 | 430 | |
daveyclk | 5:a27fdb811237 | 431 | StartHaptic(); |
daveyclk | 5:a27fdb811237 | 432 | oled.DrawImage(sober,0,0); |
daveyclk | 5:a27fdb811237 | 433 | } |
daveyclk | 5:a27fdb811237 | 434 | Thread::wait(5000); |
daveyclk | 5:a27fdb811237 | 435 | |
daveyclk | 5:a27fdb811237 | 436 | /* Go back to start screen */ |
daveyclk | 5:a27fdb811237 | 437 | sysinit(); |
daveyclk | 5:a27fdb811237 | 438 | } |
daveyclk | 5:a27fdb811237 | 439 | |
daveyclk | 5:a27fdb811237 | 440 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////// |
daveyclk | 5:a27fdb811237 | 441 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////// |
daveyclk | 5:a27fdb811237 | 442 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////// |
daveyclk | 5:a27fdb811237 | 443 | /************************************************************************************************** |
daveyclk | 5:a27fdb811237 | 444 | * Function void sysint(void) |
daveyclk | 5:a27fdb811237 | 445 | * ------------------------------------------------------------------------------------------------- |
daveyclk | 5:a27fdb811237 | 446 | * Overview: System Initial Values Set up |
daveyclk | 5:a27fdb811237 | 447 | * Input: None |
daveyclk | 5:a27fdb811237 | 448 | * Output: None |
daveyclk | 5:a27fdb811237 | 449 | **************************************************************************************************/ |
daveyclk | 5:a27fdb811237 | 450 | void sysinit(void) |
daveyclk | 5:a27fdb811237 | 451 | { |
daveyclk | 5:a27fdb811237 | 452 | /* Set LED to Blue by default*/ |
daveyclk | 5:a27fdb811237 | 453 | redLed = LED_OFF; |
daveyclk | 5:a27fdb811237 | 454 | greenLed = LED_OFF; |
daveyclk | 5:a27fdb811237 | 455 | blueLed = LED_ON; |
daveyclk | 5:a27fdb811237 | 456 | |
khuang | 1:42e8e50ae4ac | 457 | /* Turn on the backlight of the OLED Display */ |
khuang | 1:42e8e50ae4ac | 458 | oled.DimScreenON(); |
khuang | 1:42e8e50ae4ac | 459 | |
daveyclk | 5:a27fdb811237 | 460 | /* Fill 96px by 96px Screen with 96px by 96px Welcome Image starting at x=0,y=0 */ |
daveyclk | 5:a27fdb811237 | 461 | oled.DrawImage(welcome,0,0); |
daveyclk | 5:a27fdb811237 | 462 | |
daveyclk | 5:a27fdb811237 | 463 | /* Register callbacks to application functions */ |
daveyclk | 5:a27fdb811237 | 464 | kw40z_device.attach_buttonUp(&ButtonUp); |
daveyclk | 5:a27fdb811237 | 465 | kw40z_device.attach_buttonLeft(&ButtonLeft); |
daveyclk | 5:a27fdb811237 | 466 | kw40z_device.attach_buttonRight(&ButtonRight); |
daveyclk | 5:a27fdb811237 | 467 | kw40z_device.attach_passkey(&PassKey); |
daveyclk | 5:a27fdb811237 | 468 | |
daveyclk | 5:a27fdb811237 | 469 | /* Send sensor data to bluetooth */ |
daveyclk | 5:a27fdb811237 | 470 | kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG); |
daveyclk | 5:a27fdb811237 | 471 | |
daveyclk | 5:a27fdb811237 | 472 | /* Change font color to White */ |
daveyclk | 5:a27fdb811237 | 473 | oled.GetTextProperties(&textProperties); |
daveyclk | 5:a27fdb811237 | 474 | textProperties.fontColor = COLOR_WHITE; |
khuang | 0:a1af4ae04b06 | 475 | oled.SetTextProperties(&textProperties); |
daveyclk | 5:a27fdb811237 | 476 | } |