Fuminori ALAI
/
SakuraIoEVB_test
sakura.io EVB
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 00003 DigitalIn sw1(PC_14); 00004 DigitalIn sw2(PH_0); 00005 DigitalIn sw3(PC_0); 00006 DigitalIn sw4(PC_1); 00007 DigitalIn sw5(PC_2); 00008 DigitalIn sw6(PC_3); 00009 00010 DigitalOut led1(PC_13); 00011 DigitalOut led2(PC_15); 00012 DigitalOut led3(PH_1); 00013 DigitalOut led4(PC_4); 00014 DigitalOut lcd_bl(PC_5); 00015 00016 DigitalOut gps_en(PA_11); 00017 00018 Serial ser_daplink(PB_6, PB_7); 00019 Serial ser_external(PA_2, PA_3); 00020 Serial ser_gps(PC_6, PC_7); 00021 00022 I2C i2c_lcd(PB_9, PB_8); 00023 00024 static void lcd_init(); 00025 static int lcd_puts(uint8_t *str, uint8_t start_index); 00026 void lcd_clear(); 00027 00028 int main() { 00029 uint8_t mode = 0xff; 00030 00031 ser_daplink.baud(9600); 00032 ser_external.baud(9600); 00033 ser_gps.baud(9600); 00034 00035 gps_en = 1; 00036 00037 lcd_init(); 00038 lcd_puts((uint8_t*)"Serial", 0); 00039 00040 while(1) { 00041 led1 = sw1; 00042 led2 = sw2; 00043 led3 = sw3; 00044 led4 = sw4; 00045 00046 int val = sw5 + (sw6 << 1); 00047 00048 if (val != mode) { 00049 char mode_str[17]; 00050 sprintf(mode_str, "Mode: %d", val); 00051 lcd_puts((uint8_t *)mode_str, 16); 00052 mode = val; 00053 } 00054 00055 switch (val) { 00056 case 0: 00057 lcd_bl = 0; 00058 break; 00059 case 1: 00060 // DAPLink loopback 00061 if (ser_daplink.readable()) { 00062 ser_daplink.putc(ser_daplink.getc()); 00063 } 00064 lcd_bl = 1; 00065 case 2: 00066 // DAPLink - Extended relay 00067 if (ser_daplink.readable()) { 00068 ser_external.putc(ser_daplink.getc()); 00069 } 00070 if (ser_external.readable()) { 00071 ser_daplink.putc(ser_external.getc()); 00072 } 00073 lcd_bl = 1; 00074 break; 00075 case 3: 00076 // DAPLink - GPS module relay 00077 if (ser_daplink.readable()) { 00078 ser_gps.putc(ser_daplink.getc()); 00079 } 00080 if (ser_gps.readable()) { 00081 ser_daplink.putc(ser_gps.getc()); 00082 } 00083 lcd_bl = 1; 00084 break; 00085 default: 00086 break; 00087 } 00088 } 00089 } 00090 00091 //#define LCD_ADR (0x3e) 00092 #define LCD_ADR (0x7c) 00093 static int lcd_write_cmd(uint8_t cmd) 00094 { 00095 uint8_t dat[2]; 00096 dat[0] = 0x00; 00097 dat[1] = cmd; 00098 return i2c_lcd.write(LCD_ADR, (char*)dat, 2, false); 00099 } 00100 00101 static int lcd_write_char(uint8_t chr) 00102 { 00103 uint8_t dat[2]; 00104 dat[0] = 0x40; 00105 dat[1] = chr; 00106 return i2c_lcd.write(LCD_ADR, (char*)dat, 2, false); 00107 } 00108 00109 static bool lcd_loc(uint8_t col, uint8_t row) 00110 { 00111 uint8_t addr = 0x80; 00112 if ((col < 16) && (row < 2)) { 00113 addr |= col; 00114 if (row == 1) { 00115 addr |= 0x40; 00116 } 00117 lcd_write_cmd(addr); //DDRAMのアドレスを設定 00118 return true; 00119 } 00120 00121 return false; 00122 } 00123 00124 void lcd_clear(void) 00125 { 00126 lcd_write_cmd(0x01); 00127 wait_us(10000); 00128 } 00129 00130 static void lcd_init() 00131 { 00132 //LCD initialize 00133 wait_us(50000); 00134 lcd_write_cmd(0x38); 00135 wait_us(30); 00136 lcd_write_cmd(0x02); 00137 wait_us(30); 00138 lcd_write_cmd(0x39); 00139 wait_us(30); 00140 lcd_write_cmd(0x14); 00141 wait_us(30); 00142 lcd_write_cmd(0x7f); 00143 wait_us(30); 00144 lcd_write_cmd(0x56); 00145 wait_us(30); 00146 lcd_write_cmd(0x6a); 00147 00148 wait_us(200000); 00149 00150 lcd_write_cmd(0x0c); 00151 wait_us(30); 00152 lcd_write_cmd(0x01); 00153 wait_us(30); 00154 lcd_write_cmd(0x04); 00155 wait_us(30); 00156 } 00157 00158 static int lcd_puts(uint8_t *str, uint8_t start_index) 00159 { 00160 int i; 00161 uint8_t addr = 0x80; 00162 00163 if (start_index >= 32) { 00164 return -1; 00165 } 00166 00167 //表示開始アドレス 00168 if (start_index < 16) { 00169 //1行目 00170 addr |= start_index; 00171 } else { 00172 //2行目 00173 addr |= (0x40 | (start_index - 16)); 00174 } 00175 lcd_write_cmd(addr); 00176 wait_us(30); 00177 00178 for (i = 0; i < 32; i++) { 00179 if (str[i] == '\0') { 00180 break; 00181 } 00182 00183 if ((i + start_index) == 16) { 00184 lcd_write_cmd(0xc0); //アドレスを2行目先頭に 00185 wait_us(30); 00186 } else if ((i + start_index) == 32) { 00187 lcd_write_cmd(0x80); //アドレスを1行目先頭に 00188 wait_us(30); 00189 } 00190 lcd_write_char(str[i]); 00191 wait_us(30); 00192 } 00193 00194 return i; 00195 }
Generated on Mon Jul 25 2022 18:07:41 by 1.7.2