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
main.cpp
- Committer:
- boddeke
- Date:
- 2016-05-19
- Revision:
- 3:23fae3efa1c0
- Parent:
- 2:5318600ca0fe
- Child:
- 4:0fb159501a04
File content as of revision 3:23fae3efa1c0:
#include "mbed.h" #include "rtos.h" #include "mDot.h" #include "MTSLog.h" #include "MTSText.h" using namespace mts; #define MIN(a,b) (((a)<(b))?(a):(b)) #define MAX(a,b) (((a)>(b))?(a):(b)) // http://www.multitech.net/developer/forums/topic/mdot-pinout-associate-and-onsleep/ // Keep in mind that if you want to put the device in deepsleep mode // and wake up with an interrupt, pin D3/XBEE_CTS/XBEE_DIO7/PA_0 is // the only pin that can be used. #define REED_PORT PA_0 #define MINIMAL_SECONDS_BETEEN_CONTACT 10 #define MAXIMAL_SECONDS_BETEEN_CONTACT 60 // https://developer.mbed.org/users/mbed_official/code/mbed/docs/252557024ec3/classmbed_1_1RawSerial.html RawSerial pc(PA_2,NC); //RawSerial pc(USBTX,USBRX); DigitalIn reed_gpio(REED_PORT); volatile int reed_has_changed = 0; int reed_value_to_send = 0; int reed_value_last_sent = 0; int reed_gpio_value = 0; void isr_reed_sensor_change(void) { reed_has_changed++; } int has_reed_changed(void) { int change; change = reed_has_changed; reed_has_changed = 0; reed_gpio_value = reed_gpio.read(); return(change); } int has_reed_changed_since_last_sent(void) { int change; has_reed_changed(); change = reed_gpio_value != reed_value_last_sent; pc.printf("%d: REED = %d (was %d)\r\n",time(NULL),reed_gpio_value,reed_value_last_sent); return(change); } void send_reed(void) { reed_value_last_sent = reed_gpio.read(); pc.printf("%d: Sending REED = %d\r\n",time(NULL),reed_value_last_sent); Thread::wait(10); } int main() { mDot* dot; long last_sent = 0; long last_sent_change = 0; 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"); reed_gpio.mode(PullUp); for(int i=0;i<10;i++) { pc.printf("before mdot: %d (%d)\r\n",reed_gpio.read(),reed_has_changed); Thread::wait(1000); } dot = mDot::getInstance(); dot->setLogLevel(MTSLog::TRACE_LEVEL); for(int i=0;i<10;i++) { pc.printf("after mdot %d (%d)\r\n",reed_gpio.read(),reed_has_changed); Thread::wait(1000); } 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); reed_sensor_change->mode(PullUp); for(int i=0;i<10;i++) { pc.printf("after int %d (%d)\r\n",reed_gpio.read(),reed_has_changed); Thread::wait(1000); } while (true) { int wakeup_time = last_sent + MAXIMAL_SECONDS_BETEEN_CONTACT; int time_since_last_sent = time(NULL) - last_sent; int time_since_last_sent_change = time(NULL) - last_sent_change; if(time_since_last_sent >= MAXIMAL_SECONDS_BETEEN_CONTACT) { pc.printf("%d: Sending because max-time\r\n",time(NULL)); has_reed_changed(); send_reed(); last_sent = time(NULL); wakeup_time = last_sent + MAXIMAL_SECONDS_BETEEN_CONTACT; } else if(has_reed_changed() || has_reed_changed_since_last_sent()) { pc.printf("%d: Change!\r\n",time(NULL)); if(time_since_last_sent_change >= MINIMAL_SECONDS_BETEEN_CONTACT) { int b=100; while(has_reed_changed()&&(b--)) { Thread::wait(10); } if(has_reed_changed_since_last_sent()) { pc.printf("%d: Sending because of change\r\n",time(NULL)); send_reed(); last_sent_change = time(NULL); wakeup_time = last_sent_change + MAXIMAL_SECONDS_BETEEN_CONTACT; } else { pc.printf("%d: No change after debounce\r\n",time(NULL)); } } else { pc.printf("%d: Not sending because min-time\r\n",time(NULL)); wakeup_time = MIN(wakeup_time, last_sent_change + MINIMAL_SECONDS_BETEEN_CONTACT); } } int sleep_time = wakeup_time - time(NULL); pc.printf("%d: Sleeping %d seconds\r\n",time(NULL),sleep_time); Thread::wait(10); dot->sleep(sleep_time, mDot::RTC_ALARM_OR_INTERRUPT, true); pc.printf("%d: Woke up\r\n",time(NULL)); } }