Provides a simple way to generate complex square wave signals on any available pin. In addition the SignalGenerator can generate a carrier wave which is useful when generating IR signals to control electronic devices like a TV etc. The signal generation can be carried out either synchronously or asynchronously. In the case of synchronous signal generation all interrupts can optionally be disabled to improve timing accuracy.
SignalGenerator.h@4:64d2d834341b, 2014-09-14 (annotated)
- Committer:
- taylorza
- Date:
- Sun Sep 14 05:36:57 2014 +0000
- Revision:
- 4:64d2d834341b
- Parent:
- 3:f30dcc6e8e70
Added support for asynchronous signal generation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
taylorza | 1:4a1bcc41c473 | 1 | /////////////////////////////////////////////////////////////////////////////// |
taylorza | 1:4a1bcc41c473 | 2 | // Signal Generator |
taylorza | 1:4a1bcc41c473 | 3 | // Author: Chris Taylor (taylorza) |
taylorza | 0:b7c65c0d82d3 | 4 | #ifndef __SIGNALGENERATOR_H__ |
taylorza | 0:b7c65c0d82d3 | 5 | #define __SIGNALGENERATOR_H__ |
taylorza | 0:b7c65c0d82d3 | 6 | |
taylorza | 0:b7c65c0d82d3 | 7 | /** Simplifies generation of a high frequency signal on any pin with optional support for a carrier frequency. |
taylorza | 0:b7c65c0d82d3 | 8 | */ |
taylorza | 0:b7c65c0d82d3 | 9 | class SignalGenerator |
taylorza | 0:b7c65c0d82d3 | 10 | { |
taylorza | 0:b7c65c0d82d3 | 11 | public: |
taylorza | 0:b7c65c0d82d3 | 12 | /** Create a SignalGenerator tied to the specified pin. */ |
taylorza | 0:b7c65c0d82d3 | 13 | SignalGenerator(PinName pin); |
taylorza | 0:b7c65c0d82d3 | 14 | |
taylorza | 4:64d2d834341b | 15 | /** Clean up the SignalGenerator data. */ |
taylorza | 4:64d2d834341b | 16 | ~SignalGenerator(); |
taylorza | 4:64d2d834341b | 17 | |
taylorza | 0:b7c65c0d82d3 | 18 | /** Set the state of the pin associated with the SignalGenerator. */ |
taylorza | 0:b7c65c0d82d3 | 19 | void set(bool pinState); |
taylorza | 0:b7c65c0d82d3 | 20 | |
taylorza | 4:64d2d834341b | 21 | /** Generates the square wave signal on the pin associated with the SignalGenerator. |
taylorza | 0:b7c65c0d82d3 | 22 | * @param initialState Defines the initial state of the signal pin |
taylorza | 0:b7c65c0d82d3 | 23 | * @param timingBuffer Specificies the wime periods in microseconds before the signal pin changes state |
taylorza | 0:b7c65c0d82d3 | 24 | * @param bufferCount The count of transition times passed in the timingBuffer |
taylorza | 4:64d2d834341b | 25 | * @param disableInterrupts If true disables interrupts during the generation of the signal |
taylorza | 0:b7c65c0d82d3 | 26 | * @param lastStateHoldTime The time in microseconds that the last state is held |
taylorza | 0:b7c65c0d82d3 | 27 | * @param carrierFrequency The carrier frequency in Hz |
taylorza | 0:b7c65c0d82d3 | 28 | */ |
taylorza | 1:4a1bcc41c473 | 29 | void set( |
taylorza | 1:4a1bcc41c473 | 30 | bool initialState, |
taylorza | 1:4a1bcc41c473 | 31 | uint32_t timingBuffer[], |
taylorza | 1:4a1bcc41c473 | 32 | uint16_t bufferCount, |
taylorza | 4:64d2d834341b | 33 | bool disableInterrupts, |
taylorza | 1:4a1bcc41c473 | 34 | uint32_t lastStateHoldTime = 0, |
taylorza | 2:b2a449bd787f | 35 | int32_t carrierFrequency = -1); |
taylorza | 4:64d2d834341b | 36 | |
taylorza | 4:64d2d834341b | 37 | /** Asynchronously generates the square wave signal on the pin associated with the SignalGenerator. |
taylorza | 4:64d2d834341b | 38 | * @param initialState Defines the initial state of the signal pin |
taylorza | 4:64d2d834341b | 39 | * @param timingBuffer Specificies the wime periods in microseconds before the signal pin changes state |
taylorza | 4:64d2d834341b | 40 | * @param bufferCount The count of transition times passed in the timingBuffer |
taylorza | 4:64d2d834341b | 41 | * @param repeat If true the signal generation will wrap around to the begining of the timingBuffer after each iteration through the buffer completes. |
taylorza | 4:64d2d834341b | 42 | * @note To stop the asynchronous signal you can call any of the non-async set commands to set the pin to a final state. |
taylorza | 4:64d2d834341b | 43 | */ |
taylorza | 4:64d2d834341b | 44 | void setAsync( |
taylorza | 4:64d2d834341b | 45 | bool initialState, |
taylorza | 4:64d2d834341b | 46 | uint32_t timingBuffer[], |
taylorza | 4:64d2d834341b | 47 | uint16_t bufferCount, |
taylorza | 4:64d2d834341b | 48 | bool repeat); |
taylorza | 1:4a1bcc41c473 | 49 | |
taylorza | 0:b7c65c0d82d3 | 50 | private: |
taylorza | 4:64d2d834341b | 51 | void asyncStep(); |
taylorza | 4:64d2d834341b | 52 | void stopAsync(); |
taylorza | 4:64d2d834341b | 53 | |
taylorza | 4:64d2d834341b | 54 | private: |
taylorza | 4:64d2d834341b | 55 | DigitalOut _pin; |
taylorza | 4:64d2d834341b | 56 | Timeout *_pTicker; |
taylorza | 4:64d2d834341b | 57 | uint32_t *_pInternalTimingBuffer; |
taylorza | 4:64d2d834341b | 58 | uint16_t _bufferCount; |
taylorza | 4:64d2d834341b | 59 | uint16_t _bufferIndex; |
taylorza | 4:64d2d834341b | 60 | bool _repeat; |
taylorza | 0:b7c65c0d82d3 | 61 | }; |
taylorza | 0:b7c65c0d82d3 | 62 | #endif //__SIGNALGENERATOR_H__ |