A sunrise alarm clock controlled through serial designed to run on a LPC 1768 and is currently being modified to run on a Nucleo board
main.cpp@4:f2f25ca84b32, 2015-08-16 (annotated)
- Committer:
- propellerhead
- Date:
- Sun Aug 16 15:19:20 2015 +0000
- Revision:
- 4:f2f25ca84b32
- Parent:
- 2:223c142956c6
Changed pin-outs for the nucleo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
propellerhead | 0:ea21a4715fed | 1 | #include "mbed.h" |
propellerhead | 1:dcd847fa6def | 2 | #include "RTC.h" |
propellerhead | 0:ea21a4715fed | 3 | |
propellerhead | 2:223c142956c6 | 4 | PwmOut reds(D7); |
propellerhead | 2:223c142956c6 | 5 | PwmOut greens(D6); |
propellerhead | 2:223c142956c6 | 6 | PwmOut yellows(D5); |
propellerhead | 2:223c142956c6 | 7 | PwmOut blues(D4); |
propellerhead | 0:ea21a4715fed | 8 | |
propellerhead | 0:ea21a4715fed | 9 | Serial pc(USBTX, USBRX); |
propellerhead | 0:ea21a4715fed | 10 | |
propellerhead | 1:dcd847fa6def | 11 | // Circular buffers for serial TX and RX data - used by interrupt routines |
propellerhead | 1:dcd847fa6def | 12 | const int buffer_size = 255; |
propellerhead | 1:dcd847fa6def | 13 | // might need to increase buffer size for high baud rates |
propellerhead | 1:dcd847fa6def | 14 | char tx_buffer[buffer_size+1]; |
propellerhead | 1:dcd847fa6def | 15 | char rx_buffer[buffer_size+1]; |
propellerhead | 1:dcd847fa6def | 16 | // Circular buffer pointers |
propellerhead | 1:dcd847fa6def | 17 | // volatile makes read-modify-write atomic |
propellerhead | 1:dcd847fa6def | 18 | volatile int tx_in=0; |
propellerhead | 1:dcd847fa6def | 19 | volatile int tx_out=0; |
propellerhead | 1:dcd847fa6def | 20 | volatile int rx_in=0; |
propellerhead | 1:dcd847fa6def | 21 | volatile int rx_out=0; |
propellerhead | 1:dcd847fa6def | 22 | // Line buffers for sprintf and sscanf |
propellerhead | 1:dcd847fa6def | 23 | char tx_line[80]; |
propellerhead | 1:dcd847fa6def | 24 | char rx_line[80]; |
propellerhead | 0:ea21a4715fed | 25 | |
propellerhead | 1:dcd847fa6def | 26 | // State of the time setting |
propellerhead | 1:dcd847fa6def | 27 | volatile int timeSetting = 0; |
propellerhead | 1:dcd847fa6def | 28 | // Alarm state |
propellerhead | 1:dcd847fa6def | 29 | volatile int alarmActive = 0; |
propellerhead | 1:dcd847fa6def | 30 | |
propellerhead | 1:dcd847fa6def | 31 | void Rx_interrupt(void); |
propellerhead | 1:dcd847fa6def | 32 | void minTick(void); |
propellerhead | 1:dcd847fa6def | 33 | void alarmRoutine(void); |
propellerhead | 1:dcd847fa6def | 34 | void lightSequence(void); |
propellerhead | 1:dcd847fa6def | 35 | int redMs = 0; |
propellerhead | 1:dcd847fa6def | 36 | int greenMs = 0; |
propellerhead | 1:dcd847fa6def | 37 | int yellowMs = 0; |
propellerhead | 1:dcd847fa6def | 38 | int blueMs = 0; |
propellerhead | 1:dcd847fa6def | 39 | |
propellerhead | 1:dcd847fa6def | 40 | int main() |
propellerhead | 1:dcd847fa6def | 41 | { |
propellerhead | 2:223c142956c6 | 42 | |
propellerhead | 0:ea21a4715fed | 43 | pc.baud(9600); |
propellerhead | 0:ea21a4715fed | 44 | reds.period_ms(1); |
propellerhead | 0:ea21a4715fed | 45 | greens.period_ms(1); |
propellerhead | 0:ea21a4715fed | 46 | yellows.period_ms(1); |
propellerhead | 0:ea21a4715fed | 47 | blues.period_ms(1); |
propellerhead | 1:dcd847fa6def | 48 | |
propellerhead | 1:dcd847fa6def | 49 | reds.pulsewidth_us(redMs); |
propellerhead | 1:dcd847fa6def | 50 | greens.pulsewidth_us(greenMs); |
propellerhead | 1:dcd847fa6def | 51 | yellows.pulsewidth_us(yellowMs); |
propellerhead | 1:dcd847fa6def | 52 | blues.pulsewidth_us(blueMs); |
propellerhead | 1:dcd847fa6def | 53 | |
propellerhead | 0:ea21a4715fed | 54 | pc.attach(&Rx_interrupt, Serial::RxIrq); |
propellerhead | 1:dcd847fa6def | 55 | |
propellerhead | 0:ea21a4715fed | 56 | set_time(1256729737); // Set RTC time to Wed, 28 Oct 2009 11:35:37 |
propellerhead | 0:ea21a4715fed | 57 | |
propellerhead | 1:dcd847fa6def | 58 | RTC::attach(&minTick, RTC::Minute); |
propellerhead | 2:223c142956c6 | 59 | while(1)sleep(); |
propellerhead | 0:ea21a4715fed | 60 | return 0; |
propellerhead | 0:ea21a4715fed | 61 | } |
propellerhead | 0:ea21a4715fed | 62 | |
propellerhead | 1:dcd847fa6def | 63 | void Rx_interrupt() |
propellerhead | 1:dcd847fa6def | 64 | { |
propellerhead | 1:dcd847fa6def | 65 | while (pc.readable()) { |
propellerhead | 1:dcd847fa6def | 66 | rx_buffer[rx_in] = pc.getc(); |
propellerhead | 1:dcd847fa6def | 67 | // Uncomment to Echo to USB serial to watch data flow |
propellerhead | 1:dcd847fa6def | 68 | pc.putc(rx_buffer[rx_in]); |
propellerhead | 1:dcd847fa6def | 69 | if(!rx_in) { |
propellerhead | 1:dcd847fa6def | 70 | if(rx_buffer[rx_in]=='a') { |
propellerhead | 1:dcd847fa6def | 71 | timeSetting = 1; |
propellerhead | 1:dcd847fa6def | 72 | printf("Enter alarm time as 'HH:MM' and press enter\r\n"); |
propellerhead | 1:dcd847fa6def | 73 | rx_in = 0; |
propellerhead | 1:dcd847fa6def | 74 | return; |
propellerhead | 1:dcd847fa6def | 75 | } else if(rx_buffer[rx_in]=='t') { |
propellerhead | 1:dcd847fa6def | 76 | printf("Enter time and date as 'HH:MM:SS_dd/mm/yyyy' and press enter\r\n"); |
propellerhead | 1:dcd847fa6def | 77 | timeSetting = 2; |
propellerhead | 1:dcd847fa6def | 78 | rx_in = 0; |
propellerhead | 1:dcd847fa6def | 79 | return; |
propellerhead | 1:dcd847fa6def | 80 | } else if(rx_buffer[rx_in]=='o') { |
propellerhead | 1:dcd847fa6def | 81 | printf("Lights off\r\n"); |
propellerhead | 1:dcd847fa6def | 82 | redMs = 0; |
propellerhead | 1:dcd847fa6def | 83 | greenMs = 0; |
propellerhead | 1:dcd847fa6def | 84 | yellowMs = 0; |
propellerhead | 1:dcd847fa6def | 85 | blueMs = 0; |
propellerhead | 1:dcd847fa6def | 86 | rx_in = 0; |
propellerhead | 1:dcd847fa6def | 87 | timeSetting = 0; |
propellerhead | 1:dcd847fa6def | 88 | reds.pulsewidth_us(redMs); |
propellerhead | 1:dcd847fa6def | 89 | greens.pulsewidth_us(greenMs); |
propellerhead | 1:dcd847fa6def | 90 | yellows.pulsewidth_us(yellowMs); |
propellerhead | 1:dcd847fa6def | 91 | blues.pulsewidth_us(blueMs); |
propellerhead | 1:dcd847fa6def | 92 | alarmActive = 0; |
propellerhead | 2:223c142956c6 | 93 | RTC::detach(RTC::Second); |
propellerhead | 1:dcd847fa6def | 94 | return; |
propellerhead | 1:dcd847fa6def | 95 | } else { |
propellerhead | 1:dcd847fa6def | 96 | if(!timeSetting) { |
propellerhead | 1:dcd847fa6def | 97 | printf("Press 'a' to set alarm and 't' to set the time\r\n"); |
propellerhead | 1:dcd847fa6def | 98 | timeSetting = 0; |
propellerhead | 1:dcd847fa6def | 99 | rx_in = 0; |
propellerhead | 1:dcd847fa6def | 100 | return; |
propellerhead | 1:dcd847fa6def | 101 | } |
propellerhead | 1:dcd847fa6def | 102 | } |
propellerhead | 1:dcd847fa6def | 103 | } else { |
propellerhead | 1:dcd847fa6def | 104 | if(rx_buffer[rx_in]== '\r') { |
propellerhead | 1:dcd847fa6def | 105 | if(timeSetting == 1) { |
propellerhead | 1:dcd847fa6def | 106 | int hours, minutes; |
propellerhead | 1:dcd847fa6def | 107 | if(sscanf(rx_buffer, "%d:%d", &hours, &minutes)==2) { |
propellerhead | 1:dcd847fa6def | 108 | printf("Alarm Stored\r\n"); |
propellerhead | 1:dcd847fa6def | 109 | } else { |
propellerhead | 1:dcd847fa6def | 110 | printf("Error\r\n"); |
propellerhead | 1:dcd847fa6def | 111 | } |
propellerhead | 1:dcd847fa6def | 112 | tm newTime; |
propellerhead | 1:dcd847fa6def | 113 | newTime.tm_min = minutes; |
propellerhead | 1:dcd847fa6def | 114 | newTime.tm_hour = hours; |
propellerhead | 1:dcd847fa6def | 115 | RTC::alarm(&alarmRoutine, newTime); |
propellerhead | 1:dcd847fa6def | 116 | } else if(timeSetting == 2) { |
propellerhead | 1:dcd847fa6def | 117 | int hours, minutes, seconds, day, month, year; |
propellerhead | 1:dcd847fa6def | 118 | if(sscanf(rx_buffer, "%d:%d:%d_%d/%x/%d", &hours, &minutes,&seconds, &day, &month, &year)==6) { |
propellerhead | 1:dcd847fa6def | 119 | printf("Time Stored: %d:%d:%d_%d/%x/%d\r\n", hours, minutes,seconds, day, month, year); |
propellerhead | 1:dcd847fa6def | 120 | tm newTime; |
propellerhead | 1:dcd847fa6def | 121 | newTime.tm_sec = seconds; |
propellerhead | 1:dcd847fa6def | 122 | newTime.tm_min = minutes; |
propellerhead | 1:dcd847fa6def | 123 | newTime.tm_hour = hours; |
propellerhead | 1:dcd847fa6def | 124 | newTime.tm_mday = day; |
propellerhead | 1:dcd847fa6def | 125 | newTime.tm_mon = month; |
propellerhead | 1:dcd847fa6def | 126 | newTime.tm_year = year-1900; |
propellerhead | 1:dcd847fa6def | 127 | |
propellerhead | 1:dcd847fa6def | 128 | time_t setTime = mktime(&newTime); |
propellerhead | 1:dcd847fa6def | 129 | set_time(setTime); |
propellerhead | 1:dcd847fa6def | 130 | } else { |
propellerhead | 1:dcd847fa6def | 131 | printf("Error\r\n"); |
propellerhead | 1:dcd847fa6def | 132 | } |
propellerhead | 1:dcd847fa6def | 133 | } |
propellerhead | 1:dcd847fa6def | 134 | rx_in = 0; |
propellerhead | 1:dcd847fa6def | 135 | timeSetting = 0; |
propellerhead | 1:dcd847fa6def | 136 | return; |
propellerhead | 1:dcd847fa6def | 137 | } |
propellerhead | 1:dcd847fa6def | 138 | } |
propellerhead | 1:dcd847fa6def | 139 | } |
propellerhead | 1:dcd847fa6def | 140 | rx_in = (rx_in + 1) % buffer_size; |
propellerhead | 1:dcd847fa6def | 141 | } |
propellerhead | 1:dcd847fa6def | 142 | |
propellerhead | 1:dcd847fa6def | 143 | void minTick() |
propellerhead | 1:dcd847fa6def | 144 | { |
propellerhead | 1:dcd847fa6def | 145 | time_t seconds = time(NULL); |
propellerhead | 1:dcd847fa6def | 146 | printf("Time as a basic string = %s\r\n", ctime(&seconds)); |
propellerhead | 1:dcd847fa6def | 147 | printf("Press 'a' to set alarm and 't' to set the time\r\n"); |
propellerhead | 1:dcd847fa6def | 148 | //reds.pulsewidth_us(redMs); |
propellerhead | 1:dcd847fa6def | 149 | //redMs+=100; |
propellerhead | 1:dcd847fa6def | 150 | //if(!(redMs-1100)) { |
propellerhead | 1:dcd847fa6def | 151 | // redMs = 0; |
propellerhead | 1:dcd847fa6def | 152 | //} |
propellerhead | 1:dcd847fa6def | 153 | } |
propellerhead | 1:dcd847fa6def | 154 | void alarmRoutine(){ |
propellerhead | 1:dcd847fa6def | 155 | RTC::attach(&lightSequence, RTC::Second); |
propellerhead | 1:dcd847fa6def | 156 | printf("ALARM!!!\r\n"); |
propellerhead | 1:dcd847fa6def | 157 | } |
propellerhead | 1:dcd847fa6def | 158 | void lightSequence(){ |
propellerhead | 4:f2f25ca84b32 | 159 | if(redMs <= 1000){ |
propellerhead | 2:223c142956c6 | 160 | redMs+=3; |
propellerhead | 1:dcd847fa6def | 161 | reds.pulsewidth_us(redMs); |
propellerhead | 4:f2f25ca84b32 | 162 | }else if (yellowMs <= 1000){ |
propellerhead | 2:223c142956c6 | 163 | yellowMs+=3; |
propellerhead | 1:dcd847fa6def | 164 | yellows.pulsewidth_us(yellowMs); |
propellerhead | 4:f2f25ca84b32 | 165 | }else if (greenMs <= 1000){ |
propellerhead | 2:223c142956c6 | 166 | greenMs+=3; |
propellerhead | 1:dcd847fa6def | 167 | greens.pulsewidth_us(greenMs); |
propellerhead | 4:f2f25ca84b32 | 168 | }else if (blueMs <= 1000){ |
propellerhead | 2:223c142956c6 | 169 | blueMs+=1; |
propellerhead | 1:dcd847fa6def | 170 | blues.pulsewidth_us(blueMs); |
propellerhead | 1:dcd847fa6def | 171 | }else{ |
propellerhead | 1:dcd847fa6def | 172 | alarmActive = 0; |
propellerhead | 1:dcd847fa6def | 173 | RTC::detach(RTC::Second); |
propellerhead | 1:dcd847fa6def | 174 | } |
propellerhead | 0:ea21a4715fed | 175 | } |