Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Revision:
1:0ba687d4196f
Child:
2:fe1566cdb6e7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/io/MySerial.h	Mon Apr 13 12:23:44 2015 +0000
@@ -0,0 +1,132 @@
+/*
+ *
+ * Replacement for Serial, so UART flow control is inited properly.    ALS 20150412
+ *
+ * MySerialBase is a replacement for SerialBase, to prevent it from calling
+ *  the faulty-for-nRF51822-uart-init serial_init()
+ *  in mbed/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/serial_api.c 
+ *
+ */
+
+#ifndef MYSERIAL_H
+#define MYSERIAL_H
+
+#include "mbed.h"
+#include <cstdarg>
+
+namespace moo
+{
+
+/** A base class for serial port implementations
+ * Can't be instantiated directly (use MySerial)
+ */
+class MySerialBase
+{
+  public:
+    /** Set the baud rate of the serial port
+     *
+     *  @param baudrate The baudrate of the serial port (default = 9600).
+     */
+    void baud( int baudrate );
+
+    /** Set the transmission format used by the serial port
+     *
+     *  @param bits The number of bits in a word (5-8; 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 (1 or 2; default = 1)
+     */
+    void format( int bits=8, SerialBase::Parity parity=SerialBase::None, int stop_bits=1 );
+
+    /** Determine if there is a character available to read
+     *
+     *  @returns
+     *    1 if there is a character available to read,
+     *    0 otherwise
+     */
+    int readable();
+
+    /** 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), SerialBase::IrqType type=SerialBase::RxIrq );
+
+    /** 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), SerialBase::IrqType type=SerialBase::RxIrq )
+    {
+        if( (mptr != NULL) && (tptr != NULL) )
+        {
+            _my_irq[type].attach( tptr, mptr );
+            serial_irq_set( &_my_serial, (SerialIrq)type, 1 );
+        }
+    }
+
+    /** Generate a break condition on the serial line
+     */
+    void send_break();
+
+    static void _irq_handler( uint32_t id, SerialIrq irq_type );
+
+  protected:
+    MySerialBase( PinName tx, PinName rx, PinName rts/*=RTS_PIN_NUMBER*/, PinName cts/*=CTS_PIN_NUMBER*/ );
+    virtual ~MySerialBase()
+    {
+    }
+
+    int _base_getc();
+    int _base_putc( int c );
+
+    serial_t        _my_serial;
+    FunctionPointer _my_irq[2];
+    int             _my_baud;
+};
+
+class MySerial : public MySerialBase, public Stream
+{
+  public:
+    /** Create a Serial port, connected to the specified transmit and receive pins
+     *
+     *  @param tx Transmit pin
+     *  @param rx Receive pin
+     *
+     *  @note
+     *    Either tx or rx may be specified as NC if unused
+     */
+    MySerial( PinName tx,                 PinName rx, const char *name=NULL,
+              PinName rts=NC/* RTS_PIN_NUMBER */, PinName cts=NC /* CTS_PIN_NUMBER */ );
+
+    /** Write a string to the serial port
+     *
+     * @param str The string to write
+     *
+     * @returns 0 if the write succeeds, EOF for error
+     */
+    int puts( const char *str );
+
+    int printf( const char *format, ... );
+
+    int vprintf( const char *format, va_list arg );
+
+  protected:
+    virtual int _getc();
+    virtual int _putc( int c );
+};
+
+}  // namespace moo
+
+#endif /* MYSERIAL_H */