![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
RGB Lights
main.cpp@8:3949ee5c0083, 2021-11-10 (annotated)
- Committer:
- 10724928
- Date:
- Wed Nov 10 13:07:28 2021 +0000
- Revision:
- 8:3949ee5c0083
- Parent:
- 7:bd8ad736bd09
RGB Lights at different light levels
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 | |
reedas | 5:9a03cb84a60c | 7 | #define LED_DIAMETER (32) |
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) |
reedas | 4:d129442ea4dd | 15 | #define TITLE_OFFSET (16+LED_RADIUS) |
reedas | 1:402b32a1025f | 16 | |
reedas | 6:528d74136b6a | 17 | AnalogIn lightLevel(P10_0); |
10724928 | 8:3949ee5c0083 | 18 | DigitalOut lights(LED4); |
10724928 | 8:3949ee5c0083 | 19 | DigitalOut lightsR(LED1); |
10724928 | 8:3949ee5c0083 | 20 | DigitalOut lightsG(LED2); |
10724928 | 8:3949ee5c0083 | 21 | DigitalOut lightsB(LED3); |
10724928 | 8:3949ee5c0083 | 22 | bool lightStatus; |
reedas | 6:528d74136b6a | 23 | |
reedas | 1:402b32a1025f | 24 | void Display_Init(void) |
reedas | 1:402b32a1025f | 25 | { |
reedas | 4:d129442ea4dd | 26 | /* Initialise EmWin driver*/ |
reedas | 4:d129442ea4dd | 27 | GUI_Init(); |
reedas | 1:402b32a1025f | 28 | |
reedas | 1:402b32a1025f | 29 | /* Set font size, foreground and background Colours */ |
reedas | 6:528d74136b6a | 30 | GUI_SetFont(GUI_FONT_24B_1); |
reedas | 1:402b32a1025f | 31 | GUI_SetColor(GUI_WHITE); |
reedas | 1:402b32a1025f | 32 | GUI_SetBkColor(GUI_BLACK); |
romilly | 0:6beb6f498640 | 33 | |
reedas | 1:402b32a1025f | 34 | /* Clear screen and print splash screen */ |
reedas | 1:402b32a1025f | 35 | GUI_Clear(); |
reedas | 1:402b32a1025f | 36 | GUI_SetTextAlign(GUI_TA_HCENTER); |
reedas | 6:528d74136b6a | 37 | GUI_DispStringAt("CITY3032 Light Level", 160, 0); |
reedas | 1:402b32a1025f | 38 | } |
reedas | 1:402b32a1025f | 39 | |
reedas | 1:402b32a1025f | 40 | int main() |
reedas | 1:402b32a1025f | 41 | { |
10724928 | 8:3949ee5c0083 | 42 | int lowThresh, highThresh; |
10724928 | 8:3949ee5c0083 | 43 | lowThresh = 40; |
10724928 | 8:3949ee5c0083 | 44 | highThresh = 70; |
10724928 | 8:3949ee5c0083 | 45 | lights = 0; |
reedas | 1:402b32a1025f | 46 | /* Initialise display */ |
reedas | 1:402b32a1025f | 47 | Display_Init(); |
reedas | 2:ad262f2c84ca | 48 | |
10724928 | 8:3949ee5c0083 | 49 | printf("Light Level Control\r\n"); |
10724928 | 8:3949ee5c0083 | 50 | lightStatus = false; |
10724928 | 7:bd8ad736bd09 | 51 | GUI_SetFont(GUI_FONT_D60X80); |
reedas | 6:528d74136b6a | 52 | GUI_SetColor(GUI_WHITE); |
reedas | 6:528d74136b6a | 53 | while(true) { |
10724928 | 7:bd8ad736bd09 | 54 | GUI_SetColor(GUI_WHITE); |
reedas | 6:528d74136b6a | 55 | int lightPercent; |
reedas | 6:528d74136b6a | 56 | lightPercent = (lightLevel.read_u16()*100)/65535; |
reedas | 6:528d74136b6a | 57 | printf("light level is %d\r\n",lightPercent); |
10724928 | 8:3949ee5c0083 | 58 | if(lightPercent > highThresh) lightStatus = 1; |
10724928 | 8:3949ee5c0083 | 59 | else if (lightPercent < lowThresh) lightStatus = 0; |
10724928 | 8:3949ee5c0083 | 60 | lights = lightStatus; |
10724928 | 8:3949ee5c0083 | 61 | if(lightPercent > highThresh) { |
10724928 | 8:3949ee5c0083 | 62 | lightsB = 0; |
10724928 | 8:3949ee5c0083 | 63 | lightsR = 1; |
10724928 | 8:3949ee5c0083 | 64 | lightsG = 1; |
10724928 | 8:3949ee5c0083 | 65 | } |
10724928 | 8:3949ee5c0083 | 66 | if(lightPercent < highThresh && lightPercent > lowThresh) { |
10724928 | 8:3949ee5c0083 | 67 | lightsB = 1; |
10724928 | 8:3949ee5c0083 | 68 | lightsR = 1; |
10724928 | 8:3949ee5c0083 | 69 | lightsG = 0; |
10724928 | 8:3949ee5c0083 | 70 | } |
10724928 | 8:3949ee5c0083 | 71 | if(lightPercent < lowThresh) { |
10724928 | 8:3949ee5c0083 | 72 | lightsB = 1; |
10724928 | 8:3949ee5c0083 | 73 | lightsR = 0; |
10724928 | 8:3949ee5c0083 | 74 | lightsG = 1; |
10724928 | 8:3949ee5c0083 | 75 | } |
10724928 | 8:3949ee5c0083 | 76 | |
reedas | 6:528d74136b6a | 77 | ThisThread::sleep_for(1000); |
10724928 | 7:bd8ad736bd09 | 78 | char buffer[16]; |
10724928 | 7:bd8ad736bd09 | 79 | sprintf(buffer,"%d",lightPercent); |
10724928 | 7:bd8ad736bd09 | 80 | GUI_SetTextAlign(GUI_TA_HCENTER); |
10724928 | 7:bd8ad736bd09 | 81 | GUI_DispStringAt(buffer,160,40); |
10724928 | 7:bd8ad736bd09 | 82 | GUI_SetColor(GUI_WHITE); |
10724928 | 7:bd8ad736bd09 | 83 | GUI_DrawRect(260, 20, 300, 220); |
10724928 | 7:bd8ad736bd09 | 84 | GUI_SetColor(GUI_CYAN); |
10724928 | 7:bd8ad736bd09 | 85 | GUI_FillRect(260, 220 - lightPercent * 2, 300, 220 ); |
reedas | 4:d129442ea4dd | 86 | } |
reedas | 6:528d74136b6a | 87 | |
romilly | 0:6beb6f498640 | 88 | } |
reedas | 1:402b32a1025f | 89 | |
reedas | 1:402b32a1025f | 90 | |
reedas | 1:402b32a1025f | 91 |