TLC59116 Library  v0.2
TLC59116.h (Sat Jun 20 14:19:53 2015 -0400)
Go to the documentation of this file.
1 #ifndef TLC59116_h
2 #define TLC59116_h
3 
4 /* Documentation assumes Doxygen */
5 
6 // FIXME: insert version here
7 
8 
9 /** \todo
10  - put in examples for pwm, etc.
11  - I think something that accumulates changes, then does them as a "block"
12 */
13 
14 #include <Arduino.h>
15 #include <Wire.h> // Usage.Include+: You have to do this in your .ino also
16 #include <avr/pgmspace.h> // Usage.Include+: You have to do this in your .ino also
17 #include "TLC59116_Unmanaged.h"
18 
19 extern TwoWire Wire;
20 class TLC59116Manager;
21 
22 #define WARN TLC59116Warn // FIXME: how about a include? or 2 copies (sed'ed)?
23 #define DEV TLC59116Dev
24 #define LOWD TLC59116LowLevel
25 
26 /// High Level interface to a single TLC59116 using Wire (I2C) interface.
27 /**
28 
29  (see TLC59116_Unmanaged for lower level library)
30 
31  ### Usage:
32 
33  \include basic_usage_single/basic_usage_single.ino
34 
35  There are examples in the Arduino IDE examples, and of course, the download site https://github.com/2splat/arduino-TLC59116 .
36 
37  This high-level interface tracks each device's state so that commands appear to be independant.
38  For example, you can set the digital-on of a channel without worrying about the
39  other 4 mode settings in each LEDOUTx_Register.
40 
41  Some optimization of the data sent to the devices is also done.
42 
43  ### The TLC59116 Device
44  See the TI spec sheet for much detail: http://www.ti.com/lit/ds/symlink/tlc59116.pdf
45 
46  - Output pins ("channels") are numbered 0..15.
47  - Channels are "sinks", acting like Arduino pins that are LOW, even for PWM.
48  - Connect the minus side of LEDs to the output pins.
49  - Instead of HIGH/LOW like DigitalWrite(), we use on/off/set.
50  - Channels are considered ON, OFF, , instead of HIGH/LOW (or, at some PWM value)
51  - PWM is 8 bit.
52  - I2C addresses start at 0x60. See TLC59116_Unmanaged for the available addresses (up to 14).
53  - The device provides a constant-current control (external and software settable).
54  This means you can set the current, usually leave out resistors on each LED/channel, and is more efficient.
55  See also, TLC59116::milliamps().
56  - This library discovers the devices on the I2C bus and keeps them in a list.
57  - Use the _index_ in this library: e.g. "0" is the device with the lowest address,
58  "1" is the next highest, etc.
59  - This library exposes I2C addresses as 7bit addresses.
60  - Individual devices cannot be "reset", all devices on the I2C bus are affected by a reset.
61  - By default, all devices respond to broadcast ("allcall") commands (a special address).
62  - This can be changed, or turned off, for each device (see TLC59116::allcall_address())
63  - Three additional "broadcast" addresses can be set. See TLC59116::SUBADR_address()
64  - Thus, a device can participate in 4 different broadcast groups (or none):
65  - allcall_address()
66  - SUBADR_address(0)
67  - SUBADR_address(1)
68  - SUBADR_address(2)
69  - See the various groupings of the functions (below) for functionality.
70 
71  ### Protocol:
72 
73  - Setting up
74  - Declare a static TLC59116Manager (static scope, or "static" keyword if you are very clever)
75  \snippet allfeatures/allfeatures.ino Need a manager
76  - Initialize the serial-output e.g.
77  - `Serial.begin(9600)`
78  - This library will warn you about some mistakes or dubious usages.
79  - You don't have to setup the serial-output, but then you won't see the warnings.
80  - I like to set the speed to 115200, because at that setting it has little effect
81  on the speed of your program.
82  - Execute \ref TLC59116Manager::init() ".init()" for the manager once, usually in setup()
83  \snippet allfeatures/allfeatures.ino Init the manager
84  - The devices will then be in a ready state (by default, outputs are enabled, but all channels are off)
85  - Obtain a reference from yourmanager\[i], for i from 0..device_count()-1
86  - This library passes and returns references so that "." can always be used.
87  \snippet allfeatures/allfeatures.ino Static referencing devices
88  \snippet allfeatures/allfeatures.ino Index referencing devices
89  - Get information about the device as needed from the \ref Info Functions
90  \snippet allfeatures/allfeatures.ino device info
91  - Set device \ref Global Functions, \ref Group/Broadcast Addresses, \ref Software Current Control, typically in setup()
92  \code{.cpp}
93  void setup() {
94  \endcode
95  \snippet allfeatures/allfeatures.ino device setup
96  - Do initial \ref Digital Functions, \ref PWM Functions, typically in setup()
97  \snippet allfeatures/allfeatures.ino initial channel settings
98  - During loop, mess with \ref Global Functions, \ref Group/Broadcast Addresses, \ref Software Current Control, \ref Digital Functions, \ref PWM Functions
99  - This library returns the device-reference for most calls, so you can chain functions, e.g.:
100  \snippet allfeatures/allfeatures.ino chain functions together
101 
102 */
103 class TLC59116 : public TLC59116_Unmanaged {
104 
105  public:
106  // No constructor, get from a TLC59116Manager[i]
107 
108  /// \name Global Functions
109  ///@{
110  //! Controlling various global features of the device (see also, the "Software Current Control" section)
111 
112  /// Master power switch (the TLC59116Manager sets this on/true by default)
113  /** It takes the device a moment to enable power, so typically leave _with_delay_ true **/
114  virtual TLC59116& enable_outputs(
115  bool yes = true, //!< false to disable outputs
116  bool with_delay = true //!< delay for 500msec
117  );
118 
119  /// Is master power on?
120  bool is_enable_outputs() { return !is_OSC_bit(shadow_registers[MODE1_Register]); };
121  bool is_enabled() { return is_enable_outputs(); } ///< Alias for is_enable_outputs()
122  ///@}
123 
124  // High level
125 
126  /** \name Digital Functions
127  Analogous to DigitalWrite.
128 
129  Arduino DigitalWrite uses HIGH and LOW, but the device is a "sink", so "on" is actually a LOW.
130  Thus, this library uses "on" and "off" for terminology.
131 
132  There are several aliases, and overloaded functions here (and for the pwm functions below).
133  The intention was to provide natural
134  overloads and aliases. I've shied away from using the Arduino DigitalWrite and AnalogWrite so far.
135 
136  These functions only change the specified channels, they'll leave other outputs, like a PWM channel alone.
137 
138  You can have some channels doing PWM, and some channels doing digital.
139 
140  */
141  ///@{
142  // FIXME: pattern_bits(bits, which). set_bits(pattern). reset_bits(pattern). on/off/set
144  word pattern, //!< on/off for each channel by bit
145  word which //!< But, only change these bits
146  ); //!< Only change the channels (that are marked in _bit_which_) to on/off as marked in _bit_pattern_
147  /**<
148  (cf. on_pattern(word), on(), off() )
149 
150  Examples:
151  \snippet allfeatures/allfeatures.ino set_outputs by bit pattern
152  */
153  /// Alias for set_outputs()
154  TLC59116& pattern(word bit_pattern, word bit_which=0xFFFF) { return set_outputs(bit_pattern, bit_which); }
155 
156  /// Turn on by bit pattern
157  /** See set_outputs(word,word) examples for hex/binary notation hints
158 
159  Examples:
160  \snippet allfeatures/allfeatures.ino on_pattern
161  */
162  TLC59116& on_pattern(word pattern) { return set_outputs(pattern, pattern); }
163 
164  /// Turn off by bit pattern
165  /** See set_outputs(word,word) examples for hex/binary notation hints
166 
167  Examples:
168  \snippet allfeatures/allfeatures.ino off_pattern
169  */
170  TLC59116& off_pattern(word pattern) { return set_outputs(~pattern, pattern); }
171 
172  /// Turn one on/off, much like DigitalWrite.
173  /** Examples:
174  \snippet allfeatures/allfeatures.ino using set
175  */
176  TLC59116& set(int led_num, bool offon /**< _true_ for on */ ) {
177  word bits = 1 << led_num;
178  return set_outputs(offon ? bits : ~bits, bits);
179  }
180  TLC59116& on(int led_num) { return set(led_num, true); } ///< Turn one on
181  TLC59116& off(int led_num) { return set(led_num, false); } ///< Turn one off
182  // \todo maybe a pattern(byte array)? maybe a pattern(a,b,c,...)?
183  ///@}
184 
185  /** \name PWM Functions
186  Analogous to AnalogWrite
187 
188  You can have some channels doing PWM, and some channels doing digital.
189 
190  Remember, the device is a "sink", even for PWM. Minus side of LED goes to the output pin.
191 
192  \bug Be careful with start/end arguments, if they are out of range (0..15) you will have mysterious problems.
193  \todo Put in range checking
194  */
195  ///@{
196 
197  /// PWM, start..end, list-of-brightness
198  /** Examples:
199  \snippet allfeatures/allfeatures.ino range of pwm
200  */
202  byte led_num_start, ///< first channel
203  byte led_num_end, ///< last channel
204  const byte brightness[] ///< A list of PWM values. Tolerates led_num_end>15 which wraps around
205  );
206 
207  TLC59116& pwm(byte led_num_start, byte led_num_end, const byte brightness[] /*[ct]*/) { return set_outputs(led_num_start, led_num_end, brightness); } ///< Alias of set_outputs() above
208 
209  /// PWM, start..end same brightness
210  /** Examples:
211  \snippet allfeatures/allfeatures.ino same pwm
212  */
213  TLC59116& pwm(byte led_num_start, byte led_num_end, byte pwm_value) {
214  const byte register_count = led_num_end - led_num_start +1;
215  // FIXME: warnings of range
216  byte pwm_buff[register_count];
217  memset(pwm_buff, pwm_value, register_count);
218  return set_outputs(led_num_start, led_num_end, pwm_buff);
219  }
220 
221  /// Set all 16 PWM according to the brightness list
222  /** Examples:
223  \snippet allfeatures/allfeatures.ino all pwm
224  */
225  TLC59116& set_outputs(const byte brightness[16] ) { return set_outputs(0,15, brightness); }
226  TLC59116& pwm(const byte (&brightness)[16]) { return set_outputs(0,15, brightness); } ///< Set all 16 PWM according to the brightness list, same as above.
227 
228  /// PWM for one channel
229  /** Examples:
230  \snippet allfeatures/allfeatures.ino one pwm
231  */
232  TLC59116& pwm(byte led_num, byte brightness) { byte ba[1] = {brightness}; return set_outputs(led_num, led_num, ba); }
233  // fixme: maybe brightness()?
234  ///@}
235 
236  /** \name Group Functions
237  \warning The TLC59116 has a hardware bug:
238 
239  If you decrease the value (blink_time or brightness) in these functions,
240  the chip may act like the value is max for one "cycle".
241  This causes a full-brightness result for group_pwm, and a 10 second blink-length for group_blink.
242  I think this is because:
243  - There is a continously running timer, counting up.
244  - The "value" is compared to the timer, and triggers the action and a reset of the timer.
245  - If you move the "value" down, you may skip over the current timer value,
246  so it runs to the maximum value before wrapping around again.
247  "reset" is the only thing that I know of that will reset the timer.
248  But, more experimentation is needed.
249 
250  I recommend you only set group functions once (or only increase values).
251 
252  Of course, you could TLC59116Manager::reset() everytime, too.
253  */
254  ///@{
255  // Warning, "group" functions have a bug in the device hardware
256 
257  /// Superpose group-pwm on current pwm setting (but, current should be 255 for best results)
258  /// \warning Hardware Bug, see \ref Group Functions
260  word bit_pattern, ///< channels by bit
261  byte brightness ///< superposed PWM
262  );
263 
264  /// Blink all the LEDs, at their current/last PWM setting.
265  /** Set PWM values first.
266  To turn off blink, set channels to PWM or digital-on/off
267 
268  See TLC59116& group_blink(double, double) for a version in seconds/percent.
269 
270  \warning Hardware Bug, see \ref Group Functions
271  */
273  byte blink_period, ///< blink-length secs = (blink_period + 1)/24
274  byte on_ratio=128 ///< ratio that the blink is on: on_ratio/256
275  ) { return group_blink(0xFFFF,blink_period,on_ratio); }
276 
277  /// As TLC59116& group_blink(byte,byte), but you can specify the channels by bit-pattern
278  //! \warning Hardware Bug, see \ref Group Functions
279  TLC59116& group_blink(word bit_pattern, int blink_delay, int on_ratio=128);
280 
281  /// Blink all the LEDs, at their current/last PWM setting by len/%.
282  //! \warning Hardware Bug, see \ref Group Functions
284  double blink_length_secs, ///< 0.041secs (24.00384hz) to 10.54secs (.09375hz) in steps of .04166secs
285  double on_percent=50.0 ///< % of blink-cycle that is on
286  ) {
287  return group_blink(0xFFFF,blink_length_secs,on_percent);
288  }
289 
290  /// As TLC59116& group_blink(double,double), but you can specify the channels by bit-pattern
291  //! \warning Hardware Bug, see \ref Group Functions
292  TLC59116& group_blink(word bit_pattern, double blink_length_secs, double on_percent=50.0) { // convenience % & len
293  // on_percent is 05..99.61%, blink_length is 0.041secs (24.00384hz) to 10.54secs (.09375hz) in steps of .04166secs
294  return group_blink(
295  bit_pattern,
296  (byte) int(blink_length_secs/0.041666667 + .0001),
297  (byte) int(on_percent * 256.0/100.0)
298  );
299  }
300 
301  /// Same as TLC59116& group_blink(word, double, double), but blink_length is an integer (1..10 secs)
302  //! \warning Hardware Bug, see \ref Group Functions
303  TLC59116& group_blink(unsigned int bit_pattern, int blink_length_secs, double on_percent=50.0) {
304  return group_blink((word)bit_pattern, blink_length_secs, int(on_percent * 256.0/100.0));
305  }
306  ///@}
307 
308  /** \name Group/Broadcast Addresses
309  Addresses for broadcast (_allcall_address_) and group-broadcase (_SUBADR_x_).
310 
311  Addresses should be specified as 0x60..0x6F.
312 
313  It is illegal to set any address to Reset_Addr (0x6B), it will be ignored with warning.
314  */
315  ///@{
316 
317  /// The reset-address, unchangeable.
318  /** Not much you should do with this. See TLC59116Manager::reset() */
319  byte Reset_address() { return Reset_Addr >> 1; }
320 
321  /// Get broadcast address, see below to set/enable.
322  byte allcall_address() { return shadow_registers[AllCall_Addr_Register] >> 1; }
323 
324  /// Set and enable/disable the broadcast address (_allcall_addr_).
325  /** Each device can have a different allcall_address, and have it disabled/enabled.
326 
327  By default, the TLC59116Manager arranges for the broadcast address to be enabled.
328 
329  \bug However, the TLC59116Manager.broadcast() object _only_ assumes the default allcall_address 0x68.
330  So, it won't work if you change the allcall_address.
331 
332  Examples:
333  \snippet allfeatures/allfeatures.ino set allcall
334  */
335  TLC59116& allcall_address(byte address, bool enable=true);
336 
337  TLC59116& allcall_address_enable(); ///< Enable broadcast
338  TLC59116& allcall_address_disable(); ///< Disable broadcast
339  bool is_allcall_address() { return shadow_registers[AllCall_Addr_Register] & MODE1_ALLCALL_mask; } ///< Is broadcast enabled?
340 
341  // SUBADR's are disabled at reset
342  // SUBADR are 1,2,3 for "which"
343  /// Get the SUBADR_n address.
344  /** Each device can have a 3 more broadcast addresses. Thus you can form various groups of the devices.
345 
346  By default, the SUBADR's are disabled. You must enable them if you want to use them (see SUBADR_address(byte, byte, bool), SUBADR_address_enable(byte), SUBADR_address_disable(byte) ).
347 
348  The default addresses are: are 0x69, 0x6a, 0x6b
349 
350  */
351  byte SUBADR_address(byte which) { return shadow_registers[SUBADRx_Register(which)] >> 1; }
352  bool is_SUBADR_address(byte which) { return is_SUBADR_bit( shadow_registers[MODE1_Register], which); } ///< Is SUBADR_n enabled?
353 
354  /// Set the SUBADR_n address.
355  /**
356  Examples:
357  \snippet allfeatures/allfeatures.ino set subadr
358 
359  \bug We don't track commands on SUBADR_n's, so this library will get very confused
360  about the state of the devices and do confusing things if you mix broadcasts to
361  SUBADR_n devices, and individual devices in that SUBADR_n group.
362 
363  \bug Also, this library provides no easy access to broadcast to a SUBADR_n group.
364 
365  \todo Provide SUBADR_n broadcast functionality.
366  */
367  TLC59116& SUBADR_address(byte which, byte address, bool enable=true);
368  TLC59116& SUBADR_address_enable(byte which); ///< Enable SUBADR_n
369  TLC59116& SUBADR_address_disable(byte which); ///< Disable SUBADR_n
370  ///@}
371 
372  /// \name Software Current Control
373  ///@{
374 
375  /** We need a Rext value (the resistor attached to the Rext pin)
376  to calculate this, e.g. 156ohms gives 120mA at reset (default), 937ohms gives 20mA.
377  This function can use a default value of 156ohms to do the calculation.
378 
379  By default, this is set to maximum current (e.g. 120mA for 156ohm).
380  Remember that common LEDs are usually 20mA, though 10mA is becoming common
381  (the tiny surface-mount LEDs are usually 5mA).
382 
383  You probably want to set the current before you turn on any LEDs!
384 
385  Note that this will tend to set the output 1milliamp low (due to rounding).
386 
387  This will also clip the calculated mA to the minimum..120mA range (for Rext).
388 
389  Check the extant/calculated value with milliamps().
390 
391  See the section "The TLC59116 Device" for some short notes on "constant current".
392 
393  Examples:
394  \code{.cpp}
395  void setup() {
396  \endcode
397  \snippet allfeatures/allfeatures.ino initial current control
398 
399  \code{.cpp}
400  void loop() {
401  \endcode
402  \snippet allfeatures/allfeatures.ino current control
403  */
404  TLC59116& set_milliamps(byte ma, int Rext=Rext_Min); // Rext_Min ohms is 120ma at reset.
405 
406  int milliamps(int Rext=Rext_Min) {return i_out(shadow_registers[IREF_Register]); } ///< The calculated current setting
407  ///@}
408 
409  // Error detect
410  // FIXME: implement. we should set error enable on reset? then read them?
411 
412  /** \name Convenience
413  Since we allow function-chaining, it seems like a few convenience functions might be nice.
414 
415  Note all the functions (not just these convenience functions) that look like
416 
417  TLC59116& xxx(...)
418 
419  They return a reference to the device, so you can chain functions.
420  */
421  ///@{
422  TLC59116& delay(int msec) { ::delay(msec); return *this;} ///< Just like the Arduino delay() function
423  ///@}
424 
425  /** \name Info Functions
426  See a few other functions whose name starts with "is_".
427  */
428  ///@{
429  // \todo checking a shadow_register value
430  // (make doxygen include this fn, but mess up the brief doc, thanks)
431  // \fn byte address()
432  ///@}
433 
434  /** \name Debugging
435  You can enable debug output by editing TLC59116.cpp, and TLC59116_Unmanaged.cpp. Add one or both:
436  \code
437  #define TLC59116_LOWLEVEL 1
438  #define TLC59116_DEV 1
439  \endcode
440  */
441  ///@{
442  TLC59116& describe_shadow(); ///< Print the registers, as we've cached them. cf. describe_actual()
443  TLC59116& resync_shadow_registers(); ///< Fetch the actual into our cached registers
444  ///@}
445 
446  private:
447  // FIXME: move Power_Up_Register_Values to FLASH
448  /// The state of the device on power-up/reset.
449  static const unsigned char Power_Up_Register_Values[TLC59116_Unmanaged::Control_Register_Max + 1] PROGMEM;
450 
451  TLC59116Manager &manager; /// so we can find the manager for a few things
452  // void (*on_reset)(byte /* manager[i] */);
453 
454  // Manager has to track for reset, so factory instead of public constructors
455  TLC59116(TwoWire& bus, byte address, TLC59116Manager& m) : TLC59116_Unmanaged(bus, address), manager(m) {reset_shadow_registers();} // factory control, must get from manager
456  TLC59116(); // none
457  TLC59116(const TLC59116&); // none
458  TLC59116& operator=(const TLC59116&); // none
459  ~TLC59116() {} // managed destructor....
460 
461 
462  // low-level
463  void reset_happened(); // reset affects the state of the devices
464  void modify_control_register(byte register_num, byte mask, byte bits);
465  void modify_control_register(byte register_num, byte value);
466 
467  // mid-level
468  void update_registers(const byte want[] /* [start..end] */, byte start_r, byte end_r); // want[0]=register[start_r]
469  TLC59116& set_address(const byte address[/* sub1,sub2,sub3,all */], byte enable_mask /* MODE1_ALLCALL_mask | MODE1_SUB1_mask... */); // set all at once w/enable'ment
470 
471 
472 
473  // We have to shadow the state
474  byte shadow_registers[Control_Register_Max+1];
475  void reset_shadow_registers() {
476  DEV(F("Reset shadow "));DEV(address(),HEX);DEV();
477  memcpy_P(shadow_registers, Power_Up_Register_Values, Control_Register_Max);
478  }
479  void sync_shadow_registers() { /* fixme: read device, for ... shadow=; */ }
480 
481  public: // Special classes
482  class Broadcast;
483 
484  private:
485  friend class TLC59116Manager; // so it can construct TLC59116's
486  };
487 
489  public:
490  Broadcast(TwoWire &bus, TLC59116Manager& m) : TLC59116(bus, TLC59116::AllCall_Addr, m) {}
491 
492  Broadcast& enable_outputs(bool yes = true, bool with_delay = true);
493 
494  private:
495  // odd, the trailing comment next is not captured (cons/decons?)
496  Broadcast(); // none
497  Broadcast(const Broadcast&); // none
498  Broadcast& operator=(const TLC59116&); // none
499  friend class TLC59116Manager;
500  ~Broadcast() {} // managed destructor....
501 
502  void propagate_register(byte register_num);
503 
504  };
505 
506 /// Holds all of the devices.
507 /**
508  - Inits the I2C bus, and devices (see TLC59116Manager::TLC59116Manager())
509  - Collects and holds the devices (cf TLC59116Manager::operator[])
510  - Provides reset (TLC59116Manager::reset())
511  - Provides a broadcast object (TLC59116Manager::broadcast())
512 
513  The reset command for TCL59116's is a broadcast command, so we manage that here.
514 
515  \bug
516  This collects all I2C devices between 0x60 and 0x6E. Non TLC59116 devices can appear in our list!
517  The TLC59116 doesn't have a feature that identifies it, so we just assume.
518 
519 */
521 
522  // FIXME: this skips the device at AllCall_Addr, allow a remediation of that
523  // FIXME: likewise, subadr is disabled at reset, allow remediation to exclude that on scan
524 
525  friend class TLC59116;
526 
527  public:
528  static const byte MaxDevicesPerI2C = 15; // 16 addresses - reset (subadr1..subadr3 and allcall can be disabled)
529 
530  // manager init flags. Default setting should be 1 FIXME: enum?
531  static const byte WireInit = 0b1; // call .begin() on the i2cbus. Unset if you've already done that
532  // After i2c init, and devices reset, turn outputs on (see TLC59116.enable_outputs)
533  static const byte EnableOutputs = 0b10; // device default at on/reset is "outputs disabled"
534  static const byte Reset = 0b100; // reset all devics after i2c init (see TLC59116Manager.reset)
535  static const byte Already = 0b1000; // internal use, leave unset
536 
537  static const long Default_Frequency = 100000L; // default to 100khz, it's the default for Wire
538 
539  // Protocol: * Get a manager via a constructor
540  // Simple constructor, uses Wire (standard I2C pins), 100khz bus speed, resets devices, and enables outputs
541  // NB: The IDE will get confused if you do: TLC59116Manager myname(); in the global section, so leave off "()"
542  TLC59116Manager() : i2cbus(Wire), init_frequency(Default_Frequency), reset_actions( WireInit | EnableOutputs | Reset) {}
543  // Allow override of setup
544  // E.g. TLC59116Manager tlcmanager(Wire, 50000, EnableOutputs | Reset); // 500khz, don't do Wire.init()
545  // You'll have to write an adaptor for other (non-Wire) I2C interfaces
546  TLC59116Manager(TwoWire &w, // Use the specified i2c bus (e.g. Wire) (shouldn't allow 2 of these on same bus)
547  long frequency = Default_Frequency, // hz. (Screws With:) This screws with TWBR
548  // Only certain speeds are actually allowed, rounds to nearest
549  // FIXME: table of speeds
550  byte dothings = WireInit | EnableOutputs | Reset // Do things now and at reset()
551  ) : i2cbus(w), init_frequency(frequency), reset_actions(dothings) { }
552 
553  // Protocol: * You have to call .init() at run-time (usually in setup)
554  /* Does the things indicated by the constructor's "dothings":
555  * inits the i2c bus (see Wire.init())
556  * set i2c frequency (TWBR)
557  * finds devices (See TLC59116Manager.scan)
558  * resets all devices (See TLC59116Manager.reset)
559  * enables outputs on all devices (See TLC59116.enable_outputs)
560  NB: Only acts if !is_inited() (i.e. .init() is only usable once)
561  */
562  void init();
563  bool is_inited() { return !!(this->reset_actions & Already); } // you can check if .init() was called already
564 
565  // Protocol: * Get a specific device with themanager[i]
566  // Get the 0th, 1st, 2nd, etc. device (index, not by i2c-address). In address order.
567  // Can return null if index is out of range, or there are none!
568  // Pattern: TLC59116& first = manager[0]; // Note the '&'
569  // Pattern: manager[i].all_on(); // you can use "." notation
570  // \todo get by I2C address, which we might could do because 0x60 > 15...
571  // FIXME: can we do: BaseClass& x = manager[0];
572  TLC59116& operator[](byte index) {
573  if (index >= device_ct) {
574  TLC59116Warn(F("Index "));TLC59116Warn(index);TLC59116Warn(F(" >= max device "));TLC59116Warn(device_ct);TLC59116Warn();
575  return *(devices[0]); // Can't return null
576  }
577  else {
578  return *(devices[index]);
579  }
580  }
581  byte device_count() { return device_ct; } // you can iterate. FIXME: implement interator protocol
582 
583  public: // global things
584  // Protocol: * OR get the .broadcast() object that sends the same command to all devices
585  // \todo reconsider the subclass, instead propagate if self.address==thebroadcast address
586  // \todo and, broadcast can change, and there are subadr's, so we should deal with those.
587  TLC59116::Broadcast& broadcast() { static TLC59116::Broadcast x(Wire, *this); return x; }
588 
589  // Protocol: * Reset all devices with .reset(), when desired
590  int reset(); // 0 is success, does enable_outputs if init(...EnableOutputs...)
591 
592  // consider: is_SUBADR(). has to track all devices subaddr settings (value & enabled)
593 
594  private:
595  TLC59116Manager(const TLC59116Manager&); // undefined
596  TLC59116Manager& operator=(const TLC59116Manager&); // undefined
597  int scan(); // client code can't rescan
598 
599  // Specific bus
600  TwoWire &i2cbus;
601  byte reset_actions;
602  long init_frequency;
603 
604  // Need to track extant
605  TLC59116* devices[MaxDevicesPerI2C]; // that's 420 bytes of ram
606  byte device_ct;
607 
608  // We have to store the power-up values, so we can set our shadows to them on reset
609  static const unsigned char Power_Up_Register_Values[TLC59116::Control_Register_Max+1] PROGMEM;
610  };
611 
612 #endif
bool is_inited()
Definition: TLC59116.h:563
bool is_allcall_address()
Is broadcast enabled?
Definition: TLC59116.h:339
TLC59116 & pwm(byte led_num, byte brightness)
PWM for one channel.
Definition: TLC59116.h:232
TLC59116 & set_milliamps(byte ma, int Rext=Rext_Min)
Definition: TLC59116.cpp:436
static const byte MaxDevicesPerI2C
Definition: TLC59116.h:528
static byte SUBADRx_Register(byte i)
Definition: TLC59116_Unmanaged.h:228
static const byte IREF_Register
Definition: TLC59116_Unmanaged.h:234
TLC59116 & pwm(byte led_num_start, byte led_num_end, const byte brightness[])
Alias of set_outputs() above.
Definition: TLC59116.h:207
static const byte Reset
Definition: TLC59116.h:534
TLC59116 & SUBADR_address_disable(byte which)
TLC59116 & group_blink(unsigned int bit_pattern, int blink_length_secs, double on_percent=50.0)
Definition: TLC59116.h:303
static const byte MODE1_ALLCALL_mask
Definition: TLC59116_Unmanaged.h:182
void TLC59116Warn(const T msg, int format=0)
Definition: TLC59116_Unmanaged.h:105
byte SUBADR_address(byte which)
Get the SUBADR_n address.
Definition: TLC59116.h:351
bool is_enable_outputs()
Is master power on?
Definition: TLC59116.h:120
TLC59116 & on(int led_num)
Turn one on.
Definition: TLC59116.h:180
TLC59116 & group_pwm(word bit_pattern, byte brightness)
Definition: TLC59116.cpp:245
TLC59116Manager()
Definition: TLC59116.h:542
#define DEV
Definition: TLC59116.h:23
Broadcast(TwoWire &bus, TLC59116Manager &m)
Definition: TLC59116.h:490
TLC59116 & group_blink(byte blink_period, byte on_ratio=128)
Blink all the LEDs, at their current/last PWM setting.
Definition: TLC59116.h:272
static const byte WireInit
Definition: TLC59116.h:531
TLC59116 & pwm(const byte(&brightness)[16])
Set all 16 PWM according to the brightness list, same as above.
Definition: TLC59116.h:226
static const byte EnableOutputs
Definition: TLC59116.h:533
static const byte Control_Register_Max
Definition: TLC59116_Unmanaged.h:247
byte Reset_address()
The reset-address, unchangeable.
Definition: TLC59116.h:319
static const byte MODE1_Register
Definition: TLC59116_Unmanaged.h:175
Holds all of the devices.
Definition: TLC59116.h:520
byte device_count()
Definition: TLC59116.h:581
static const byte Reset_Addr
Definition: TLC59116_Unmanaged.h:149
int milliamps(int Rext=Rext_Min)
Definition: TLC59116.h:406
byte allcall_address()
Get broadcast address, see below to set/enable.
Definition: TLC59116.h:322
TLC59116 & group_blink(word bit_pattern, double blink_length_secs, double on_percent=50.0)
Definition: TLC59116.h:292
TLC59116 & pwm(byte led_num_start, byte led_num_end, byte pwm_value)
PWM, start..end same brightness.
Definition: TLC59116.h:213
void init()
Definition: TLC59116.cpp:36
virtual TLC59116 & enable_outputs(bool yes=true, bool with_delay=true)
Master power switch (the TLC59116Manager sets this on/true by default)
Definition: TLC59116.cpp:142
TLC59116 & group_blink(double blink_length_secs, double on_percent=50.0)
Definition: TLC59116.h:283
TwoWire Wire
TLC59116 & pattern(word bit_pattern, word bit_which=0xFFFF)
Alias for set_outputs()
Definition: TLC59116.h:154
TLC59116 & describe_shadow()
Print the registers, as we've cached them. cf. describe_actual()
Definition: TLC59116.cpp:361
static const byte Already
Definition: TLC59116.h:535
Definition: TLC59116.h:488
High Level interface to a single TLC59116 using Wire (I2C) interface.
Definition: TLC59116.h:103
TLC59116 & allcall_address_disable()
Disable broadcast.
Definition: TLC59116.cpp:425
static const int Rext_Min
Definition: TLC59116_Unmanaged.h:238
int reset()
Definition: TLC59116.cpp:120
Definition: TLC59116_Unmanaged.h:133
TLC59116 & SUBADR_address_enable(byte which)
Enable SUBADR_n.
static const byte AllCall_Addr_Register
Definition: TLC59116_Unmanaged.h:233
TwoWire & i2cbus
Definition: TLC59116_Unmanaged.h:285
bool is_SUBADR_bit(byte mode1_value, byte which)
Definition: TLC59116_Unmanaged.h:181
TLC59116Manager(TwoWire &w, long frequency=Default_Frequency, byte dothings=WireInit|EnableOutputs|Reset)
Definition: TLC59116.h:546
TLC59116 & set(int led_num, bool offon)
Turn one on/off, much like DigitalWrite.
Definition: TLC59116.h:176
static const byte AllCall_Addr
Definition: TLC59116_Unmanaged.h:146
bool is_SUBADR_address(byte which)
Is SUBADR_n enabled?
Definition: TLC59116.h:352
static const long Default_Frequency
Definition: TLC59116.h:537
TLC59116::Broadcast & broadcast()
Definition: TLC59116.h:587
TLC59116 & delay(int msec)
Definition: TLC59116.h:422
byte address()
Definition: TLC59116_Unmanaged.h:303
TLC59116 & on_pattern(word pattern)
Turn on by bit pattern.
Definition: TLC59116.h:162
TLC59116 & set_outputs(const byte brightness[16])
Set all 16 PWM according to the brightness list.
Definition: TLC59116.h:225
bool is_enabled()
Definition: TLC59116.h:121
bool is_OSC_bit(byte mode1_value)
Definition: TLC59116_Unmanaged.h:177
static byte i_out(byte CM, byte HC, byte CC, int Rext=Rext_Min)
Definition: TLC59116_Unmanaged.cpp:145
TLC59116 & allcall_address_enable()
Enable broadcast.
Definition: TLC59116.cpp:419
TLC59116 & set_outputs(word pattern, word which)
Only change the channels (that are marked in bit_which) to on/off as marked in bit_pattern ...
Definition: TLC59116.cpp:177
TLC59116 & resync_shadow_registers()
Definition: TLC59116.cpp:431
Broadcast & enable_outputs(bool yes=true, bool with_delay=true)
Master power switch (the TLC59116Manager sets this on/true by default)
Definition: TLC59116.cpp:446
TLC59116 & off(int led_num)
Turn one off.
Definition: TLC59116.h:181
TLC59116 & operator[](byte index)
Definition: TLC59116.h:572
static Scan & scan(void)
Definition: TLC59116_Unmanaged.h:399
TLC59116 & off_pattern(word pattern)
Turn off by bit pattern.
Definition: TLC59116.h:170