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.
Dependencies: libmDot mbed-rtos mbed
Diff: main.cpp
- Revision:
- 0:208bd045dd31
- Child:
- 1:3348d15fefdb
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu May 12 13:46:17 2016 +0000 @@ -0,0 +1,112 @@ +#include "mbed.h" +#include "mDot.h" +#include "MTSLog.h" +#include "MTSText.h" + +using namespace mts; + +#define REED_PORT PA_3 +Serial pc(USBTX,USBRX); +DigitalIn reed_gpio(REED_PORT); + + +int reed_lock_write = 0; +int reed_lock_read = 0; +int reed_has_changed = 0; +int reed_value = 0; +int reed_value_to_send = 0; +int reed_value_last_sent = 0; + + void isr_reed_sensor_change(void) { + + while(reed_lock_read) {} + reed_lock_write = 1; + + reed_has_changed = 1; + reed_value = reed_gpio; + + reed_lock_write = 0; +} + +int has_reed_changed(void) { + + int change; + + while(reed_lock_write) {} + reed_lock_read = 1; + + change = reed_value_last_sent != reed_value; + reed_value_to_send = reed_value; + reed_has_changed = 0; + + reed_lock_read = 0; + + return(change); +} + +void has_reed_changed_since_last_sent(void) { + + int change; + + has_reed_changed(); + + change = reed_value_to_send != reed_value_last_sent; + + return(change); +} + +void send_reed(void) { + + reed_value_last_sent = reed_value_to_send; + + pc.printf("Sending REED = %d\r\n",reed_value_to_send); +} + + +int main() { + + mDot* dot; + long last_sent = 0; + + pc.attach(&callback,RawSerial::RxIrq); + pc.baud(115200); + + pc.printf("\r\n\r\n********************************\r\n"); + pc.printf( "* *\r\n"); + pc.printf( "* Edge Reed *\r\n"); + pc.printf( "* *\r\n"); + pc.printf( "* Build: " __DATE__ ", " __TIME__" *\r\n"); + pc.printf( "********************************\r\n\r\n"); + + dot = mDot::getInstance(); + + InterruptIn *reed_sensor_change; + reed_sensor_change = new InterruptIn(REED_PORT); + reed_sensor_change->fall(&isr_reed_sensor_change); + reed_sensor_change->rise(&isr_reed_sensor_change); + + while (true) { + + int time_since_last_sent = time(NULL) - last_sent; + + if(time_since_last_sent > 60) { + + has_reed_changed(); + send_reed(); + last_sent = time(NULL); + } + + if(has_reed_changed() && time_since_last_sent>10) { + + Thread::wait(100); + + if(has_reed_changed_since_last_sent()) { + + send_reed(); + last_sent = time(NULL); + } + } + + dot->sleep(sleep_time, mDot::RTC_ALARM_OR_INTERRUPT, true); + } +} \ No newline at end of file