Software UART program using Infra-Red LED and IR-Detector

Dependents:   TestVirtualisation Bf_SoftSerial_IR

Committer:
kenjiArai
Date:
Fri May 15 04:11:01 2020 +0000
Revision:
15:8d343be3382d
Parent:
14:dc766032cdd6
Updated several functions based on SoftSerial library.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 15:8d343be3382d 1 // Modified by K.Arai / JH1PJL May 15th, 2020
kenjiArai 13:2b5649a1278a 2
kenjiArai 12:79d63607bbb1 3 #include "SoftSerial_IR.h"
Sissors 0:8edaa7abe724 4
kenjiArai 15:8d343be3382d 5 SoftSerial_IR::SoftSerial_IR(PinName TX, PinName RX, const char* name)
kenjiArai 15:8d343be3382d 6 {
Sissors 0:8edaa7abe724 7 tx_en = rx_en = false;
SonyPony 11:a0029614de72 8 read_buffer = 0;
Sissors 0:8edaa7abe724 9 if (TX != NC) {
kenjiArai 15:8d343be3382d 10 //tx = new DigitalOut(TX);
kenjiArai 12:79d63607bbb1 11 tx = new PwmOut(TX);
Sissors 0:8edaa7abe724 12 tx_en = true;
kenjiArai 15:8d343be3382d 13 tx->period(1.0f / 38000.0f); // PWM = 38kHz
kenjiArai 15:8d343be3382d 14 //tx->write(1);
kenjiArai 12:79d63607bbb1 15 tx->write(0.0f);
Sissors 0:8edaa7abe724 16 tx_bit = -1;
kenjiArai 12:79d63607bbb1 17 txticker.attach(this, &SoftSerial_IR::tx_handler);
Sissors 0:8edaa7abe724 18 }
Sissors 0:8edaa7abe724 19 if (RX != NC) {
Sissors 0:8edaa7abe724 20 rx = new InterruptIn(RX);
Sissors 0:8edaa7abe724 21 rx_en = true;
Sissors 1:f8b4b764ace7 22 out_valid = false;
kenjiArai 12:79d63607bbb1 23 rxticker.attach(this, &SoftSerial_IR::rx_handler);
kenjiArai 15:8d343be3382d 24 rx->fall(callback(this, &SoftSerial_IR::rx_gpio_irq_handler));
Sissors 0:8edaa7abe724 25 }
kenjiArai 15:8d343be3382d 26
kenjiArai 13:2b5649a1278a 27 baud(2400);
Sissors 0:8edaa7abe724 28 format();
Sissors 0:8edaa7abe724 29 }
Sissors 0:8edaa7abe724 30
kenjiArai 15:8d343be3382d 31 SoftSerial_IR::~SoftSerial_IR()
kenjiArai 15:8d343be3382d 32 {
kenjiArai 15:8d343be3382d 33 if (tx_en) {
Sissors 9:4e4617c4a441 34 delete(tx);
kenjiArai 12:79d63607bbb1 35 }
kenjiArai 15:8d343be3382d 36 if (rx_en) {
Sissors 9:4e4617c4a441 37 delete(rx);
kenjiArai 12:79d63607bbb1 38 }
Sissors 9:4e4617c4a441 39 }
Sissors 9:4e4617c4a441 40
kenjiArai 15:8d343be3382d 41 void SoftSerial_IR::baud(int baudrate)
kenjiArai 15:8d343be3382d 42 {
Sissors 0:8edaa7abe724 43 bit_period = 1000000 / baudrate;
Sissors 0:8edaa7abe724 44 }
Sissors 0:8edaa7abe724 45
kenjiArai 15:8d343be3382d 46 void SoftSerial_IR::format(int bits, Parity parity, int stop_bits)
kenjiArai 15:8d343be3382d 47 {
Sissors 0:8edaa7abe724 48 _bits = bits;
Sissors 0:8edaa7abe724 49 _parity = parity;
Sissors 0:8edaa7abe724 50 _stop_bits = stop_bits;
kenjiArai 15:8d343be3382d 51 _total_bits = 2 + _bits + _stop_bits + (bool)_parity;
Sissors 0:8edaa7abe724 52 }