Last active
December 29, 2017 07:01
-
-
Save lamunes40forever/9bbf10aae338aef2fea46912d03737fc to your computer and use it in GitHub Desktop.
Arduino Code to connect CyberVoice and PC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 此Arduino Code適用在連接CyberVoice與PC | |
* 注意用此連接方式無法使用VP的錄音, 其餘功能皆正常 | |
* | |
* 連接方式: | |
* Arduino CyberVoice | |
* 5V <-----> USB5V | |
* GND <-----> GND | |
* Pin.10 <-----> PA.8 | |
* Pin.11 <-----> PA.9 | |
*/ | |
#include <SoftwareSerial.h> | |
#define CYB_MODEL ('8') //Update VR model | |
#define CYB_FW ('9') //Update VR model | |
#define CYB_PCM ('H') //Update PCM model | |
#define CYB_GET_CONNECT_DEVICE ('W') //Return "CyberVoice" or "Arduino" | |
void mySerialDefault(void); | |
void drain_buf(void); | |
SoftwareSerial mySerial(10, 11); // RX, TX | |
int tmp; | |
char buf[8] = {0}; | |
unsigned long previous_time, present_time; | |
static int nIdxCmd = 0; | |
static char cmdBuf[4] = {0}; | |
void setup() { | |
Serial.begin(57600); | |
while(!Serial); | |
mySerial.begin(57600); | |
while(!mySerial); | |
} | |
void loop() { | |
if(mySerial.available()) | |
Serial.write(mySerial.read()); | |
} | |
void serialEvent(void) | |
{ | |
if(Serial.available()) | |
{ | |
cmdBuf[nIdxCmd++] = Serial.read(); | |
if (nIdxCmd == 4) | |
{ | |
if(strncmp(cmdBuf, "CYB", 3) != 0) | |
{ | |
memmove(cmdBuf, cmdBuf + 1, 3); | |
nIdxCmd = 3; | |
} | |
else | |
{ | |
if((cmdBuf[3] == CYB_MODEL) || (cmdBuf[3] == CYB_FW) || (cmdBuf[3] == CYB_PCM)) | |
{ | |
mySerial.write("CYBo"); | |
mySerial.flush(); | |
while(!mySerial.available()); | |
mySerial.read(); // Don't pass to pc | |
mySerial.end(); | |
mySerial.begin(115200); | |
//************************************************// | |
while(!mySerial); | |
delay(500); | |
mySerial.write(cmdBuf, 4); | |
if(cmdBuf[3] == CYB_PCM) | |
delay(1000*18); | |
else | |
delay(1000); | |
Serial.write(0); | |
for(int x = 0; x < 64; x++) | |
mySerial.read(); | |
int flag = 0; | |
while(1) | |
{ | |
if(Serial.available()) | |
{ | |
flag = 1; | |
previous_time = millis(); | |
mySerial.write(Serial.read()); | |
} | |
else if(!flag) | |
previous_time = millis(); | |
present_time = millis(); | |
if(((present_time - previous_time) > 2000) && flag) | |
{ | |
mySerialDefault(); | |
delay(300); | |
mySerial.write("CYBY"); | |
while(!mySerial.available()); | |
Serial.write(mySerial.read()); | |
Serial.flush(); | |
flag = 0; | |
break; | |
} | |
} | |
} | |
else if(cmdBuf[3] == CYB_GET_CONNECT_DEVICE) | |
{ | |
Serial.write("\r\nArduino\r\n"); | |
} | |
else | |
{ | |
mySerial.flush(); | |
mySerial.write(cmdBuf, 4); | |
mySerial.flush(); | |
} | |
// Some more else if statements are listed here ….. | |
nIdxCmd = 0; | |
} | |
} | |
} | |
} | |
void mySerialDefault() | |
{ | |
mySerial.write("CYBn"); | |
mySerial.flush(); | |
Serial.flush(); | |
mySerial.read(); | |
mySerial.end(); | |
mySerial.begin(57600); | |
while(!mySerial); | |
} | |
void drain_buf() | |
{ | |
for(int i = 0; i < 12; i++) | |
{ | |
Serial.read(); | |
mySerial.read(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment