Skip to content

Community.Hardware.SignalGenerator

NicolasG3 edited this page Jun 10, 2013 · 4 revisions

Community.Hardware.SignalGenerator allows the background generation of a binary signal on any available output pin.

  • the signal generation is a background non blocking task
  • any output pin can be used to generate a signal
  • the signal timing can be changed on the fly during generation
  • SignalGenerator can modulate a carrier frequency as used in IR remote control.
  • Easy settings to generate a led pulse signal.
  • The generation start can be delayed to a specified DateTime to synchronize multiple signals. Note that only one toggle can occur at a time for all signals. Toggles can not be closer than about 6.6µs on STM32F4.
  • The signal is stopped if it is too fast to be properly generated (Community.Hardware.SignalGenerator.Status is set to Error)

Implementation
The code is implemented at HAL level and is platform independent. No memory allocation is involved to allow usage with no native heap as in FEZCerberus.

Sample code

using System;
using System.Threading;
using Community.Hardware;
using GHI.OSHW.Hardware;
using Microsoft.SPOT;

namespace SignalGeneratorTest
    {
    public class Program
        {
        static SignalGenerator ledBlink;
        public static void Main() {
            Debug.Print("Signal generator test start");
            Debug.Print("FEZCerberus led pulse 3 times repeatedly");
            ledBlink = SignalGenerator.StartNewLedPulse(FEZCerberus.Led, 3);

            Debug.Print("Start a custom signal on PB13 (socket 1, pin 3) and the same, inverted, on PB14 - Note the Led still blinking");
            SignalGenerator signal1 = new SignalGenerator(FEZCerberus.Pin.PB13);
            SignalGenerator signal2 = new SignalGenerator(FEZCerberus.Pin.PB14, true);
            //signal1 and signal2 will share the same level times array, this is not a copy
            //level times array can also be changed on the fly
            signal1.LevelTimes = new uint[] { 50, 40, 30, 20 };
            signal2.LevelTimes = signal1.LevelTimes;

            //Start the signals at the same date time to synchronize
            //Note that this is not perfectly accurate: only one toggle can occur at a time
            //for all signals (about every 6.6µs on FEZCerberus)
            DateTime startTime = DateTime.Now.AddSeconds(1.0);
            signal1.Start(startTime);
            signal2.Start(startTime);
            //keep the signals active for 60s to measure it
            for (int i = 0; i < 600; i++) {
                //modify the timing on the fly!
                signal1.LevelTimes[0]++;
                Thread.Sleep(100);
                }
            signal1.Dispose();
            signal2.Dispose();
            Debug.Print("Signal generator test end");
            }

        }
    }