Test program for PwmReader library

Dependencies:   PwmReader mbed Shinyei

Committer:
abouillot
Date:
Wed Jan 25 08:53:15 2017 +0000
Revision:
0:3e9bf225bd52
Child:
1:e38ff2920f0e
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
abouillot 0:3e9bf225bd52 1 /*!
abouillot 0:3e9bf225bd52 2 * Test application
abouillot 0:3e9bf225bd52 3 *
abouillot 0:3e9bf225bd52 4 * Read signal occupacy on a pin in a non blocking way using timer and interrupt
abouillot 0:3e9bf225bd52 5 *
abouillot 0:3e9bf225bd52 6 * Copyright (c) 2017 - Alexandre Bouillot github.com/abouillot
abouillot 0:3e9bf225bd52 7 *
abouillot 0:3e9bf225bd52 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
abouillot 0:3e9bf225bd52 9 * of this software and associated documnetation files (the "Software"), to deal
abouillot 0:3e9bf225bd52 10 * in the Software without restriction, including without limitation the rights
abouillot 0:3e9bf225bd52 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
abouillot 0:3e9bf225bd52 12 * copies of the Software, and to permit persons to whom the Software is
abouillot 0:3e9bf225bd52 13 * furished to do so, subject to the following conditions:
abouillot 0:3e9bf225bd52 14 *
abouillot 0:3e9bf225bd52 15 * The above copyright notice and this permission notice shall be included in
abouillot 0:3e9bf225bd52 16 * all copies or substantial portions of the Software.
abouillot 0:3e9bf225bd52 17 *
abouillot 0:3e9bf225bd52 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
abouillot 0:3e9bf225bd52 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
abouillot 0:3e9bf225bd52 20 * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
abouillot 0:3e9bf225bd52 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
abouillot 0:3e9bf225bd52 22 * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
abouillot 0:3e9bf225bd52 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
abouillot 0:3e9bf225bd52 24 * THE SOFTWARE.
abouillot 0:3e9bf225bd52 25 */
abouillot 0:3e9bf225bd52 26
abouillot 0:3e9bf225bd52 27 #include "mbed.h"
abouillot 0:3e9bf225bd52 28 #include "PwmReader.h"
abouillot 0:3e9bf225bd52 29
abouillot 0:3e9bf225bd52 30 Timeout timer;
abouillot 0:3e9bf225bd52 31 DigitalOut my_pwm(LED1); // IO used by pwm_io function
abouillot 0:3e9bf225bd52 32
abouillot 0:3e9bf225bd52 33
abouillot 0:3e9bf225bd52 34 float delay = 5.0; // 5 sec
abouillot 0:3e9bf225bd52 35
abouillot 0:3e9bf225bd52 36 int on_delay = 0;
abouillot 0:3e9bf225bd52 37 int off_delay = 0;
abouillot 0:3e9bf225bd52 38
abouillot 0:3e9bf225bd52 39 void toggleOff(void);
abouillot 0:3e9bf225bd52 40
abouillot 0:3e9bf225bd52 41 void toggleOn(void)
abouillot 0:3e9bf225bd52 42 {
abouillot 0:3e9bf225bd52 43 my_pwm = 1;
abouillot 0:3e9bf225bd52 44 timer.attach_us(toggleOff, on_delay);
abouillot 0:3e9bf225bd52 45 }
abouillot 0:3e9bf225bd52 46
abouillot 0:3e9bf225bd52 47 void toggleOff(void)
abouillot 0:3e9bf225bd52 48 {
abouillot 0:3e9bf225bd52 49 my_pwm = 0;
abouillot 0:3e9bf225bd52 50 timer.attach_us(toggleOn, off_delay);
abouillot 0:3e9bf225bd52 51 }
abouillot 0:3e9bf225bd52 52
abouillot 0:3e9bf225bd52 53 // p_us = signal period in micro_seconds
abouillot 0:3e9bf225bd52 54 // dc = signal duty-cycle (0.0 to 1.0)
abouillot 0:3e9bf225bd52 55 void pwm_io(int p_us, float dc)
abouillot 0:3e9bf225bd52 56 {
abouillot 0:3e9bf225bd52 57 timer.detach();
abouillot 0:3e9bf225bd52 58 if ((p_us == 0) || (dc == 0)) {
abouillot 0:3e9bf225bd52 59 my_pwm = 0;
abouillot 0:3e9bf225bd52 60 return;
abouillot 0:3e9bf225bd52 61 }
abouillot 0:3e9bf225bd52 62 if (dc >= 1) {
abouillot 0:3e9bf225bd52 63 my_pwm = 1;
abouillot 0:3e9bf225bd52 64 return;
abouillot 0:3e9bf225bd52 65 }
abouillot 0:3e9bf225bd52 66 on_delay = (int)(p_us * dc);
abouillot 0:3e9bf225bd52 67 off_delay = p_us - on_delay;
abouillot 0:3e9bf225bd52 68 toggleOn();
abouillot 0:3e9bf225bd52 69 }
abouillot 0:3e9bf225bd52 70
abouillot 0:3e9bf225bd52 71
abouillot 0:3e9bf225bd52 72
abouillot 0:3e9bf225bd52 73 int main()
abouillot 0:3e9bf225bd52 74 {
abouillot 0:3e9bf225bd52 75 printf("************ PwmReader test program ****************\n");
abouillot 0:3e9bf225bd52 76 int i, j;
abouillot 0:3e9bf225bd52 77 PwmReader reader(USER_BUTTON);
abouillot 0:3e9bf225bd52 78
abouillot 0:3e9bf225bd52 79 while (1) {
abouillot 0:3e9bf225bd52 80 for (i=0; i<=10; i++) {
abouillot 0:3e9bf225bd52 81 for (j=1; j<=10; j++) {
abouillot 0:3e9bf225bd52 82 printf("test with %f %% occupacy ", 0.10 * i);
abouillot 0:3e9bf225bd52 83 printf("test with %ld ms period ", j * 10);
abouillot 0:3e9bf225bd52 84
abouillot 0:3e9bf225bd52 85 pwm_io(j * 10000, 0.10 * i); // 2s - 25%
abouillot 0:3e9bf225bd52 86
abouillot 0:3e9bf225bd52 87 reader.start();
abouillot 0:3e9bf225bd52 88 wait(delay);
abouillot 0:3e9bf225bd52 89 reader.stop();
abouillot 0:3e9bf225bd52 90
abouillot 0:3e9bf225bd52 91 printf("down:%f %% up:%f %%\n", reader.occupacyLow(), reader.occupacyHigh());
abouillot 0:3e9bf225bd52 92
abouillot 0:3e9bf225bd52 93 }
abouillot 0:3e9bf225bd52 94 }
abouillot 0:3e9bf225bd52 95 }
abouillot 0:3e9bf225bd52 96 }