Clock Timer
Dependencies: LCD_DISCO_F429ZI mbed BSP_DISCO_F429ZI
main.cpp@0:d27ad797b936, 2018-12-14 (annotated)
- Committer:
- mosm
- Date:
- Fri Dec 14 20:00:47 2018 +0000
- Revision:
- 0:d27ad797b936
thing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mosm | 0:d27ad797b936 | 1 | #include "mbed.h" |
mosm | 0:d27ad797b936 | 2 | #include "LCD_DISCO_F429ZI.h" |
mosm | 0:d27ad797b936 | 3 | #include <math.h> |
mosm | 0:d27ad797b936 | 4 | |
mosm | 0:d27ad797b936 | 5 | LCD_DISCO_F429ZI lcd; |
mosm | 0:d27ad797b936 | 6 | |
mosm | 0:d27ad797b936 | 7 | DigitalOut green(LED1, 0); |
mosm | 0:d27ad797b936 | 8 | DigitalOut red(LED2, 1); |
mosm | 0:d27ad797b936 | 9 | |
mosm | 0:d27ad797b936 | 10 | InterruptIn button(USER_BUTTON); |
mosm | 0:d27ad797b936 | 11 | |
mosm | 0:d27ad797b936 | 12 | uint8_t str1[30], str2[30]; |
mosm | 0:d27ad797b936 | 13 | |
mosm | 0:d27ad797b936 | 14 | int qsecs = 0; |
mosm | 0:d27ad797b936 | 15 | int seconds = 0; |
mosm | 0:d27ad797b936 | 16 | int minutes = 0; |
mosm | 0:d27ad797b936 | 17 | int hours = 0; |
mosm | 0:d27ad797b936 | 18 | |
mosm | 0:d27ad797b936 | 19 | float multiplier = 9.5; |
mosm | 0:d27ad797b936 | 20 | |
mosm | 0:d27ad797b936 | 21 | int radius = 70; |
mosm | 0:d27ad797b936 | 22 | int centerx = 120; |
mosm | 0:d27ad797b936 | 23 | int centery = 240; |
mosm | 0:d27ad797b936 | 24 | |
mosm | 0:d27ad797b936 | 25 | float delaytime = 0.25; |
mosm | 0:d27ad797b936 | 26 | |
mosm | 0:d27ad797b936 | 27 | struct save |
mosm | 0:d27ad797b936 | 28 | { |
mosm | 0:d27ad797b936 | 29 | bool exists; |
mosm | 0:d27ad797b936 | 30 | int h; |
mosm | 0:d27ad797b936 | 31 | int m; |
mosm | 0:d27ad797b936 | 32 | int s; |
mosm | 0:d27ad797b936 | 33 | }one, two; |
mosm | 0:d27ad797b936 | 34 | |
mosm | 0:d27ad797b936 | 35 | void button_pressed() |
mosm | 0:d27ad797b936 | 36 | { |
mosm | 0:d27ad797b936 | 37 | |
mosm | 0:d27ad797b936 | 38 | one.exists = 1; |
mosm | 0:d27ad797b936 | 39 | one.h = hours; |
mosm | 0:d27ad797b936 | 40 | one.m = minutes; |
mosm | 0:d27ad797b936 | 41 | one.s = seconds; |
mosm | 0:d27ad797b936 | 42 | |
mosm | 0:d27ad797b936 | 43 | red = 0; |
mosm | 0:d27ad797b936 | 44 | green = 1; |
mosm | 0:d27ad797b936 | 45 | hours = 0; |
mosm | 0:d27ad797b936 | 46 | minutes = 0; |
mosm | 0:d27ad797b936 | 47 | seconds = 0; |
mosm | 0:d27ad797b936 | 48 | qsecs = 0; |
mosm | 0:d27ad797b936 | 49 | |
mosm | 0:d27ad797b936 | 50 | lcd.Clear(LCD_COLOR_WHITE); |
mosm | 0:d27ad797b936 | 51 | } |
mosm | 0:d27ad797b936 | 52 | |
mosm | 0:d27ad797b936 | 53 | void button_notpressed() |
mosm | 0:d27ad797b936 | 54 | { |
mosm | 0:d27ad797b936 | 55 | green = 0; |
mosm | 0:d27ad797b936 | 56 | red = 0; |
mosm | 0:d27ad797b936 | 57 | } |
mosm | 0:d27ad797b936 | 58 | |
mosm | 0:d27ad797b936 | 59 | void DrawClock(int second) |
mosm | 0:d27ad797b936 | 60 | { |
mosm | 0:d27ad797b936 | 61 | |
mosm | 0:d27ad797b936 | 62 | if (second == 0) |
mosm | 0:d27ad797b936 | 63 | lcd.Clear(LCD_COLOR_WHITE); |
mosm | 0:d27ad797b936 | 64 | |
mosm | 0:d27ad797b936 | 65 | double angle = second/multiplier; |
mosm | 0:d27ad797b936 | 66 | double endx = centerx+radius*sin(angle); |
mosm | 0:d27ad797b936 | 67 | double endy = centery-radius*cos(angle); |
mosm | 0:d27ad797b936 | 68 | lcd.DrawCircle(centerx, centery, radius); |
mosm | 0:d27ad797b936 | 69 | lcd.DrawLine(centerx, centery, endx, endy); |
mosm | 0:d27ad797b936 | 70 | } |
mosm | 0:d27ad797b936 | 71 | |
mosm | 0:d27ad797b936 | 72 | int main() |
mosm | 0:d27ad797b936 | 73 | { |
mosm | 0:d27ad797b936 | 74 | BSP_LCD_SetFont(&Font20); |
mosm | 0:d27ad797b936 | 75 | lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"Timer Program", CENTER_MODE); |
mosm | 0:d27ad797b936 | 76 | lcd.DisplayStringAt(0, LINE(6), (uint8_t *)"Mohammad Mustafa", CENTER_MODE); |
mosm | 0:d27ad797b936 | 77 | wait(2); |
mosm | 0:d27ad797b936 | 78 | lcd.Clear(LCD_COLOR_WHITE); |
mosm | 0:d27ad797b936 | 79 | |
mosm | 0:d27ad797b936 | 80 | one.exists = 0; |
mosm | 0:d27ad797b936 | 81 | |
mosm | 0:d27ad797b936 | 82 | while(1) |
mosm | 0:d27ad797b936 | 83 | { |
mosm | 0:d27ad797b936 | 84 | button.rise(&button_pressed); |
mosm | 0:d27ad797b936 | 85 | button.fall(&button_notpressed); |
mosm | 0:d27ad797b936 | 86 | |
mosm | 0:d27ad797b936 | 87 | if(button.read()==0) |
mosm | 0:d27ad797b936 | 88 | { |
mosm | 0:d27ad797b936 | 89 | red = !red; |
mosm | 0:d27ad797b936 | 90 | qsecs++; |
mosm | 0:d27ad797b936 | 91 | } |
mosm | 0:d27ad797b936 | 92 | if(qsecs%4 == 0 && qsecs != 0) |
mosm | 0:d27ad797b936 | 93 | { |
mosm | 0:d27ad797b936 | 94 | qsecs = 0; |
mosm | 0:d27ad797b936 | 95 | seconds++; |
mosm | 0:d27ad797b936 | 96 | } |
mosm | 0:d27ad797b936 | 97 | |
mosm | 0:d27ad797b936 | 98 | if(seconds%60 == 0 && seconds != 0) |
mosm | 0:d27ad797b936 | 99 | { |
mosm | 0:d27ad797b936 | 100 | seconds = 0; |
mosm | 0:d27ad797b936 | 101 | minutes++; |
mosm | 0:d27ad797b936 | 102 | } |
mosm | 0:d27ad797b936 | 103 | if(minutes%12 == 0 && minutes != 0) |
mosm | 0:d27ad797b936 | 104 | { |
mosm | 0:d27ad797b936 | 105 | minutes = 0; |
mosm | 0:d27ad797b936 | 106 | hours++; |
mosm | 0:d27ad797b936 | 107 | } |
mosm | 0:d27ad797b936 | 108 | |
mosm | 0:d27ad797b936 | 109 | if(one.exists) |
mosm | 0:d27ad797b936 | 110 | { |
mosm | 0:d27ad797b936 | 111 | sprintf((char*)str1, "Last Time: %d:%d:%d", one.h, one.m, one.s); |
mosm | 0:d27ad797b936 | 112 | lcd.DisplayStringAt(0, LINE(0), (uint8_t *)&str1, LEFT_MODE); |
mosm | 0:d27ad797b936 | 113 | } |
mosm | 0:d27ad797b936 | 114 | |
mosm | 0:d27ad797b936 | 115 | sprintf((char*)str1, "HR:MIN:SEC:qSEC"); |
mosm | 0:d27ad797b936 | 116 | sprintf((char*)str2, "%d:%d:%d:%d", hours, minutes, seconds, qsecs); |
mosm | 0:d27ad797b936 | 117 | lcd.DisplayStringAt(0, LINE(6), (uint8_t *)&str1, CENTER_MODE); |
mosm | 0:d27ad797b936 | 118 | lcd.DisplayStringAt(0, LINE(7), (uint8_t *)&str2, CENTER_MODE); |
mosm | 0:d27ad797b936 | 119 | |
mosm | 0:d27ad797b936 | 120 | DrawClock(seconds%60); |
mosm | 0:d27ad797b936 | 121 | wait(delaytime); |
mosm | 0:d27ad797b936 | 122 | } |
mosm | 0:d27ad797b936 | 123 | } |
mosm | 0:d27ad797b936 | 124 | |
mosm | 0:d27ad797b936 | 125 | |
mosm | 0:d27ad797b936 | 126 |