Demo Team / Renard_UI_Only

Dependencies:   DebouncedInterrupt SharpLCD_LucidaFont mbed-src

Fork of Renard_UI_Only by Eric Gowland

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "DebouncedInterrupt.h"
00003 #include "font.h"
00004 #include "SharpLCD.hpp"
00005 #include "icon.h"
00006 
00007 //I/O Initialisation
00008 DigitalOut myled(LED1);
00009 DigitalOut motor(P0_23);
00010 #define BUTTON_INTERRUPT_DEBOUNCE_TIME_MS 150
00011 DebouncedInterrupt buttonOne (P0_16);
00012 DebouncedInterrupt buttonTwo (P0_17);
00013 
00014 SharpLCD lcd(P0_0, P0_24, P0_20, P0_22, P0_25, P0_27);
00015 
00016 uint8_t framebuffer[SharpLCD::SIZEOF_FRAMEBUFFER_FOR_ALLOC];
00017 SharpLCD::FrameBuffer fb(framebuffer);
00018 
00019 //Notification Data struct
00020 #define NOTIFICATION_TEXT_MAX_LENGTH 20
00021 #define NOTIFICATION_TYPE_ALARM    0x01
00022 #define NOTIFICATION_TYPE_CALL     0x02
00023 #define NOTIFICATION_TYPE_SMS      0x03
00024 #define NOTIFICATION_TYPE_EMAIL    0x04
00025 #define NOTIFICATION_TYPE_EVENT    0x05
00026 #define NOTIFICATION_STATE_DELETED 0x00
00027 #define NOTIFICATION_STATE_UNREAD  0x01
00028 #define NOTIFICATION_STATE_READ    0x02
00029 struct Notification{
00030     uint8_t type;
00031     uint8_t state;
00032     char primary_text[NOTIFICATION_TEXT_MAX_LENGTH];
00033     char secondary_text[NOTIFICATION_TEXT_MAX_LENGTH];
00034     };
00035 
00036 //notification storage
00037 #define MAX_NOTIFICATIONS 20
00038 Notification notifications[MAX_NOTIFICATIONS];
00039 uint8_t notification_count = 0;
00040 int visible_notification_index = -1;
00041 
00042 //States
00043 uint8_t needs_display_update = 0;
00044 uint8_t has_unread = 0;
00045 
00046 //For LED
00047 #define LED_BLINK_TIME_MS 500
00048 uint8_t led_on = 0;
00049 Timer led_timer;
00050 
00051 //For Buzzer
00052 #define BUZZ_TIME_MS 200
00053 #define MAX_BUZZ_COUNT 3
00054 uint8_t buzz_count = MAX_BUZZ_COUNT;
00055 Timer buzz_timer;
00056 
00057 void setType(Notification* note, uint8_t type) {
00058     note->type = type;
00059 }
00060 
00061 uint8_t isCall(Notification note) {
00062     return note.type == NOTIFICATION_TYPE_CALL;
00063 }
00064 
00065 uint8_t isSMS(Notification note) {
00066     return note.type == NOTIFICATION_TYPE_SMS;
00067 }
00068 
00069 uint8_t isEmail(Notification note) {
00070     return note.type == NOTIFICATION_TYPE_EMAIL;
00071 }
00072 
00073 uint8_t isEvent(Notification note) {
00074     return note.type == NOTIFICATION_TYPE_EVENT;
00075 }
00076 
00077 void setState(Notification* note, uint8_t state) {
00078     note->state = state;
00079 }
00080 
00081 uint8_t isDeleted(Notification note) {
00082     return note.state == NOTIFICATION_STATE_DELETED;
00083 }
00084 
00085 uint8_t isUnread(Notification note) {
00086     return note.state == NOTIFICATION_STATE_UNREAD;
00087 }
00088 
00089 uint8_t isRead(Notification note) {
00090     return note.state == NOTIFICATION_STATE_READ;
00091 }
00092 
00093 //Layout Coordinates
00094 #define TYPE_ICON_X         10 //5
00095 #define TYPE_ICON_Y         60 //10
00096 #define UNREAD_ICON_X       60 //96
00097 #define UNREAD_ICON_Y       60 //14
00098 #define PRIMARY_TEXT_X      10 //5
00099 #define PRIMARY_TEXT_Y      30 //64
00100 #define SECONDARY_TEXT_X    10 //5
00101 #define SECONDARY_TEXT_Y    50 //82
00102 //Update Display to show current notification...
00103 
00104 //Serial  pc(USBTX, USBRX);
00105 
00106 void doDisplayUpdate() {
00107     //pc.printf("display update"); //Debug
00108     lcd.clear();
00109     if(visible_notification_index >= 0) {
00110         //Write current notification...        
00111         //Draw type icon...
00112         switch(notifications[visible_notification_index].type) {
00113             case NOTIFICATION_TYPE_ALARM:
00114                 fb.bitBlit(Alarm, 32, 32, TYPE_ICON_X, TYPE_ICON_Y);
00115                 break;
00116                 
00117             case NOTIFICATION_TYPE_CALL:
00118                 fb.bitBlit(Call, 32, 32, TYPE_ICON_X, TYPE_ICON_Y);
00119                 break;
00120                 
00121             case NOTIFICATION_TYPE_SMS:
00122                 fb.bitBlit(SMS, 32, 32, TYPE_ICON_X, TYPE_ICON_Y);
00123                 break;
00124                 
00125             case NOTIFICATION_TYPE_EMAIL:
00126                 fb.bitBlit(Email, 32, 32, TYPE_ICON_X, TYPE_ICON_Y);
00127                 break;
00128                 
00129             case NOTIFICATION_TYPE_EVENT:
00130                 fb.bitBlit(Calendar, 32, 32, TYPE_ICON_X, TYPE_ICON_Y);
00131                 break;
00132         }
00133                 
00134         //Unread notification
00135         if(isUnread(notifications[visible_notification_index])){
00136             fb.bitBlit(Unread, 16, 24, UNREAD_ICON_X, UNREAD_ICON_Y);
00137         }
00138         //Text fields...
00139         fb.printString(lookupFontFace("Lucida 8pt", 8), PRIMARY_TEXT_X, PRIMARY_TEXT_Y, BLACK, notifications[visible_notification_index].primary_text);
00140         fb.printString(lookupFontFace("Lucida 8pt", 8), SECONDARY_TEXT_X, SECONDARY_TEXT_Y, BLACK, notifications[visible_notification_index].secondary_text);
00141     } else {
00142         //no notifications...
00143         //Write current notification...
00144         //fb.printString(lookupFontFace("Lucida 8pt", 8), PRIMARY_TEXT_X, PRIMARY_TEXT_Y, BLACK, "No Notifications");
00145     }
00146     //epaper.write_disp();
00147     lcd.drawFrameBuffer(fb);
00148     needs_display_update = 0;
00149 }
00150 
00151 //Request a display update..
00152 void requestDisplayUpdate() {
00153     needs_display_update = 1;
00154 }
00155 
00156 //Starts buzz pattern...
00157 void startBuzz() {
00158     //Buzz
00159     buzz_count = 0;
00160     buzz_timer.reset();
00161     buzz_timer.start();
00162 }
00163 
00164 
00165 //Add Notification...
00166 int addNotification(Notification note) {
00167     //Find insertion point...
00168     uint8_t index = 0;
00169     for(index = 0; index < MAX_NOTIFICATIONS; index++) {
00170         if(isDeleted(notifications[index])) {
00171             //Here...
00172             break;
00173         }
00174     }
00175     //If here, didn't find insertion point... wrap to beginning.
00176     if(index >= MAX_NOTIFICATIONS) index = 0;
00177     notifications[index] = note;
00178     //Set buzzer
00179     startBuzz();
00180     //Set unread
00181     if(isUnread(note)) has_unread = 1;
00182     return index;
00183 }
00184 
00185 void deleteNotification(int index) {
00186     setState(&notifications[index], NOTIFICATION_STATE_DELETED);
00187     //Shift array elements left...
00188     int i = 0;
00189     for (i = index + 1; i < MAX_NOTIFICATIONS; i++) {
00190         notifications[i - 1] = notifications[i];
00191     }
00192 }
00193 
00194 void checkUnread(){
00195     uint8_t i = 0;
00196     led_timer.stop();
00197     has_unread = 0;
00198     for(i = 0; i < MAX_NOTIFICATIONS; i++) {
00199         if(isUnread(notifications[i])) {
00200             has_unread = 1;
00201             led_timer.reset();
00202             led_timer.start();
00203             break;
00204         }
00205     }
00206 }
00207 
00208 //Button One Handler
00209 void buttonOnePressed(){
00210     if(visible_notification_index >= 0) {
00211         //Increment index, wrap to beginning if last.
00212         visible_notification_index++;
00213         if(visible_notification_index >= MAX_NOTIFICATIONS || isDeleted(notifications[visible_notification_index])) {
00214             visible_notification_index = 0;
00215             if(isDeleted(notifications[visible_notification_index])) {
00216                 //Still deleted... none...
00217                 visible_notification_index = -1;
00218             }
00219         }
00220         //Trigger display update...
00221         requestDisplayUpdate();
00222     }
00223 }
00224 
00225 //Button Two handler
00226 void buttonTwoPressed(){
00227     if(visible_notification_index >= 0) {
00228         if(!isDeleted(notifications[visible_notification_index])) {
00229             //Exists.
00230             if(isUnread(notifications[visible_notification_index])) {
00231                 //Toggle to 'read'
00232                 setState(&notifications[visible_notification_index], NOTIFICATION_STATE_READ);
00233                 checkUnread();
00234             } else if (isRead(notifications[visible_notification_index])) {
00235                 //Already 'read'... delete, this also shifts remaining notifications down...
00236                 deleteNotification(visible_notification_index);
00237                 //If current is deleted...
00238                 if(isDeleted(notifications[visible_notification_index])) {
00239                     //We're at end, so wrap...
00240                     visible_notification_index = 0;
00241                     if(isDeleted(notifications[visible_notification_index])) {
00242                         //Still deleted... so there are none...
00243                         visible_notification_index = -1;
00244                     }
00245                 }
00246                 //Otherwise, we've got one, so we should be ok...
00247             }
00248             
00249             //Trigger display update...
00250             requestDisplayUpdate();
00251         }
00252     }
00253 }
00254 
00255 //Initialise notification data...
00256 void initNotificationData() {
00257     //For debug purposes...
00258     
00259     notifications[0].type = NOTIFICATION_TYPE_ALARM;
00260     notifications[0].state = NOTIFICATION_STATE_UNREAD;
00261     //strcpy(notifications[0].primary_text,"Wake Up");
00262     //strcpy(notifications[0].secondary_text, "07:00");
00263     strcpy(notifications[0].primary_text,"Test");
00264     strcpy(notifications[0].secondary_text, "1");
00265     
00266     notifications[1].type = NOTIFICATION_TYPE_CALL;
00267     notifications[1].state = NOTIFICATION_STATE_UNREAD;
00268     //strcpy(notifications[1].primary_text,"Eric Gowland");
00269     //strcpy(notifications[1].secondary_text, "07770909177");
00270     strcpy(notifications[1].primary_text,"Test");
00271     strcpy(notifications[1].secondary_text, "2");
00272     
00273     notifications[2].type = NOTIFICATION_TYPE_SMS;
00274     notifications[2].state = NOTIFICATION_STATE_UNREAD;
00275     //strcpy(notifications[2].primary_text,"Hi, txt me...");
00276     //strcpy(notifications[2].secondary_text, "07770909177");
00277     strcpy(notifications[2].primary_text,"Test");
00278     strcpy(notifications[2].secondary_text, "3");
00279     
00280     notifications[3].type = NOTIFICATION_TYPE_EMAIL;
00281     notifications[3].state = NOTIFICATION_STATE_UNREAD;
00282     //strcpy(notifications[3].primary_text, "Dear Sir I have $US");
00283     //strcpy(notifications[3].secondary_text, "not@scam.net");
00284     strcpy(notifications[3].primary_text, "Test");
00285     strcpy(notifications[3].secondary_text, "4");
00286     
00287     notifications[4].type = NOTIFICATION_TYPE_EVENT;
00288     notifications[4].state = NOTIFICATION_STATE_UNREAD;
00289     //strcpy(notifications[4].primary_text,"Review Meeting");
00290     //strcpy(notifications[4].secondary_text, "10:00 - 10:30");
00291     strcpy(notifications[4].primary_text,"Test");
00292     strcpy(notifications[4].secondary_text, "5");
00293     
00294     //Set location, etc.
00295     visible_notification_index = 0;
00296     checkUnread();
00297     requestDisplayUpdate();
00298     startBuzz();
00299 }
00300 
00301 //Main Program Function
00302 int main() {
00303     lcd.enableDisplay();
00304     //Init Data
00305     initNotificationData();
00306     //Attach interrupt handlers...
00307     buttonOne.attach(buttonOnePressed, IRQ_RISE, BUTTON_INTERRUPT_DEBOUNCE_TIME_MS);
00308     buttonTwo.attach(buttonTwoPressed, IRQ_RISE, BUTTON_INTERRUPT_DEBOUNCE_TIME_MS);
00309     //Request display update...
00310     requestDisplayUpdate();
00311     while(1) {
00312         if(needs_display_update){
00313             doDisplayUpdate();
00314         } else {
00315             //If state hasn't changed, just action timers...
00316             //Unread LED
00317             if(has_unread) {
00318                 //LED flashing...
00319                 if(led_timer.read_ms() > LED_BLINK_TIME_MS) {
00320                     myled = !myled;
00321                     led_timer.reset();
00322                 }
00323             } else {
00324                 myled = 0;
00325                 led_timer.stop();
00326             }
00327             
00328             //Buzz
00329             if(buzz_count < MAX_BUZZ_COUNT) {
00330                 //Buzzing...
00331                 if(buzz_timer.read_ms() > BUZZ_TIME_MS) {
00332                     motor = !motor;
00333                     if(!motor) buzz_count++;
00334                     buzz_timer.reset();
00335                 }
00336             } else {
00337                 motor = 0;
00338                 buzz_timer.stop();
00339             }
00340         }
00341     }
00342 }