test program for 4.2inch e-Paper

Dependencies:   EPD_4R2

/media/uploads/kenjiArai/epaper4r2.jpg

Revision:
0:2b41523348df
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/set_RTC.cpp	Wed Aug 28 23:37:43 2019 +0000
@@ -0,0 +1,157 @@
+/*
+ * Set time into RTC
+ *
+ * Copyright (c) 2019 Kenji Arai / JH1PJL
+ *  http://www.page.sannet.ne.jp/kenjia/index.html
+ *  http://mbed.org/users/kenjiArai/
+ *      Created:    April     27th, 2019
+ *      Revised:    August    23rd, 2019
+ */
+
+#include "mbed.h"
+
+extern Serial pc;
+
+static void chk_and_set_time(char *ptr);
+static int32_t  xatoi (char **str, int32_t *res);
+static void get_line (char *buff, int len);
+
+void time_enter_mode(void)
+{
+    char *ptr;
+    char linebuf[64];
+
+    pc.printf("\r\nSet time into RTC\r\n");
+    pc.printf(" e.g. >19 8 23 10 11 12 -> August 23rd,'19, 10:11:12\r\n");
+    pc.printf(" If time is fine, just hit enter\r\n");
+    pc.putc('>');
+    ptr = linebuf;
+    get_line(ptr, sizeof(linebuf));
+    pc.printf("\r");
+    chk_and_set_time(ptr);
+}
+
+static void get_line (char *buff, int len)
+{
+    char c;
+    uint32_t idx = 0;
+
+    while(true) {
+        c = pc.getc();
+        if (c == '\r') {
+            buff[idx++] = c;
+            break;
+        }
+        if ((c == '\b') && idx) {
+            idx--;
+            pc.putc(c);
+            pc.putc(' ');
+            pc.putc(c);
+        }
+        if (((uint8_t)c >= ' ') && (idx < len - 1)) {
+            buff[idx++] = c;
+            pc.putc(c);
+        }
+    }
+    buff[idx] = 0;
+    pc.puts("\r\n");
+}
+
+static void chk_and_set_time(char *ptr)
+{
+    int32_t p1;
+    struct tm t;
+    time_t seconds;
+
+    if (xatoi(&ptr, &p1)) {
+        t.tm_year       = (uint8_t)p1 + 100;
+        pc.printf("Year:%d ",p1);
+        xatoi( &ptr, &p1 );
+        t.tm_mon        = (uint8_t)p1 - 1;
+        pc.printf("Month:%d ",p1);
+        xatoi( &ptr, &p1 );
+        t.tm_mday       = (uint8_t)p1;
+        pc.printf("Day:%d ",p1);
+        xatoi( &ptr, &p1 );
+        t.tm_hour       = (uint8_t)p1;
+        pc.printf("Hour:%d ",p1);
+        xatoi( &ptr, &p1 );
+        t.tm_min        = (uint8_t)p1;
+        pc.printf("Min:%d ",p1);
+        xatoi( &ptr, &p1 );
+        t.tm_sec        = (uint8_t)p1;
+        pc.printf("Sec: %d \r\n",p1);
+    } else {
+        return;
+    }
+    seconds = mktime(&t);
+    set_time(seconds);
+    pc.printf(
+        "Date: %04d/%02d/%02d, %02d:%02d:%02d\r\n",
+        t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec
+    );
+}
+
+static int32_t xatoi (char **str, int32_t *res)
+{
+    int32_t val;
+    uint8_t c, radix, s = 0;
+
+    while ((c = **str) == ' ') {
+        (*str)++;
+    }
+    if (c == '-') {
+        s = 1;
+        c = *(++(*str));
+    }
+    if (c == '0') {
+        c = *(++(*str));
+        if (c <= ' ') {
+            *res = 0;
+            return 1;
+        }
+        if (c == 'x') {
+            radix = 16;
+            c = *(++(*str));
+        } else {
+            if (c == 'b') {
+                radix = 2;
+                c = *(++(*str));
+            } else {
+                if ((c >= '0')&&(c <= '9')) {
+                    radix = 8;
+                } else {
+                    return 0;
+                }
+            }
+        }
+    } else {
+        if ((c < '1')||(c > '9')) {
+            return 0;
+        }
+        radix = 10;
+    }
+    val = 0;
+    while (c > ' ') {
+        if (c >= 'a') {
+            c -= 0x20;
+        }
+        c -= '0';
+        if (c >= 17) {
+            c -= 7;
+            if (c <= 9) {
+                return 0;
+            }
+        }
+        if (c >= radix) {
+            return 0;
+        }
+        val = val * radix + c;
+        c = *(++(*str));
+    }
+    if (s) {
+        val = -val;
+    }
+    *res = val;
+    return 1;
+}