Liam Grazier
/
0NicksCoursework-lg
diajzifsajoifajsoifaj
Fork of ELEC350-CWTEMPLATE-2017 by
components.cpp@7:dd303488e55d, 2018-01-07 (annotated)
- Committer:
- liam_grazier
- Date:
- Sun Jan 07 21:29:12 2018 +0000
- Revision:
- 7:dd303488e55d
- Parent:
- 5:9b4844128e09
working LCD, SD, (NON FIFO) , webpage, all threaded
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
liam_grazier | 5:9b4844128e09 | 1 | #include "mbed.h" |
liam_grazier | 5:9b4844128e09 | 2 | #include "components.hpp" |
liam_grazier | 5:9b4844128e09 | 3 | #include "lglcd.h" |
liam_grazier | 7:dd303488e55d | 4 | #include "stdio.h" |
liam_grazier | 5:9b4844128e09 | 5 | #define RED_DONE 1 |
liam_grazier | 5:9b4844128e09 | 6 | #define YELLOW_DONE 2 |
liam_grazier | 5:9b4844128e09 | 7 | //Digital outputs |
liam_grazier | 7:dd303488e55d | 8 | DigitalIn onBoardSwitch(USER_BUTTON); |
liam_grazier | 5:9b4844128e09 | 9 | DigitalOut onBoardLED(LED1); |
liam_grazier | 5:9b4844128e09 | 10 | DigitalOut redLED(PE_15); |
liam_grazier | 5:9b4844128e09 | 11 | DigitalOut yellowLED(PB_10); |
liam_grazier | 5:9b4844128e09 | 12 | DigitalOut greenLED(PB_11); |
liam_grazier | 7:dd303488e55d | 13 | SDBlockDevice sd(PB_5, D12, D13, D10);// miso, sclk, cs |
liam_grazier | 7:dd303488e55d | 14 | Mutex Lock1; |
liam_grazier | 7:dd303488e55d | 15 | Mutex Lock2; |
liam_grazier | 7:dd303488e55d | 16 | Mutex Remove; |
liam_grazier | 5:9b4844128e09 | 17 | //Inputs |
liam_grazier | 7:dd303488e55d | 18 | |
liam_grazier | 5:9b4844128e09 | 19 | DigitalIn SW1(PE_12); |
liam_grazier | 5:9b4844128e09 | 20 | DigitalIn SW2(PE_14); |
liam_grazier | 5:9b4844128e09 | 21 | //Serial pc(USBTX, USBRX); |
liam_grazier | 5:9b4844128e09 | 22 | AnalogIn adcIn(PA_0); |
liam_grazier | 5:9b4844128e09 | 23 | |
liam_grazier | 5:9b4844128e09 | 24 | //Environmental Sensor driver |
liam_grazier | 5:9b4844128e09 | 25 | #ifdef BME |
liam_grazier | 5:9b4844128e09 | 26 | BME280 sensor(D14, D15); |
liam_grazier | 5:9b4844128e09 | 27 | #else |
liam_grazier | 5:9b4844128e09 | 28 | BMP280 sensor(D14, D15); |
liam_grazier | 5:9b4844128e09 | 29 | #endif |
liam_grazier | 5:9b4844128e09 | 30 | lglcd mylcd(D7,D6,D5,D4,D3,D2); |
liam_grazier | 5:9b4844128e09 | 31 | |
liam_grazier | 5:9b4844128e09 | 32 | //POWER ON SELF TEST |
liam_grazier | 5:9b4844128e09 | 33 | void post() |
liam_grazier | 5:9b4844128e09 | 34 | { |
liam_grazier | 5:9b4844128e09 | 35 | //posttest |
liam_grazier | 5:9b4844128e09 | 36 | } |
liam_grazier | 7:dd303488e55d | 37 | void runanalysis(void){ |
liam_grazier | 7:dd303488e55d | 38 | while(1){ |
liam_grazier | 7:dd303488e55d | 39 | Lock1.lock(); |
liam_grazier | 7:dd303488e55d | 40 | double temp = sensor.getTemperature(); |
liam_grazier | 7:dd303488e55d | 41 | double pressure = sensor.getPressure(); |
liam_grazier | 7:dd303488e55d | 42 | double lightin = adcIn; |
liam_grazier | 7:dd303488e55d | 43 | char TEM[6]; |
liam_grazier | 7:dd303488e55d | 44 | char PRE[5]; |
liam_grazier | 7:dd303488e55d | 45 | char LIGHT[6]; |
liam_grazier | 7:dd303488e55d | 46 | printf("REFRESH SENSORS\n\r"); |
liam_grazier | 7:dd303488e55d | 47 | sprintf(TEM,"%.2f", temp); |
liam_grazier | 7:dd303488e55d | 48 | sprintf(PRE,"%.2f", pressure); |
liam_grazier | 7:dd303488e55d | 49 | sprintf(LIGHT,"%.2f", lightin); |
liam_grazier | 7:dd303488e55d | 50 | mylcd.setline(1,1); |
liam_grazier | 7:dd303488e55d | 51 | mylcd.write("L:"); |
liam_grazier | 7:dd303488e55d | 52 | if(lightin > 0.7 && lightin < 0.9) |
liam_grazier | 7:dd303488e55d | 53 | { |
liam_grazier | 7:dd303488e55d | 54 | mylcd.setline(1,4); |
liam_grazier | 7:dd303488e55d | 55 | mylcd.write("|||||||||MAX"); |
liam_grazier | 7:dd303488e55d | 56 | } |
liam_grazier | 7:dd303488e55d | 57 | else if(lightin > 0.55 && lightin < 0.69) |
liam_grazier | 7:dd303488e55d | 58 | { |
liam_grazier | 7:dd303488e55d | 59 | mylcd.setline(1,4); |
liam_grazier | 7:dd303488e55d | 60 | mylcd.write("||||||| "); |
liam_grazier | 7:dd303488e55d | 61 | } |
liam_grazier | 7:dd303488e55d | 62 | else if(lightin > 0.5 && lightin < 0.54) |
liam_grazier | 7:dd303488e55d | 63 | { |
liam_grazier | 7:dd303488e55d | 64 | mylcd.setline(1,4); |
liam_grazier | 7:dd303488e55d | 65 | mylcd.write("||||| "); |
liam_grazier | 7:dd303488e55d | 66 | } |
liam_grazier | 7:dd303488e55d | 67 | else if(lightin > 0.4 && lightin < 0.54) |
liam_grazier | 7:dd303488e55d | 68 | { |
liam_grazier | 7:dd303488e55d | 69 | mylcd.setline(1,4); |
liam_grazier | 7:dd303488e55d | 70 | mylcd.write("||| "); |
liam_grazier | 7:dd303488e55d | 71 | } |
liam_grazier | 7:dd303488e55d | 72 | else if(lightin > 0.3 && lightin < 0.39) |
liam_grazier | 7:dd303488e55d | 73 | { |
liam_grazier | 7:dd303488e55d | 74 | mylcd.setline(1,4); |
liam_grazier | 7:dd303488e55d | 75 | mylcd.write("|| "); |
liam_grazier | 7:dd303488e55d | 76 | } |
liam_grazier | 7:dd303488e55d | 77 | else if(lightin > 0.06 && lightin < 0.29) |
liam_grazier | 7:dd303488e55d | 78 | { |
liam_grazier | 7:dd303488e55d | 79 | mylcd.setline(1,4); |
liam_grazier | 7:dd303488e55d | 80 | mylcd.write("LOW LIGHT "); |
liam_grazier | 7:dd303488e55d | 81 | } |
liam_grazier | 7:dd303488e55d | 82 | else if(lightin < 0.05) |
liam_grazier | 7:dd303488e55d | 83 | { |
liam_grazier | 7:dd303488e55d | 84 | mylcd.setline(1,4); |
liam_grazier | 7:dd303488e55d | 85 | mylcd.write(" "); |
liam_grazier | 7:dd303488e55d | 86 | mylcd.setline(1,4); |
liam_grazier | 7:dd303488e55d | 87 | mylcd.write("disconnected"); |
liam_grazier | 7:dd303488e55d | 88 | } |
liam_grazier | 7:dd303488e55d | 89 | mylcd.setline(2,1); |
liam_grazier | 7:dd303488e55d | 90 | mylcd.write("P:"); |
liam_grazier | 7:dd303488e55d | 91 | mylcd.write(PRE); |
liam_grazier | 7:dd303488e55d | 92 | mylcd.setline(2,10); |
liam_grazier | 7:dd303488e55d | 93 | mylcd.write("T:"); |
liam_grazier | 7:dd303488e55d | 94 | mylcd.write(TEM); |
liam_grazier | 7:dd303488e55d | 95 | wait(0.01); |
liam_grazier | 7:dd303488e55d | 96 | Lock1.unlock(); |
liam_grazier | 7:dd303488e55d | 97 | Thread::signal_wait(SIG_READY); |
liam_grazier | 7:dd303488e55d | 98 | } |
liam_grazier | 7:dd303488e55d | 99 | } |
liam_grazier | 7:dd303488e55d | 100 | void sdwrite(void) |
liam_grazier | 7:dd303488e55d | 101 | { |
liam_grazier | 7:dd303488e55d | 102 | while(1) |
liam_grazier | 7:dd303488e55d | 103 | { |
liam_grazier | 7:dd303488e55d | 104 | Lock2.lock(); |
liam_grazier | 7:dd303488e55d | 105 | FATFileSystem fs("sd", &sd); |
liam_grazier | 7:dd303488e55d | 106 | FILE* fp = fopen("/sd/test.csv","a"); |
liam_grazier | 7:dd303488e55d | 107 | if (fp == NULL) { |
liam_grazier | 7:dd303488e55d | 108 | errorCode(FATAL); |
liam_grazier | 7:dd303488e55d | 109 | printf("SD FAIL\n\r"); |
liam_grazier | 7:dd303488e55d | 110 | mylcd.clear(); |
liam_grazier | 7:dd303488e55d | 111 | mylcd.setline(1,0); |
liam_grazier | 7:dd303488e55d | 112 | mylcd.write("SD FAIL"); |
liam_grazier | 7:dd303488e55d | 113 | } |
liam_grazier | 7:dd303488e55d | 114 | if (fp != NULL){ |
liam_grazier | 7:dd303488e55d | 115 | printf("SD Success\n\r"); |
liam_grazier | 7:dd303488e55d | 116 | fprintf(fp, "hello,hello,hello\n\r"); |
liam_grazier | 7:dd303488e55d | 117 | wait(0.01); |
liam_grazier | 7:dd303488e55d | 118 | } |
liam_grazier | 7:dd303488e55d | 119 | fclose(fp); |
liam_grazier | 7:dd303488e55d | 120 | Lock2.unlock(); |
liam_grazier | 7:dd303488e55d | 121 | Thread::signal_wait(SIG_READY2); |
liam_grazier | 5:9b4844128e09 | 122 | |
liam_grazier | 7:dd303488e55d | 123 | } |
liam_grazier | 7:dd303488e55d | 124 | } |
liam_grazier | 7:dd303488e55d | 125 | void sdrun(void) |
liam_grazier | 7:dd303488e55d | 126 | { |
liam_grazier | 7:dd303488e55d | 127 | if ( sd.init() != 0) { |
liam_grazier | 7:dd303488e55d | 128 | printf("Init failed \n"); |
liam_grazier | 7:dd303488e55d | 129 | mylcd.clear(); |
liam_grazier | 7:dd303488e55d | 130 | mylcd.setline(1,1); |
liam_grazier | 7:dd303488e55d | 131 | mylcd.write("CANNOT INIT SD"); |
liam_grazier | 7:dd303488e55d | 132 | errorCode(FATAL); |
liam_grazier | 7:dd303488e55d | 133 | } |
liam_grazier | 7:dd303488e55d | 134 | if( sd.init() == 0){ |
liam_grazier | 7:dd303488e55d | 135 | printf("Init Success \n"); |
liam_grazier | 7:dd303488e55d | 136 | mylcd.clear(); |
liam_grazier | 7:dd303488e55d | 137 | mylcd.setline(1,1); |
liam_grazier | 7:dd303488e55d | 138 | mylcd.write("SD GOOD MAN"); |
liam_grazier | 7:dd303488e55d | 139 | wait(0.5); //flash the SD error / good code! |
liam_grazier | 7:dd303488e55d | 140 | } |
liam_grazier | 7:dd303488e55d | 141 | } |
liam_grazier | 7:dd303488e55d | 142 | void sdcheck(void){ |
liam_grazier | 7:dd303488e55d | 143 | if ( sd.init() != 0) { |
liam_grazier | 7:dd303488e55d | 144 | printf("Init failed \n\r"); |
liam_grazier | 7:dd303488e55d | 145 | mylcd.clear(); |
liam_grazier | 7:dd303488e55d | 146 | mylcd.setline(1,1); |
liam_grazier | 7:dd303488e55d | 147 | mylcd.write("CANNOT INIT SD\n\r"); |
liam_grazier | 7:dd303488e55d | 148 | errorCode(FATAL); |
liam_grazier | 7:dd303488e55d | 149 | } |
liam_grazier | 7:dd303488e55d | 150 | if( sd.init() == 0){ |
liam_grazier | 7:dd303488e55d | 151 | printf("SD Good\n\r"); |
liam_grazier | 7:dd303488e55d | 152 | } |
liam_grazier | 7:dd303488e55d | 153 | } |
liam_grazier | 7:dd303488e55d | 154 | void sdremove(void) |
liam_grazier | 7:dd303488e55d | 155 | { |
liam_grazier | 7:dd303488e55d | 156 | Thread::signal_wait(SIG_REMOVE); |
liam_grazier | 7:dd303488e55d | 157 | mylcd.clear(); |
liam_grazier | 7:dd303488e55d | 158 | Remove.lock(); |
liam_grazier | 7:dd303488e55d | 159 | sd.deinit(); |
liam_grazier | 7:dd303488e55d | 160 | mylcd.clear(); |
liam_grazier | 7:dd303488e55d | 161 | mylcd.setline(1,0); |
liam_grazier | 7:dd303488e55d | 162 | greenLED = 1; |
liam_grazier | 7:dd303488e55d | 163 | mylcd.write("R"); |
liam_grazier | 7:dd303488e55d | 164 | printf("SD REMOVED"); |
liam_grazier | 7:dd303488e55d | 165 | errorCode(FATAL); |
liam_grazier | 7:dd303488e55d | 166 | Remove.unlock(); |
liam_grazier | 7:dd303488e55d | 167 | } |
liam_grazier | 5:9b4844128e09 | 168 | void errorCode(ELEC350_ERROR_CODE err) |
liam_grazier | 5:9b4844128e09 | 169 | { |
liam_grazier | 5:9b4844128e09 | 170 | switch (err) { |
liam_grazier | 5:9b4844128e09 | 171 | case OK: |
liam_grazier | 5:9b4844128e09 | 172 | greenLED = 1; |
liam_grazier | 5:9b4844128e09 | 173 | wait(1.0); |
liam_grazier | 5:9b4844128e09 | 174 | greenLED = 0; |
liam_grazier | 5:9b4844128e09 | 175 | return; |
liam_grazier | 5:9b4844128e09 | 176 | case FATAL: |
liam_grazier | 5:9b4844128e09 | 177 | while(1) { |
liam_grazier | 5:9b4844128e09 | 178 | redLED = 1; |
liam_grazier | 5:9b4844128e09 | 179 | wait(0.1); |
liam_grazier | 5:9b4844128e09 | 180 | redLED = 0; |
liam_grazier | 5:9b4844128e09 | 181 | wait(0.1); |
liam_grazier | 5:9b4844128e09 | 182 | } |
liam_grazier | 5:9b4844128e09 | 183 | }; |
liam_grazier | 5:9b4844128e09 | 184 | } |