In the signal generator application packaged with USBee SX, every waveform has to be manually 'designed'. I wanted to implement a led fading "chase" effect using Pulse Width Modulation (PWM). It is far too much work to manually design the waveforms in that case. Also, as I wanted a solution that also works on Linux, I did not use the DLL API, but instead what I've learnt up to now about the USBee protocol and put it to use with Python and libusb, a convenient way to interface to USB devices without writing kernel modules or device drivers. This is the kind of effect I wanted to achieve (works browsers supporting HTML5 canvas only):
First, I implemented the PWM algorithm. The most basic algorithm, Time Proportioning, uses a counter that increments periodically and is reset at the end of every period of the PWM. When the counter value is more than the reference value, the PWM output changes state from high to low (or low to high). To simulate this I wrote the following piece of Python to go from an array of 8 intensity levels to 256 samples in binary format efficiently:
This allows setting the intensity of each of the 8 leds separately. Now, we just have to set up the hardware to start sending samples (see link at bottom of this post for the source code of the USBee class).
Then we can perform the actual effect:
The full source (including setup code) can be found on my bitbucket page, just click on "get source" or use Mercurial. The signal generator test is test.py. I call this "Poor man's PWM" as the ideal solution would be to implement it on the micro-controller itself, instead of computing the waveform on the PC and sending all the data over USB. Then again, this is a proof of concept.