Add calendar display command "cal"
authorNils Faerber <nils.faerber@kernelconcepts.de>
Fri, 16 Sep 2011 16:39:41 +0000 (18:39 +0200)
committerNils Faerber <nils.faerber@kernelconcepts.de>
Fri, 16 Sep 2011 16:40:00 +0000 (18:40 +0200)
mw_main.c

index a5c1083..406e09c 100644 (file)
--- a/mw_main.c
+++ b/mw_main.c
@@ -233,6 +233,122 @@ void test_notification(mwdevice_t *mwdevice)
        mw_set_vibrate_mode(mwdevice, 1, 300, 300, 3);
 }
 
+int days(int m1, int y1)
+{
+       int d;
+       if(m1==1 || m1==3 || m1==5 || m1==7 || m1==8 || m1==10 || m1==12)
+               d=31;
+       else if(m1==4 || m1==6 || m1==9 || m1==11)
+               d=30;
+       else if((y1%100!=0 && y1%4==0) || y1%400==0)
+               d=29;
+       else
+               d=28;
+       return d;
+}
+
+void draw_idle_calendar(mwdevice_t *mwdevice)
+{
+       long unsigned int t;
+       unsigned int y,y1,m,m1,d,da,i,j,k;
+       const char a[12][20]={"January","February","March","April","May","June","July","August","September","October","November","December"};
+       const char dnames[7][3] = { "Mo", "Di", "Mi", "Do", "Fr", "Sa", "So" };
+       mw_buffer *mwbuf;
+       unsigned char *bbuf;
+       int len, px, py;
+       char pnum[32];
+       int today;
+       time_t mtime;
+       struct tm mtm;
+                               
+       mtime = time(NULL);
+       localtime_r(&mtime, &mtm);
+
+       today = mtm.tm_mday;
+       y = mtm.tm_year + 1900;
+       m = mtm.tm_mon + 1;
+
+       mwbuf = mw_alloc_pbuffer(96, 66, 1);
+       mw_buf_clear(mwbuf, MW_BLACK);
+
+       if (y < 0)
+               y = -y;
+       if (m <= 0 || m >= 13)
+               m = 1;
+       y1 = 0;
+       t = 0;
+       while (y1 < y) {
+               if (((y1 % 100) != 0 && (y1 % 4) == 0) || (y1 % 400) == 0)
+                       t += 366;
+               else
+                       t += 365;
+               y1++;
+       }
+       m1 = 1;
+       while (m1 < m) {
+               d = days(m1, y);
+               t = t + d;
+               m1++;
+       };
+#if 0
+       printf("Year: '%u'\n",y);
+       printf("Month: '%s'\n",a[m-1]);
+
+       printf("%6s%6s%6s%6s%6s%6s%6s\n","Sun","Mon","Tue","Wed","Thu","Fri","Sat");
+#endif
+
+       k = 1;
+       /* 0 = 1st day is Monday, 1 = 1st day is Sunday */
+       d = (t % 7)-1;
+       px=0;
+       py=3;
+       for (i=0; i<7; i++) {
+               snprintf(pnum, 3, dnames[i]);
+               mw_buf_print(mwbuf, px, py, pnum, 0, MW_WHITE, MW_BLACK);
+               px += 14;
+       };
+       px = 0;
+       py += 10;
+       for (i = 1; i <= days(m,y); i++, k++) {
+               if (i == 1) {
+                       if (d == 0) {
+                               for (j = 1; j < 7; j++, k++) {
+                                       printf("+--"); //printf("%3s","");
+                                       px += 14;
+                               }
+                       } else {
+                               for (j = 1; j < d; j++, k++) {
+                                       px += 14;
+                                       printf("--+"); //printf("%3s","");
+                               }
+                       }
+               }
+               snprintf(pnum, 8, "%2d", i);
+               if (i == today)
+                       mw_buf_print(mwbuf, px, py, pnum, 0, MW_BLACK, MW_WHITE);
+               else
+                       mw_buf_print(mwbuf, px, py, pnum, 0, MW_WHITE, MW_BLACK);
+               px += 12;
+               printf("%2d", i);
+               if ((k % 7) == 0) {
+                       printf("\n");
+                       px = 0;
+                       py += 10;
+               } else {
+                       printf("+");
+                       px += 2;
+               }
+       }
+       // printf("\n");
+
+       //mw_buf_print(mwbuf, 1, 10, "Font 0", 0, MW_WHITE, MW_BLACK);
+
+       bbuf = mw_make_mw_buffer(mwbuf, &len);
+       mw_send_bitmap(mwdevice, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
+       mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1);
+       mw_free_pbuffer(mwbuf);
+}
+
 void mw_send_notify(mwdevice_t *mwdevice, char *header, char *body)
 {
        mw_buffer *mwbuf;
@@ -343,7 +459,7 @@ void process_cmd(char *cmdline, int clinep, mwdata_t *mwdata)
        }
        if (strncmp(cmdline, "srtc", 4) == 0) {
                fprintf(stderr, "Setting RTC from system time...");
-               mw_set_rtc(&mwdata->mwdevice, MW_RTC_CLOCK_12HR, MW_RTC_DATE_MMDD);
+               mw_set_rtc(&mwdata->mwdevice, MW_RTC_CLOCK_24HR, MW_RTC_DATE_DDMM);
                fprintf(stderr, "OK\n");
        }
        if (strncmp(cmdline, "grtc", 4) == 0) {
@@ -416,6 +532,9 @@ void process_cmd(char *cmdline, int clinep, mwdata_t *mwdata)
                //mw_write_oled_buffer(mwdevice, 0, 80, 0, mdata, 10);
                //mw_send_frame(mwdevice, MW_UPDATE_OLED_DISPLAY_MSG, 0, NULL, 0);
        }
+       if (strncmp(cmdline, "cal", 3) == 0) {
+               draw_idle_calendar(&mwdata->mwdevice);
+       }
 }