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 Oct 01 19:26:27 2015 +0000
Revision:
4:2c4154aae49e
Parent:
2:e084bab7bc1c
Complete project for workshop

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 4:2c4154aae49e 15 uint32_t RGB_sel = 0;
lvagasi 0:28b9efbdeffc 16
lvagasi 2:e084bab7bc1c 17 extern uint32_t Index;
lvagasi 2:e084bab7bc1c 18 extern uint32_t new_key;
lvagasi 2:e084bab7bc1c 19 extern Serial pc; // UART to communicate with PC
lvagasi 2:e084bab7bc1c 20 extern DigitalOut myled; // On-board LED
lvagasi 2:e084bab7bc1c 21 extern FSM_State Main_FSM_States;
lvagasi 2:e084bab7bc1c 22 extern RTC_HandleTypeDef rtch;
lvagasi 2:e084bab7bc1c 23 extern RTC_TimeTypeDef rtc_time;
lvagasi 2:e084bab7bc1c 24 extern RTC_DateTypeDef rtc_date;
lvagasi 2:e084bab7bc1c 25 extern RTC_AlarmTypeDef rtc_alarm;
lvagasi 2:e084bab7bc1c 26 extern int tmp_date, tmp_sec;
lvagasi 2:e084bab7bc1c 27 extern int AlarmA_Enabled;
lvagasi 2:e084bab7bc1c 28 extern int AlarmA_triggered;
lvagasi 0:28b9efbdeffc 29
lvagasi 2:e084bab7bc1c 30 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 31 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 32 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 33 864.0f };
lvagasi 0:28b9efbdeffc 34
lvagasi 2:e084bab7bc1c 35 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 36 {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 37 {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 38 {16, 4}, {18, 4}, {20, 4}, {18, 4}, {16, 2}, { 0, 2} };
lvagasi 2:e084bab7bc1c 39
lvagasi 0:28b9efbdeffc 40 void SysTick_Handler(void) {
lvagasi 0:28b9efbdeffc 41 HAL_SYSTICK_Callback();
lvagasi 0:28b9efbdeffc 42 }
lvagasi 0:28b9efbdeffc 43
lvagasi 0:28b9efbdeffc 44 void HAL_SYSTICK_Callback(void) {
lvagasi 0:28b9efbdeffc 45 Systck_cnt++;
lvagasi 1:f76b625bd36e 46 Systck_evt++;
lvagasi 0:28b9efbdeffc 47 }
lvagasi 0:28b9efbdeffc 48
lvagasi 4:2c4154aae49e 49 void Drive_RGB_LED(void) {
lvagasi 4:2c4154aae49e 50 switch (RGB_sel % 3) {
lvagasi 4:2c4154aae49e 51 case 0:
lvagasi 4:2c4154aae49e 52 R_LED = 0;
lvagasi 4:2c4154aae49e 53 G_LED = 1;
lvagasi 4:2c4154aae49e 54 B_LED = 1;
lvagasi 4:2c4154aae49e 55 break;
lvagasi 4:2c4154aae49e 56 case 1:
lvagasi 4:2c4154aae49e 57 R_LED = 1;
lvagasi 4:2c4154aae49e 58 G_LED = 0;
lvagasi 4:2c4154aae49e 59 B_LED = 1;
lvagasi 4:2c4154aae49e 60 break;
lvagasi 4:2c4154aae49e 61 case 2:
lvagasi 4:2c4154aae49e 62 R_LED = 1;
lvagasi 4:2c4154aae49e 63 G_LED = 1;
lvagasi 4:2c4154aae49e 64 B_LED = 0;
lvagasi 4:2c4154aae49e 65 break;
lvagasi 4:2c4154aae49e 66 default:
lvagasi 4:2c4154aae49e 67 R_LED = 1;
lvagasi 4:2c4154aae49e 68 G_LED = 1;
lvagasi 4:2c4154aae49e 69 B_LED = 1;
lvagasi 4:2c4154aae49e 70 break;
lvagasi 4:2c4154aae49e 71 }
lvagasi 4:2c4154aae49e 72 RGB_sel++;
lvagasi 4:2c4154aae49e 73 }
lvagasi 4:2c4154aae49e 74
lvagasi 0:28b9efbdeffc 75 int main() {
lvagasi 1:f76b625bd36e 76 uint32_t dummy;
lvagasi 2:e084bab7bc1c 77 int i;
lvagasi 2:e084bab7bc1c 78 float tone;
lvagasi 0:28b9efbdeffc 79
lvagasi 2:e084bab7bc1c 80 speaker = 0;
lvagasi 0:28b9efbdeffc 81 R_LED = 1;
lvagasi 0:28b9efbdeffc 82 G_LED = 1;
lvagasi 0:28b9efbdeffc 83 B_LED = 1;
lvagasi 0:28b9efbdeffc 84
lvagasi 2:e084bab7bc1c 85 InitRTC();
lvagasi 2:e084bab7bc1c 86 Init_keypad();
lvagasi 0:28b9efbdeffc 87
lvagasi 0:28b9efbdeffc 88 NVIC_SetVector(SysTick_IRQn, (uint32_t)SysTick_Handler);
lvagasi 0:28b9efbdeffc 89 HAL_SYSTICK_Config(14400000);
lvagasi 0:28b9efbdeffc 90
lvagasi 0:28b9efbdeffc 91 __enable_irq();
lvagasi 0:28b9efbdeffc 92
lvagasi 0:28b9efbdeffc 93 init_ser_lcd();
lvagasi 0:28b9efbdeffc 94 write_ser_lcd(0x80, false); // set DDRAM addr to 0x00
lvagasi 4:2c4154aae49e 95 write_ser_text((char *)"ARM SKOOL",16);
lvagasi 0:28b9efbdeffc 96 write_ser_lcd(0xC0, false); // set DDRAM addr to 0x40, beginning of 2nd line
lvagasi 4:2c4154aae49e 97 write_ser_text((char *)" Alarm clock ", 16);
lvagasi 0:28b9efbdeffc 98
lvagasi 2:e084bab7bc1c 99 wait(3.0);
lvagasi 0:28b9efbdeffc 100
lvagasi 0:28b9efbdeffc 101 write_ser_lcd(0x01, false); // Clear display
lvagasi 2:e084bab7bc1c 102 wait_us(1100);
lvagasi 0:28b9efbdeffc 103 write_ser_lcd(0x06, false); // Entry mode set
lvagasi 0:28b9efbdeffc 104 wait_us(30);
lvagasi 2:e084bab7bc1c 105 ShowTime();
lvagasi 2:e084bab7bc1c 106 ShowDate();
lvagasi 0:28b9efbdeffc 107
lvagasi 0:28b9efbdeffc 108 while (1) {
lvagasi 0:28b9efbdeffc 109 if (new_key != 0) {
lvagasi 2:e084bab7bc1c 110 if ((Index < 0xFF) & (Keytable[Index] == '#')) {
lvagasi 2:e084bab7bc1c 111 Main_FSM_States = IN_MENU;
lvagasi 2:e084bab7bc1c 112 MainMenu_Handler();
lvagasi 4:2c4154aae49e 113 if (Main_FSM_States == FINISH) {Main_FSM_States = IN_LOOP;}
lvagasi 0:28b9efbdeffc 114 }
lvagasi 0:28b9efbdeffc 115 new_key = 0;
lvagasi 0:28b9efbdeffc 116 Index = 0xFF;
lvagasi 0:28b9efbdeffc 117 HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
lvagasi 0:28b9efbdeffc 118 }
lvagasi 0:28b9efbdeffc 119
lvagasi 0:28b9efbdeffc 120
lvagasi 0:28b9efbdeffc 121 HAL_RTC_GetTime(&rtch, &rtc_time, FORMAT_BCD);
lvagasi 1:f76b625bd36e 122 HAL_RTC_GetDate(&rtch, &rtc_date, FORMAT_BCD);
lvagasi 0:28b9efbdeffc 123 if (rtc_time.Seconds != tmp_sec) {
lvagasi 2:e084bab7bc1c 124 ShowTime();
lvagasi 1:f76b625bd36e 125 dummy = 0;
lvagasi 1:f76b625bd36e 126
lvagasi 1:f76b625bd36e 127 if (rtc_date.Date != tmp_date) {
lvagasi 2:e084bab7bc1c 128 ShowDate();
lvagasi 1:f76b625bd36e 129 }
lvagasi 0:28b9efbdeffc 130 }
lvagasi 2:e084bab7bc1c 131
lvagasi 2:e084bab7bc1c 132 if (AlarmA_triggered) {
lvagasi 2:e084bab7bc1c 133 AlarmA_triggered = 0;
lvagasi 2:e084bab7bc1c 134 ShowAlarmText();
lvagasi 4:2c4154aae49e 135 Drive_RGB_LED();
lvagasi 2:e084bab7bc1c 136 for (i = 0; i < 54; i++) {
lvagasi 2:e084bab7bc1c 137 if (song[i][0] > 0.0f) {
lvagasi 2:e084bab7bc1c 138 tone = float(1.0f/scala[song[i][0]]);
lvagasi 2:e084bab7bc1c 139 speaker.period(tone);
lvagasi 2:e084bab7bc1c 140 speaker = 0.5;
lvagasi 2:e084bab7bc1c 141 } else {
lvagasi 2:e084bab7bc1c 142 speaker = 0;
lvagasi 2:e084bab7bc1c 143 }
lvagasi 2:e084bab7bc1c 144 wait(float(1.0f/song[i][1]));
lvagasi 2:e084bab7bc1c 145 if ((new_key != 0) & (Index < 0xFF) & (Keytable[Index] == '#')) {
lvagasi 4:2c4154aae49e 146 speaker = 0;
lvagasi 2:e084bab7bc1c 147 i = 54;
lvagasi 2:e084bab7bc1c 148 new_key = 0;
lvagasi 2:e084bab7bc1c 149 Index = 0xFF;
lvagasi 2:e084bab7bc1c 150 HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
lvagasi 2:e084bab7bc1c 151 }
lvagasi 4:2c4154aae49e 152 Drive_RGB_LED();
lvagasi 2:e084bab7bc1c 153 }
lvagasi 2:e084bab7bc1c 154 write_ser_lcd(0x01, false); // Clear display
lvagasi 2:e084bab7bc1c 155 wait_us(1100);
lvagasi 2:e084bab7bc1c 156 write_ser_lcd(0x06, false); // Entry mode set
lvagasi 2:e084bab7bc1c 157 wait_us(30);
lvagasi 2:e084bab7bc1c 158 ShowTime();
lvagasi 2:e084bab7bc1c 159 ShowDate();
lvagasi 4:2c4154aae49e 160 R_LED = 1;
lvagasi 4:2c4154aae49e 161 G_LED = 1;
lvagasi 4:2c4154aae49e 162 B_LED = 1;
lvagasi 2:e084bab7bc1c 163 }
lvagasi 2:e084bab7bc1c 164
lvagasi 0:28b9efbdeffc 165 if (Systck_cnt > 5) {
lvagasi 0:28b9efbdeffc 166 Systck_cnt = 0;
lvagasi 0:28b9efbdeffc 167 }
lvagasi 0:28b9efbdeffc 168 //HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
lvagasi 4:2c4154aae49e 169 __WFI();
lvagasi 4:2c4154aae49e 170 //while (Systck_evt == 0) {
lvagasi 4:2c4154aae49e 171 // dummy++;
lvagasi 4:2c4154aae49e 172 //}
lvagasi 1:f76b625bd36e 173 Systck_evt = 0;
lvagasi 0:28b9efbdeffc 174 }
lvagasi 0:28b9efbdeffc 175 }