Library for using the TLC5940 as a servo controller.
Diff: TLC5940Servo.h
- Revision:
- 2:1d2251574d35
- Parent:
- 0:9fc434ff7a03
- Child:
- 3:3b04a122e508
--- a/TLC5940Servo.h Tue Oct 21 02:32:57 2014 +0000 +++ b/TLC5940Servo.h Tue Oct 21 06:04:24 2014 +0000 @@ -2,7 +2,7 @@ #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 @@ -14,65 +14,81 @@ * 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 + * + * Since the Servo period is 20ms (50Hz), we set this clock to 50*4096 = 204,800Hz according to the equation above + * Also, since this clock is so slow, there is almost no 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. + * 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. After 4096 pulses, the private member funciton reset is called by + * the ticker. It resets the driver by pulsing the BLANK pin. If new data has been set to be sent by the function setNewData, it + * is sent here. + * + * This class is a heavily modified versoin of the TLC5940 library created by Spencer Davis. This class also uses the FastPWM + * library by Erik Olieman to continuously pulse the GSLCK pin without CPU intervention. */ -class TLC5940Servo -{ +class TLC5940Servo { public: + /* + * Servo inner class to abstract some of the details. Based off of the mbed official Servo class + */ + class Servo { + public: + Servo(); + void write(float percent); + void calibrate(float range=0.0005, float degrees=45.0); + float read(); + int pulsewidth(); + Servo& operator= (float percent); + operator float(); + private: + float _p; + float _range; + float _degrees; + int _pw; + }; /** - * 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) + * 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 + 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 + /** + * 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 + + /** + * 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); - + 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 + * Array index operator for reading and writing from servos + * + * @param index Index of Servo in array */ - int& operator[](int index); + Servo& operator[](int index); private: // SPI port - only MOSI and SCK are used @@ -98,7 +114,7 @@ volatile bool need_xlat; // Buffers to store data until it is sent - int* dataBuffer; + Servo* dataBuffer; // Function to reset the display and send the next chunks of data void reset();