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: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
Diff: io.cpp
- Revision:
- 15:0ccf0f530a05
- Parent:
- 14:56da550a1baa
- Child:
- 17:877a9a3148a4
--- a/io.cpp Tue Feb 18 13:59:21 2014 +0000
+++ b/io.cpp Tue Feb 18 15:36:12 2014 +0000
@@ -1,11 +1,7 @@
#include "io.h"
#include "rtos.h"
-#define S_INIT 0
-#define S_OPEN 1
-#define S_GONE 2
-
-void thread_callback(void const*);
+void timer_callback(void const*);
// Using Arduino pin notation
LM75B tempSensor(SDA, SCL);
@@ -13,23 +9,26 @@
DigitalIn button(p14);
//C12832 lcdDisplay(D11, D13, D12, D7, D10);
-Thread worker(thread_callback);
+RtosTimer *timer;
+
+bool tempFound = false, accFound = false;
+uint32_t count = 0;
+bool btnPressed = false;
-uint8_t tempState = S_INIT;
-uint8_t accState = S_INIT;
-uint32_t count = 0;
+void io_init(void)
+{
+ timer = new RtosTimer(&timer_callback, osTimerPeriodic);
+ timer->start(50);
+ tempFound = tempSensor.open();
+ accFound = accSensor.testConnection();
+}
float temperature()
{
- if ((tempState == S_INIT) && (tempSensor.open()))
- tempState = S_OPEN;
- else
- tempState = S_GONE;
-
- if (tempState == S_OPEN)
- return tempSensor.temp();
+ if (!tempFound)
+ return 0.0;
- return 0.0;
+ return tempSensor.temp();
}
acceleration_t acceleration()
@@ -37,12 +36,7 @@
float data[3];
acceleration_t ret = { 0.0, 0.0, 0.0 };
- if ((accState == S_INIT) && (accSensor.testConnection()))
- accState = S_OPEN;
- else
- accState = S_GONE;
-
- if (accState == S_OPEN) {
+ if (accFound) {
accSensor.readData(data);
ret.x = data[0];
ret.y = data[1];
@@ -57,13 +51,9 @@
return count;
}
-void thread_callback(void const*)
+void timer_callback(void const*)
{
- bool pressed = false;
-
- while (true) {
- if ((!pressed) && (button))
- count++;
- pressed = button;
- }
+ if ((!btnPressed) && (button))
+ count++;
+ btnPressed = button;
}
