![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
TEMPLATE light sensor 3 fork Light threshold
main.cpp@8:ab7982d1ce50, 2021-11-10 (annotated)
- Committer:
- 10740761
- Date:
- Wed Nov 10 13:06:53 2021 +0000
- Revision:
- 8:ab7982d1ce50
- Parent:
- 7:5e220d3e8a94
light sensor v3
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
reedas | 2:ad262f2c84ca | 1 | /* Hello World! for the Emwin TFT Library */ |
reedas | 1:402b32a1025f | 2 | |
romilly | 0:6beb6f498640 | 3 | #include "mbed.h" |
reedas | 1:402b32a1025f | 4 | #include "GUI.h" |
reedas | 1:402b32a1025f | 5 | #include "cy8ckit_028_tft.h" |
reedas | 2:ad262f2c84ca | 6 | |
10740761 | 5:25c7ff9c9085 | 7 | #define LED_DIAMETER (8) |
reedas | 4:d129442ea4dd | 8 | #define LED_RADIUS (LED_DIAMETER/2) |
reedas | 4:d129442ea4dd | 9 | #define DISPLAY_X (320) |
reedas | 4:d129442ea4dd | 10 | #define DISPLAY_Y (240) |
reedas | 4:d129442ea4dd | 11 | #define NUMLEDS_X (DISPLAY_X/LED_DIAMETER) |
reedas | 4:d129442ea4dd | 12 | #define NUMLEDS_Y (DISPLAY_Y/LED_DIAMETER) |
reedas | 4:d129442ea4dd | 13 | #define LED_OFF (0) |
reedas | 4:d129442ea4dd | 14 | #define LED_ON (1) |
10740761 | 6:e85892436f70 | 15 | #define TITLE_OFFSET (16+LED_RADIUS) |
10740761 | 6:e85892436f70 | 16 | |
10740761 | 6:e85892436f70 | 17 | AnalogIn lightlevel(P10_0); |
10740761 | 8:ab7982d1ce50 | 18 | DigitalOut lights(LED4); |
10740761 | 8:ab7982d1ce50 | 19 | DigitalOut lightsR(LED1); |
10740761 | 8:ab7982d1ce50 | 20 | DigitalOut lightsG(LED3); |
10740761 | 8:ab7982d1ce50 | 21 | DigitalOut lightsB(LED2); |
10740761 | 8:ab7982d1ce50 | 22 | |
10740761 | 8:ab7982d1ce50 | 23 | |
10740761 | 8:ab7982d1ce50 | 24 | bool lightStatus; |
reedas | 1:402b32a1025f | 25 | |
reedas | 1:402b32a1025f | 26 | void Display_Init(void) |
reedas | 1:402b32a1025f | 27 | { |
reedas | 4:d129442ea4dd | 28 | /* Initialise EmWin driver*/ |
reedas | 4:d129442ea4dd | 29 | GUI_Init(); |
reedas | 1:402b32a1025f | 30 | |
reedas | 1:402b32a1025f | 31 | /* Set font size, foreground and background Colours */ |
10740761 | 6:e85892436f70 | 32 | GUI_SetFont(GUI_FONT_24B_1); |
reedas | 1:402b32a1025f | 33 | GUI_SetColor(GUI_WHITE); |
reedas | 1:402b32a1025f | 34 | GUI_SetBkColor(GUI_BLACK); |
romilly | 0:6beb6f498640 | 35 | |
reedas | 1:402b32a1025f | 36 | /* Clear screen and print splash screen */ |
reedas | 1:402b32a1025f | 37 | GUI_Clear(); |
reedas | 1:402b32a1025f | 38 | GUI_SetTextAlign(GUI_TA_HCENTER); |
10740761 | 6:e85892436f70 | 39 | GUI_DispStringAt("LIGHT SENSOR", 160, 0); |
reedas | 1:402b32a1025f | 40 | } |
reedas | 1:402b32a1025f | 41 | |
reedas | 1:402b32a1025f | 42 | int main() |
reedas | 1:402b32a1025f | 43 | { |
10740761 | 8:ab7982d1ce50 | 44 | int lowThresh, highThresh; |
10740761 | 8:ab7982d1ce50 | 45 | lowThresh = 40; |
10740761 | 8:ab7982d1ce50 | 46 | highThresh = 70; |
10740761 | 8:ab7982d1ce50 | 47 | lights, 0; |
10740761 | 8:ab7982d1ce50 | 48 | |
10740761 | 8:ab7982d1ce50 | 49 | |
reedas | 1:402b32a1025f | 50 | /* Initialise display */ |
reedas | 1:402b32a1025f | 51 | Display_Init(); |
reedas | 2:ad262f2c84ca | 52 | |
10740761 | 8:ab7982d1ce50 | 53 | printf("LIGHT LEVEL CONTROL\r\n"); |
10740761 | 8:ab7982d1ce50 | 54 | lightStatus = false; |
reedas | 1:402b32a1025f | 55 | |
10740761 | 7:5e220d3e8a94 | 56 | GUI_SetFont(GUI_FONT_D80); |
10740761 | 7:5e220d3e8a94 | 57 | GUI_SetColor(GUI_BLUE); |
10740761 | 6:e85892436f70 | 58 | while(true){ |
10740761 | 6:e85892436f70 | 59 | int lightpercent; |
10740761 | 6:e85892436f70 | 60 | lightpercent = (lightlevel.read_u16()*100)/65535; |
10740761 | 6:e85892436f70 | 61 | printf("light level is %d\r\n", lightpercent); |
10740761 | 8:ab7982d1ce50 | 62 | if(lightpercent > highThresh) lightStatus = 1; |
10740761 | 8:ab7982d1ce50 | 63 | else if(lightpercent < lowThresh) lightStatus = 0; |
10740761 | 8:ab7982d1ce50 | 64 | lights = lightStatus; |
10740761 | 8:ab7982d1ce50 | 65 | if(lightpercent > highThresh) { |
10740761 | 8:ab7982d1ce50 | 66 | lightsB = 0; |
10740761 | 8:ab7982d1ce50 | 67 | lightsR = 1; |
10740761 | 8:ab7982d1ce50 | 68 | lightsG = 1; |
10740761 | 8:ab7982d1ce50 | 69 | } |
10740761 | 8:ab7982d1ce50 | 70 | if(lightpercent < highThresh && lightpercent > lowThresh) { |
10740761 | 8:ab7982d1ce50 | 71 | lightsB = 1; |
10740761 | 8:ab7982d1ce50 | 72 | lightsR = 1; |
10740761 | 8:ab7982d1ce50 | 73 | lightsG = 0; |
10740761 | 8:ab7982d1ce50 | 74 | } |
10740761 | 8:ab7982d1ce50 | 75 | if(lightpercent < lowThresh) { |
10740761 | 8:ab7982d1ce50 | 76 | lightsB = 1; |
10740761 | 8:ab7982d1ce50 | 77 | lightsR = 0; |
10740761 | 8:ab7982d1ce50 | 78 | lightsG = 1; |
10740761 | 8:ab7982d1ce50 | 79 | } |
10740761 | 8:ab7982d1ce50 | 80 | |
10740761 | 6:e85892436f70 | 81 | ThisThread::sleep_for(1000); |
10740761 | 7:5e220d3e8a94 | 82 | char buffer[16]; |
10740761 | 7:5e220d3e8a94 | 83 | sprintf(buffer, "%d", lightpercent); |
10740761 | 7:5e220d3e8a94 | 84 | GUI_DispStringAt(buffer, 30, 30); |
10740761 | 7:5e220d3e8a94 | 85 | GUI_SetColor(GUI_DARKGREEN); |
10740761 | 7:5e220d3e8a94 | 86 | GUI_FillRect(260, 30, 300, 220); |
10740761 | 7:5e220d3e8a94 | 87 | GUI_SetColor(GUI_GREEN); |
10740761 | 7:5e220d3e8a94 | 88 | GUI_FillRect(260, 220 - lightpercent * 2, 300, 220); |
10740761 | 6:e85892436f70 | 89 | |
reedas | 4:d129442ea4dd | 90 | } |
10740761 | 6:e85892436f70 | 91 | |
romilly | 0:6beb6f498640 | 92 | } |
reedas | 1:402b32a1025f | 93 | |
reedas | 1:402b32a1025f | 94 | |
reedas | 1:402b32a1025f | 95 |