Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TestVirtualisation Bf_SoftSerial_IR
SoftSerial_rx_IR.cpp
- Committer:
- kenjiArai
- Date:
- 2018-12-28
- Revision:
- 14:dc766032cdd6
- Parent:
- 13:2b5649a1278a
- Child:
- 15:8d343be3382d
File content as of revision 14:dc766032cdd6:
// Apply for Infrared LED and IR-Detector
// Modified by JH1PJL Dec. 28th, 2018
#include "SoftSerial_IR.h"
//extern DigitalOut myled; // Debug
int SoftSerial_IR::_getc( void ) {
while(!readable());
out_valid = false;
return out_buffer;
}
int SoftSerial_IR::readable(void) {
return out_valid;
}
//Start receiving byte
void SoftSerial_IR::rx_gpio_irq_handler(void) {
//myled = !myled; // Debug
rxticker.prime(timestamp_offset);
rxticker.setNext(bit_period + (bit_period >> 1) - overhead_us_IR);
rx->fall(NULL);
rx_bit = 0;
rx_error = false;
};
void SoftSerial_IR::rx_handler(void) {
//myled = !myled; // Debug
//Receive data
int val = rx->read();
rxticker.setNext(bit_period);
rx_bit++;
if (rx_bit <= _bits) {
read_buffer |= val << (rx_bit - 1);
return;
}
//Receive parity
bool parity_count;
if (rx_bit == _bits + 1) {
switch (_parity) {
case Forced1:
if (val == 0)
rx_error = true;
return;
case Forced0:
if (val == 1)
rx_error = true;
return;
case Even:
case Odd:
parity_count = val;
for (int i = 0; i<_bits; i++) {
if (((read_buffer >> i) & 0x01) == 1)
parity_count = !parity_count;
}
if ((parity_count) && (_parity == Even))
rx_error = true;
if ((!parity_count) && (_parity == Odd))
rx_error = true;
return;
}
}
//Receive stop
if (rx_bit < _bits + (bool)_parity + _stop_bits) {
if (!val)
rx_error = true;
return;
}
//The last stop bit
if (!val)
rx_error = true;
if (!rx_error) {
out_valid = true;
out_buffer = read_buffer;
fpointer[RxIrq].call();
}
read_buffer = 0;
rxticker.detach();
rx->fall(this, &SoftSerial_IR::rx_gpio_irq_handler);
}