ワークショップ用に公開しているものです。

Dependencies:   mbed

Fork of Arch_Analog_Thermistor_Blinker by Yihui Xiong

Files at this revision

API Documentation at this revision

Comitter:
jksoft
Date:
Sat Feb 27 05:38:49 2016 +0000
Parent:
8:58795209d9c2
Commit message:
????????????

Changed in this revision

SoftSerialSendOnly/SoftSerialSendOnry.cpp Show annotated file Show diff for this revision Revisions of this file
SoftSerialSendOnly/SoftSerialSendOnry.h Show annotated file Show diff for this revision Revisions of this file
SoftSerialSendOnly/SoftSerialSendOnry_tx.cpp Show annotated file Show diff for this revision Revisions of this file
SoftSerialSendOnly/SoftSerial_Ticker.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
softserial.lib Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SoftSerialSendOnly/SoftSerialSendOnry.cpp	Sat Feb 27 05:38:49 2016 +0000
@@ -0,0 +1,36 @@
+#include "SoftSerialSendOnry.h"
+
+SoftSerialSendOnry::SoftSerialSendOnry(PinName TX, const char* name) {
+    tx_en = false;
+    if (TX != NC) {
+        tx = new DigitalOut(TX);
+        tx_en = true;
+        tx->write(1);
+        tx_bit = -1;
+        txticker.attach(this, &SoftSerialSendOnry::tx_handler);
+    }
+    
+    baud(9600);
+    format();
+}
+
+SoftSerialSendOnry::~SoftSerialSendOnry() {
+    if (tx_en)
+        delete(tx);
+}
+
+void SoftSerialSendOnry::baud(int baudrate) {
+    bit_period = 1000000 / baudrate;
+}
+
+void SoftSerialSendOnry::format(int bits, Parity parity, int stop_bits) {
+    _bits = bits;
+    _parity = parity;
+    _stop_bits = stop_bits;
+    _total_bits = 1 + _bits + _stop_bits + (bool)_parity;
+}
+
+int SoftSerialSendOnry::_getc()
+{
+    return(0);    
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SoftSerialSendOnly/SoftSerialSendOnry.h	Sat Feb 27 05:38:49 2016 +0000
@@ -0,0 +1,105 @@
+#ifndef SOFTSERIAL_SEND_ONRY_H
+#define SOFTSERIAL_SEND_ONRY_H
+
+#include "mbed.h"
+#include "SoftSerial_Ticker.h"
+/** A software serial implementation
+ *
+ */
+class SoftSerialSendOnry: public Stream {
+
+public:
+    /**
+    * Constructor
+    *
+    * @param TX Name of the TX pin, NC for not connected
+    * @param name Name of the connection
+    */
+    SoftSerialSendOnry(PinName TX, const char* name = NULL);
+    virtual ~SoftSerialSendOnry();
+    
+    /** Set the baud rate of the serial port
+     *
+     *  @param baudrate The baudrate of the serial port (default = 9600).
+     */
+    void baud(int baudrate);
+
+    enum Parity {
+        None = 0,
+        Odd,
+        Even,
+        Forced1,
+        Forced0
+    };
+
+    enum IrqType {
+        RxIrq = 0,
+        TxIrq
+    };
+
+    /** Set the transmission format used by the serial port
+     *
+     *  @param bits The number of bits in a word (default = 8)
+     *  @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None)
+     *  @param stop The number of stop bits (default = 1)
+     */
+    void format(int bits=8, Parity parity=SoftSerialSendOnry::None, int stop_bits=1);
+
+    /** Determine if there is space available to write a character
+     *
+     *  @returns
+     *    1 if there is space to write a character,
+     *    0 otherwise
+     */
+    int writeable();
+
+    /** Attach a function to call whenever a serial interrupt is generated
+     *
+     *  @param fptr A pointer to a void function, or 0 to set as none
+     *  @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
+     */
+    void attach(void (*fptr)(void), IrqType type=RxIrq) {
+        fpointer[type].attach(fptr);
+    }
+
+    /** Attach a member function to call whenever a serial interrupt is generated
+     *
+     *  @param tptr pointer to the object to call the member function on
+     *  @param mptr pointer to the member function to be called
+     *  @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
+     */
+    template<typename T>
+    void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
+        fpointer[type].attach(tptr, mptr);
+    }
+
+    /** Generate a break condition on the serial line
+     */
+    void send_break();
+
+protected:
+    DigitalOut *tx;
+    
+    bool tx_en;
+    int bit_period;
+    int _bits, _stop_bits, _total_bits;
+    Parity _parity;
+    
+    FunctionPointer fpointer[2];
+    
+    //tx
+    void tx_handler(void);
+    void prepare_tx(int c);
+    FlexTicker txticker;
+    int _char;
+    volatile int tx_bit;
+    
+    
+    
+    virtual int _getc();
+    virtual int _putc(int c);
+};
+
+
+#endif
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SoftSerialSendOnly/SoftSerialSendOnry_tx.cpp	Sat Feb 27 05:38:49 2016 +0000
@@ -0,0 +1,80 @@
+#include "SoftSerialSendOnry.h"
+
+int SoftSerialSendOnry::_putc(int c)
+{
+    while(!writeable());
+    prepare_tx(c);
+    tx_bit = 0;
+    txticker.prime();
+    tx_handler();
+    return 0;
+}
+
+void SoftSerialSendOnry::send_break(void) {
+    while(!writeable());
+    tx_bit = 0;         //Just to make sure it appears as non-writable to other threads/IRQs
+    tx->write(0);
+    wait_us((bit_period * _total_bits * 3) / 2);
+    tx->write(1);
+    tx_bit = -1;
+}
+
+int SoftSerialSendOnry::writeable(void)
+{
+    if (!tx_en)
+        return false;
+    if (tx_bit == -1)
+        return true;
+    return false;
+}
+
+void SoftSerialSendOnry::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++;
+    }
+
+    txticker.setNext(count);
+}
+
+void SoftSerialSendOnry::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);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SoftSerialSendOnly/SoftSerial_Ticker.h	Sat Feb 27 05:38:49 2016 +0000
@@ -0,0 +1,38 @@
+//A modified version of the regular ticker/timeout libraries to allow us to do timeout without losing accuracy
+
+#ifndef FLEXTICKER_H
+#define FLEXTICKER_H
+
+#include "mbed.h"
+
+class FlexTicker: public TimerEvent {
+    public:
+    template<typename T>
+    void attach(T* tptr, void (T::*mptr)(void)) {
+        _function.attach(tptr, mptr);
+    }
+ 
+    /** Detach the function
+     */
+    void detach() {
+        remove();
+    }
+    
+    void setNext(int delay) {
+        insert(event.timestamp + delay);
+    }
+    
+    void prime(void) {
+        event.timestamp = us_ticker_read();
+    }
+ 
+protected:
+    virtual void handler() {
+        _function.call();
+    }
+ 
+    unsigned int _delay;
+    FunctionPointer _function;
+};
+
+#endif
\ No newline at end of file
--- a/main.cpp	Sat Nov 14 06:42:28 2015 +0000
+++ b/main.cpp	Sat Feb 27 05:38:49 2016 +0000
@@ -1,9 +1,9 @@
 #include "mbed.h"
-#include "SoftSerial.h"
+#include "SoftSerialSendOnry.h"
 
 AnalogIn thermistor(dp13);   /* Temperature sensor connected to Analog Grove connector */
 
-SoftSerial pc(dp10,dp11);
+SoftSerialSendOnry pc(dp10); // tx
 
 int main()
 {
--- a/softserial.lib	Sat Nov 14 06:42:28 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://developer.mbed.org/users/stueckler/code/softserial/#480d9575ac47