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.
Revision 0:fefcd43a3608, committed 2015-01-22
- Comitter:
- BuntarouShizuki
- Date:
- Thu Jan 22 08:48:09 2015 +0000
- Commit message:
- hello
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/TouchSense.cpp Thu Jan 22 08:48:09 2015 +0000
@@ -0,0 +1,6 @@
+#include "TouchSense.h"
+
+void TouchSense::threadStarter(void const *p) {
+ TouchSense *instance = (TouchSense *)p;
+ instance->loop();
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/TouchSense.h Thu Jan 22 08:48:09 2015 +0000
@@ -0,0 +1,195 @@
+#include "mbed.h"
+#include "rtos.h"
+
+//#define DEBUG
+
+// for profiling
+#define PRSIZE 500
+#define START_THREAD 1
+
+class FrameRate {
+ float rate;
+ int counter;
+ Timer timer;
+
+public:
+ FrameRate() {
+ rate = 0;
+ counter = 0;
+ timer.start();
+ }
+
+ void update() {
+ counter++;
+ if (counter == 10) {
+ rate = 1000*10.0/timer.read_ms();
+ counter = 0;
+ timer.reset();
+ }
+ }
+
+ float getRate() {
+ return rate;
+ }
+};
+
+class TouchSense
+{
+ float V0;
+ float V1;
+ float C1;
+
+ DigitalOut &dout;
+ AnalogIn &ain;
+
+ static const int BUFSIZE = 3;
+ int buf[BUFSIZE];
+ int bufindex;
+
+ int state;
+
+ FrameRate rater;
+
+ static void threadStarter(void const *p);
+ Thread _thread;
+public:
+ TouchSense(DigitalOut &dout, AnalogIn &ain): V0(0.0), V1(0.5), C1(20), dout(dout), ain(ain),
+ _thread(&TouchSense::threadStarter, this, osPriorityNormal, 10240) {
+ // XXX: 0.5 must be defined automatically
+ state = 0;
+ calibrate();
+
+ _thread.signal_set(START_THREAD);
+ }
+
+ int touched() {
+ return state;
+ }
+
+private:
+ void calibrate() {
+ printf("TouchSense.calibrate\r\n");
+ dout.write(0);
+ Thread::wait(1000);
+
+ // initialize V0
+ float s = 0.0;
+ for (int i = 0; i < 10; i++) {
+ float level = ain.read();
+ printf("TouchSense.calibrate: level=%f\r\n", level);
+ if (s < level)
+ s = level;
+ Thread::wait(100);
+ }
+ V0 = s;
+ printf("TouchSense.calibrate: V0=%f\r\n", V0);
+
+ // initialize buf, bufindex, and C1
+ for (int i = 0; i < BUFSIZE; i++) {
+ buf[i] = sense();
+ printf("TouchSense.calibrate: sense=%d\r\n", buf[i]);
+ }
+ int sum = 0;
+ for (int i = 0; i < BUFSIZE; i++)
+ sum += buf[i];
+ C1 = sum*3.0/BUFSIZE; // XXX: 3 is a magic number
+ bufindex = 0;
+ printf("TouchSense.calibrate: C1=%f\r\n", C1);
+ }
+
+ void loop() {
+ _thread.signal_wait(START_THREAD);
+ printf("TouchSense.loop: start\r\n");
+ for (;;) {
+ int r = sense();
+ if (r == -1)
+ buf[bufindex] = C1+1;
+ else
+ buf[bufindex] = r;
+ bufindex = (bufindex+1)%BUFSIZE;
+
+ int sum = 0;
+ for (int i = 0; i < BUFSIZE; i++)
+ sum += buf[i];
+ float avg = (float)sum/BUFSIZE;
+
+ if (avg > C1) // touhced for 0.001*BUFSIZE sec
+ state = 1; // touched
+ else
+ state = 0; // untouched
+
+ rater.update();
+ Thread::wait(5);
+ }
+ }
+
+ int sense() {
+ DigitalIn prswitch(p5);
+ prswitch.mode(PullUp);
+
+ // step 1
+ int xxx = 0;
+ dout.write(0);
+#ifdef DEBUG
+ printf("touched: ain=%f\n\r", ain.read());
+#endif
+ while (ain.read() > V0) {
+ xxx++;
+ if (xxx > 100) { // 0.1 sec
+ printf("touched: warning ain=%f\n\r", ain.read());
+ return -1;
+ }
+ Thread::wait(1);
+ }
+
+ // step 2
+ dout.write(1);
+
+ // step 3
+ int count = 0;
+
+ if (prswitch) {
+ // mode: normal
+ for (;;) {
+ if (count > C1)
+ break;
+ if (ain.read() > V1)
+ break;
+ count++;
+ }
+#ifdef DEBUG
+ printf("touched: count=%d\r\n", count);
+#endif
+ } else {
+ // mode: profiling
+ printf("rate = %f\r\n", rater.getRate());
+
+ float prdata[PRSIZE];
+ float prtime[PRSIZE];
+ Timer timer;
+
+ timer.start();
+ for (;;) {
+ prdata[count] = ain.read();
+ prtime[count] = timer.read();
+ if (prdata[count] > V1)
+ break;
+ count++;
+ if (count == PRSIZE) {
+ printf("touched: warning[count==PRSIZE]\r\n");
+ count--;
+ break;
+ }
+ }
+ printf("# n=%d V0=%f V1=%f\r\n", count+1, V0, V1);
+ for (int i = 0; i <= count; i++)
+ printf("%f %f\r\n", prtime[i], prdata[i]);
+ printf("\r\n");
+ }
+
+ // step 4
+ dout.write(0);
+
+ return count;
+ }
+};
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Thu Jan 22 08:48:09 2015 +0000
@@ -0,0 +1,90 @@
+#include "mbed.h"
+#include "TouchSense.h"
+
+/*
+// 1\
+// |\
+// | \
+// | \
+// +---\---------> t
+// 1
+float brightness(float t) {
+if (t < 0)
+return 0;
+if (t > 1)
+return 0;
+return 1-t;
+}
+*/
+
+/*
+// 1| /\
+// |/ \
+// .5| \
+// | \
+// +--|---\---------> t
+// 1/3 1
+float brightness(float t) {
+if (t < 0)
+return 0;
+if (0 <= t && t < 1/3.0)
+return .5+t*1.5;
+if (1/3.0 <= t && t < 1)
+return 1.5-t*1.5;
+if (1 <= t)
+return 0;
+}
+*/
+
+// 1| /\
+// | / \
+// | / \
+// |/ \
+// +---|----\---------> t
+// 1/2 1
+float brightness(float t)
+{
+ if (t < 0)
+ return 0;
+ if (0 <= t && t < 0.5)
+ return t*2;
+ if (0.5 <= t && t < 1)
+ return 2-t*2;
+ if (1 <= t)
+ return 0;
+}
+
+int main()
+{
+ PwmOut led1(p21), led2(p22), led3(p23);
+ Timer timer;
+
+ DigitalOut out(p19);
+ AnalogIn in(p20);
+
+ TouchSense sensor1(out, in);
+ //sensor1.calibrate();
+
+ for (;;) {
+ if (! sensor1.touched()) {
+ Thread::wait(1);
+ continue;
+ }
+
+ timer.reset();
+ timer.start();
+ for (;;) {
+ float t = timer.read();
+ if (t > 1.2)
+ break;
+ led1 = brightness(t);
+ led2 = brightness(t-0.1);
+ led3 = brightness(t-0.2);
+ Thread::wait(1);
+ }
+ }
+}
+
+// Local Variables: **
+// c-basic-offset:4 **
+// End: **
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Thu Jan 22 08:48:09 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#f1ef95efa5ad
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Jan 22 08:48:09 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/4fc01daae5a5 \ No newline at end of file