![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
this is the library of the code of the second laboration.
Dependencies: SILABS_RHT mbed MemoryLCD
main.cpp@0:33ae04d01fd6, 2020-09-04 (annotated)
- Committer:
- mara1802
- Date:
- Fri Sep 04 11:39:49 2020 +0000
- Revision:
- 0:33ae04d01fd6
lab2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mara1802 | 0:33ae04d01fd6 | 1 | #include "LS013B7DH03.h" |
mara1802 | 0:33ae04d01fd6 | 2 | #include "SILABS_RHT.h" |
mara1802 | 0:33ae04d01fd6 | 3 | #include "mbed_logo_thermo.h" |
mara1802 | 0:33ae04d01fd6 | 4 | #include "Animation.h" |
mara1802 | 0:33ae04d01fd6 | 5 | |
mara1802 | 0:33ae04d01fd6 | 6 | /******************** Define I/O *****************************/ |
mara1802 | 0:33ae04d01fd6 | 7 | |
mara1802 | 0:33ae04d01fd6 | 8 | InterruptIn in(SW0); |
mara1802 | 0:33ae04d01fd6 | 9 | |
mara1802 | 0:33ae04d01fd6 | 10 | #define SCK PE12 |
mara1802 | 0:33ae04d01fd6 | 11 | #define MOSI PE10 |
mara1802 | 0:33ae04d01fd6 | 12 | |
mara1802 | 0:33ae04d01fd6 | 13 | DigitalOut CS(PA10); |
mara1802 | 0:33ae04d01fd6 | 14 | DigitalOut EXTCOM(PF3); |
mara1802 | 0:33ae04d01fd6 | 15 | DigitalOut DISP(PA8); |
mara1802 | 0:33ae04d01fd6 | 16 | |
mara1802 | 0:33ae04d01fd6 | 17 | SPI displaySPI(MOSI, NC, SCK); |
mara1802 | 0:33ae04d01fd6 | 18 | silabs::LS013B7DH03 display(&displaySPI, &CS, &EXTCOM); |
mara1802 | 0:33ae04d01fd6 | 19 | |
mara1802 | 0:33ae04d01fd6 | 20 | I2C sensorI2C(PD6, PD7); |
mara1802 | 0:33ae04d01fd6 | 21 | DigitalOut SENS_EN(PC8); |
mara1802 | 0:33ae04d01fd6 | 22 | silabs::SILABS_RHT rhtSensor(&sensorI2C); |
mara1802 | 0:33ae04d01fd6 | 23 | |
mara1802 | 0:33ae04d01fd6 | 24 | /******************** Define Timers *****************************/ |
mara1802 | 0:33ae04d01fd6 | 25 | |
mara1802 | 0:33ae04d01fd6 | 26 | LowPowerTicker timeKeeping; |
mara1802 | 0:33ae04d01fd6 | 27 | |
mara1802 | 0:33ae04d01fd6 | 28 | /***************** Define global variables **********************/ |
mara1802 | 0:33ae04d01fd6 | 29 | #define INIT_SECONDS 17600 |
mara1802 | 0:33ae04d01fd6 | 30 | #define TEST_DURATION 10 |
mara1802 | 0:33ae04d01fd6 | 31 | |
mara1802 | 0:33ae04d01fd6 | 32 | volatile uint32_t prevSeconds = INIT_SECONDS, seconds = INIT_SECONDS; |
mara1802 | 0:33ae04d01fd6 | 33 | volatile bool refreshed = false; |
mara1802 | 0:33ae04d01fd6 | 34 | volatile bool clockShown = false; |
mara1802 | 0:33ae04d01fd6 | 35 | volatile bool measured = false; |
mara1802 | 0:33ae04d01fd6 | 36 | |
mara1802 | 0:33ae04d01fd6 | 37 | typedef enum { |
mara1802 | 0:33ae04d01fd6 | 38 | CLOCK, |
mara1802 | 0:33ae04d01fd6 | 39 | DODECA |
mara1802 | 0:33ae04d01fd6 | 40 | } display_mode_t; |
mara1802 | 0:33ae04d01fd6 | 41 | |
mara1802 | 0:33ae04d01fd6 | 42 | volatile display_mode_t currentMode = CLOCK; |
mara1802 | 0:33ae04d01fd6 | 43 | uint8_t currentFrame = 0; |
mara1802 | 0:33ae04d01fd6 | 44 | |
mara1802 | 0:33ae04d01fd6 | 45 | /***************** Define callback handlers *********************/ |
mara1802 | 0:33ae04d01fd6 | 46 | void secondsCallback(void); |
mara1802 | 0:33ae04d01fd6 | 47 | void in_handler(); |
mara1802 | 0:33ae04d01fd6 | 48 | void toggleCallback(void); |
mara1802 | 0:33ae04d01fd6 | 49 | |
mara1802 | 0:33ae04d01fd6 | 50 | /***************** Define helper functions ***********************/ |
mara1802 | 0:33ae04d01fd6 | 51 | void drawTemperature(int32_t temperature); |
mara1802 | 0:33ae04d01fd6 | 52 | |
mara1802 | 0:33ae04d01fd6 | 53 | void drawTemperature(int32_t temperature) { |
mara1802 | 0:33ae04d01fd6 | 54 | uint32_t line = 20; |
mara1802 | 0:33ae04d01fd6 | 55 | int8_t int_temp = temperature / 500; |
mara1802 | 0:33ae04d01fd6 | 56 | |
mara1802 | 0:33ae04d01fd6 | 57 | for(int8_t iterator = 80; iterator > -6; iterator--) { |
mara1802 | 0:33ae04d01fd6 | 58 | if(int_temp >= iterator) display.fill(108,line,10,1,Black); |
mara1802 | 0:33ae04d01fd6 | 59 | else display.fill(108,line,10,1,White); |
mara1802 | 0:33ae04d01fd6 | 60 | |
mara1802 | 0:33ae04d01fd6 | 61 | line += 1; |
mara1802 | 0:33ae04d01fd6 | 62 | } |
mara1802 | 0:33ae04d01fd6 | 63 | } |
mara1802 | 0:33ae04d01fd6 | 64 | |
mara1802 | 0:33ae04d01fd6 | 65 | /** |
mara1802 | 0:33ae04d01fd6 | 66 | * Callback for pushbutton interrupt |
mara1802 | 0:33ae04d01fd6 | 67 | */ |
mara1802 | 0:33ae04d01fd6 | 68 | void in_handler() { |
mara1802 | 0:33ae04d01fd6 | 69 | if(currentMode == CLOCK) { |
mara1802 | 0:33ae04d01fd6 | 70 | currentMode = DODECA; |
mara1802 | 0:33ae04d01fd6 | 71 | } else { |
mara1802 | 0:33ae04d01fd6 | 72 | currentMode = CLOCK; |
mara1802 | 0:33ae04d01fd6 | 73 | clockShown = false; |
mara1802 | 0:33ae04d01fd6 | 74 | } |
mara1802 | 0:33ae04d01fd6 | 75 | } |
mara1802 | 0:33ae04d01fd6 | 76 | |
mara1802 | 0:33ae04d01fd6 | 77 | void secondsCallback(void) { |
mara1802 | 0:33ae04d01fd6 | 78 | seconds++; |
mara1802 | 0:33ae04d01fd6 | 79 | } |
mara1802 | 0:33ae04d01fd6 | 80 | |
mara1802 | 0:33ae04d01fd6 | 81 | /** |
mara1802 | 0:33ae04d01fd6 | 82 | * Callback for refresh completion |
mara1802 | 0:33ae04d01fd6 | 83 | */ |
mara1802 | 0:33ae04d01fd6 | 84 | void refreshCallback(void) { |
mara1802 | 0:33ae04d01fd6 | 85 | refreshed = true; |
mara1802 | 0:33ae04d01fd6 | 86 | } |
mara1802 | 0:33ae04d01fd6 | 87 | |
mara1802 | 0:33ae04d01fd6 | 88 | /** |
mara1802 | 0:33ae04d01fd6 | 89 | * Callback for measurement completion |
mara1802 | 0:33ae04d01fd6 | 90 | */ |
mara1802 | 0:33ae04d01fd6 | 91 | void measureCallback(void) { |
mara1802 | 0:33ae04d01fd6 | 92 | if(rhtSensor.get_active()) { |
mara1802 | 0:33ae04d01fd6 | 93 | measured = true; |
mara1802 | 0:33ae04d01fd6 | 94 | } |
mara1802 | 0:33ae04d01fd6 | 95 | } |
mara1802 | 0:33ae04d01fd6 | 96 | |
mara1802 | 0:33ae04d01fd6 | 97 | /*************************** MAIN *******************************/ |
mara1802 | 0:33ae04d01fd6 | 98 | int main() { |
mara1802 | 0:33ae04d01fd6 | 99 | // Initialize pushbutton handler |
mara1802 | 0:33ae04d01fd6 | 100 | in.rise(NULL); |
mara1802 | 0:33ae04d01fd6 | 101 | in.fall(in_handler); |
mara1802 | 0:33ae04d01fd6 | 102 | |
mara1802 | 0:33ae04d01fd6 | 103 | // Enable the LCD |
mara1802 | 0:33ae04d01fd6 | 104 | DISP = 1; |
mara1802 | 0:33ae04d01fd6 | 105 | |
mara1802 | 0:33ae04d01fd6 | 106 | // Enable the I2C RHT sensor |
mara1802 | 0:33ae04d01fd6 | 107 | SENS_EN = 1; |
mara1802 | 0:33ae04d01fd6 | 108 | |
mara1802 | 0:33ae04d01fd6 | 109 | // Start generating the 1Hz call for the timekeeping |
mara1802 | 0:33ae04d01fd6 | 110 | timeKeeping.attach(&secondsCallback, 1.0f); |
mara1802 | 0:33ae04d01fd6 | 111 | |
mara1802 | 0:33ae04d01fd6 | 112 | // Reset the LCD to a blank state. (All white) |
mara1802 | 0:33ae04d01fd6 | 113 | refreshed = false; |
mara1802 | 0:33ae04d01fd6 | 114 | display.clearImmediate(refreshCallback); |
mara1802 | 0:33ae04d01fd6 | 115 | while(refreshed == false) sleep(); |
mara1802 | 0:33ae04d01fd6 | 116 | |
mara1802 | 0:33ae04d01fd6 | 117 | printf("Initialization done! \n"); |
mara1802 | 0:33ae04d01fd6 | 118 | |
mara1802 | 0:33ae04d01fd6 | 119 | // Apply mbed logo bitmap to the pixel buffer |
mara1802 | 0:33ae04d01fd6 | 120 | display.showBMP((uint8_t*)mbed_logo_thermometer, 128, 128, 0, 0); |
mara1802 | 0:33ae04d01fd6 | 121 | display.background(Black); |
mara1802 | 0:33ae04d01fd6 | 122 | display.foreground(White); |
mara1802 | 0:33ae04d01fd6 | 123 | display.printf("I like MBED!"); |
mara1802 | 0:33ae04d01fd6 | 124 | |
mara1802 | 0:33ae04d01fd6 | 125 | // Push update to the display |
mara1802 | 0:33ae04d01fd6 | 126 | uint32_t refreshCount = display.getRefreshTicks(); |
mara1802 | 0:33ae04d01fd6 | 127 | refreshed = false; |
mara1802 | 0:33ae04d01fd6 | 128 | display.update(refreshCallback); |
mara1802 | 0:33ae04d01fd6 | 129 | |
mara1802 | 0:33ae04d01fd6 | 130 | // Sleep while doing the transmit |
mara1802 | 0:33ae04d01fd6 | 131 | while(refreshed == false) sleep(); |
mara1802 | 0:33ae04d01fd6 | 132 | |
mara1802 | 0:33ae04d01fd6 | 133 | // Calculate and print refresh duration |
mara1802 | 0:33ae04d01fd6 | 134 | refreshCount = display.getRefreshTicks() - refreshCount; |
mara1802 | 0:33ae04d01fd6 | 135 | printf("Refresh duration: %d cycles @ 125Hz \n", (int)refreshCount); |
mara1802 | 0:33ae04d01fd6 | 136 | |
mara1802 | 0:33ae04d01fd6 | 137 | // Perform a measurement |
mara1802 | 0:33ae04d01fd6 | 138 | rhtSensor.check_availability(si7021, measureCallback); |
mara1802 | 0:33ae04d01fd6 | 139 | while(measured == false); |
mara1802 | 0:33ae04d01fd6 | 140 | |
mara1802 | 0:33ae04d01fd6 | 141 | /* Main loop */ |
mara1802 | 0:33ae04d01fd6 | 142 | while(1) { |
mara1802 | 0:33ae04d01fd6 | 143 | sleep(); |
mara1802 | 0:33ae04d01fd6 | 144 | |
mara1802 | 0:33ae04d01fd6 | 145 | // In clock mode, only update once per second |
mara1802 | 0:33ae04d01fd6 | 146 | if((currentMode == CLOCK) && (prevSeconds != seconds)) { |
mara1802 | 0:33ae04d01fd6 | 147 | /* Redraw background when coming from video mode */ |
mara1802 | 0:33ae04d01fd6 | 148 | if(clockShown == false) { |
mara1802 | 0:33ae04d01fd6 | 149 | display.showBMP((uint8_t*)mbed_logo_thermometer, 128, 128, 0, 0); |
mara1802 | 0:33ae04d01fd6 | 150 | clockShown = true; |
mara1802 | 0:33ae04d01fd6 | 151 | } |
mara1802 | 0:33ae04d01fd6 | 152 | |
mara1802 | 0:33ae04d01fd6 | 153 | /* Show numeric measurements */ |
mara1802 | 0:33ae04d01fd6 | 154 | display.locate(0,13); |
mara1802 | 0:33ae04d01fd6 | 155 | display.printf("%02d.%01d degC", rhtSensor.get_temperature() / 1000, (rhtSensor.get_temperature() % 1000) / 100); |
mara1802 | 0:33ae04d01fd6 | 156 | display.locate(0,14); |
mara1802 | 0:33ae04d01fd6 | 157 | display.printf("%03d.%03d%%H", rhtSensor.get_humidity() / 1000, rhtSensor.get_humidity() % 1000); |
mara1802 | 0:33ae04d01fd6 | 158 | |
mara1802 | 0:33ae04d01fd6 | 159 | /* Show clock */ |
mara1802 | 0:33ae04d01fd6 | 160 | display.locate(4,15); |
mara1802 | 0:33ae04d01fd6 | 161 | display.printf("%02d:%02d:%02d", (seconds / 1200) % 24, (seconds / 60) % 60, seconds % 60); |
mara1802 | 0:33ae04d01fd6 | 162 | |
mara1802 | 0:33ae04d01fd6 | 163 | /* Update the graphical thermometer */ |
mara1802 | 0:33ae04d01fd6 | 164 | drawTemperature(rhtSensor.get_temperature()); |
mara1802 | 0:33ae04d01fd6 | 165 | |
mara1802 | 0:33ae04d01fd6 | 166 | if(refreshed == true) { |
mara1802 | 0:33ae04d01fd6 | 167 | prevSeconds = seconds; |
mara1802 | 0:33ae04d01fd6 | 168 | refreshed = false; |
mara1802 | 0:33ae04d01fd6 | 169 | |
mara1802 | 0:33ae04d01fd6 | 170 | /* Perform both I/O tasks simultaneously */ |
mara1802 | 0:33ae04d01fd6 | 171 | display.update(refreshCallback); |
mara1802 | 0:33ae04d01fd6 | 172 | rhtSensor.measure(si7021); |
mara1802 | 0:33ae04d01fd6 | 173 | } |
mara1802 | 0:33ae04d01fd6 | 174 | } |
mara1802 | 0:33ae04d01fd6 | 175 | // In dodecaeder mode, show frames as fast as possible |
mara1802 | 0:33ae04d01fd6 | 176 | else if(currentMode == DODECA) { |
mara1802 | 0:33ae04d01fd6 | 177 | if(refreshed == true) { |
mara1802 | 0:33ae04d01fd6 | 178 | display.showBMP((uint8_t*)(frames[currentFrame]->pData), 128, 128, 0, 0); |
mara1802 | 0:33ae04d01fd6 | 179 | currentFrame++; |
mara1802 | 0:33ae04d01fd6 | 180 | if( currentFrame >= NUM_FRAMES ) currentFrame = 0; |
mara1802 | 0:33ae04d01fd6 | 181 | refreshed = false; |
mara1802 | 0:33ae04d01fd6 | 182 | display.update(refreshCallback); |
mara1802 | 0:33ae04d01fd6 | 183 | } |
mara1802 | 0:33ae04d01fd6 | 184 | } |
mara1802 | 0:33ae04d01fd6 | 185 | } |
mara1802 | 0:33ae04d01fd6 | 186 | |
mara1802 | 0:33ae04d01fd6 | 187 | //notify_completion(true); |
mara1802 | 0:33ae04d01fd6 | 188 | } |
mara1802 | 0:33ae04d01fd6 | 189 |