
USED IMAGE2GLCD
Dependencies: BLE_API SharpLCD_LucidaFont mbed nRF51822
Fork of Renard_YO by
main.cpp@0:9bea6067730f, 2014-07-02 (annotated)
- Committer:
- erigow01
- Date:
- Wed Jul 02 13:19:34 2014 +0000
- Revision:
- 0:9bea6067730f
- Child:
- 1:f0635f12df8c
Basic UI working - needs some additional work on Icons, Layout
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
erigow01 | 0:9bea6067730f | 1 | #include "mbed.h" |
erigow01 | 0:9bea6067730f | 2 | #include "DebouncedInterrupt.h" |
erigow01 | 0:9bea6067730f | 3 | #include "EaEpaper.h" |
erigow01 | 0:9bea6067730f | 4 | #include "Arial28x28.h" |
erigow01 | 0:9bea6067730f | 5 | #include "Arial12x12.h" |
erigow01 | 0:9bea6067730f | 6 | |
erigow01 | 0:9bea6067730f | 7 | //I/O Initialisation |
erigow01 | 0:9bea6067730f | 8 | DigitalOut myled(LED1); |
erigow01 | 0:9bea6067730f | 9 | DigitalOut motor(P0_23); |
erigow01 | 0:9bea6067730f | 10 | #define BUTTON_INTERRUPT_DEBOUNCE_TIME_MS 150 |
erigow01 | 0:9bea6067730f | 11 | DebouncedInterrupt buttonOne (P0_16); |
erigow01 | 0:9bea6067730f | 12 | DebouncedInterrupt buttonTwo (P0_17); |
erigow01 | 0:9bea6067730f | 13 | |
erigow01 | 0:9bea6067730f | 14 | //Epaper Init |
erigow01 | 0:9bea6067730f | 15 | EaEpaper epaper(P0_0, //PWR_CONTROL |
erigow01 | 0:9bea6067730f | 16 | P0_1, //BORDER CONTROL |
erigow01 | 0:9bea6067730f | 17 | P0_2, //DISCHARGE |
erigow01 | 0:9bea6067730f | 18 | P0_3, //RESET |
erigow01 | 0:9bea6067730f | 19 | P0_4, //BUSY |
erigow01 | 0:9bea6067730f | 20 | P0_5, //SSEL |
erigow01 | 0:9bea6067730f | 21 | P0_6, //PWM |
erigow01 | 0:9bea6067730f | 22 | P0_12,P0_13,P0_15, //MOSI,MISO,SCLK |
erigow01 | 0:9bea6067730f | 23 | P0_22,P0_20);//SDA, SDL |
erigow01 | 0:9bea6067730f | 24 | |
erigow01 | 0:9bea6067730f | 25 | //Notification Data struct |
erigow01 | 0:9bea6067730f | 26 | #define NOTIFICATION_TEXT_MAX_LENGTH 20 |
erigow01 | 0:9bea6067730f | 27 | #define NOTIFICATION_TYPE_CALL 0x01 |
erigow01 | 0:9bea6067730f | 28 | #define NOTIFICATION_TYPE_SMS 0x02 |
erigow01 | 0:9bea6067730f | 29 | #define NOTIFICATION_TYPE_EMAIL 0x03 |
erigow01 | 0:9bea6067730f | 30 | #define NOTIFICATION_TYPE_EVENT 0x04 |
erigow01 | 0:9bea6067730f | 31 | #define NOTIFICATION_STATE_DELETED 0x00 |
erigow01 | 0:9bea6067730f | 32 | #define NOTIFICATION_STATE_UNREAD 0x01 |
erigow01 | 0:9bea6067730f | 33 | #define NOTIFICATION_STATE_READ 0x02 |
erigow01 | 0:9bea6067730f | 34 | struct Notification{ |
erigow01 | 0:9bea6067730f | 35 | uint8_t type; |
erigow01 | 0:9bea6067730f | 36 | uint8_t state; |
erigow01 | 0:9bea6067730f | 37 | char primary_text[NOTIFICATION_TEXT_MAX_LENGTH]; |
erigow01 | 0:9bea6067730f | 38 | char secondary_text[NOTIFICATION_TEXT_MAX_LENGTH]; |
erigow01 | 0:9bea6067730f | 39 | }; |
erigow01 | 0:9bea6067730f | 40 | |
erigow01 | 0:9bea6067730f | 41 | //notification storage |
erigow01 | 0:9bea6067730f | 42 | #define MAX_NOTIFICATIONS 20 |
erigow01 | 0:9bea6067730f | 43 | Notification notifications[MAX_NOTIFICATIONS]; |
erigow01 | 0:9bea6067730f | 44 | uint8_t notification_count = 0; |
erigow01 | 0:9bea6067730f | 45 | int visible_notification_index = -1; |
erigow01 | 0:9bea6067730f | 46 | |
erigow01 | 0:9bea6067730f | 47 | //States |
erigow01 | 0:9bea6067730f | 48 | uint8_t needs_display_update = 0; |
erigow01 | 0:9bea6067730f | 49 | uint8_t has_unread = 0; |
erigow01 | 0:9bea6067730f | 50 | |
erigow01 | 0:9bea6067730f | 51 | //For LED |
erigow01 | 0:9bea6067730f | 52 | #define LED_BLINK_TIME_MS 500 |
erigow01 | 0:9bea6067730f | 53 | uint8_t led_on = 0; |
erigow01 | 0:9bea6067730f | 54 | Timer led_timer; |
erigow01 | 0:9bea6067730f | 55 | |
erigow01 | 0:9bea6067730f | 56 | //For Buzzer |
erigow01 | 0:9bea6067730f | 57 | #define BUZZ_TIME_MS 200 |
erigow01 | 0:9bea6067730f | 58 | #define MAX_BUZZ_COUNT 3 |
erigow01 | 0:9bea6067730f | 59 | uint8_t buzz_count = MAX_BUZZ_COUNT; |
erigow01 | 0:9bea6067730f | 60 | Timer buzz_timer; |
erigow01 | 0:9bea6067730f | 61 | |
erigow01 | 0:9bea6067730f | 62 | void setType(Notification* note, uint8_t type) { |
erigow01 | 0:9bea6067730f | 63 | note->type = type; |
erigow01 | 0:9bea6067730f | 64 | } |
erigow01 | 0:9bea6067730f | 65 | |
erigow01 | 0:9bea6067730f | 66 | uint8_t isCall(Notification note) { |
erigow01 | 0:9bea6067730f | 67 | return note.type == NOTIFICATION_TYPE_CALL; |
erigow01 | 0:9bea6067730f | 68 | } |
erigow01 | 0:9bea6067730f | 69 | |
erigow01 | 0:9bea6067730f | 70 | uint8_t isSMS(Notification note) { |
erigow01 | 0:9bea6067730f | 71 | return note.type == NOTIFICATION_TYPE_SMS; |
erigow01 | 0:9bea6067730f | 72 | } |
erigow01 | 0:9bea6067730f | 73 | |
erigow01 | 0:9bea6067730f | 74 | uint8_t isEmail(Notification note) { |
erigow01 | 0:9bea6067730f | 75 | return note.type == NOTIFICATION_TYPE_EMAIL; |
erigow01 | 0:9bea6067730f | 76 | } |
erigow01 | 0:9bea6067730f | 77 | |
erigow01 | 0:9bea6067730f | 78 | uint8_t isEvent(Notification note) { |
erigow01 | 0:9bea6067730f | 79 | return note.type == NOTIFICATION_TYPE_EVENT; |
erigow01 | 0:9bea6067730f | 80 | } |
erigow01 | 0:9bea6067730f | 81 | |
erigow01 | 0:9bea6067730f | 82 | void setState(Notification* note, uint8_t state) { |
erigow01 | 0:9bea6067730f | 83 | note->state = state; |
erigow01 | 0:9bea6067730f | 84 | } |
erigow01 | 0:9bea6067730f | 85 | |
erigow01 | 0:9bea6067730f | 86 | uint8_t isDeleted(Notification note) { |
erigow01 | 0:9bea6067730f | 87 | return note.state == NOTIFICATION_STATE_DELETED; |
erigow01 | 0:9bea6067730f | 88 | } |
erigow01 | 0:9bea6067730f | 89 | |
erigow01 | 0:9bea6067730f | 90 | uint8_t isUnread(Notification note) { |
erigow01 | 0:9bea6067730f | 91 | return note.state == NOTIFICATION_STATE_UNREAD; |
erigow01 | 0:9bea6067730f | 92 | } |
erigow01 | 0:9bea6067730f | 93 | |
erigow01 | 0:9bea6067730f | 94 | uint8_t isRead(Notification note) { |
erigow01 | 0:9bea6067730f | 95 | return note.state == NOTIFICATION_STATE_READ; |
erigow01 | 0:9bea6067730f | 96 | } |
erigow01 | 0:9bea6067730f | 97 | |
erigow01 | 0:9bea6067730f | 98 | //Update Display to show current notification... |
erigow01 | 0:9bea6067730f | 99 | void doDisplayUpdate() { |
erigow01 | 0:9bea6067730f | 100 | epaper.cls(); |
erigow01 | 0:9bea6067730f | 101 | if(visible_notification_index >= 0) { |
erigow01 | 0:9bea6067730f | 102 | //Write current notification... |
erigow01 | 0:9bea6067730f | 103 | epaper.set_font((unsigned char*)Arial12x12); |
erigow01 | 0:9bea6067730f | 104 | epaper.locate(5,5); |
erigow01 | 0:9bea6067730f | 105 | epaper.printf(notifications[visible_notification_index].primary_text); |
erigow01 | 0:9bea6067730f | 106 | epaper.locate(5,15); |
erigow01 | 0:9bea6067730f | 107 | epaper.printf(notifications[visible_notification_index].secondary_text); |
erigow01 | 0:9bea6067730f | 108 | } else { |
erigow01 | 0:9bea6067730f | 109 | //no notifications... |
erigow01 | 0:9bea6067730f | 110 | //Write current notification... |
erigow01 | 0:9bea6067730f | 111 | epaper.set_font((unsigned char*)Arial12x12); |
erigow01 | 0:9bea6067730f | 112 | epaper.locate(5,5); |
erigow01 | 0:9bea6067730f | 113 | epaper.printf("No Notifications"); |
erigow01 | 0:9bea6067730f | 114 | } |
erigow01 | 0:9bea6067730f | 115 | epaper.write_disp(); |
erigow01 | 0:9bea6067730f | 116 | needs_display_update = 0; |
erigow01 | 0:9bea6067730f | 117 | } |
erigow01 | 0:9bea6067730f | 118 | |
erigow01 | 0:9bea6067730f | 119 | //Request a display update.. |
erigow01 | 0:9bea6067730f | 120 | void requestDisplayUpdate() { |
erigow01 | 0:9bea6067730f | 121 | needs_display_update = 1; |
erigow01 | 0:9bea6067730f | 122 | } |
erigow01 | 0:9bea6067730f | 123 | |
erigow01 | 0:9bea6067730f | 124 | //Starts buzz pattern... |
erigow01 | 0:9bea6067730f | 125 | void startBuzz() { |
erigow01 | 0:9bea6067730f | 126 | //Buzz |
erigow01 | 0:9bea6067730f | 127 | buzz_count = 0; |
erigow01 | 0:9bea6067730f | 128 | buzz_timer.reset(); |
erigow01 | 0:9bea6067730f | 129 | buzz_timer.start(); |
erigow01 | 0:9bea6067730f | 130 | } |
erigow01 | 0:9bea6067730f | 131 | |
erigow01 | 0:9bea6067730f | 132 | |
erigow01 | 0:9bea6067730f | 133 | //Add Notification... |
erigow01 | 0:9bea6067730f | 134 | int addNotification(Notification note) { |
erigow01 | 0:9bea6067730f | 135 | //Find insertion point... |
erigow01 | 0:9bea6067730f | 136 | uint8_t index = 0; |
erigow01 | 0:9bea6067730f | 137 | for(index = 0; index < MAX_NOTIFICATIONS; index++) { |
erigow01 | 0:9bea6067730f | 138 | if(isDeleted(notifications[index])) { |
erigow01 | 0:9bea6067730f | 139 | //Here... |
erigow01 | 0:9bea6067730f | 140 | break; |
erigow01 | 0:9bea6067730f | 141 | } |
erigow01 | 0:9bea6067730f | 142 | } |
erigow01 | 0:9bea6067730f | 143 | //If here, didn't find insertion point... wrap to beginning. |
erigow01 | 0:9bea6067730f | 144 | if(index >= MAX_NOTIFICATIONS) index = 0; |
erigow01 | 0:9bea6067730f | 145 | notifications[index] = note; |
erigow01 | 0:9bea6067730f | 146 | //Set buzzer |
erigow01 | 0:9bea6067730f | 147 | startBuzz(); |
erigow01 | 0:9bea6067730f | 148 | //Set unread |
erigow01 | 0:9bea6067730f | 149 | if(isUnread(note)) has_unread = 1; |
erigow01 | 0:9bea6067730f | 150 | return index; |
erigow01 | 0:9bea6067730f | 151 | } |
erigow01 | 0:9bea6067730f | 152 | |
erigow01 | 0:9bea6067730f | 153 | void deleteNotification(int index) { |
erigow01 | 0:9bea6067730f | 154 | setState(¬ifications[index], NOTIFICATION_STATE_DELETED); |
erigow01 | 0:9bea6067730f | 155 | //Shift array elements left... |
erigow01 | 0:9bea6067730f | 156 | int i = 0; |
erigow01 | 0:9bea6067730f | 157 | for (i = index + 1; i < MAX_NOTIFICATIONS; i++) { |
erigow01 | 0:9bea6067730f | 158 | notifications[i - 1] = notifications[i]; |
erigow01 | 0:9bea6067730f | 159 | } |
erigow01 | 0:9bea6067730f | 160 | } |
erigow01 | 0:9bea6067730f | 161 | |
erigow01 | 0:9bea6067730f | 162 | void checkUnread(){ |
erigow01 | 0:9bea6067730f | 163 | uint8_t i = 0; |
erigow01 | 0:9bea6067730f | 164 | led_timer.stop(); |
erigow01 | 0:9bea6067730f | 165 | has_unread = 0; |
erigow01 | 0:9bea6067730f | 166 | for(i = 0; i < MAX_NOTIFICATIONS; i++) { |
erigow01 | 0:9bea6067730f | 167 | if(isUnread(notifications[i])) { |
erigow01 | 0:9bea6067730f | 168 | has_unread = 1; |
erigow01 | 0:9bea6067730f | 169 | led_timer.reset(); |
erigow01 | 0:9bea6067730f | 170 | led_timer.start(); |
erigow01 | 0:9bea6067730f | 171 | break; |
erigow01 | 0:9bea6067730f | 172 | } |
erigow01 | 0:9bea6067730f | 173 | } |
erigow01 | 0:9bea6067730f | 174 | } |
erigow01 | 0:9bea6067730f | 175 | |
erigow01 | 0:9bea6067730f | 176 | //Button One Handler |
erigow01 | 0:9bea6067730f | 177 | void buttonOnePressed(){ |
erigow01 | 0:9bea6067730f | 178 | if(visible_notification_index >= 0) { |
erigow01 | 0:9bea6067730f | 179 | //Increment index, wrap to beginning if last. |
erigow01 | 0:9bea6067730f | 180 | visible_notification_index++; |
erigow01 | 0:9bea6067730f | 181 | if(visible_notification_index >= MAX_NOTIFICATIONS || isDeleted(notifications[visible_notification_index])) { |
erigow01 | 0:9bea6067730f | 182 | visible_notification_index = 0; |
erigow01 | 0:9bea6067730f | 183 | if(isDeleted(notifications[visible_notification_index])) { |
erigow01 | 0:9bea6067730f | 184 | //Still deleted... none... |
erigow01 | 0:9bea6067730f | 185 | visible_notification_index = -1; |
erigow01 | 0:9bea6067730f | 186 | } |
erigow01 | 0:9bea6067730f | 187 | } |
erigow01 | 0:9bea6067730f | 188 | //Trigger display update... |
erigow01 | 0:9bea6067730f | 189 | requestDisplayUpdate(); |
erigow01 | 0:9bea6067730f | 190 | } |
erigow01 | 0:9bea6067730f | 191 | } |
erigow01 | 0:9bea6067730f | 192 | |
erigow01 | 0:9bea6067730f | 193 | //Button Two handler |
erigow01 | 0:9bea6067730f | 194 | void buttonTwoPressed(){ |
erigow01 | 0:9bea6067730f | 195 | if(visible_notification_index >= 0) { |
erigow01 | 0:9bea6067730f | 196 | if(!isDeleted(notifications[visible_notification_index])) { |
erigow01 | 0:9bea6067730f | 197 | //Exists. |
erigow01 | 0:9bea6067730f | 198 | if(isUnread(notifications[visible_notification_index])) { |
erigow01 | 0:9bea6067730f | 199 | //Toggle to 'read' |
erigow01 | 0:9bea6067730f | 200 | setState(¬ifications[visible_notification_index], NOTIFICATION_STATE_READ); |
erigow01 | 0:9bea6067730f | 201 | checkUnread(); |
erigow01 | 0:9bea6067730f | 202 | } else if (isRead(notifications[visible_notification_index])) { |
erigow01 | 0:9bea6067730f | 203 | //Already 'read'... delete, this also shifts remaining notifications down... |
erigow01 | 0:9bea6067730f | 204 | deleteNotification(visible_notification_index); |
erigow01 | 0:9bea6067730f | 205 | //If current is deleted... |
erigow01 | 0:9bea6067730f | 206 | if(isDeleted(notifications[visible_notification_index])) { |
erigow01 | 0:9bea6067730f | 207 | //We're at end, so wrap... |
erigow01 | 0:9bea6067730f | 208 | visible_notification_index = 0; |
erigow01 | 0:9bea6067730f | 209 | if(isDeleted(notifications[visible_notification_index])) { |
erigow01 | 0:9bea6067730f | 210 | //Still deleted... so there are none... |
erigow01 | 0:9bea6067730f | 211 | visible_notification_index = -1; |
erigow01 | 0:9bea6067730f | 212 | } |
erigow01 | 0:9bea6067730f | 213 | } |
erigow01 | 0:9bea6067730f | 214 | //Otherwise, we've got one, so we should be ok... |
erigow01 | 0:9bea6067730f | 215 | } |
erigow01 | 0:9bea6067730f | 216 | |
erigow01 | 0:9bea6067730f | 217 | //Trigger display update... |
erigow01 | 0:9bea6067730f | 218 | requestDisplayUpdate(); |
erigow01 | 0:9bea6067730f | 219 | } |
erigow01 | 0:9bea6067730f | 220 | } |
erigow01 | 0:9bea6067730f | 221 | } |
erigow01 | 0:9bea6067730f | 222 | |
erigow01 | 0:9bea6067730f | 223 | //Initialise notification data... |
erigow01 | 0:9bea6067730f | 224 | void initNotificationData() { |
erigow01 | 0:9bea6067730f | 225 | //For debug purposes... |
erigow01 | 0:9bea6067730f | 226 | notifications[0].type = NOTIFICATION_TYPE_CALL; |
erigow01 | 0:9bea6067730f | 227 | notifications[0].state = NOTIFICATION_STATE_UNREAD; |
erigow01 | 0:9bea6067730f | 228 | strcpy(notifications[0].primary_text,"Eric Gowland"); |
erigow01 | 0:9bea6067730f | 229 | strcpy(notifications[0].secondary_text, "07770909177"); |
erigow01 | 0:9bea6067730f | 230 | |
erigow01 | 0:9bea6067730f | 231 | notifications[1].type = NOTIFICATION_TYPE_EMAIL; |
erigow01 | 0:9bea6067730f | 232 | notifications[1].state = NOTIFICATION_STATE_UNREAD; |
erigow01 | 0:9bea6067730f | 233 | strcpy(notifications[1].primary_text, "Dear Sir I have $US"); |
erigow01 | 0:9bea6067730f | 234 | strcpy(notifications[1].secondary_text, "not@scam.net"); |
erigow01 | 0:9bea6067730f | 235 | |
erigow01 | 0:9bea6067730f | 236 | //Set location, etc. |
erigow01 | 0:9bea6067730f | 237 | visible_notification_index = 0; |
erigow01 | 0:9bea6067730f | 238 | checkUnread(); |
erigow01 | 0:9bea6067730f | 239 | requestDisplayUpdate(); |
erigow01 | 0:9bea6067730f | 240 | startBuzz(); |
erigow01 | 0:9bea6067730f | 241 | } |
erigow01 | 0:9bea6067730f | 242 | |
erigow01 | 0:9bea6067730f | 243 | //Main Program Function |
erigow01 | 0:9bea6067730f | 244 | int main() { |
erigow01 | 0:9bea6067730f | 245 | //Init Data |
erigow01 | 0:9bea6067730f | 246 | initNotificationData(); |
erigow01 | 0:9bea6067730f | 247 | //Attach interrupt handlers... |
erigow01 | 0:9bea6067730f | 248 | buttonOne.attach(buttonOnePressed, IRQ_RISE, BUTTON_INTERRUPT_DEBOUNCE_TIME_MS); |
erigow01 | 0:9bea6067730f | 249 | buttonTwo.attach(buttonTwoPressed, IRQ_RISE, BUTTON_INTERRUPT_DEBOUNCE_TIME_MS); |
erigow01 | 0:9bea6067730f | 250 | //Request display update... |
erigow01 | 0:9bea6067730f | 251 | requestDisplayUpdate(); |
erigow01 | 0:9bea6067730f | 252 | while(1) { |
erigow01 | 0:9bea6067730f | 253 | if(needs_display_update){ |
erigow01 | 0:9bea6067730f | 254 | doDisplayUpdate(); |
erigow01 | 0:9bea6067730f | 255 | } else { |
erigow01 | 0:9bea6067730f | 256 | //If state hasn't changed, just action timers... |
erigow01 | 0:9bea6067730f | 257 | //Unread LED |
erigow01 | 0:9bea6067730f | 258 | if(has_unread) { |
erigow01 | 0:9bea6067730f | 259 | //LED flashing... |
erigow01 | 0:9bea6067730f | 260 | if(led_timer.read_ms() > LED_BLINK_TIME_MS) { |
erigow01 | 0:9bea6067730f | 261 | myled = !myled; |
erigow01 | 0:9bea6067730f | 262 | led_timer.reset(); |
erigow01 | 0:9bea6067730f | 263 | } |
erigow01 | 0:9bea6067730f | 264 | } else { |
erigow01 | 0:9bea6067730f | 265 | myled = 0; |
erigow01 | 0:9bea6067730f | 266 | led_timer.stop(); |
erigow01 | 0:9bea6067730f | 267 | } |
erigow01 | 0:9bea6067730f | 268 | |
erigow01 | 0:9bea6067730f | 269 | //Buzz |
erigow01 | 0:9bea6067730f | 270 | if(buzz_count < MAX_BUZZ_COUNT) { |
erigow01 | 0:9bea6067730f | 271 | //Buzzing... |
erigow01 | 0:9bea6067730f | 272 | if(buzz_timer.read_ms() > BUZZ_TIME_MS) { |
erigow01 | 0:9bea6067730f | 273 | motor = !motor; |
erigow01 | 0:9bea6067730f | 274 | if(!motor) buzz_count++; |
erigow01 | 0:9bea6067730f | 275 | buzz_timer.reset(); |
erigow01 | 0:9bea6067730f | 276 | } |
erigow01 | 0:9bea6067730f | 277 | } else { |
erigow01 | 0:9bea6067730f | 278 | motor = 0; |
erigow01 | 0:9bea6067730f | 279 | buzz_timer.stop(); |
erigow01 | 0:9bea6067730f | 280 | } |
erigow01 | 0:9bea6067730f | 281 | } |
erigow01 | 0:9bea6067730f | 282 | } |
erigow01 | 0:9bea6067730f | 283 | } |