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.
Fork of SoftSerial by
SoftSerial_tx.cpp
- Committer:
- Sissors
- Date:
- 2014-04-26
- Revision:
- 4:c010265ed202
- Parent:
- 2:9e01a38606b4
- Child:
- 6:517082212c00
File content as of revision 4:c010265ed202:
#include "SoftSerial.h"
int SoftSerial::_putc(int c)
{
while(!writeable());
prepare_tx(c);
tx_bit = 0;
tx_handler();
return 0;
}
int SoftSerial::writeable(void)
{
if (!tx_en)
return false;
if (tx_bit == -1)
return true;
return false;
}
void SoftSerial::tx_handler(void)
{
if (tx_bit == _total_bits) {
tx_bit = -1;
fpointer[TxIrq].call();
return;
}
//Flip output
int cur_out = tx->read();
tx->write(!cur_out);
//Calculate when to do it again
int count = bit_period;
tx_bit++;
while(((_char >> tx_bit) & 0x01) == !cur_out) {
count+=bit_period;
tx_bit++;
}
txout.attach_us(this, &SoftSerial::tx_handler, count);
}
void SoftSerial::prepare_tx(int c)
{
_char = c << 1;
bool parity;
switch (_parity) {
case Forced1:
_char |= 1 << (_bits + 1);
case Even:
parity = false;
for (int i = 0; i<_bits; i++) {
if (((_char >> i) & 0x01) == 1)
parity = !parity;
}
_char |= parity << (_bits + 1);
case Odd:
parity = true;
for (int i = 0; i<_bits; i++) {
if (((_char >> i) & 0x01) == 1)
parity = !parity;
}
_char |= parity << (_bits + 1);
}
_char |= 0xFFFF << (1 + _bits + (bool)_parity);
_char &= ~(1<<_total_bits);
}
