Yoshiyuki Uehara / Mbed 2 deprecated Maple

Dependencies:   mbed

Revision:
1:aefa1992ce0f
Parent:
0:0be38b583cf7
Child:
3:eec13a411e94
--- a/Maple.cpp	Sun Sep 25 11:37:21 2011 +0000
+++ b/Maple.cpp	Mon Oct 10 11:45:51 2011 +0000
@@ -1,99 +1,98 @@
+//copyright 2011 Uehara Yoshiyuki
+//====================================================================
+//The author provide the programs without any guarantees or warranty.
+//The author is not responsible for any damage or losses of any kind 
+//caused by using or misusing of the programs.
+//The author is under no obligation to provide support, service, 
+//corrections, or upgrades to the programs.
+//====================================================================
 // MAPLE board[MARM01-BASE]
 // common functions
-//
 #include "Maple_RTC.h"
 #include "Maple_I2C.h"
 #include "Maple_LCD.h"
 #include "Maple.h"
 #include "mbed.h"
 
-// char to hexadecimal (4 bits to 1 hexadecimal digit)
-char char_to_hex1(int c) {
-    const char hex_code[] = "0123456789abcdef";
-
-    return hex_code[c];
+// string copy
+char* copy_string(char destination_string[], int destination_position, int copy_length, const char source_string[]) {
+    for(int i = 0; i < copy_length; ++i) {
+        destination_string[destination_position + i] = source_string[i];
+    }
+    return destination_string;
 }
 
-// char to hexadecimal (1 byte, 2 hexadecimal digits)
-void char_to_hex(int c, char h[]) {
-    h[0] = char_to_hex1(c >> 4);
-    h[1] = char_to_hex1(c & 0x0f);
-    h[2] = '\0';
+// integer(4 bits) to hexadecimal character(1 digit)
+char int_to_hex1(int i) {
+    const char hex[] = "0123456789abcdef";
+
+    return hex[i & 0x0f];
 }
 
-// BCD to integer
+// integer(1 byte) to hexadecimal character(2 digits)
+char* int_to_hex2(int i, char h[]) {
+    h[0] = int_to_hex1(i >> 4);
+    h[1] = int_to_hex1(i);
+    h[2] = '\0';
+    return h;
+}
+
+// BCD(1 byte) to integer
 int bcd_to_int(char b) {
     return ((b >> 4) * 10) + (b & 0x0f);
 }
 
-// integer to BCD
+// integer to BCD(1 byte)
 char int_to_bcd(int i) {
     return ((i / 10) << 4) + (i % 10);
 }
 
-// increment BCD
-//   (min, max+1)
+// decrement/increment BCD
+//   flag 0:decrement, 1:increment
+//    min,  max
 //   0x00, 0x99 .. 00-99: year  0x00
 //   0x01, 0x12 .. 01-12: month
 //   0x01, max  .. 01-max: day
 //   0x00, 0x23 .. 00-23: hour
 //   0x00, 0x59 .. 00-59: minute, second
-char increment_bcd(char bcd_data, char bcd_min, char bcd_max) {
-    if((bcd_data & 0x0f) == 0x09) {
-        bcd_data += 0x07;   // + 0x10 - 0x0a + 0x01
+char xxcrement_bcd(int flag, char bcd_data, char bcd_min, char bcd_max) {
+    if(flag == 0) {
+        if(bcd_data < (bcd_min + 0x01)) {
+            bcd_data = bcd_max;
+        }        
+        else if((bcd_data & 0x0f) == 0x00) {
+            bcd_data -= 0x07;   // - 0x10 + 0x0a - 0x01;
+        }
+        else {
+            bcd_data -= 0x01;
+        }
+        return bcd_data;
     }
     else {
-        bcd_data += 0x01;
+        if((bcd_data & 0x0f) == 0x09) {
+            bcd_data += 0x07;   // + 0x10 - 0x0a + 0x01
+        }
+        else {
+            bcd_data += 0x01;
+        }
+        if(bcd_data > bcd_max) {
+            bcd_data = bcd_min;
+        }
+        return bcd_data;
     }
-    if(bcd_data > bcd_max) {
-        bcd_data = bcd_min;
-    }
-    return bcd_data;
 }
 
-// decrement bcd
-//   (min, max+1)
-//   0x00, 0x99 .. 00-99: year  0x00
-//   0x01, 0x12 .. 01-12: month
-//   0x01, max  .. 01-max: day
-//   0x00, 0x23 .. 00-23: hour
-//   0x00, 0x59 .. 00-59: minute, second
-char decrement_bcd(char bcd_data, char bcd_min, char bcd_max) {
-    if(bcd_data < (bcd_min + 0x01)) {
-        bcd_data = bcd_max;
-    }        
-    else if((bcd_data & 0x0f) == 0x00) {
-        bcd_data -= 0x07;   // - 0x10 + 0x0a - 0x01;
-    }
-    else {
-        bcd_data -= 0x01;
-    }
-    return bcd_data;
-}
+// weekday to string(3 characters)
+//   0(SUN) .. 6(SAT)
+const char* weekday_to_string(int w, const char* s) {
+    const char* weekday_string[7] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
 
-// weekday string(2 characters)
-//  weekday: 0(SU) .. 6(SA)
-void int_to_weekday2(int weekday, char s[]) {
-    const char weekday_string2[7][3] = {"SU", "MO", "TU", "WE", "TH", "FR", "SA"};
-
-    s[0] = weekday_string2[weekday][0];
-    s[1] = weekday_string2[weekday][1];
-    s[2] = '\0';
-}
-
-// weekday string(3 characters)
-//  weekday: 0(SUN) .. 6(SAT)
-void int_to_weekday3(int weekday, char s[]) {
-    const char weekday_string3[7][4] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
-
-    s[0] = weekday_string3[weekday][0];
-    s[1] = weekday_string3[weekday][1];
-    s[2] = weekday_string3[weekday][2];
-    s[3] = '\0';
+    s = weekday_string[w];
+    return s;
 }
 
 // year in 2cyy format
-//   c: century .. 0x00/0x01
+//   bcd_century .. 0x00/0x01
 int bcd_to_year(char century, char bcd_year) {
     return 2000 + (century * 100) + bcd_to_int(bcd_year);
 }
@@ -109,7 +108,7 @@
     const int days_leap[13]   = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
     const int days_common[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
 
-    if (leap_year(year)) {
+    if(leap_year(year)) {
         return days_leap[month];
     }
     else {
@@ -117,9 +116,9 @@
     }
 }
 
-// (days in a month) + 1 in BCD
-char bcd_days_in_month(char century, char bcd_year, char bcd_month) {
-    return int_to_bcd(days_in_month(bcd_to_year(century, bcd_year), bcd_to_int(bcd_month)));
+// days(BCD  in a month in BCD
+char bcd_days_in_month(char bcd_century, char bcd_year, char bcd_month) {
+    return int_to_bcd(days_in_month(bcd_to_year(bcd_century, bcd_year), bcd_to_int(bcd_month)));
 }
 
 // Zeller's congruence for Calendario gregoriano
@@ -128,14 +127,14 @@
     int century, year, month, day;
 
     century = 20 + bcd_century;
-    year  = bcd_to_int(bcd_year);
-    month = bcd_to_int(bcd_month);
-    day   = bcd_to_int(bcd_day);
+    year    = bcd_to_int(bcd_year);
+    month   = bcd_to_int(bcd_month);
+    day     = bcd_to_int(bcd_day);
     return (day + (((month + 1) * 26 ) / 10) + year + year / 4 + century / 4 - century * 2 + 6) % 7;
 }
 
-// test test test test test test test test
-// test: RTC read and raw print to LCD
+// test
+// RTC read and raw print to LCD
 void RTC_to_LCD_raw() {
     char d[17];
 
@@ -150,8 +149,8 @@
     }
 }
 
-// test 2 of LCD
-//   display 64 characters in 1st/2nd row and shift 
+// test
+// LCD display 64 characters in 1st/2nd row and shift 
 //
 void LCD_display_all_char_shift() {
         LCD_display_32char_shift(0x00);
@@ -161,32 +160,30 @@
 }
 
 static void LCD_display_32char_shift(char base) {
-    int i;
-    
-    LCD_clear_display();                             // select 1st row and clear display
-    for(i = 0; i < 32; ++i) {
-        LCD_print_char(base + i);     // write 32 characters
+    LCD_clear_display();                    // select 1st row and clear display
+    for(int i = 0; i < 32; ++i) {
+        LCD_print_char(base + i);           // write 32 characters
     }
-    LCD_locate(1, 0);                                // select 2nd row
-    for(i = 32; i < 64; ++i) {
-        LCD_print_char(base + i);     // write 32 characters
+    LCD_locate(1, 0);                       // select 2nd row
+    for(int i = 32; i < 64; ++i) {
+        LCD_print_char(base + i);           // write 32 characters
     }
-    for(i = 0; i < 24; ++i) {
-        LCD_cursor_or_display_shift(1, 0);           // shift right 24 times
+    for(int i = 0; i < 24; ++i) {
+        LCD_cursor_or_display_shift(1, 0);  // shift right 24 times
         wait_ms(1000);
     }
-    for(i = 0; i < 32; ++i) {
-        LCD_cursor_or_display_shift(1, 1);           // shift left 32 times
+    for(int i = 0; i < 32; ++i) {
+        LCD_cursor_or_display_shift(1, 1);  // shift left 32 times
         wait_ms(1000);
     }
-    for(i = 0; i < 8; ++i) {
-        LCD_cursor_or_display_shift(1, 0);           // shift right 8 times
+    for(int i = 0; i < 8; ++i) {
+        LCD_cursor_or_display_shift(1, 0);  // shift right 8 times
         wait_ms(1000);
     }
 }
 
-// test 1 of LCD
-//   display 16x2 characters with 1st and 2nd row 
+// test
+// LCD display 16x2 characters with 1st and 2nd row 
 void LCD_display_all_char() { 
     LCD_display_32char(0x00);
     LCD_display_32char(0x20);
@@ -199,15 +196,13 @@
 }
 
 static void LCD_display_32char(char base) {
-    int i;
-
-    LCD_clear_display();                             // select 1st row and clear display
-    for(i = 0; i < 16; ++i) {
-        LCD_print_char(base + i);     // write 1st-half 16 characters
+    LCD_clear_display();            // select 1st row and clear display
+    for(int i = 0; i < 16; ++i) {
+        LCD_print_char(base + i);   // write 1st-half 16 characters
     }
-    LCD_locate(1, 0);                                // select 2nd row
-    for(i = 16; i < 32; ++i) {
-        LCD_print_char(base + i);     // write 2nd-half 16 characters
+    LCD_locate(1, 0);               // select 2nd row
+    for(int i = 16; i < 32; ++i) {
+        LCD_print_char(base + i);   // write 2nd-half 16 characters
     }
     wait_ms(3000);
 }