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:
- 1:3348d15fefdb
- Parent:
- 0:208bd045dd31
- Child:
- 2:5318600ca0fe
--- a/main.cpp Thu May 12 13:46:17 2016 +0000
+++ b/main.cpp Mon May 16 20:15:32 2016 +0000
@@ -5,26 +5,30 @@
using namespace mts;
+#define MIN(a,b) (((a)<(b))?(a):(b))
+#define MAX(a,b) (((a)>(b))?(a):(b))
+
#define REED_PORT PA_3
-Serial pc(USBTX,USBRX);
+#define MINIMAL_SECONDS_BETEEN_CONTACT 10
+#define MAXIMAL_SECONDS_BETEEN_CONTACT 60
+
+//Serial pc(USBTX,USBRX);
+RawSerial 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;
+int reed_gpio_value = 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;
}
@@ -34,32 +38,36 @@
while(reed_lock_write) {}
reed_lock_read = 1;
-
- change = reed_value_last_sent != reed_value;
- reed_value_to_send = reed_value;
+ change = reed_has_changed;
reed_has_changed = 0;
-
reed_lock_read = 0;
-
+
+ reed_gpio_value = reed_gpio.read();
+
return(change);
}
-void has_reed_changed_since_last_sent(void) {
+int has_reed_changed_since_last_sent(void) {
int change;
-
+
has_reed_changed();
+ reed_gpio_value = reed_gpio.read();
- change = reed_value_to_send != reed_value_last_sent;
+ 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_value_to_send;
-
- pc.printf("Sending REED = %d\r\n",reed_value_to_send);
+ reed_value_last_sent = reed_gpio_value;
+
+ pc.printf("%d: Sending REED = %d\r\n",time(NULL),reed_value_last_sent);
+
+ Thread::wait(10);
}
@@ -67,8 +75,8 @@
mDot* dot;
long last_sent = 0;
+ long last_sent_change = 0;
- pc.attach(&callback,RawSerial::RxIrq);
pc.baud(115200);
pc.printf("\r\n\r\n********************************\r\n");
@@ -79,6 +87,7 @@
pc.printf( "********************************\r\n\r\n");
dot = mDot::getInstance();
+ dot->setLogLevel(MTSLog::TRACE_LEVEL);
InterruptIn *reed_sensor_change;
reed_sensor_change = new InterruptIn(REED_PORT);
@@ -87,26 +96,61 @@
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 > 60) {
+ 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);
+ }
}
- 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);
- }
- }
-
+ 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));
}
}
\ No newline at end of file