while (digitalRead(_receivePin));
// confirm that this is a real start bit, not line noise
if (digitalRead(_receivePin) == LOW) {
// frame start indicated by a falling edge and low start bit
// jump to the middle of the low start bit
delayMicroseconds(bitDelay / 2 - clockCyclesToMicroseconds(50));
// offset of the bit in the byte: from 0 (LSB) to 7 (MSB)
for (int offset = 0; offset < 8; offset++) {
// jump to middle of next bit
delayMicroseconds(bitDelay);
// read bit
val |= digitalRead(_receivePin) << offset;
}
delayMicroseconds(_bitPeriod);
return val;
}
return -1;
}
void VRbot_write(uint8_t b)
{
if (_baudRate == 0)
return;
int bitDelay = _bitPeriod - clockCyclesToMicroseconds(50); // a digitalWrite is about 50 cycles
byte mask;
digitalWrite(_transmitPin, LOW);
delayMicroseconds(bitDelay);
for (mask = 0x01; mask; mask <<= 1) {
if (b & mask){ // choose bit
digitalWrite(_transmitPin,HIGH); // send 1
}
else{
digitalWrite(_transmitPin,LOW); // send 1
}
delayMicroseconds(bitDelay);
}
digitalWrite(_transmitPin, HIGH);
delayMicroseconds(bitDelay);
}
/*******************************************************************************/
unsigned char VRbot_Detect(void) {
unsigned char i;
for (i = 0; i < 5; ++i) {
delay(100);
VRbot_write(CMD_BREAK);
if ( VRbot_read() == STS_SUCCESS)
return 255;
}
return 0;
}
unsigned char VRbot_SetLanguage(unsigned char lang) {
VRbot_write(CMD_LANGUAGE);
delay(5);
VRbot_write(ARG_ZERO + lang);
if (VRbot_read() == STS_SUCCESS)
return 255;
return 0;
}
void VRbot_RecognizeSD(unsigned char group) {
VRbot_write(CMD_RECOG_SD);
delay(5);
VRbot_write(ARG_ZERO + group);
}
void VRbot_RecognizeSI(unsigned char ws) {
VRbot_write(CMD_RECOG_SI);
delay(5);
VRbot_write(ARG_ZERO + ws);
}
void VRbot_SetTimeout(unsigned char s) {
VRbot_write(CMD_TIMEOUT);
delay(5);
VRbot_write(ARG_ZERO + s);
delay(5);
}
signed char VRbot_CheckResult(void) {
unsigned char rx;
rx = VRbot_read();
if (rx == STS_SIMILAR || rx == STS_RESULT) {
delay(5);
VRbot_write(ARG_ACK);
return (VRbot_read() - ARG_ZERO);
}
if (rx == STS_TIMEOUT)
return -1;
return -2;
}