// // filename: serial.c // // tutorial file to use wireless cybiko as a general purpose terminal // // by Sonny Cruz 2/27/2003 // // loop // wait for synch byte (0x1b) from serial port // read 8 bytes from serial // translate 2nd byte to cybiko id // send 8 bytes to the wireless network // received 8 bytes from the wireless network // translate 2nd byte to char // send 8 bytes to serial // // use delete key to quit the program // serial settings 57600 baud, 8 data, 1 stop, no parity // shows status on the cybiko screen // beeps if serial ports timeout // // to add more cybiko's // add define for id, use console and type id in the command to get id // adjust functions convertCharToID and convertIDToChar accordingly // 2nd char of the buffer is the id before conversion // #include #define MY_APP (MSG_USER + 1) // my own message id #define ID1 0x00035FE1 // blue cybiko #define ID2 0x000339DC // pink cybiko #define ID3 0x0002C3C8 // green cybiko #define SYNCH 0x1b // synch character #define LIMIT 8 // limit of char to read / write #define TIMEOUT 100 // time out in millisecond for read #define MAX_RETRY 20 // maximun retry to wait for synch #define LINE_INCREMENT 10 // increment y axis for screen position static timeout = 0; // count of time out static int X = 0; // used for positioning the text on screen static int Y = 0; // used for positioning the text on screen char buffer[LIMIT]; // buffer to use for read / write char str[40]; // used for status message struct module_t m; // holder for graphics and thread message queue com_t port; // cybiko's serial port struct Message* mess; // cybiko's message queue /*********************************** * function: serialWrite * ***********************************/ void serialWrite() { int i; for (i = 0; i < LIMIT; i++) com_write(port, buffer[i], 0); } /*********************************** * function serialRead * ***********************************/ bool serialRead() { int timeout; int i; int data; data = 0x00; timeout = 0; while ((data != SYNCH) && (timeout < MAX_RETRY)) { // wait for synch data data = com_read(port, TIMEOUT); timeout++; } if (data == SYNCH) { for (i = 0; i < LIMIT; i++) buffer[i] = (char) com_read(port, TIMEOUT); return TRUE; } return FALSE; } /*********************************** * function: getKey * ***********************************/ int getKey() { return Message_get_key_param(mess)->scancode; } /*********************************** * function: convertIDToChar * ***********************************/ char convertIDToChar(long l) { char c; switch (l) { case ID1 : c = 0x31; break; case ID2 : c = 0x32; break; case ID3 : c = 0x33; break; default : c = 0x00; break; } return c; } /************************************ * function: convertCharToID * ************************************/ long convertCharToID(char c) { long l; switch (c) { case 0x31 : l = ID1; break; case 0x32 : l = ID2; break; case 0x33 : l = ID3; break; default : l = 0x00; break; } return l; } /************************************ * function: clearScreen * ************************************/ void clearScreen() { DisplayGraphics_set_color(m.m_gfx, CLR_WHITE); DisplayGraphics_fill_rect(m.m_gfx, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); DisplayGraphics_show(m.m_gfx); } /*********************************** * function: putString * ***********************************/ void putString(char* s) { if (Y > SCREEN_HEIGHT) { Y = 0; clearScreen(); } DisplayGraphics_set_color(m.m_gfx, CLR_BLACK); DisplayGraphics_draw_text(m.m_gfx, s, X, Y); DisplayGraphics_show(m.m_gfx); Y += LINE_INCREMENT; } /************************************** * main * **************************************/ long main(int argc, char* argv[], bool start) { long tempID; bool exitApp = FALSE; init_module(&m); port = com_open(COMM_DEV_DEFAULT, 500); DisplayGraphics_set_font(m.m_gfx, mini_normal_font); clearScreen(); putString("Starting program..."); while (!exitApp) { putString("reading serial"); if (!serialRead()) { putString("Timeout reading serial"); beep(BEEP_ERROR); } else { sprintf(str, "Sending wireless to %c", buffer[1]); putString(str); send_remote_msg(convertCharToID(buffer[1]), "serial", MY_APP, 0L, 0L, buffer, LIMIT); } mess = cWinApp_get_message(m.m_process, TIMEOUT, 1, MY_APP); if (mess) { switch(mess->msgid) { case MSG_SHUTUP : case MSG_QUIT : exitApp = TRUE; break; case MY_APP : Buffer_load(Message_get_buffer(mess), buffer, 0, LIMIT); sprintf(str, "Receiving wireless from %c", buffer[1]); putString(str); buffer[1] = convertIDToChar(Message_get_sender_id(mess)); putString("sending serial"); serialWrite(); break; case MSG_KEYDOWN : if (getKey() == KEY_DEL) exitApp = TRUE; break; default : cWinApp_defproc(m.m_process, mess); } Message_delete(mess); } } com_close(port); return 0L; }