A simple alarm clock for the 1 day workshop presented by Skool and ARM Hungary in 2015.

Dependencies:   Skool_wkshp_lib2015 mbed

Committer:
lvagasi
Date:
Thu Sep 24 15:49:20 2015 +0000
Revision:
2:e084bab7bc1c
Parent:
1:f76b625bd36e
Child:
4:2c4154aae49e
This is a working version. Seems functional. Value checking and error handling is not implemented yet.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lvagasi 0:28b9efbdeffc 1 #include "mbed.h"
lvagasi 0:28b9efbdeffc 2 #include "serial_lcd.h"
lvagasi 2:e084bab7bc1c 3 #include "pc_uart.h"
lvagasi 2:e084bab7bc1c 4 #include "rtc.h"
lvagasi 2:e084bab7bc1c 5 #include "keypad.h"
lvagasi 2:e084bab7bc1c 6 #include "menu.h"
lvagasi 0:28b9efbdeffc 7
lvagasi 0:28b9efbdeffc 8 DigitalOut R_LED(PA_10); // RED part of the RGB LED
lvagasi 0:28b9efbdeffc 9 DigitalOut G_LED(PB_5); // GREEN part of the RGB LED
lvagasi 0:28b9efbdeffc 10 DigitalOut B_LED(PA_9); // BLUE part of the RGB LED
lvagasi 0:28b9efbdeffc 11 PwmOut speaker(PB_4); // Speaker
lvagasi 0:28b9efbdeffc 12
lvagasi 0:28b9efbdeffc 13 uint32_t Systck_cnt = 0;
lvagasi 1:f76b625bd36e 14 uint32_t Systck_evt = 0;
lvagasi 0:28b9efbdeffc 15
lvagasi 2:e084bab7bc1c 16 extern uint32_t Index;
lvagasi 2:e084bab7bc1c 17 extern uint32_t new_key;
lvagasi 2:e084bab7bc1c 18 extern Serial pc; // UART to communicate with PC
lvagasi 2:e084bab7bc1c 19 extern DigitalOut myled; // On-board LED
lvagasi 2:e084bab7bc1c 20 extern FSM_State Main_FSM_States;
lvagasi 2:e084bab7bc1c 21 extern RTC_HandleTypeDef rtch;
lvagasi 2:e084bab7bc1c 22 extern RTC_TimeTypeDef rtc_time;
lvagasi 2:e084bab7bc1c 23 extern RTC_DateTypeDef rtc_date;
lvagasi 2:e084bab7bc1c 24 extern RTC_AlarmTypeDef rtc_alarm;
lvagasi 2:e084bab7bc1c 25 extern int tmp_date, tmp_sec;
lvagasi 2:e084bab7bc1c 26 extern int AlarmA_Enabled;
lvagasi 2:e084bab7bc1c 27 extern int AlarmA_triggered;
lvagasi 0:28b9efbdeffc 28
lvagasi 2:e084bab7bc1c 29 const float scala[38] = { 0.0f, 108.0f, 112.5f, 121.5f, 129.6f, 135.0f, 144.0f, 150.0f, 162.0f, 172.8f, 180.0f, 194.4f, 202.5f,
lvagasi 2:e084bab7bc1c 30 216.0f, 225.0f, 243.0f, 259.2f, 270.0f, 288.0f, 300.0f, 324.0f, 345.6f, 360.0f, 388.8f, 405.0f,
lvagasi 2:e084bab7bc1c 31 432.0f, 450.0f, 486.0f, 518.4f, 540.0f, 576.0f, 600.0f, 648.0f, 691.2f, 720.0f, 777.6f, 810.0f,
lvagasi 2:e084bab7bc1c 32 864.0f };
lvagasi 0:28b9efbdeffc 33
lvagasi 2:e084bab7bc1c 34 const uint32_t song[54][2] = { {16, 4}, {18, 4}, {20, 4}, {18, 4}, {16, 2}, { 0, 2}, {16, 4}, {18, 4}, {20, 4}, {18, 4}, {16, 2}, { 0, 2}, {20, 4}, {20, 4}, {21, 4}, {21, 4},
lvagasi 2:e084bab7bc1c 35 {20, 4}, {20, 4}, {18, 2}, {20, 4}, {20, 4}, {21, 4}, {21, 4}, {20, 4}, {20, 4}, {18, 2}, {20, 4}, {18, 4}, {20, 4}, {21, 4}, {23, 4}, {21, 4},
lvagasi 2:e084bab7bc1c 36 {20, 4}, {18, 4}, {20, 4}, {18, 4}, {20, 4}, {21, 4}, {23, 4}, {21, 4}, {20, 4}, {18, 4}, {16, 4}, {18, 4}, {20, 4}, {18, 4}, {16, 2}, { 0, 2},
lvagasi 2:e084bab7bc1c 37 {16, 4}, {18, 4}, {20, 4}, {18, 4}, {16, 2}, { 0, 2} };
lvagasi 2:e084bab7bc1c 38
lvagasi 0:28b9efbdeffc 39 void SysTick_Handler(void) {
lvagasi 0:28b9efbdeffc 40 HAL_SYSTICK_Callback();
lvagasi 0:28b9efbdeffc 41 }
lvagasi 0:28b9efbdeffc 42
lvagasi 0:28b9efbdeffc 43 void HAL_SYSTICK_Callback(void) {
lvagasi 0:28b9efbdeffc 44 Systck_cnt++;
lvagasi 1:f76b625bd36e 45 Systck_evt++;
lvagasi 0:28b9efbdeffc 46 }
lvagasi 0:28b9efbdeffc 47
lvagasi 0:28b9efbdeffc 48 int main() {
lvagasi 0:28b9efbdeffc 49 uint32_t RGB_sel = 0;
lvagasi 1:f76b625bd36e 50 uint32_t dummy;
lvagasi 2:e084bab7bc1c 51 int i;
lvagasi 2:e084bab7bc1c 52 float tone;
lvagasi 0:28b9efbdeffc 53
lvagasi 2:e084bab7bc1c 54 speaker = 0;
lvagasi 0:28b9efbdeffc 55 R_LED = 1;
lvagasi 0:28b9efbdeffc 56 G_LED = 1;
lvagasi 0:28b9efbdeffc 57 B_LED = 1;
lvagasi 0:28b9efbdeffc 58
lvagasi 2:e084bab7bc1c 59 InitRTC();
lvagasi 2:e084bab7bc1c 60 Init_keypad();
lvagasi 0:28b9efbdeffc 61
lvagasi 0:28b9efbdeffc 62 NVIC_SetVector(SysTick_IRQn, (uint32_t)SysTick_Handler);
lvagasi 0:28b9efbdeffc 63 HAL_SYSTICK_Config(14400000);
lvagasi 0:28b9efbdeffc 64
lvagasi 0:28b9efbdeffc 65 __enable_irq();
lvagasi 0:28b9efbdeffc 66
lvagasi 0:28b9efbdeffc 67 init_ser_lcd();
lvagasi 0:28b9efbdeffc 68 write_ser_lcd(0x80, false); // set DDRAM addr to 0x00
lvagasi 2:e084bab7bc1c 69 write_ser_text((char *)" Alarm clock ", 16);
lvagasi 0:28b9efbdeffc 70 write_ser_lcd(0xC0, false); // set DDRAM addr to 0x40, beginning of 2nd line
lvagasi 2:e084bab7bc1c 71 write_ser_text((char *)"ARM SKOOL",16);
lvagasi 0:28b9efbdeffc 72
lvagasi 2:e084bab7bc1c 73 wait(3.0);
lvagasi 0:28b9efbdeffc 74
lvagasi 0:28b9efbdeffc 75 write_ser_lcd(0x01, false); // Clear display
lvagasi 2:e084bab7bc1c 76 wait_us(1100);
lvagasi 0:28b9efbdeffc 77 write_ser_lcd(0x06, false); // Entry mode set
lvagasi 0:28b9efbdeffc 78 wait_us(30);
lvagasi 2:e084bab7bc1c 79 ShowTime();
lvagasi 2:e084bab7bc1c 80 ShowDate();
lvagasi 0:28b9efbdeffc 81
lvagasi 0:28b9efbdeffc 82 while (1) {
lvagasi 0:28b9efbdeffc 83 if (new_key != 0) {
lvagasi 2:e084bab7bc1c 84 if ((Index < 0xFF) & (Keytable[Index] == '#')) {
lvagasi 2:e084bab7bc1c 85 Main_FSM_States = IN_MENU;
lvagasi 2:e084bab7bc1c 86 MainMenu_Handler();
lvagasi 0:28b9efbdeffc 87 }
lvagasi 0:28b9efbdeffc 88 new_key = 0;
lvagasi 0:28b9efbdeffc 89 Index = 0xFF;
lvagasi 0:28b9efbdeffc 90 HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
lvagasi 0:28b9efbdeffc 91 }
lvagasi 0:28b9efbdeffc 92
lvagasi 0:28b9efbdeffc 93
lvagasi 0:28b9efbdeffc 94 HAL_RTC_GetTime(&rtch, &rtc_time, FORMAT_BCD);
lvagasi 1:f76b625bd36e 95 HAL_RTC_GetDate(&rtch, &rtc_date, FORMAT_BCD);
lvagasi 0:28b9efbdeffc 96 if (rtc_time.Seconds != tmp_sec) {
lvagasi 2:e084bab7bc1c 97 ShowTime();
lvagasi 1:f76b625bd36e 98 dummy = 0;
lvagasi 1:f76b625bd36e 99
lvagasi 1:f76b625bd36e 100 if (rtc_date.Date != tmp_date) {
lvagasi 2:e084bab7bc1c 101 ShowDate();
lvagasi 1:f76b625bd36e 102 }
lvagasi 0:28b9efbdeffc 103 }
lvagasi 2:e084bab7bc1c 104
lvagasi 2:e084bab7bc1c 105 if (AlarmA_triggered) {
lvagasi 2:e084bab7bc1c 106 AlarmA_triggered = 0;
lvagasi 2:e084bab7bc1c 107 ShowAlarmText();
lvagasi 2:e084bab7bc1c 108 for (i = 0; i < 54; i++) {
lvagasi 2:e084bab7bc1c 109 if (song[i][0] > 0.0f) {
lvagasi 2:e084bab7bc1c 110 tone = float(1.0f/scala[song[i][0]]);
lvagasi 2:e084bab7bc1c 111 speaker.period(tone);
lvagasi 2:e084bab7bc1c 112 speaker = 0.5;
lvagasi 2:e084bab7bc1c 113 } else {
lvagasi 2:e084bab7bc1c 114 speaker = 0;
lvagasi 2:e084bab7bc1c 115 }
lvagasi 2:e084bab7bc1c 116 wait(float(1.0f/song[i][1]));
lvagasi 2:e084bab7bc1c 117 if ((new_key != 0) & (Index < 0xFF) & (Keytable[Index] == '#')) {
lvagasi 2:e084bab7bc1c 118 i = 54;
lvagasi 2:e084bab7bc1c 119 new_key = 0;
lvagasi 2:e084bab7bc1c 120 Index = 0xFF;
lvagasi 2:e084bab7bc1c 121 HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
lvagasi 2:e084bab7bc1c 122 }
lvagasi 2:e084bab7bc1c 123 }
lvagasi 2:e084bab7bc1c 124 write_ser_lcd(0x01, false); // Clear display
lvagasi 2:e084bab7bc1c 125 wait_us(1100);
lvagasi 2:e084bab7bc1c 126 write_ser_lcd(0x06, false); // Entry mode set
lvagasi 2:e084bab7bc1c 127 wait_us(30);
lvagasi 2:e084bab7bc1c 128 ShowTime();
lvagasi 2:e084bab7bc1c 129 ShowDate();
lvagasi 2:e084bab7bc1c 130 }
lvagasi 2:e084bab7bc1c 131
lvagasi 0:28b9efbdeffc 132 if (Systck_cnt > 5) {
lvagasi 0:28b9efbdeffc 133 switch (RGB_sel % 3) {
lvagasi 0:28b9efbdeffc 134 case 0:
lvagasi 0:28b9efbdeffc 135 R_LED = 0;
lvagasi 0:28b9efbdeffc 136 G_LED = 1;
lvagasi 0:28b9efbdeffc 137 B_LED = 1;
lvagasi 0:28b9efbdeffc 138 break;
lvagasi 0:28b9efbdeffc 139 case 1:
lvagasi 0:28b9efbdeffc 140 R_LED = 1;
lvagasi 0:28b9efbdeffc 141 G_LED = 0;
lvagasi 0:28b9efbdeffc 142 B_LED = 1;
lvagasi 0:28b9efbdeffc 143 break;
lvagasi 0:28b9efbdeffc 144 case 2:
lvagasi 0:28b9efbdeffc 145 R_LED = 1;
lvagasi 0:28b9efbdeffc 146 G_LED = 1;
lvagasi 0:28b9efbdeffc 147 B_LED = 0;
lvagasi 0:28b9efbdeffc 148 break;
lvagasi 0:28b9efbdeffc 149 default:
lvagasi 0:28b9efbdeffc 150 R_LED = 1;
lvagasi 0:28b9efbdeffc 151 G_LED = 1;
lvagasi 0:28b9efbdeffc 152 B_LED = 1;
lvagasi 0:28b9efbdeffc 153 break;
lvagasi 0:28b9efbdeffc 154 }
lvagasi 0:28b9efbdeffc 155 RGB_sel++;
lvagasi 0:28b9efbdeffc 156 Systck_cnt = 0;
lvagasi 0:28b9efbdeffc 157 }
lvagasi 0:28b9efbdeffc 158 //HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
lvagasi 1:f76b625bd36e 159 //__WFI();
lvagasi 1:f76b625bd36e 160 while (Systck_evt == 0) {
lvagasi 1:f76b625bd36e 161 dummy++;
lvagasi 1:f76b625bd36e 162 }
lvagasi 1:f76b625bd36e 163 Systck_evt = 0;
lvagasi 0:28b9efbdeffc 164 }
lvagasi 0:28b9efbdeffc 165 }