Yoshiyuki Uehara / Mbed 2 deprecated Maple

Dependencies:   mbed

Maple.cpp

Committer:
uehara00
Date:
2011-09-25
Revision:
0:0be38b583cf7
Child:
1:aefa1992ce0f

File content as of revision 0:0be38b583cf7:

// 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];
}

// 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';
}

// BCD to integer
int bcd_to_int(char b) {
    return ((b >> 4) * 10) + (b & 0x0f);
}

// integer to BCD
char int_to_bcd(int i) {
    return ((i / 10) << 4) + (i % 10);
}

// increment 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 increment_bcd(char bcd_data, char bcd_min, char bcd_max) {
    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;
}

// 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 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';
}

// year in 2cyy format
//   c: century .. 0x00/0x01
int bcd_to_year(char century, char bcd_year) {
    return 2000 + (century * 100) + bcd_to_int(bcd_year);
}

// check leap year
//   false: no, true:yes
bool leap_year(int year) {
    return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}

// days in a month
int days_in_month(int year, int month) {
    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)) {
        return days_leap[month];
    }
    else {
        return days_common[month];
    }
}

// (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)));
}

// Zeller's congruence for Calendario gregoriano
//   0:SUN .. 6:SAT
int bcd_date_to_weekday(char bcd_century, char bcd_year, char bcd_month, char bcd_day) {
    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);
    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
void RTC_to_LCD_raw() {
    char d[17];

    i2c_RTC_read(RTC_REG_CONTROL1, d, 16);
    LCD_locate(0, 0);
    for(int i = 0; i < 8; ++i) {
        LCD_print_hex(d[i]);
    }
    LCD_locate(1, 0);
    for(int i = 8; i < 16; ++i) {
        LCD_print_hex(d[i]);
    }
}

// test 2 of LCD
//   display 64 characters in 1st/2nd row and shift 
//
void LCD_display_all_char_shift() {
        LCD_display_32char_shift(0x00);
        LCD_display_32char_shift(0x40);
        LCD_display_32char_shift(0x80);
        LCD_display_32char_shift(0xC0);
}

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_locate(1, 0);                                // select 2nd row
    for(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
        wait_ms(1000);
    }
    for(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
        wait_ms(1000);
    }
}

// test 1 of LCD
//   display 16x2 characters with 1st and 2nd row 
void LCD_display_all_char() { 
    LCD_display_32char(0x00);
    LCD_display_32char(0x20);
    LCD_display_32char(0x40);
    LCD_display_32char(0x60);
    LCD_display_32char(0x80);
    LCD_display_32char(0xa0);
    LCD_display_32char(0xc0);
    LCD_display_32char(0xe0);
}

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_locate(1, 0);                                // select 2nd row
    for(i = 16; i < 32; ++i) {
        LCD_print_char(base + i);     // write 2nd-half 16 characters
    }
    wait_ms(3000);
}