Andrew Duda / TLC5940Servo

Dependencies:   FastPWM

Dependents:   TLC5940ServoTest

Revision:
0:9fc434ff7a03
Child:
2:1d2251574d35
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TLC5940Servo.h	Tue Oct 21 00:02:48 2014 +0000
@@ -0,0 +1,107 @@
+#ifndef TLC5940Servo_H
+#define TLC5940Servo_H
+
+#include "FastPWM.h"
+ 
+/**
+  * SPI speed used by the mbed to communicate with the TLC5940
+  * The TLC5940 supports up to 30Mhz. This should be kept as high
+  * as possible to ensure that data has time to be sent each reset cycle.
+  */
+#define SPI_SPEED 30000000
+
+/**
+  * The rate at which the GSCLK pin is pulsed
+  * This also controls how often the reset function is called
+  * The rate at which the reset function is called can be calculated by: (1/GSCLK_SPEED) * 4096
+  * 
+  * Since the Servo period is 20ms (50Hz), we set this clock to 50*4096 = 204,800Hz according to the eqn above
+  * Also, since this clock is so slow, there is no real limit to the number of TLC5940s you can chain
+  */
+#define GSCLK_SPEED 204800
+
+/**
+  *  This class controls a TLC5940 PWM driver to control Servo motors.
+  *  It supports sending grayscale PWM data calibrated for Servos, but it does not support error checking or writing the EEPROM.
+  *  This class uses the FastPWM library by Erik Olieman to continuously pulse the GSLCK pin without CPU intervention. After
+  *  4096 pulses, the private member funciton reset is called by the ticker. It resets the display by pulsing the BLANK pin. If new
+  *  data has been set to be sent by the function setNewData, it is sent here.
+  */
+class TLC5940Servo
+{
+public:
+    /**
+      *  Set up the TLC5940
+      * 
+      *  @param MOSI - The MOSI pin of the SPI bus
+      *  @param SCLK - The SCK pin of the SPI bus
+      *  @param XLAT - The XLAT pin of the TLC5940(s)
+      *  @param BLANK - The BLANK pin of the TLC5940(s)
+      *  @param GSCLK - The GSCLK pin of the TLC5940(s)
+      *  @param number - The number of TLC5940s (optional)
+      */
+    TLC5940Servo(PinName MOSI, PinName SCLK, PinName XLAT, PinName BLANK,
+            PinName GSCLK, const int number = 1);
+    
+    // Destructor used to delete memory
+    ~TLC5940Servo();
+
+    /**  Allows calibration of the range and angles for all servos
+     *
+     * @param range Pulsewidth range from center (1.5ms) to maximum/minimum position in seconds
+     * @param degrees Angle from centre to maximum/minimum position in degrees
+     */
+    void calibrate(float range, float degrees); 
+    
+    /**  Allows calibration of the range and angles for a particular servo
+     *
+     * @param range Pulsewidth range from center (1.5ms) to maximum/minimum position in seconds
+     * @param degrees Angle from centre to maximum/minimum position in degrees
+     */
+    void calibrate(int index, float range, float degrees);
+    
+    /**  Allows calibration of the range and angles for a particular servo
+     *
+     * @param range Pulsewidth range from center (1.5ms) to maximum/minimum position in seconds
+     * @param degrees Angle from centre to maximum/minimum position in degrees
+     */
+    void calibrate(float *range, float *degrees);  
+    
+    /**
+      *  Set the next chunk of grayscale data to be sent
+      *  @param data - Array of 16 bit shorts containing 16 12 bit grayscale data chunks per TLC5940
+      *  @note These must be in intervals of at least (1/GSCLK_SPEED) * 4096 to be sent
+      */
+    int& operator[](int index);
+
+private:
+    // SPI port - only MOSI and SCK are used
+    SPI spi;
+
+    // PWM output using the FastPWM library by Erik Olieman
+    FastPWM gsclk;
+
+    // Digital out pins used for the TLC5940
+    DigitalOut blank;
+    DigitalOut xlat;
+
+    // Number of TLC5940s in series
+    const int number;
+
+    // Call a reset function to manage sending data and GSCLK updating
+    Ticker reset_ticker;
+
+    // Has new data been loaded?
+    volatile bool newData;
+
+    // Do we need to send an XLAT pulse? (Was GS data clocked in last reset?)
+    volatile bool need_xlat;
+
+    // Buffers to store data until it is sent
+    int* dataBuffer;
+
+    // Function to reset the display and send the next chunks of data
+    void reset();
+};
+
+#endif
\ No newline at end of file