// S信号,R信号によるハンドシェーク bool succeedS_R(serial_port& port) { //Sは'S' char S[2] = "S"; if(0 < port.write_some(buffer(S))){ printf("S sent successfully\n"); char R[2] = "0"; //Rを受信して,かつ'R'だったら if(0 < port.read_some(buffer(R)) && 'R' == R[0]){ return true; } } return false; }
// 1行毎にLEDの情報を送信 bool sendImage(serial_port& port) { char P[2] = "P"; for(int i = 0; i < NUM_LED; i++){ port.write_some(buffer(P)); //P信号を送信 char dev[3] = "\0\0"; //デバイスの番号, 行数を受け取る if(0 < port.read_some(buffer(dev))){ int dev_num = atoi(dev); printf("dev number[%d] : %s\n", dev_num, dev); //P信号を送信 port.write_some(buffer(P)); dev[0] = dev[1] = '\0'; if(0 < port.read_some(buffer(dev))){ int dev_row = atoi(dev); printf("dev row[%d] : %s\n", dev_row, dev); port.write_some(buffer(P)); //P信号を送信 makeLedArray(dev_row, 0); port.write_some(buffer(led_array)); printLedArray(); char C[2] = "\0"; if(0 < port.read_some(buffer(C)) && 'C' == C[0]){ //printf("\tC received : %s\n", C); port.write_some(buffer(P)); //P信号を送信 makeLedArray(dev_row, 1); port.write_some(buffer(led_array)); printLedArray(); } } } } //終了信号(F)を待って終了 char dev[2] = "\0"; while(1){ if(0 < port.read_some(buffer(dev)) && 'F' == dev[0]){ return true; } } }
DriverComms(std::string port, int baudrate){ sp = new serial_port(io); //connect to port sp->open(port, ec); if(ec) { //Failure to connect ROS_ERROR_STREAM("Error connecting to expression board: " << ec.message()); sp->close(); delete sp; throw std::exception(); } //Set the baudrate sp->set_option(serial_port_base::baud_rate(baudrate)); }
namespace debug { extern serial_port com1; void init() {} void partial_init() { com1.init(serial_port::COM1); } }
// シリアル通信を行う void writeBySerial(void) { // 1回目の通信なら初期設定 if(0 == config_counter){ port.set_option(serial_port_base::baud_rate(38400)); port.set_option(serial_port_base::character_size(8)); port.set_option(serial_port_base::flow_control(serial_port_base::flow_control::none)); port.set_option(serial_port_base::parity(serial_port_base::parity::none)); port.set_option(serial_port_base::stop_bits(serial_port_base::stop_bits::one)); config_counter++; } if(succeedS_R(port)){ printf("S_R Handshake successfully done\n"); if(sendImage(port)){ printf("data correctly written\n"); } } return; }
void partial_init() { com1.init(serial_port::COM1); }
int main() { SDL_Surface* wavs = NULL; SDL_Surface* ffts = NULL; SDL_Init( SDL_INIT_EVERYTHING ); wavs = SDL_SetVideoMode( 128, 256, 32, SDL_HWSURFACE ); port.serial_open("/dev/ttyUSB0", 38400); bus.fanbus_set_port(&port); ALCdevice *device = alcCaptureOpenDevice(NULL, 8000, AL_FORMAT_MONO8, 128); alcCaptureStart(device); blackman(win, 128); while (true) { for(int i = 0; i < 128; i++) { buffer[i] = 0; } alcCaptureSamples(device, (ALCvoid *)buffer, 128); for(int i = 0; i < 128; i++) { fft[i] = buffer[i]*5; } rfft(fft, 128, 1); apply_window(fft, win, 128); boxRGBA(wavs, 0, 0, 256, 256, 0, 0, 0, 255); for(int i = 0; i < 128; i++) { pixelRGBA(wavs, i, 255-buffer[i], 255, 0, 0, 255); if(i%2==0) { lineRGBA(wavs, i, 255, i, 255-(unsigned char)fabs(fft[i])*3, 0, 255, 0, 255); } else { lineRGBA(wavs, i, 255, i, 255-(unsigned char)fabs(fft[i])*3, 0, 0, 255, 255); } SDL_Flip(wavs); } SDL_Delay(25); char red = (char)fabs(fft[10]); char grn = (char)fabs(fft[20]); char blu = (char)fabs(fft[30]); bus.fanbus_write(0x10, 0x02, red); bus.fanbus_write(0x13, 0x02, red); bus.fanbus_write(0x16, 0x02, red); bus.fanbus_write(0x19, 0x02, red); bus.fanbus_write(0x11, 0x02, grn); bus.fanbus_write(0x14, 0x02, grn); bus.fanbus_write(0x17, 0x02, grn); bus.fanbus_write(0x1A, 0x02, grn); bus.fanbus_write(0x12, 0x02, blu); bus.fanbus_write(0x15, 0x02, blu); bus.fanbus_write(0x18, 0x02, blu); bus.fanbus_write(0x1B, 0x02, blu); bus.fanbus_write(0x0C, 0x02, 0x01); } alcCaptureStop(device); alcCaptureCloseDevice(device); return 0; }
~DriverComms() { //Disconnect from port sp->close(); delete sp; }