Test program for PwmReader library
Dependencies: PwmReader mbed Shinyei
Revision 1:e38ff2920f0e, committed 2017-01-26
- Comitter:
- abouillot
- Date:
- Thu Jan 26 15:10:16 2017 +0000
- Parent:
- 0:3e9bf225bd52
- Commit message:
- Test program for Shinyei PPD42NJ particles sensor.
Changed in this revision
diff -r 3e9bf225bd52 -r e38ff2920f0e PwmReader.lib --- a/PwmReader.lib Wed Jan 25 08:53:15 2017 +0000 +++ b/PwmReader.lib Thu Jan 26 15:10:16 2017 +0000 @@ -1,1 +1,1 @@ -https://developer.mbed.org/users/abouillot/code/PwmReader/#15aa9d3aeb2e +https://developer.mbed.org/users/abouillot/code/PwmReader/#ebc39fb22351
diff -r 3e9bf225bd52 -r e38ff2920f0e Shinyei.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Shinyei.lib Thu Jan 26 15:10:16 2017 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/abouillot/code/Shinyei/#995d42a37328
diff -r 3e9bf225bd52 -r e38ff2920f0e main.cpp --- a/main.cpp Wed Jan 25 08:53:15 2017 +0000 +++ b/main.cpp Thu Jan 26 15:10:16 2017 +0000 @@ -1,7 +1,7 @@ /*! * Test application * - * Read signal occupacy on a pin in a non blocking way using timer and interrupt + * Read particle concentration using a Shinyei PPD42NJ sensor * * Copyright (c) 2017 - Alexandre Bouillot github.com/abouillot * @@ -23,74 +23,57 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ - - #include "mbed.h" -#include "PwmReader.h" + +#include "mbed.h" +#include "Shinyei.h" + +Serial pc(SERIAL_TX, SERIAL_RX); -Timeout timer; -DigitalOut my_pwm(LED1); // IO used by pwm_io function +DigitalOut myled(LED1); - -float delay = 5.0; // 5 sec +// define asynch_test to demonstrate the non blocking usage +//#define asynch_test +#define asynch_test -int on_delay = 0; -int off_delay = 0; - -void toggleOff(void); +#if defined(asynch_test) +Shinyei shinyei(USER_BUTTON); -void toggleOn(void) +void completeSampling() { - my_pwm = 1; - timer.attach_us(toggleOff, on_delay); + printf("Concentration: %f\n", shinyei.concentration()); + shinyei.startSampling(completeSampling, 30); } +#endif + +int main() +{ + printf("**************** PwmReader test program ****************\n"); -void toggleOff(void) -{ - my_pwm = 0; - timer.attach_us(toggleOn, off_delay); -} +#if defined(asynch_test) + shinyei.startSampling(completeSampling, 30); -// p_us = signal period in micro_seconds -// dc = signal duty-cycle (0.0 to 1.0) -void pwm_io(int p_us, float dc) -{ - timer.detach(); - if ((p_us == 0) || (dc == 0)) { - my_pwm = 0; - return; + int i = 1; + pc.printf("Hello World !\n"); + while(1) { + wait(1); + pc.printf(".", i++, shinyei.concentration()); + myled = !myled; } - if (dc >= 1) { - my_pwm = 1; - return; +#else + Shinyei shinyei(USER_BUTTON); + while (1) { + shinyei.startSampling(30); + + while (!shinyei.dataReady()) { + pc.printf("."); + wait(.5); + } + wait(5); + pc.printf("\n"); + + pc.printf("Concentration: %f\n", shinyei.concentration()); } - on_delay = (int)(p_us * dc); - off_delay = p_us - on_delay; - toggleOn(); +#endif } - -int main() -{ - printf("************ PwmReader test program ****************\n"); - int i, j; - PwmReader reader(USER_BUTTON); - - while (1) { - for (i=0; i<=10; i++) { - for (j=1; j<=10; j++) { - printf("test with %f %% occupacy ", 0.10 * i); - printf("test with %ld ms period ", j * 10); - - pwm_io(j * 10000, 0.10 * i); // 2s - 25% - - reader.start(); - wait(delay); - reader.stop(); - - printf("down:%f %% up:%f %%\n", reader.occupacyLow(), reader.occupacyHigh()); - - } - } - } -} \ No newline at end of file