smple
Dependencies: ESP8266Interface IFTTT mbed
Fork of IFTTT_ESP8266_Example by
Revision 4:3994cd534bd5, committed 2015-07-27
- Comitter:
- jksoft
- Date:
- Mon Jul 27 11:43:18 2015 +0000
- Parent:
- 3:1f278004a3c7
- Commit message:
- rev0
Changed in this revision
diff -r 1f278004a3c7 -r 3994cd534bd5 SoftSerialSendOnly/SoftSerialSendOnry.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SoftSerialSendOnly/SoftSerialSendOnry.cpp Mon Jul 27 11:43:18 2015 +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
diff -r 1f278004a3c7 -r 3994cd534bd5 SoftSerialSendOnly/SoftSerialSendOnry.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SoftSerialSendOnly/SoftSerialSendOnry.h Mon Jul 27 11:43:18 2015 +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 +
diff -r 1f278004a3c7 -r 3994cd534bd5 SoftSerialSendOnly/SoftSerialSendOnry_tx.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SoftSerialSendOnly/SoftSerialSendOnry_tx.cpp Mon Jul 27 11:43:18 2015 +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); +}
diff -r 1f278004a3c7 -r 3994cd534bd5 SoftSerialSendOnly/SoftSerial_Ticker.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SoftSerialSendOnly/SoftSerial_Ticker.h Mon Jul 27 11:43:18 2015 +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
diff -r 1f278004a3c7 -r 3994cd534bd5 main.cpp --- a/main.cpp Tue Jul 14 21:48:01 2015 +0000 +++ b/main.cpp Mon Jul 27 11:43:18 2015 +0000 @@ -2,28 +2,22 @@ #include "ESP8266Interface.h" #include "TCPSocketConnection.h" #include "ifttt.h" +#include "SoftSerialSendOnry.h" -ESP8266Interface wifi(D1,D0,D2,"ssid","passkey",115200); // TX,RX,Reset,SSID,Password,Baud -RawSerial pc(USBTX, USBRX); // tx, rx +ESP8266Interface wifi(dp16,dp15,dp4,"SSID","Password",115200); // TX,RX,Reset,SSID,Password,Baud +SoftSerialSendOnry pc(dp10); // tx int main() { pc.baud(9600); wifi.init(); //Reset wifi.connect(); //Use DHCP - printf("IP Address is %s \n\r", wifi.getIPAddress()); + pc.printf("IP Address is %s \n\r", wifi.getIPAddress()); TCPSocketConnection socket; // Initialize ifttt object, add up to 3 optional values, trigger event. - IFTTT ifttt("YourEventName","ChangeToYourSecretKey", &socket); // EventName, Secret Key, socket to use - ifttt.addIngredients("this is awesome","test-ing","data!!!"); // 3 optional Values to send along with trigger. - ifttt.trigger(); - - ifttt.addIngredients("Sending","GET","data"); - ifttt.trigger(IFTTT_GET); - - ifttt.addIngredients("Sending","POST","things"); + IFTTT ifttt("EventName","Secret Key", &socket); // EventName, Secret Key, socket to use + + ifttt.addIngredients("value1","value2","value3"); ifttt.trigger(IFTTT_POST); - while(1) { - } }
diff -r 1f278004a3c7 -r 3994cd534bd5 mbed.bld --- a/mbed.bld Tue Jul 14 21:48:01 2015 +0000 +++ b/mbed.bld Mon Jul 27 11:43:18 2015 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/7cff1c4259d7 \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/bad568076d81 \ No newline at end of file