Brian Pearson
/
LPC1768OpacityMeterUSB
Opacity meter with USB interface
main.cpp@0:8ce24e6c4de1, 2016-01-05 (annotated)
- Committer:
- BPPearson
- Date:
- Tue Jan 05 16:44:57 2016 +0000
- Revision:
- 0:8ce24e6c4de1
Opacity meter with USB interface
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
BPPearson | 0:8ce24e6c4de1 | 1 | #include "mbed.h" |
BPPearson | 0:8ce24e6c4de1 | 2 | #include "USBHID.h" |
BPPearson | 0:8ce24e6c4de1 | 3 | #include "ByteOperations.h" |
BPPearson | 0:8ce24e6c4de1 | 4 | #include "USBHIDProtocol.h" |
BPPearson | 0:8ce24e6c4de1 | 5 | |
BPPearson | 0:8ce24e6c4de1 | 6 | #define COMMAND 0 |
BPPearson | 0:8ce24e6c4de1 | 7 | #define DATA 1 |
BPPearson | 0:8ce24e6c4de1 | 8 | |
BPPearson | 0:8ce24e6c4de1 | 9 | #define AVERAGE 0 |
BPPearson | 0:8ce24e6c4de1 | 10 | #define MEDIAN 1 |
BPPearson | 0:8ce24e6c4de1 | 11 | #define BINAVERAGE 2 |
BPPearson | 0:8ce24e6c4de1 | 12 | #define RATEOFCHANGE 3 |
BPPearson | 0:8ce24e6c4de1 | 13 | |
BPPearson | 0:8ce24e6c4de1 | 14 | #define VERSION 0x01 |
BPPearson | 0:8ce24e6c4de1 | 15 | |
BPPearson | 0:8ce24e6c4de1 | 16 | Serial pc(USBTX, USBRX); |
BPPearson | 0:8ce24e6c4de1 | 17 | DigitalOut led1(LED1); |
BPPearson | 0:8ce24e6c4de1 | 18 | DigitalOut led2(LED2); |
BPPearson | 0:8ce24e6c4de1 | 19 | DigitalOut led3(LED3); |
BPPearson | 0:8ce24e6c4de1 | 20 | DigitalOut led4(LED4); |
BPPearson | 0:8ce24e6c4de1 | 21 | AnalogIn opacity(p20); |
BPPearson | 0:8ce24e6c4de1 | 22 | AnalogOut fOpacity(p18); |
BPPearson | 0:8ce24e6c4de1 | 23 | BusInOut databus(p8, p9, p10, p11, p12, p13, p14, p15); |
BPPearson | 0:8ce24e6c4de1 | 24 | DigitalOut registerSelect(p5); |
BPPearson | 0:8ce24e6c4de1 | 25 | DigitalOut readWriteClock(p7); |
BPPearson | 0:8ce24e6c4de1 | 26 | DigitalOut readWrite(p6); |
BPPearson | 0:8ce24e6c4de1 | 27 | //DigitalIn unlocked(p8); |
BPPearson | 0:8ce24e6c4de1 | 28 | Ticker updateLCD; |
BPPearson | 0:8ce24e6c4de1 | 29 | Ticker processOpacity; |
BPPearson | 0:8ce24e6c4de1 | 30 | LocalFileSystem local("local"); |
BPPearson | 0:8ce24e6c4de1 | 31 | |
BPPearson | 0:8ce24e6c4de1 | 32 | USBHID *hid; |
BPPearson | 0:8ce24e6c4de1 | 33 | HID_REPORT send_report __attribute__((aligned (4))); // Aligned for fast access |
BPPearson | 0:8ce24e6c4de1 | 34 | HID_REPORT recv_report __attribute__((aligned (4))); // Aligned for fast access |
BPPearson | 0:8ce24e6c4de1 | 35 | |
BPPearson | 0:8ce24e6c4de1 | 36 | float instantOpacity; |
BPPearson | 0:8ce24e6c4de1 | 37 | float filteredOpacity = 0.0; |
BPPearson | 0:8ce24e6c4de1 | 38 | float Opacity = 0.0; |
BPPearson | 0:8ce24e6c4de1 | 39 | char *helloStr = "Hello"; |
BPPearson | 0:8ce24e6c4de1 | 40 | int filterAlgorithm = AVERAGE; |
BPPearson | 0:8ce24e6c4de1 | 41 | float anIn1Sum; |
BPPearson | 0:8ce24e6c4de1 | 42 | float anIn1SumSqr; |
BPPearson | 0:8ce24e6c4de1 | 43 | float stdDevCount; |
BPPearson | 0:8ce24e6c4de1 | 44 | float standardDeviation; |
BPPearson | 0:8ce24e6c4de1 | 45 | float calibFactor = 1.0; |
BPPearson | 0:8ce24e6c4de1 | 46 | int showCalibFactor = 0; |
BPPearson | 0:8ce24e6c4de1 | 47 | float anInVals[100]; |
BPPearson | 0:8ce24e6c4de1 | 48 | int anInIdx = 0; |
BPPearson | 0:8ce24e6c4de1 | 49 | float binVal[10]; |
BPPearson | 0:8ce24e6c4de1 | 50 | int binCnt[10]; |
BPPearson | 0:8ce24e6c4de1 | 51 | int maxCnt = 0; |
BPPearson | 0:8ce24e6c4de1 | 52 | int maxIdx = 0; |
BPPearson | 0:8ce24e6c4de1 | 53 | |
BPPearson | 0:8ce24e6c4de1 | 54 | |
BPPearson | 0:8ce24e6c4de1 | 55 | void readConfigFile() |
BPPearson | 0:8ce24e6c4de1 | 56 | { |
BPPearson | 0:8ce24e6c4de1 | 57 | |
BPPearson | 0:8ce24e6c4de1 | 58 | FILE *fp = fopen("/local/config.dat", "r"); |
BPPearson | 0:8ce24e6c4de1 | 59 | |
BPPearson | 0:8ce24e6c4de1 | 60 | if (fp != NULL) |
BPPearson | 0:8ce24e6c4de1 | 61 | { |
BPPearson | 0:8ce24e6c4de1 | 62 | fscanf(fp, "%f", &calibFactor); |
BPPearson | 0:8ce24e6c4de1 | 63 | fscanf(fp, "%d", &filterAlgorithm); |
BPPearson | 0:8ce24e6c4de1 | 64 | fclose(fp); |
BPPearson | 0:8ce24e6c4de1 | 65 | } |
BPPearson | 0:8ce24e6c4de1 | 66 | } |
BPPearson | 0:8ce24e6c4de1 | 67 | |
BPPearson | 0:8ce24e6c4de1 | 68 | |
BPPearson | 0:8ce24e6c4de1 | 69 | |
BPPearson | 0:8ce24e6c4de1 | 70 | void writeConfigFile() |
BPPearson | 0:8ce24e6c4de1 | 71 | { |
BPPearson | 0:8ce24e6c4de1 | 72 | FILE *fp = fopen("/local/config.dat", "w"); |
BPPearson | 0:8ce24e6c4de1 | 73 | |
BPPearson | 0:8ce24e6c4de1 | 74 | if (fp != NULL) |
BPPearson | 0:8ce24e6c4de1 | 75 | { |
BPPearson | 0:8ce24e6c4de1 | 76 | fprintf(fp, "%5.3f\n", calibFactor); |
BPPearson | 0:8ce24e6c4de1 | 77 | fprintf(fp, "%1d\n", filterAlgorithm); |
BPPearson | 0:8ce24e6c4de1 | 78 | fclose(fp); |
BPPearson | 0:8ce24e6c4de1 | 79 | } |
BPPearson | 0:8ce24e6c4de1 | 80 | } |
BPPearson | 0:8ce24e6c4de1 | 81 | |
BPPearson | 0:8ce24e6c4de1 | 82 | |
BPPearson | 0:8ce24e6c4de1 | 83 | |
BPPearson | 0:8ce24e6c4de1 | 84 | void empty_report(HID_REPORT *data){ |
BPPearson | 0:8ce24e6c4de1 | 85 | register uint32_t *p = (uint32_t *)data->data; |
BPPearson | 0:8ce24e6c4de1 | 86 | for( register int i=0; i<((sizeof(HID_REPORT)-1)/4); i++ ){ |
BPPearson | 0:8ce24e6c4de1 | 87 | *p = 0xFFFFFFFF; |
BPPearson | 0:8ce24e6c4de1 | 88 | p++; |
BPPearson | 0:8ce24e6c4de1 | 89 | } |
BPPearson | 0:8ce24e6c4de1 | 90 | } |
BPPearson | 0:8ce24e6c4de1 | 91 | |
BPPearson | 0:8ce24e6c4de1 | 92 | void checkForUSBRequest() |
BPPearson | 0:8ce24e6c4de1 | 93 | { |
BPPearson | 0:8ce24e6c4de1 | 94 | char *cptrR; |
BPPearson | 0:8ce24e6c4de1 | 95 | char *cptrT; |
BPPearson | 0:8ce24e6c4de1 | 96 | float fTmp; |
BPPearson | 0:8ce24e6c4de1 | 97 | //bool updatePIDValues = false; |
BPPearson | 0:8ce24e6c4de1 | 98 | |
BPPearson | 0:8ce24e6c4de1 | 99 | //try to read a msg |
BPPearson | 0:8ce24e6c4de1 | 100 | if(hid->readNB(&recv_report)){ |
BPPearson | 0:8ce24e6c4de1 | 101 | |
BPPearson | 0:8ce24e6c4de1 | 102 | // set character pointer to start of the parameter for set commands |
BPPearson | 0:8ce24e6c4de1 | 103 | cptrR = (char *)&recv_report.data[1]; |
BPPearson | 0:8ce24e6c4de1 | 104 | cptrT = (char *)&send_report.data[1]; |
BPPearson | 0:8ce24e6c4de1 | 105 | |
BPPearson | 0:8ce24e6c4de1 | 106 | led2 = 1; |
BPPearson | 0:8ce24e6c4de1 | 107 | |
BPPearson | 0:8ce24e6c4de1 | 108 | // Data packet received, start parsing |
BPPearson | 0:8ce24e6c4de1 | 109 | int irx=0; |
BPPearson | 0:8ce24e6c4de1 | 110 | int itx=0; |
BPPearson | 0:8ce24e6c4de1 | 111 | |
BPPearson | 0:8ce24e6c4de1 | 112 | send_report.data[itx++] = recv_report.data[0]; |
BPPearson | 0:8ce24e6c4de1 | 113 | |
BPPearson | 0:8ce24e6c4de1 | 114 | switch ( recv_report.data[irx++] ){ |
BPPearson | 0:8ce24e6c4de1 | 115 | case CMD_SYS_CHECK : |
BPPearson | 0:8ce24e6c4de1 | 116 | send_report.data[itx++] = VERSION; |
BPPearson | 0:8ce24e6c4de1 | 117 | break; |
BPPearson | 0:8ce24e6c4de1 | 118 | |
BPPearson | 0:8ce24e6c4de1 | 119 | case CMD_SYS_RESET : |
BPPearson | 0:8ce24e6c4de1 | 120 | // Soft reset |
BPPearson | 0:8ce24e6c4de1 | 121 | empty_report(&recv_report); |
BPPearson | 0:8ce24e6c4de1 | 122 | break; |
BPPearson | 0:8ce24e6c4de1 | 123 | |
BPPearson | 0:8ce24e6c4de1 | 124 | case CMD_GET_RAW_OPACITY: |
BPPearson | 0:8ce24e6c4de1 | 125 | // return the raw opacity value |
BPPearson | 0:8ce24e6c4de1 | 126 | sprintf(cptrT, "%6.5f", instantOpacity * 100.0); |
BPPearson | 0:8ce24e6c4de1 | 127 | pc.printf("instant opacity = %f\n", instantOpacity * 100.0); |
BPPearson | 0:8ce24e6c4de1 | 128 | break; |
BPPearson | 0:8ce24e6c4de1 | 129 | case CMD_GET_FILTERED_OPACITY: |
BPPearson | 0:8ce24e6c4de1 | 130 | // return filtered opacity |
BPPearson | 0:8ce24e6c4de1 | 131 | sprintf(cptrT, "%6.5f", Opacity); |
BPPearson | 0:8ce24e6c4de1 | 132 | pc.printf("filtered opacity = %f\n", Opacity); |
BPPearson | 0:8ce24e6c4de1 | 133 | break; |
BPPearson | 0:8ce24e6c4de1 | 134 | case CMD_GET_CALIB_FACTOR: |
BPPearson | 0:8ce24e6c4de1 | 135 | // return calibration factor |
BPPearson | 0:8ce24e6c4de1 | 136 | sprintf(cptrT, "%6.5f", calibFactor); |
BPPearson | 0:8ce24e6c4de1 | 137 | pc.printf("calibration factor = %f\n", calibFactor); |
BPPearson | 0:8ce24e6c4de1 | 138 | break; |
BPPearson | 0:8ce24e6c4de1 | 139 | case CMD_SET_CALIB_FACTOR: |
BPPearson | 0:8ce24e6c4de1 | 140 | // set calibration factor to value in packet |
BPPearson | 0:8ce24e6c4de1 | 141 | sscanf(cptrR, "%f", &calibFactor); |
BPPearson | 0:8ce24e6c4de1 | 142 | writeConfigFile(); |
BPPearson | 0:8ce24e6c4de1 | 143 | pc.printf("Set calibFactor to %f\n", calibFactor); |
BPPearson | 0:8ce24e6c4de1 | 144 | break; |
BPPearson | 0:8ce24e6c4de1 | 145 | case CMD_GET_FILTER_MODE: |
BPPearson | 0:8ce24e6c4de1 | 146 | // return filter algorithm value |
BPPearson | 0:8ce24e6c4de1 | 147 | sprintf(cptrT, "%6.3f", (float)filterAlgorithm); |
BPPearson | 0:8ce24e6c4de1 | 148 | pc.printf("filterAlgorithm = %d\n", filterAlgorithm); |
BPPearson | 0:8ce24e6c4de1 | 149 | break; |
BPPearson | 0:8ce24e6c4de1 | 150 | case CMD_SET_FILTER_MODE: |
BPPearson | 0:8ce24e6c4de1 | 151 | // set filter alogirthm |
BPPearson | 0:8ce24e6c4de1 | 152 | sscanf(cptrR, "%f", &fTmp); |
BPPearson | 0:8ce24e6c4de1 | 153 | filterAlgorithm = (int)fTmp; |
BPPearson | 0:8ce24e6c4de1 | 154 | writeConfigFile(); |
BPPearson | 0:8ce24e6c4de1 | 155 | pc.printf("Set filter algorithm to %d\n", filterAlgorithm); |
BPPearson | 0:8ce24e6c4de1 | 156 | break; |
BPPearson | 0:8ce24e6c4de1 | 157 | case 0xEE : { |
BPPearson | 0:8ce24e6c4de1 | 158 | hid->sendNB(&send_report); |
BPPearson | 0:8ce24e6c4de1 | 159 | //WatchDog_us bello(100); |
BPPearson | 0:8ce24e6c4de1 | 160 | } |
BPPearson | 0:8ce24e6c4de1 | 161 | break; |
BPPearson | 0:8ce24e6c4de1 | 162 | |
BPPearson | 0:8ce24e6c4de1 | 163 | default: |
BPPearson | 0:8ce24e6c4de1 | 164 | send_report.data[0] = 0xFF; //Failure |
BPPearson | 0:8ce24e6c4de1 | 165 | break; |
BPPearson | 0:8ce24e6c4de1 | 166 | } // Switch |
BPPearson | 0:8ce24e6c4de1 | 167 | |
BPPearson | 0:8ce24e6c4de1 | 168 | // Return command + optional new args |
BPPearson | 0:8ce24e6c4de1 | 169 | hid->send(&send_report); |
BPPearson | 0:8ce24e6c4de1 | 170 | |
BPPearson | 0:8ce24e6c4de1 | 171 | // 0xFF unused bytes |
BPPearson | 0:8ce24e6c4de1 | 172 | empty_report(&recv_report); |
BPPearson | 0:8ce24e6c4de1 | 173 | empty_report(&send_report); |
BPPearson | 0:8ce24e6c4de1 | 174 | |
BPPearson | 0:8ce24e6c4de1 | 175 | led2 = 0; |
BPPearson | 0:8ce24e6c4de1 | 176 | } // if packet |
BPPearson | 0:8ce24e6c4de1 | 177 | } |
BPPearson | 0:8ce24e6c4de1 | 178 | |
BPPearson | 0:8ce24e6c4de1 | 179 | |
BPPearson | 0:8ce24e6c4de1 | 180 | |
BPPearson | 0:8ce24e6c4de1 | 181 | void writeToLCD(bool rs, char data){ |
BPPearson | 0:8ce24e6c4de1 | 182 | |
BPPearson | 0:8ce24e6c4de1 | 183 | // set register select pin |
BPPearson | 0:8ce24e6c4de1 | 184 | registerSelect = rs; |
BPPearson | 0:8ce24e6c4de1 | 185 | |
BPPearson | 0:8ce24e6c4de1 | 186 | // set read/write pin to write |
BPPearson | 0:8ce24e6c4de1 | 187 | readWrite = 0; |
BPPearson | 0:8ce24e6c4de1 | 188 | |
BPPearson | 0:8ce24e6c4de1 | 189 | // set bus as output |
BPPearson | 0:8ce24e6c4de1 | 190 | databus.output(); |
BPPearson | 0:8ce24e6c4de1 | 191 | |
BPPearson | 0:8ce24e6c4de1 | 192 | // put data onto bus |
BPPearson | 0:8ce24e6c4de1 | 193 | databus = data; |
BPPearson | 0:8ce24e6c4de1 | 194 | |
BPPearson | 0:8ce24e6c4de1 | 195 | // pulse read/write clock |
BPPearson | 0:8ce24e6c4de1 | 196 | readWriteClock = 1; |
BPPearson | 0:8ce24e6c4de1 | 197 | |
BPPearson | 0:8ce24e6c4de1 | 198 | wait_us(1); |
BPPearson | 0:8ce24e6c4de1 | 199 | |
BPPearson | 0:8ce24e6c4de1 | 200 | readWriteClock = 0; |
BPPearson | 0:8ce24e6c4de1 | 201 | |
BPPearson | 0:8ce24e6c4de1 | 202 | wait_us(1); |
BPPearson | 0:8ce24e6c4de1 | 203 | |
BPPearson | 0:8ce24e6c4de1 | 204 | // clear data bus |
BPPearson | 0:8ce24e6c4de1 | 205 | databus = 0; |
BPPearson | 0:8ce24e6c4de1 | 206 | |
BPPearson | 0:8ce24e6c4de1 | 207 | //pc.printf("%02x\n", data); |
BPPearson | 0:8ce24e6c4de1 | 208 | |
BPPearson | 0:8ce24e6c4de1 | 209 | } |
BPPearson | 0:8ce24e6c4de1 | 210 | |
BPPearson | 0:8ce24e6c4de1 | 211 | |
BPPearson | 0:8ce24e6c4de1 | 212 | char readFromLCD(bool rs){ |
BPPearson | 0:8ce24e6c4de1 | 213 | |
BPPearson | 0:8ce24e6c4de1 | 214 | char data; |
BPPearson | 0:8ce24e6c4de1 | 215 | |
BPPearson | 0:8ce24e6c4de1 | 216 | // set register select pin |
BPPearson | 0:8ce24e6c4de1 | 217 | registerSelect = rs; |
BPPearson | 0:8ce24e6c4de1 | 218 | |
BPPearson | 0:8ce24e6c4de1 | 219 | // set read/write pin to read |
BPPearson | 0:8ce24e6c4de1 | 220 | readWrite = 1; |
BPPearson | 0:8ce24e6c4de1 | 221 | |
BPPearson | 0:8ce24e6c4de1 | 222 | // set bus as output |
BPPearson | 0:8ce24e6c4de1 | 223 | databus.input(); |
BPPearson | 0:8ce24e6c4de1 | 224 | |
BPPearson | 0:8ce24e6c4de1 | 225 | // put data onto bus |
BPPearson | 0:8ce24e6c4de1 | 226 | data = databus; |
BPPearson | 0:8ce24e6c4de1 | 227 | |
BPPearson | 0:8ce24e6c4de1 | 228 | // pulse read/write clock |
BPPearson | 0:8ce24e6c4de1 | 229 | readWriteClock = 1; |
BPPearson | 0:8ce24e6c4de1 | 230 | |
BPPearson | 0:8ce24e6c4de1 | 231 | wait_us(10); |
BPPearson | 0:8ce24e6c4de1 | 232 | |
BPPearson | 0:8ce24e6c4de1 | 233 | readWriteClock = 0; |
BPPearson | 0:8ce24e6c4de1 | 234 | |
BPPearson | 0:8ce24e6c4de1 | 235 | return data; |
BPPearson | 0:8ce24e6c4de1 | 236 | } |
BPPearson | 0:8ce24e6c4de1 | 237 | |
BPPearson | 0:8ce24e6c4de1 | 238 | |
BPPearson | 0:8ce24e6c4de1 | 239 | void resetLCD(){ |
BPPearson | 0:8ce24e6c4de1 | 240 | } |
BPPearson | 0:8ce24e6c4de1 | 241 | |
BPPearson | 0:8ce24e6c4de1 | 242 | |
BPPearson | 0:8ce24e6c4de1 | 243 | void initLCD(){ |
BPPearson | 0:8ce24e6c4de1 | 244 | |
BPPearson | 0:8ce24e6c4de1 | 245 | // wait 15 ms to allow LCD to initialise |
BPPearson | 0:8ce24e6c4de1 | 246 | wait_ms(15); |
BPPearson | 0:8ce24e6c4de1 | 247 | |
BPPearson | 0:8ce24e6c4de1 | 248 | // set interface for 8 bit mode |
BPPearson | 0:8ce24e6c4de1 | 249 | writeToLCD(COMMAND, 0x30); |
BPPearson | 0:8ce24e6c4de1 | 250 | |
BPPearson | 0:8ce24e6c4de1 | 251 | // give it time |
BPPearson | 0:8ce24e6c4de1 | 252 | wait_ms(5); |
BPPearson | 0:8ce24e6c4de1 | 253 | |
BPPearson | 0:8ce24e6c4de1 | 254 | // set interface for 8 bit mode again |
BPPearson | 0:8ce24e6c4de1 | 255 | writeToLCD(COMMAND, 0x30); |
BPPearson | 0:8ce24e6c4de1 | 256 | |
BPPearson | 0:8ce24e6c4de1 | 257 | // give it time |
BPPearson | 0:8ce24e6c4de1 | 258 | wait_us(100); |
BPPearson | 0:8ce24e6c4de1 | 259 | |
BPPearson | 0:8ce24e6c4de1 | 260 | // set interface for 8 bit mode again, last one before we can configure the display |
BPPearson | 0:8ce24e6c4de1 | 261 | writeToLCD(COMMAND, 0x30); |
BPPearson | 0:8ce24e6c4de1 | 262 | |
BPPearson | 0:8ce24e6c4de1 | 263 | // give it time |
BPPearson | 0:8ce24e6c4de1 | 264 | wait_us(500); |
BPPearson | 0:8ce24e6c4de1 | 265 | |
BPPearson | 0:8ce24e6c4de1 | 266 | // set interface for 8 bit mode, 2 display lines and 5 x 8 character font |
BPPearson | 0:8ce24e6c4de1 | 267 | writeToLCD(COMMAND, 0x38); |
BPPearson | 0:8ce24e6c4de1 | 268 | |
BPPearson | 0:8ce24e6c4de1 | 269 | // give it time |
BPPearson | 0:8ce24e6c4de1 | 270 | wait_us(100); |
BPPearson | 0:8ce24e6c4de1 | 271 | |
BPPearson | 0:8ce24e6c4de1 | 272 | // display off |
BPPearson | 0:8ce24e6c4de1 | 273 | writeToLCD(COMMAND, 0x08); |
BPPearson | 0:8ce24e6c4de1 | 274 | |
BPPearson | 0:8ce24e6c4de1 | 275 | // give it time |
BPPearson | 0:8ce24e6c4de1 | 276 | wait_us(100); |
BPPearson | 0:8ce24e6c4de1 | 277 | |
BPPearson | 0:8ce24e6c4de1 | 278 | // clear the screen |
BPPearson | 0:8ce24e6c4de1 | 279 | writeToLCD(COMMAND, 0x01); |
BPPearson | 0:8ce24e6c4de1 | 280 | |
BPPearson | 0:8ce24e6c4de1 | 281 | // give it time to finish |
BPPearson | 0:8ce24e6c4de1 | 282 | wait_ms(2); |
BPPearson | 0:8ce24e6c4de1 | 283 | |
BPPearson | 0:8ce24e6c4de1 | 284 | // set entry mode to increment cursor position cursor on write |
BPPearson | 0:8ce24e6c4de1 | 285 | writeToLCD(COMMAND, 0x03); |
BPPearson | 0:8ce24e6c4de1 | 286 | |
BPPearson | 0:8ce24e6c4de1 | 287 | // give it time to finish |
BPPearson | 0:8ce24e6c4de1 | 288 | wait_us(100); |
BPPearson | 0:8ce24e6c4de1 | 289 | |
BPPearson | 0:8ce24e6c4de1 | 290 | // position cursor at home |
BPPearson | 0:8ce24e6c4de1 | 291 | writeToLCD(COMMAND, 0x02); |
BPPearson | 0:8ce24e6c4de1 | 292 | |
BPPearson | 0:8ce24e6c4de1 | 293 | // give it time to finish |
BPPearson | 0:8ce24e6c4de1 | 294 | wait_ms(2); |
BPPearson | 0:8ce24e6c4de1 | 295 | |
BPPearson | 0:8ce24e6c4de1 | 296 | // display on |
BPPearson | 0:8ce24e6c4de1 | 297 | writeToLCD(COMMAND, 0x0F); |
BPPearson | 0:8ce24e6c4de1 | 298 | } |
BPPearson | 0:8ce24e6c4de1 | 299 | |
BPPearson | 0:8ce24e6c4de1 | 300 | |
BPPearson | 0:8ce24e6c4de1 | 301 | |
BPPearson | 0:8ce24e6c4de1 | 302 | |
BPPearson | 0:8ce24e6c4de1 | 303 | void positionCursor(uint8_t x, uint8_t y){ |
BPPearson | 0:8ce24e6c4de1 | 304 | |
BPPearson | 0:8ce24e6c4de1 | 305 | if (x > 7) x = 0; |
BPPearson | 0:8ce24e6c4de1 | 306 | |
BPPearson | 0:8ce24e6c4de1 | 307 | if (y == 1) |
BPPearson | 0:8ce24e6c4de1 | 308 | writeToLCD(COMMAND, 0x80 + 0x40 + x); |
BPPearson | 0:8ce24e6c4de1 | 309 | else |
BPPearson | 0:8ce24e6c4de1 | 310 | writeToLCD(COMMAND, 0x80 + 0x00 + x); |
BPPearson | 0:8ce24e6c4de1 | 311 | |
BPPearson | 0:8ce24e6c4de1 | 312 | wait_us(50); |
BPPearson | 0:8ce24e6c4de1 | 313 | } |
BPPearson | 0:8ce24e6c4de1 | 314 | |
BPPearson | 0:8ce24e6c4de1 | 315 | |
BPPearson | 0:8ce24e6c4de1 | 316 | void displayString(int x, int y, char *str){ |
BPPearson | 0:8ce24e6c4de1 | 317 | |
BPPearson | 0:8ce24e6c4de1 | 318 | // position cursor |
BPPearson | 0:8ce24e6c4de1 | 319 | positionCursor(x, y); |
BPPearson | 0:8ce24e6c4de1 | 320 | |
BPPearson | 0:8ce24e6c4de1 | 321 | // write string to screen |
BPPearson | 0:8ce24e6c4de1 | 322 | for (int i=0; i<strlen(str); i++){ |
BPPearson | 0:8ce24e6c4de1 | 323 | writeToLCD(DATA, str[i]); |
BPPearson | 0:8ce24e6c4de1 | 324 | |
BPPearson | 0:8ce24e6c4de1 | 325 | wait_us(50); |
BPPearson | 0:8ce24e6c4de1 | 326 | } |
BPPearson | 0:8ce24e6c4de1 | 327 | } |
BPPearson | 0:8ce24e6c4de1 | 328 | |
BPPearson | 0:8ce24e6c4de1 | 329 | |
BPPearson | 0:8ce24e6c4de1 | 330 | void standardDeviationCalc(float opacity) |
BPPearson | 0:8ce24e6c4de1 | 331 | { |
BPPearson | 0:8ce24e6c4de1 | 332 | // add to standard deviation accumulators |
BPPearson | 0:8ce24e6c4de1 | 333 | anIn1Sum += opacity; |
BPPearson | 0:8ce24e6c4de1 | 334 | anIn1SumSqr += (opacity * opacity); |
BPPearson | 0:8ce24e6c4de1 | 335 | |
BPPearson | 0:8ce24e6c4de1 | 336 | // increment standard deviation counter |
BPPearson | 0:8ce24e6c4de1 | 337 | stdDevCount++; |
BPPearson | 0:8ce24e6c4de1 | 338 | |
BPPearson | 0:8ce24e6c4de1 | 339 | // if enough readings for the standard deviation calculation |
BPPearson | 0:8ce24e6c4de1 | 340 | if (stdDevCount >= 100) |
BPPearson | 0:8ce24e6c4de1 | 341 | { |
BPPearson | 0:8ce24e6c4de1 | 342 | // calculate the standard deviation |
BPPearson | 0:8ce24e6c4de1 | 343 | // std dev = sqrt( (n * sum(x2) - sum(x)2)) / (n * (n - 1))) |
BPPearson | 0:8ce24e6c4de1 | 344 | standardDeviation = ((stdDevCount * anIn1SumSqr) - (anIn1Sum * anIn1Sum)) / (stdDevCount * (stdDevCount - 1)); |
BPPearson | 0:8ce24e6c4de1 | 345 | if (standardDeviation > 0.0) |
BPPearson | 0:8ce24e6c4de1 | 346 | standardDeviation = sqrt(standardDeviation); |
BPPearson | 0:8ce24e6c4de1 | 347 | else |
BPPearson | 0:8ce24e6c4de1 | 348 | standardDeviation = sqrt(-standardDeviation); |
BPPearson | 0:8ce24e6c4de1 | 349 | |
BPPearson | 0:8ce24e6c4de1 | 350 | // clear standard deviation accumulators for next set of readings |
BPPearson | 0:8ce24e6c4de1 | 351 | anIn1Sum = 0.0; |
BPPearson | 0:8ce24e6c4de1 | 352 | anIn1SumSqr = 0.0; |
BPPearson | 0:8ce24e6c4de1 | 353 | stdDevCount = 0; |
BPPearson | 0:8ce24e6c4de1 | 354 | } |
BPPearson | 0:8ce24e6c4de1 | 355 | } |
BPPearson | 0:8ce24e6c4de1 | 356 | |
BPPearson | 0:8ce24e6c4de1 | 357 | |
BPPearson | 0:8ce24e6c4de1 | 358 | void updateDisplay(){ |
BPPearson | 0:8ce24e6c4de1 | 359 | char str[20]; |
BPPearson | 0:8ce24e6c4de1 | 360 | |
BPPearson | 0:8ce24e6c4de1 | 361 | sprintf( str, "o %5.1f", Opacity); |
BPPearson | 0:8ce24e6c4de1 | 362 | |
BPPearson | 0:8ce24e6c4de1 | 363 | displayString(0, 0, str); |
BPPearson | 0:8ce24e6c4de1 | 364 | |
BPPearson | 0:8ce24e6c4de1 | 365 | //if (showCalibFactor == 0){ |
BPPearson | 0:8ce24e6c4de1 | 366 | //sprintf( str, "s %5.2f", standardDeviation); |
BPPearson | 0:8ce24e6c4de1 | 367 | |
BPPearson | 0:8ce24e6c4de1 | 368 | //displayString(0, 1, str); |
BPPearson | 0:8ce24e6c4de1 | 369 | //} |
BPPearson | 0:8ce24e6c4de1 | 370 | //else{ |
BPPearson | 0:8ce24e6c4de1 | 371 | //sprintf( str, "m %5.2f", calibFactor); |
BPPearson | 0:8ce24e6c4de1 | 372 | |
BPPearson | 0:8ce24e6c4de1 | 373 | //displayString(0, 1, str); |
BPPearson | 0:8ce24e6c4de1 | 374 | |
BPPearson | 0:8ce24e6c4de1 | 375 | //showCalibFactor--; |
BPPearson | 0:8ce24e6c4de1 | 376 | //} |
BPPearson | 0:8ce24e6c4de1 | 377 | } |
BPPearson | 0:8ce24e6c4de1 | 378 | |
BPPearson | 0:8ce24e6c4de1 | 379 | |
BPPearson | 0:8ce24e6c4de1 | 380 | void updateOpacity() |
BPPearson | 0:8ce24e6c4de1 | 381 | { |
BPPearson | 0:8ce24e6c4de1 | 382 | |
BPPearson | 0:8ce24e6c4de1 | 383 | // read next analog input value into circular buffer, adjust reading for max value is 3.1 on 3.3V input |
BPPearson | 0:8ce24e6c4de1 | 384 | anInVals[anInIdx] = opacity.read() * 1.0674; |
BPPearson | 0:8ce24e6c4de1 | 385 | |
BPPearson | 0:8ce24e6c4de1 | 386 | // increment anInIdx and check for wrap |
BPPearson | 0:8ce24e6c4de1 | 387 | anInIdx++; |
BPPearson | 0:8ce24e6c4de1 | 388 | if (anInIdx >= 100) |
BPPearson | 0:8ce24e6c4de1 | 389 | anInIdx = 0; |
BPPearson | 0:8ce24e6c4de1 | 390 | |
BPPearson | 0:8ce24e6c4de1 | 391 | // filter analog inputs with required algorithm |
BPPearson | 0:8ce24e6c4de1 | 392 | switch (filterAlgorithm) |
BPPearson | 0:8ce24e6c4de1 | 393 | { |
BPPearson | 0:8ce24e6c4de1 | 394 | case AVERAGE: |
BPPearson | 0:8ce24e6c4de1 | 395 | float accumulator = 0.0; |
BPPearson | 0:8ce24e6c4de1 | 396 | for (int i=0; i<100; i++) |
BPPearson | 0:8ce24e6c4de1 | 397 | { |
BPPearson | 0:8ce24e6c4de1 | 398 | accumulator += anInVals[i]; |
BPPearson | 0:8ce24e6c4de1 | 399 | } |
BPPearson | 0:8ce24e6c4de1 | 400 | instantOpacity = accumulator / 100; |
BPPearson | 0:8ce24e6c4de1 | 401 | break; |
BPPearson | 0:8ce24e6c4de1 | 402 | case MEDIAN: |
BPPearson | 0:8ce24e6c4de1 | 403 | float tempF; |
BPPearson | 0:8ce24e6c4de1 | 404 | for (int j=1; j<100; j++) |
BPPearson | 0:8ce24e6c4de1 | 405 | { |
BPPearson | 0:8ce24e6c4de1 | 406 | for (int i=1; i<100; i++) |
BPPearson | 0:8ce24e6c4de1 | 407 | { |
BPPearson | 0:8ce24e6c4de1 | 408 | if (anInVals[i] < anInVals[i-1]) |
BPPearson | 0:8ce24e6c4de1 | 409 | { |
BPPearson | 0:8ce24e6c4de1 | 410 | // swap places |
BPPearson | 0:8ce24e6c4de1 | 411 | tempF = anInVals[i-1] ; |
BPPearson | 0:8ce24e6c4de1 | 412 | anInVals[i-1] = anInVals[i]; |
BPPearson | 0:8ce24e6c4de1 | 413 | anInVals[i] = tempF; |
BPPearson | 0:8ce24e6c4de1 | 414 | } |
BPPearson | 0:8ce24e6c4de1 | 415 | } |
BPPearson | 0:8ce24e6c4de1 | 416 | } |
BPPearson | 0:8ce24e6c4de1 | 417 | instantOpacity = anInVals[49]; |
BPPearson | 0:8ce24e6c4de1 | 418 | break; |
BPPearson | 0:8ce24e6c4de1 | 419 | case BINAVERAGE: |
BPPearson | 0:8ce24e6c4de1 | 420 | // initialise bins to zero |
BPPearson | 0:8ce24e6c4de1 | 421 | for (int i=0; i<10; i++) |
BPPearson | 0:8ce24e6c4de1 | 422 | { |
BPPearson | 0:8ce24e6c4de1 | 423 | binVal[i] = 0.0; |
BPPearson | 0:8ce24e6c4de1 | 424 | binCnt[i] = 0; |
BPPearson | 0:8ce24e6c4de1 | 425 | } |
BPPearson | 0:8ce24e6c4de1 | 426 | |
BPPearson | 0:8ce24e6c4de1 | 427 | // sort analog input values into one of ten bins |
BPPearson | 0:8ce24e6c4de1 | 428 | for (int i=0; i<100; i++) |
BPPearson | 0:8ce24e6c4de1 | 429 | { |
BPPearson | 0:8ce24e6c4de1 | 430 | int binIdx = anInVals[i] * 10.0; |
BPPearson | 0:8ce24e6c4de1 | 431 | if (binIdx > 9) |
BPPearson | 0:8ce24e6c4de1 | 432 | binIdx = 9; |
BPPearson | 0:8ce24e6c4de1 | 433 | binVal[binIdx] += anInVals[i]; |
BPPearson | 0:8ce24e6c4de1 | 434 | binCnt[binIdx]++; |
BPPearson | 0:8ce24e6c4de1 | 435 | } |
BPPearson | 0:8ce24e6c4de1 | 436 | |
BPPearson | 0:8ce24e6c4de1 | 437 | maxCnt = 0; |
BPPearson | 0:8ce24e6c4de1 | 438 | maxIdx = 0; |
BPPearson | 0:8ce24e6c4de1 | 439 | // find the bin with most values added |
BPPearson | 0:8ce24e6c4de1 | 440 | for (int i=0; i<10; i++) |
BPPearson | 0:8ce24e6c4de1 | 441 | { |
BPPearson | 0:8ce24e6c4de1 | 442 | if (binCnt[i] > maxCnt) |
BPPearson | 0:8ce24e6c4de1 | 443 | { |
BPPearson | 0:8ce24e6c4de1 | 444 | maxCnt = binCnt[i]; |
BPPearson | 0:8ce24e6c4de1 | 445 | maxIdx = i; |
BPPearson | 0:8ce24e6c4de1 | 446 | } |
BPPearson | 0:8ce24e6c4de1 | 447 | } |
BPPearson | 0:8ce24e6c4de1 | 448 | |
BPPearson | 0:8ce24e6c4de1 | 449 | instantOpacity = binVal[maxIdx] / binCnt[maxIdx]; |
BPPearson | 0:8ce24e6c4de1 | 450 | break; |
BPPearson | 0:8ce24e6c4de1 | 451 | case RATEOFCHANGE: |
BPPearson | 0:8ce24e6c4de1 | 452 | break; |
BPPearson | 0:8ce24e6c4de1 | 453 | default: |
BPPearson | 0:8ce24e6c4de1 | 454 | break; |
BPPearson | 0:8ce24e6c4de1 | 455 | } |
BPPearson | 0:8ce24e6c4de1 | 456 | |
BPPearson | 0:8ce24e6c4de1 | 457 | // do standard deviation on the smoothed opacity value |
BPPearson | 0:8ce24e6c4de1 | 458 | standardDeviationCalc(instantOpacity); |
BPPearson | 0:8ce24e6c4de1 | 459 | |
BPPearson | 0:8ce24e6c4de1 | 460 | // apply a filter to the instant reading to get the filtered reading |
BPPearson | 0:8ce24e6c4de1 | 461 | filteredOpacity = (instantOpacity * 0.05) + (filteredOpacity * 0.95); |
BPPearson | 0:8ce24e6c4de1 | 462 | |
BPPearson | 0:8ce24e6c4de1 | 463 | // calculate opacity reading as 0..100% |
BPPearson | 0:8ce24e6c4de1 | 464 | Opacity = filteredOpacity * calibFactor * 100.0; |
BPPearson | 0:8ce24e6c4de1 | 465 | |
BPPearson | 0:8ce24e6c4de1 | 466 | // write opacity value to analog output as 0..1.0 value |
BPPearson | 0:8ce24e6c4de1 | 467 | fOpacity.write(Opacity / 100.0); |
BPPearson | 0:8ce24e6c4de1 | 468 | } |
BPPearson | 0:8ce24e6c4de1 | 469 | |
BPPearson | 0:8ce24e6c4de1 | 470 | |
BPPearson | 0:8ce24e6c4de1 | 471 | int main() { |
BPPearson | 0:8ce24e6c4de1 | 472 | |
BPPearson | 0:8ce24e6c4de1 | 473 | printf("initLCD()\n"); |
BPPearson | 0:8ce24e6c4de1 | 474 | |
BPPearson | 0:8ce24e6c4de1 | 475 | initLCD(); |
BPPearson | 0:8ce24e6c4de1 | 476 | |
BPPearson | 0:8ce24e6c4de1 | 477 | filterAlgorithm = BINAVERAGE; |
BPPearson | 0:8ce24e6c4de1 | 478 | |
BPPearson | 0:8ce24e6c4de1 | 479 | //calibUp.mode(PullUp); |
BPPearson | 0:8ce24e6c4de1 | 480 | //calibDown.mode(PullUp); |
BPPearson | 0:8ce24e6c4de1 | 481 | //unlocked.mode(PullUp); |
BPPearson | 0:8ce24e6c4de1 | 482 | //calibUp.attach_deasserted(&incCalibFactor); |
BPPearson | 0:8ce24e6c4de1 | 483 | //calibUp.attach_deasserted_held(&incCalibFactor); |
BPPearson | 0:8ce24e6c4de1 | 484 | //calibDown.attach_deasserted(&decCalibFactor); |
BPPearson | 0:8ce24e6c4de1 | 485 | //calibDown.attach_deasserted_held(&decCalibFactor); |
BPPearson | 0:8ce24e6c4de1 | 486 | |
BPPearson | 0:8ce24e6c4de1 | 487 | //calibUp.setSampleFrequency(); |
BPPearson | 0:8ce24e6c4de1 | 488 | //calibDown.setSampleFrequency(); |
BPPearson | 0:8ce24e6c4de1 | 489 | |
BPPearson | 0:8ce24e6c4de1 | 490 | printf("readConfigFile()\n"); |
BPPearson | 0:8ce24e6c4de1 | 491 | |
BPPearson | 0:8ce24e6c4de1 | 492 | readConfigFile(); |
BPPearson | 0:8ce24e6c4de1 | 493 | |
BPPearson | 0:8ce24e6c4de1 | 494 | printf("start updateLCD ticker\n"); |
BPPearson | 0:8ce24e6c4de1 | 495 | |
BPPearson | 0:8ce24e6c4de1 | 496 | updateLCD.attach(&updateDisplay, 0.5); |
BPPearson | 0:8ce24e6c4de1 | 497 | |
BPPearson | 0:8ce24e6c4de1 | 498 | printf("read opacity 10 times\n"); |
BPPearson | 0:8ce24e6c4de1 | 499 | |
BPPearson | 0:8ce24e6c4de1 | 500 | // initialise analog input values |
BPPearson | 0:8ce24e6c4de1 | 501 | for (int i=0; i<100; i++) |
BPPearson | 0:8ce24e6c4de1 | 502 | anInVals[i] = opacity.read(); |
BPPearson | 0:8ce24e6c4de1 | 503 | |
BPPearson | 0:8ce24e6c4de1 | 504 | printf("start processOpacity ticker\n"); |
BPPearson | 0:8ce24e6c4de1 | 505 | |
BPPearson | 0:8ce24e6c4de1 | 506 | // start ticker to read and filter the opacity input |
BPPearson | 0:8ce24e6c4de1 | 507 | processOpacity.attach(&updateOpacity, 0.1); |
BPPearson | 0:8ce24e6c4de1 | 508 | |
BPPearson | 0:8ce24e6c4de1 | 509 | printf("initialise USB\n"); |
BPPearson | 0:8ce24e6c4de1 | 510 | |
BPPearson | 0:8ce24e6c4de1 | 511 | // USB Initialize |
BPPearson | 0:8ce24e6c4de1 | 512 | static USBHID hid_object(64, 64); |
BPPearson | 0:8ce24e6c4de1 | 513 | hid = &hid_object; |
BPPearson | 0:8ce24e6c4de1 | 514 | send_report.length = 64; |
BPPearson | 0:8ce24e6c4de1 | 515 | |
BPPearson | 0:8ce24e6c4de1 | 516 | while(1){ |
BPPearson | 0:8ce24e6c4de1 | 517 | |
BPPearson | 0:8ce24e6c4de1 | 518 | // check for any commands from host computer |
BPPearson | 0:8ce24e6c4de1 | 519 | checkForUSBRequest(); |
BPPearson | 0:8ce24e6c4de1 | 520 | |
BPPearson | 0:8ce24e6c4de1 | 521 | wait_ms(10); |
BPPearson | 0:8ce24e6c4de1 | 522 | } |
BPPearson | 0:8ce24e6c4de1 | 523 | |
BPPearson | 0:8ce24e6c4de1 | 524 | } |