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-16
- Revision:
- 2:5318600ca0fe
- Parent:
- 1:3348d15fefdb
- Child:
- 3:23fae3efa1c0
File content as of revision 2:5318600ca0fe:
#include "mbed.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))
#define REED_PORT PA_3
#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_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_lock_write = 0;
}
int has_reed_changed(void) {
int change;
while(reed_lock_write) {}
reed_lock_read = 1;
change = reed_has_changed;
reed_has_changed = 0;
reed_lock_read = 0;
reed_gpio_value = reed_gpio.read();
return(change);
}
int has_reed_changed_since_last_sent(void) {
int change;
has_reed_changed();
reed_gpio_value = reed_gpio.read();
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_value;
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");
dot = mDot::getInstance();
dot->setLogLevel(MTSLog::TRACE_LEVEL);
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 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, false);
pc.printf("%d: Woke up\r\n",time(NULL));
}
}