Test program for PwmReader library
Dependencies: PwmReader mbed Shinyei
main.cpp
- Committer:
- abouillot
- Date:
- 2017-01-25
- Revision:
- 0:3e9bf225bd52
- Child:
- 1:e38ff2920f0e
File content as of revision 0:3e9bf225bd52:
/*! * Test application * * Read signal occupacy on a pin in a non blocking way using timer and interrupt * * Copyright (c) 2017 - Alexandre Bouillot github.com/abouillot * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documnetation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #include "mbed.h" #include "PwmReader.h" Timeout timer; DigitalOut my_pwm(LED1); // IO used by pwm_io function float delay = 5.0; // 5 sec int on_delay = 0; int off_delay = 0; void toggleOff(void); void toggleOn(void) { my_pwm = 1; timer.attach_us(toggleOff, on_delay); } void toggleOff(void) { my_pwm = 0; timer.attach_us(toggleOn, off_delay); } // 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; } if (dc >= 1) { my_pwm = 1; return; } on_delay = (int)(p_us * dc); off_delay = p_us - on_delay; toggleOn(); } 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()); } } } }