int main(){ char s[32]; // 文字データ用 int len=0; // 文字長 char c; // 文字入力用の文字変数 int ctrl=0; // 制御用 0:先頭, -1~-3:「-」入力数 // 1:コマンド, 2:プログラム printf("Ichigo Term for Raspberry Pi\n"); if(open_serial_port() <= 0){ printf("UART OPEN ERROR\n"); return -1; } printf("CONNECTED\nHit '---' to exit.\nTX-> "); write(ComFd, "\x1b\x10 CLS\n", 7); // IchigoJamの画面制御 while(1){ if( kbhit() ){ c=getchar(); // キーボードからの文字入力 s[len]=c; // 入力文字を保持 if(len < 31) len++; // 最大長未満のときに文字長に1を加算 write(ComFd, &c, 1 ); // IchigoJamへ送信 if( ctrl<=0 && c =='-' ){ // 先頭かつ入力された文字が「-」の時 ctrl--; // ctrl値を1減算 if(ctrl <= -3) break; // 「-」が3回入力された場合に終了 }else if(ctrl==0){ if(isdigit(c)) ctrl=2; else ctrl=1; // 先頭に数値でプログラムと判定し、 } // そうで無い時はコマンド入力と判定 if(c=='\n'){ // 入力文字が改行の時 if(ctrl==2 || len==1) printf("TX-> "); // 無入力とプログラム入力時 ctrl=0; // 入力文字状態を「先頭」にセット len=0; usleep(250000); // 250msの(IchigoJam処理)待ち時間 } usleep(25000); // 25msの(IchigoJam処理)待ち時間 } c=read_serial_port(); // シリアルからデータを受信 if(c){ printf("\nRX<- "); // 受信を識別するための表示 while(c){ if( isprint(c) ) printf("%c",c); // 表示可能な文字の時に表示する if( c=='\n' ) printf("\n "); // 改行時に改行と5文字インデントする if( c=='\t' ) printf(", "); // タブの時にカンマで区切る c=read_serial_port(); // シリアルからデータを受信 } s[len]='\0'; printf("\nTX-> %s",s); // キーボードの入力待ち表示 } } printf("\nDONE\n"); close_serial_port(); return 0; }
static int trace_device_console(void) { int result; int count = 0; time_t base = time(0); if ((result = reset_device(0))) return result; while (count < trace_size) { if (time(0) - base > trace_time) break; if ((result = read_serial_port(device_buffer, 1))) { if (result == NO_DEVICE_REPLY) continue; return result; } fprintf(stdout, isprint(device_buffer[0]) || isspace(device_buffer[0]) ? TTY_NONE "%c" : TTY_NONE "[%02X]", device_buffer[0]); fflush(stdout); count++; base = time(0); } return DONE; }
uint8_t serial_read_byte() { unsigned char c; read_serial_port(&c, 1); return (uint8_t)c; }
/* Inicia a comunicação serial com o Arduino */ void *thread_comunicacao_driver() { int info; start_serial_communication(); do { info = read_serial_port(buffer); } while(!strcmp(buffer, "_stop_")); }
static int device_response(size_t size) { int result; if ((result = read_serial_port(device_buffer, size + 1))) return result; return device_buffer[size] == 0x79 ? DONE : INVALID_DEVICE_REPLY; }
void* serial_reader_func(void* arg) { unsigned char buf[255]; while(1) { int n = read_serial_port(buf, sizeof(buf)); if (n > 0) { write(tty_fd, buf, n); } } }
void handle_port_input() { unsigned char buf[256]; int num; num = read_serial_port(buf, sizeof(buf)); if (num > 0) { download_rx_port(buf, num); } }
/* コマンドの受信を行う関数 */ int bt_rx(void){ int i,loop=50; // 変数iは受信したデータのサイズ for(i=0;i<(RX_MAX-1);i++) rx_data[i]='\0'; // 変数の初期化 memset(rx_data,0,RX_MAX) i=0; // 受信済データの数の初期化 while(loop>0){ // 50回、くりかえす。 loop--; rx_data[0] = read_serial_port(); // 受信データを読取り、変数rx_data[0]へ if( rx_data[0] ){ // 何らかの受信データ(応答)があった場合 for(i=1;i<(RX_MAX-2);i++){ // 受信データの変数の数だけ繰り返す rx_data[i]=read_serial_port(); // 受信データを保存する if( rx_data[i]==0 ) break; // 受信データ無しの時にforループを抜ける usleep(2000); // 待ち時間を付与 } bt_rx_clear(); // シリアルの受信バッファを消去する loop=0; // whileループを抜けるためにloop値を0に }else usleep(10000); // 応答待ち } return(i); // 受信したデータの大きさを戻り値にする }
static int device_request(size_t size) { int result; device_buffer[size] = device_checksum(device_buffer, size); if ((result = write_serial_port(device_buffer, size + 1))) return result; if ((result = read_serial_port(device_buffer, 1))) return result; return device_buffer[0] == 0x79 ? DONE : INVALID_DEVICE_REPLY; }
static int try_to_handshake_device(void) { int result; device_buffer[0] = 0x7F; if ((result = wait_serial_port(5))) return result; if ((result = flush_serial_port())) return result; if ((result = write_serial_port(&device_buffer, 1))) return result; if ((result = read_serial_port(&device_buffer, 1))) return result; return device_buffer[0] == 0x79 ? DONE : INVALID_DEVICE_REPLY; }
static int read_device_memory(const struct buffer *buffer) { uint32_t address = buffer->origin; uint8_t *data = buffer->data; size_t size = buffer->size; while (size) { int result; size_t count = size < 256 ? size : 256; device_buffer[0] = 0x11; if ((result = device_request(1))) return result; device_buffer[0] = address >> 24; device_buffer[1] = address >> 16; device_buffer[2] = address >> 8; device_buffer[3] = address; if ((result = device_request(4))) return result; device_buffer[0] = count - 1; if ((result = device_request(1))) return result; if ((result = read_serial_port(data, size))) return result; size -= count; data += count; address += count; } return DONE; }
int main(void) { int i; int reading1; int reading2; int address; int test_array[100]; for(i=0;i<100;i++) { motor(0,i); //spin the left motor forward motor(1,i); //spin the right motor forward } i=0; while(i>-100) { motor(0,i); //spin the left motor backwards motor(1,i); //spin the right motor backwards i--; } i=50; set_servo(0,i); //set servo motor 0 to move to 50 degrees set_servo(3,i); //set servo motor 3 to move to 50 degrees delay_milliseconds(100); //pause 100 milliseconds delay_seconds(1); //pause 1 second lcd_clear(); lcd_cursor(0,0); printf ("Test1\n"); //the LCD will be 8x2 (8chars x 2lines) printf ("Test2\n"); reading1 = analog(0); //get a reading from analog pin 0 reading2 = analog(5); //get a reading from analog pin 5 reading1 = digital(0); //get a reading from digital pin 0 reading2 = digital(1); //get a reading from digital pin 1 if (reading1 > 100) { printf ("%d\n", reading1); } reading1 = accelerometer(0); //read x-axis reading2 = accelerometer(1); //read y-axis reading1 = accelerometer(2); //read z-axis reading1 = battery_voltage(); //battery voltage reading1 = read_serial_port(); //get a byte from the serial port write_serial_port(reading1); //send a byte on the serial port led1(1); //turn on on-board led1 led1(0); //turn off on-board led1 reading1 = read_ir(); //get a reading from the IR receiver reset(); //reset the board write_eeprom(address, reading1); //write a value to the non-volatile eeprom (these values will be stored across resets) reading1 = read_eeprom(address); //get a reading from the non-volatile eeprom reading1 = button(); //read the state of the on-board button return 0; }
/* シリアルの受信バッファを消去する関数 */ void bt_rx_clear(){ while( read_serial_port() ){ // 受信データが残っている場合 usleep(2000); // 受信中を考慮し、2ms待ち時間を付与 } }