-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqwiic_pca9685.py
1385 lines (1110 loc) · 45.1 KB
/
qwiic_pca9685.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#-----------------------------------------------------------------------
# SparkFun PCA9685 Python Library
#-----------------------------------------------------------------------
#
# Written by SparkFun Electronics, June 2019
# Author: Wes Furuya
#
# Compatibility:
# * Original: https://www.sparkfun.com/products/14328
# * v2: https://www.sparkfun.com/products/15316
#
# Do you like this library? Help support SparkFun. Buy a board!
# For more information on Pi Servo Hat, check out the product page
# linked above.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http:www.gnu.org/licenses/>.
#
#=======================================================================
# Copyright (c) 2019 SparkFun Electronics
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#=======================================================================
#
# pylint: disable=line-too-long, bad-whitespace, invalid-name
"""!
qwiic_pca9685
===============
Python module for the `SparkFun Pi Servo HAT <https://www.sparkfun.com/products/14328>` and `SparkFun Servo pHAT for Raspberry Pi <https://www.sparkfun.com/products/15316>`
This package should be used in conjunction with the i2c_driver package contained in the `SparkFun qwiic Python Package <https://github.com/sparkfun/Qwiic_Py>`
"""
# Load Necessary Modules:
#----------------------------------------------
import time # Time access and conversion package
import math # Basic math package
import qwiic_i2c # I2C bus driver package
# Device Name:
#----------------------------------------------
_DEFAULT_NAME = "Qwiic PCA9685"
# Device Address:
#----------------------------------------------
"""!
Some devices have multiple available addresses - this is a list of these
addresses.
NOTE: The first address in this list is considered the default I2C
address for the device.
According to the datasheet, the I2C address for the PCA9865 chip is
hardware selectable, with 64 options (7-bit: 1000 000 to 1111 111);
excluding the Software Reset (0000 0110) and LED All Call (1110 000),
leaving a maximum of 62 addresses.
Reserved Addresses:
All Call Address = 0x70 (7-bit: 1110 000X or E0h)
General Call Address = 0x00 (Part of I2C Protocol)
Reserved Addresses (Optional):
Sub Call Address 1 = 0x71 (7-bit: 1110 001X or E2h)
Sub Call Address 2 = 0x72 (7-bit: 1110 010X or E4h)
Sub Call Address 3 = 0x74 (7-bit: 1110 100X or E8h)
10-bit Addressing Scheme = 0x78 to 0x7B
Reserved Addresses (I2C Protocol):
General Call Address = 0x00 (7-bit: 0000 000)
CBUS Address = 0x01 (not listed in Datasheet)
Alternative Bus Format = 0x02 (not listed in Datasheet)
Future Use = 0x03 (7-bit: 0000 011)
Hs-mode Master Code = 0x04 to 0x07 (7-bit: 0000 1XX)
10-bit Addressing Scheme = 0x78 to 0x7B (7-bit: 1111 0XX)
Future Use = 0x7C to 0x7F (7-bit: 1111 1XX)
NOTE: I'm not sure how NXP got to 62 as the Software Reset address
doesn't fall in the available options. Additionally, it is a command
bit sent after the General Call Address. (Let me know if you
figure this out, so I can comment this library properly.)
"""
_AVAILABLE_I2C_ADDRESS = list(range(0x40,0x7F+1)) # Full Address List
# Special Use Addresses:
_gcAddr = 0x00 # General Call address for software reset
_acAddr = 0x70 # All Call address- used for modifications to
# multiple PCA9685 chips regardless of their
# I2C address set by hardware pins (A0 to A5).
_subAddr_1 = 0x71 # 1110 001X or 0xE2 (7-bit)
_subAddr_2 = 0x72 # 1110 010X or 0xE4 (7-bit)
_subAddr_3 = 0x74 # 1110 100X or 0xE8 (7-bit)
# Software Reset "Address":
#----------------------------------------------
"""!
Used when a reset needs to be performed by the master.
NOTE: Used with General Call address... more like a command
"""
_SWRST = 0x06 # Equivalent of reset byte 0000 011
# Registers:
#----------------------------------------------
"""!
List of available registers and their functions
According to the datasheet, these registers are access by the 'Control
Register', which acts as a pointer register. The control register takes
the following format:
[D7 D6 D5 D4 D3 D2 D1 D0]
Reset State = 00h
To list all available registers:
list(range(69+1)) + list(range(250, 255+1))
Auto Increment past register 69 will point to MODE1 register (register
0). Auto Increment also works from register 250 to register 254, then
rolls over to register 0.
"""
MODE1 = 0x00 # Mode register 1
MODE2 = 0x01 # Mode register 2
SUBADR1 = 0x02 # I2C subaddress 1
SUBADR2 = 0x03 # I2C subaddress 2
SUBADR3 = 0x04 # I2C subaddress 3
ALLCALLADR = 0x05 # LED All Call I2C adress
LED0_ON_L = 0X06 # LED0 output and brightness control byte 0
LED0_ON_H = 0X07 # LED0 output and brightness control byte 1
LED0_OFF_L = 0X08 # LED0 output and brightness control byte 2
LED0_OFF_H = 0X09 # LED0 output and brightness control byte 3
ALL_LED_ON_L = 0XFA # ALL_LED output and brightness control byte 0
# ALL_LED_ON_H = 0XFB # ALL_LED output and brightness control byte 1
ALL_LED_OFF_L = 0XFC # ALL_LED output and brightness control byte 2
# ALL_LED_OFF_H = 0XFD # ALL_LED output and brightness control byte 3
PRE_SCALE = 0XFE # Prescaler for PWM output frequency
_TESTMODE = 0xFF # Defines test mode to be entered (Should not be used!)
# LED1_ON_L = 0X0A # LED1 output and brightness control byte 0
# LED1_ON_H = 0X0B # LED1 output and brightness control byte 1
# LED1_OFF_L = 0X0C # LED1 output and brightness control byte 2
# LED1_OFF_H = 0X0D # LED1 output and brightness control byte 3
# LED2_ON_L = 0X0E # LED2 output and brightness control byte 0
# LED2_ON_H = 0X0F # LED2 output and brightness control byte 1
# LED2_OFF_L = 0X10 # LED2 output and brightness control byte 2
# LED2_OFF_H = 0X11 # LED2 output and brightness control byte 3
# LED3_ON_L = 0X12 # LED3 output and brightness control byte 0
# LED3_ON_H = 0X13 # LED3 output and brightness control byte 1
# LED3_OFF_L = 0X14 # LED3 output and brightness control byte 2
# LED3_OFF_H = 0X15 # LED3 output and brightness control byte 3
# LED4_ON_L = 0X16 # LED4 output and brightness control byte 0
# LED4_ON_H = 0X17 # LED4 output and brightness control byte 1
# LED4_OFF_L = 0X18 # LED4 output and brightness control byte 2
# LED4_OFF_H = 0X19 # LED4 output and brightness control byte 3
# LED5_ON_L = 0X1A # LED5 output and brightness control byte 0
# LED5_ON_H = 0X1B # LED5 output and brightness control byte 1
# LED5_OFF_L = 0X1C # LED5 output and brightness control byte 2
# LED5_OFF_H = 0X1D # LED5 output and brightness control byte 3
# LED6_ON_L = 0X1E # LED6 output and brightness control byte 0
# LED6_ON_H = 0X1F # LED6 output and brightness control byte 1
# LED6_OFF_L = 0X20 # LED6 output and brightness control byte 2
# LED6_OFF_H = 0X21 # LED6 output and brightness control byte 3
# LED7_ON_L = 0X22 # LED7 output and brightness control byte 0
# LED7_ON_H = 0X23 # LED7 output and brightness control byte 1
# LED7_OFF_L = 0X24 # LED7 output and brightness control byte 2
# LED7_OFF_H = 0X25 # LED7 output and brightness control byte 3
# LED8_ON_L = 0X26 # LED8 output and brightness control byte 0
# LED8_ON_H = 0X27 # LED8 output and brightness control byte 1
# LED8_OFF_L = 0X28 # LED8 output and brightness control byte 2
# LED8_OFF_H = 0X29 # LED8 output and brightness control byte 3
# LED9_ON_L = 0X2A # LED9 output and brightness control byte 0
# LED9_ON_H = 0X2B # LED9 output and brightness control byte 1
# LED9_OFF_L = 0X2C # LED9 output and brightness control byte 2
# LED9_OFF_H = 0X2D # LED9 output and brightness control byte 3
# LED10_ON_L = 0X2E # LED10 output and brightness control byte 0
# LED10_ON_H = 0X2F # LED10 output and brightness control byte 1
# LED10_OFF_L = 0X30 # LED10 output and brightness control byte 2
# LED10_OFF_H = 0X31 # LED10 output and brightness control byte 3
# LED11_ON_L = 0X32 # LED11 output and brightness control byte 0
# LED11_ON_H = 0X33 # LED11 output and brightness control byte 1
# LED11_OFF_L = 0X34 # LED11 output and brightness control byte 2
# LED11_OFF_H = 0X35 # LED11 output and brightness control byte 3
# LED12_ON_L = 0X36 # LED12 output and brightness control byte 0
# LED12_ON_H = 0X37 # LED12 output and brightness control byte 1
# LED12_OFF_L = 0X38 # LED12 output and brightness control byte 2
# LED12_OFF_H = 0X39 # LED12 output and brightness control byte 3
# LED13_ON_L = 0X3A # LED13 output and brightness control byte 0
# LED13_ON_H = 0X3B # LED13 output and brightness control byte 1
# LED13_OFF_L = 0X3C # LED13 output and brightness control byte 2
# LED13_OFF_H = 0X3D # LED13 output and brightness control byte 3
# LED14_ON_L = 0X3E # LED14 output and brightness control byte 0
# LED14_ON_H = 0X3F # LED14 output and brightness control byte 1
# LED14_OFF_L = 0X40 # LED14 output and brightness control byte 2
# LED14_OFF_H = 0X41 # LED14 output and brightness control byte 3
# LED15_ON_L = 0X42 # LED15 output and brightness control byte 0
# LED15_ON_H = 0X43 # LED15 output and brightness control byte 1
# LED15_OFF_L = 0X44 # LED15 output and brightness control byte 2
# LED15_OFF_H = 0X45 # LED15 output and brightness control byte 3
# PWM Channels
#----------------------------------------------
"""!
This device has multiple PWM channels - this is a list of these
channels.
NOTE: The first address in this list is considered the default PWM
channel for the device.
The variable 'pwm_channels' can be modified for chip sets with similar
operations.
"""
pwm_channels = 16
_AVAILABLE_PWM_CHANNELS = list(range(pwm_channels)) # Full List of PWM Channels
# SparkFun PCA9685 Class/Object
#-----------------------------------------------------------------------
class QwiicPCA9685(object):
"""!
SparkFunPCA9685
Initialise the PCA9685 chip at ``address`` with ``i2c_driver``.
@param address: The I2C address to use for the device.
If not provided, the default address is
used.
@param i2c_driver: An existing i2c driver object. If not
provided a driver object is created.
@return **Bool** Constructor Initialization
True- Successful
False- Issue loading I2C driver
"""
#----------------------------------------------
# Device Name:
device_name = _DEFAULT_NAME
#----------------------------------------------
# Available Addresses:
available_addresses = _AVAILABLE_I2C_ADDRESS
#----------------------------------------------
# Available Channels:
available_pwm_channels = _AVAILABLE_PWM_CHANNELS
#----------------------------------------------
# Constructor
def __init__(self, address = None, debug = None, i2c_driver = None):
"""!
This method initializes the class object. If no 'address' or
'i2c_driver' are inputted or 'None' is specified, the method will
use the defaults.
@param address: The I2C address to use for the device.
If not provided, the method will default to
the first address in the
'available_addresses' list.
Default = 0x40
@param debug: Designated whether or not to print debug
statements.
0- Don't print debug statements
1- Print debug statements
@param i2c_driver: An existing i2c driver object. If not
provided a driver object is created from the
'qwiic_i2c' I2C driver of the SparkFun Qwiic
library.
"""
# Did the user specify an I2C address?
# Defaults to 0x40 if unspecified.
self.address = address if address != None else self.available_addresses[0]
# Load the I2C driver if one isn't provided
if i2c_driver == None:
self._i2c = qwiic_i2c.getI2CDriver()
if self._i2c == None:
print("Unable to load I2C driver for this platform.")
return
else:
self._i2c = i2c_driver
# Do you want debug statements?
if debug == None:
self.debug = 0 # Debug Statements Disabled
else:
self.debug = debug # Debug Statements Enabled (1)
#=======================================================================
# Secondary Functions
#=======================================================================
#----------------------------------------------
# Reads the value of a specific bit in the byte
def __readBit__(self, byte, bit_number):
"""!
This method returns the value of a specific bit in a byte.
@param byte: Integer or Hex value.
@param bit_number: The index number of the bit you are
interested in, starting from LSB = 0 to MSB = 7.
@return **Integer** Value of bit at bit_number location. (0 or 1)
:example:
byte = 0x12 (HEX), 12h, or 16 (DEC)
Binary: 0001 0010
index: 7654 3210
| |
MSB LSB
bit_number = 4
returns: 1
"""
if (bit_number < 0) or (bit_number > 7):
# Debug Message:
if self.debug == 1:
print("Bit number is outside the bounds of a byte")
return 0
# Returns the value of the bit
mask = 1 << bit_number
return (byte & mask) >> bit_number
#----------------------------------------------
# Writes a value to a specific bit in the byte
def __writeBit__(self, byte, bit_number, value):
"""!
This method modifies a specific bit in a byte, with a specified
value.
@param byte: Integer or Hex value.
@param bit_number: The index number of the bit you are
interested in, starting from LSB = 0.
@param value: Value to be set at specific bit location (0
or 1).
@return **Integer** Value of modified byte
:example:
Original Byte:
byte = 0x12 (HEX), 12h, or 16
Binary: 0001 0010
index: 7654 3210
| |
MSB LSB
Change:
bit_number = 4
value = 0
Output Byte:
returns: 2
Binary: 0000 0010
"""
if value not in [0, 1]:
# Debug Message:
if self.debug == 1:
print("Invalid value input. Value must be 0 or 1.")
return byte
if bit_number < 0 or bit_number > 7:
# Debug Message:
if self.debug == 1:
print("Bit number is outside the bounds of a byte.")
return byte
# Returns modified byte
return (byte & ~(1 << bit_number)) | (value << bit_number)
#----------------------------------------------
# Checks I2C connection
def is_connected(self):
"""!
This method checks if the "i2c_driver" can connect to the device
at the specified or default address.
@return **Bool** Device Connection
True- Successful
False- Can't find device
"""
return qwiic_i2c.isDeviceConnected(self.address)
#----------------------------------------------
# Checks Value of Address Bits
def get_addr_bit(self, addr_bit = None):
"""!
Reads the value of a specified address bit in MODE 1 register.
@param addr_bit: Specify address bit.
0- ALLCALL Bit (Default)
1- SUB1 Bit
2- SUB2 Bit
3- SUB3 Bit
@return **Integer** Value of specified address bit.
0- Normal Mode
1- Low Power Mode; Oscillator Off.
(Default)
"""
# Read MODE1 register
mode1 = self._i2c.readByte(self.address, MODE1)
if addr_bit == None: # If no input for addr_bit, sets to ALLCALL
get_addr_bit = 0 # ALLCALL bit
elif addr_bit < 0 or addr_bit > 3: # Checks for valid input
# Debug Message:
if self.debug == 1:
print("Invalid addr_bit input. Selecting ALLCALL bit.")
get_addr_bit = 0 # ALLCALL bit
else: # Use input 'addr_bit'
get_addr_bit = addr_bit
# Reads specified address bit in MODE1.
addrMode = self.__readBit__(mode1, get_addr_bit)
return addrMode
#----------------------------------------------
# Writes Value to Address Bits
def set_addr_bit(self, addr_bit, value):
"""!
Writes the value to a specified address bit in MODE 1 register.
@param addr_bit: Specify address bit.
0- ALLCALL Bit
1- SUB1 Bit
2- SUB2 Bit
3- SUB3 Bit
@param value: Specify address bit.
0- Disables specified address (Default on
SUB1, SUB2, SUB3)
1- Enables specified address (Default on
ALLCALL)
"""
# Read MODE1 register
mode1 = self._i2c.readByte(self.address, MODE1)
# Checks for valid input
if addr_bit < 0 or addr_bit > 3:
# Debug Message:
if self.debug == 1:
print("Invalid addr_bit input.")
return #False
else: # Use input 'addr_bit'
set_addr_bit = addr_bit
if value != 0 and value != 1: # Checks for valid input
# Debug Message:
if self.debug == 1:
print("Invalid value input.")
return #False
else: # Use input 'value'
set_value = value
# Writes to specified address bit in MODE1.
addrMode = self.__writeBit__(mode1, set_addr_bit, set_value)
self._i2c.writeByte(self.address, MODE1, addrMode)
#----------------------------------------------
# Checks Value of SLEEP Bit
def get_sleep_bit(self):
"""!
Reads the value of SLEEP bit in MODE 1 register. When enabled, it
the chip there is no PWM control.
@return **Integer** Value of SLEEP bit.
0- Normal Mode
1- Low Power Mode; Oscillator Off. (Default)
"""
# Read MODE1 register
mode1 = self._i2c.readByte(self.address, MODE1)
sleep_bit = 4 # Fifth bit in register
# Reads SLEEP bit in MODE1.
sleepMode = self.__readBit__(mode1, sleep_bit)
return sleepMode
#----------------------------------------------
# Writes Value to SLEEP Bit
def set_sleep_bit(self, value = None):
"""!
Changes the value of SLEEP bit in MODE 1 register.
@param value: Value to set SLEEP bit.
0- Normal Mode
1- Low Power Mode; Oscillator Off. (Default)
"""
# Read MODE1 register
mode1 = self._i2c.readByte(self.address, MODE1)
sleep_bit = 4 # Fifth bit in register
if value == None: # If no input value, sets to default value
set_value = 1 # Default
elif value != 0 and value != 1: # Checks for valid input
# Debug Message:
if self.debug == 1:
print("Invalid value input. Setting to default value (1 = Low Power Mode; Oscillator Off).")
set_value = 1 # Default
else: # Use input 'value'
set_value = value
# Writes 'value' to the SLEEP bit in the MODE 1 register
sleepMode = self.__writeBit__(mode1, sleep_bit, set_value)
self._i2c.writeByte(self.address, MODE1, sleepMode)
#----------------------------------------------
# Checks Value of AI Bit
def get_auto_increment_bit(self):
"""!
Reads the value of AI bit in MODE 1 register. When enabled, it
allows users to write of multiple bytes (i.e. words).
@return **Integer** Value of AI bit.
0- Auto-Increment Disabled (Default)
1- Auto-Increment Enabled
"""
# Read MODE1 register
mode1 = self._i2c.readByte(self.address, MODE1)
ai_bit = 5 # Sixth bit in register
# Reads AI bit in MODE1.
aiMode = self.__readBit__(mode1, ai_bit)
return aiMode
#----------------------------------------------
# Writes Value to AI Bit
def set_auto_increment_bit(self, value = None):
"""!
Changes value of AI bit in MODE 1 register. When enabled, it
allows users to write of multiple bytes (i.e. words).
@param value: Value to set AI bit.
0- Auto-Increment Disabled (Default)
1- Auto-Increment Enabled
"""
# Read MODE1 register
mode1 = self._i2c.readByte(self.address, MODE1)
ai_bit = 5 # Sixth bit in register
if value == None: # If no input value, sets to default value
set_value = 0 # Default
elif value != 0 and value != 1: # Checks for valid input
# Debug Message:
if self.debug == 1:
print("Invalid value input. Setting to default value (0- Auto-Increment Disabled).")
set_value = 0 # Default
else: # Use input 'value'
set_value = value
# Writes 'value' to the AI bit in the MODE 1 register
aiMode = self.__writeBit__(mode1, ai_bit, set_value)
self._i2c.writeByte(self.address, MODE1, aiMode)
#----------------------------------------------
# Checks Value of EXTCLK Bit
def get_extclock_bit(self):
"""!
Reads the value of EXTCLK bit in MODE 1 register. When enabled, it
allows for an external clock signal. It also affects the refresh
rate:
EXTCLK
refresh_rate = ----------------------
4096 x (prescale +1)
@return **Integer** Value of EXTCLK bit.
0- Use Internal Clock (Default)
1- Use EXTCLK Pin Clock.
NOTE: This bit is a "sticky bit", that is, it cannot be cleared
by writing a logic 0 to it. The EXTCLK can only be cleared by a
power cycle or software reset.
"""
# Read MODE1 register
mode1 = self._i2c.readByte(self.address, MODE1)
extclk_bit = 6 # Seventh bit in register
# Reads EXTCLK bit in MODE1.
extclkMode = self.__readBit__(mode1, extclk_bit)
return extclkMode
#----------------------------------------------
# Checks Value of RESTART Bit
def get_restart_bit(self):
"""!
Reads the value of RESTART bit in MODE 1 register.
@return **Integer** Value of Restart Mode bit.
0- Restart Disabled (Default)
1- Restart Enabled
"""
# Reads MODE 1 register
mode1 = self._i2c.readByte(self.address, MODE1)
rs_bit = 7 # Eighth bit in register
# Reads the RESTART bit from the MODE 1 register
rsMode = self.__readBit__(mode1, rs_bit)
# Returns the RESTART bit value
return rsMode
#----------------------------------------------
# Writes Value to RESTART Mode Bit
def write_restart_bit(self, value = None):
"""!
Writes values to RESTART bit in MODE 1 register.
@param value: Value to write to Restart Mode bit.
0 or 1
@return **Integer** Value of RESTART bit after changes.
0- Restart Disabled (Default)
1- Restart Enabled
NOTE: Value aren't set, just written. The bit is set by the
state of the chip and its current operation.
"""
# Reads MODE 1 register
mode1 = self._i2c.readByte(self.address, MODE1)
rs_bit = 7 # Eighth bit in register
if value == None: # If no input value
# Debug Message:
if self.debug == 1:
print("Invalid value input.")
# value = 0 # Default
elif value != 0 and value != 1: # Checks for valid input
# Debug Message:
if self.debug == 1:
print("Invalid value input.")
# value = 0 # Default
else:
rsMode = self.__writeBit__(mode1, rs_bit, value)
self._i2c.writeByte(self.address, MODE1, rsMode)
# Returns RESTART bit value
return self.get_restart_bit()
#----------------------------------------------
# Checks Value of OUTNE Bits
def get_outne_bits(self):
"""!
Reads the value of OUTNE bits in MODE 2 register. When the active
LOW output (OE pin) is enabled, this setting allows users to
enable or disable all the LED outputs at the same time.
@return **Integer** Value of OUTNE bits. When OE = 1:
0- LEDn = 0
1- If OUTDRV = 1 then LEDn = 1
If OUTDRV = 0 then LEDn = high-impedence
(same as OUTNE[1:0] = b'10')
2 to 3- LEDn = high-impedence
"""
# Read MODE2 register
mode2 = self._i2c.readByte(self.address, MODE2)
# outne0_bit = 0 # First bit in register
# outne1_bit = 1 # Second bit in register
# Reads OUTNE bits in MODE2.
outne_bits = mode2 & 3
# outne0 = self.__readBit__(mode2, outne0_bit)
# outne1 = self.__readBit__(mode2, outne1_bit)
# outne_bits = outne0 << 1 | outne1
return outne_bits
#----------------------------------------------
# Writes Value to OUTNE Bits
def set_outne_bit(self, value = None):
"""!
Reads the value of OUTNE bits in MODE 2 register. When the active
LOW output (OE pin) is enabled, this setting allows users to
enable or disable all the LED outputs at the same time.
@param value: Value of OUTNE bits. When OE = 1:
0- LEDn = 0
1- If OUTDRV = 1 then LEDn = 1
If OUTDRV = 0 then LEDn = high-impedence
(same as OUTNE[1:0] = b'10')
2 to 3- LEDn = high-impedence
@return **Bool** Function Operation
True- Successful
False- Issue in Execution
"""
# Read MODE2 register
mode2 = self._i2c.readByte(self.address, MODE2)
outne0_bit = 0 # First bit in register
outne1_bit = 1 # Second bit in register
if value == None: # If no input value
# Debug Message:
if self.debug == 1:
print("Invalid value input. Set bit value.")
return False
elif value < 0 or value > 3: # Checks for valid input
# Debug Message:
if self.debug == 1:
print("Invalid value input. Value outside bounds.")
return False
else: # Use input 'value'
set_value = value
# Pulls 'values' to modify OUTNE bits
outne0_val = (set_value & (1 << outne0_bit)) >> outne0_bit
outne1_val = (set_value & (1 << outne1_bit)) >> outne1_bit
# Writes to 'values' OUTNE bits in MODE2.
outne_temp = self.__writeBit__(mode2, outne0_bit, outne0_val)
outne_byte = self.__writeBit__(outne_temp, outne1_bit, outne1_val)
self._i2c.writeByte(self.address, MODE2, outne_byte)
return True
#----------------------------------------------
# Checks Value of OUTDRV Bit
def get_outdrv_bit(self):
"""!
Reads the value of OUTDRV bit in MODE 2 register. Determines how the
outputs are driven.
@return **Integer** Value of OUTDRV bits.
0- Outputs are configured with an open-drain
structure
1- Outputs are configured with a totem-pole
structure
"""
# Read MODE2 register
mode2 = self._i2c.readByte(self.address, MODE2)
outdrv_bit = 2 # Third bit in register
# Reads OUTDRV bit in MODE2.
outdrvMode = self.__readBit__(mode2, outdrv_bit)
return outdrvMode
#----------------------------------------------
# Writes Value to OUTDRV Bits
def set_outdrv_bit(self, value = None):
"""!
Reads value of OUTDRV bits in MODE 2 register. Configures how
the outputs are driven.
@param value: Value of OUTDRV bits.
0- Outputs are configured with an open-drain
structure
1- Outputs are configured with a totem-pole
structure
"""
# Read MODE2 register
mode2 = self._i2c.readByte(self.address, MODE2)
outdrv_bit = 2 # Third bit in register
if value == None: # If no input value
# Debug Message:
if self.debug == 1:
print("Invalid value input. Set bit value.")
return False
elif value != 0 and value != 1: # Checks for valid input
# Debug Message:
if self.debug == 1:
print("Invalid value input. Value outside bounds.")
return False
else: # Use input 'value'
set_value = value
# Writes 'value' to the OUTDRV bit in the MODE 2 register
outdrvMode = self.__writeBit__(mode2, outdrv_bit, set_value)
self._i2c.writeByte(self.address, MODE2, outdrvMode)
#----------------------------------------------
# Checks Value of OCH Bit
def get_och_bit(self):
"""!
Reads the value of OCH bit in MODE 2 register. Determines when the
outputs change.
@return **Integer** Value of OCH bits.
0- Outputs change on STOP command.
NOTE: Change of the outputs at the STOP command
allows synchronizing outputs of more than one
PCA9685. Applicable to registers from 06h
(LED0_ON_L) to 45h (LED15_OFF_H) only. 1 or more
registers can be written, in any order, before
STOP.
1- Outputs change on ACK.
NOTE: Update on ACK requires all 4 PWM channel
registers to be loaded before outputs will
change on the last ACK.
"""
# Read MODE2 register
mode2 = self._i2c.readByte(self.address, MODE2)
och_bit = 3 # Forth bit in register
# Reads OCH bit in MODE2.
ochMode = self.__readBit__(mode2, och_bit)
return ochMode
#----------------------------------------------
# Writes Value to OCH Bits
def set_och_bit(self, value = None):
"""!
Reads the value of OCH bits in MODE 2 register. Configures when the
outputs change.
@param value: Value of OCH bits.
0- Outputs change on STOP command.
NOTE: Change of the outputs at the STOP
command allows synchronizing outputs of more
than one PCA9685. Applicable to registers
from 06h (LED0_ON_L) to 45h (LED15_OFF_H)
only. 1 or more registers can be written,
in any order, before STOP.
1- Outputs change on ACK.
NOTE: Update on ACK requires all 4 PWM
channel registers to be loaded before
outputs will change on the last ACK.
"""
# Read MODE2 register
mode2 = self._i2c.readByte(self.address, MODE2)
och_bit = 3 # Forth bit in register
if value == None: # If no input value
# Debug Message:
if self.debug == 1:
print("Invalid value input. Set bit value.")
return False
elif value != 0 and value != 1: # Checks for valid input
# Debug Message:
if self.debug == 1:
print("Invalid value input. Value outside bounds.")
return False
else: # Use input 'value'
set_value = value
# Writes 'value' to the OCH bit in the MODE 2 register
ochMode = self.__writeBit__(mode2, och_bit, set_value)
self._i2c.writeByte(self.address, MODE2, ochMode)
#----------------------------------------------
# Checks Value of INVRT Bit
def get_invrt_bit(self):
"""!
Reads the value of INVRT bit in MODE 2 register. Determines how the
outputs are driven. See Section 7.7 "Using the PCA9685 with and
without external drivers" of the datasheet.
@return **Integer** Value of INVRT bits.
0- Outputs change on STOP command.
1- Outputs change on ACK.
"""
# Read MODE2 register
mode2 = self._i2c.readByte(self.address, MODE2)
invrt_bit = 4 # Fifth bit in register
# Reads INVRT bit in MODE2.
invrtMode = self.__readBit__(mode2, invrt_bit)
return invrtMode
#----------------------------------------------
# Writes Value to INVRT bit
def set_invrt_bit(self, value = None):
"""!
Configures value of INVRT bits in MODE 2 register. Configures
how the outputs are driven. See Section 7.7 "Using the PCA9685
with and without external drivers" of the datasheet.
@param value: Value of INVRT bits.
0- Outputs change on STOP command.
1- Outputs change on ACK.
"""
# Read MODE2 register
mode2 = self._i2c.readByte(self.address, MODE2)
invrt_bit = 4 # Fifth bit in register
if value == None: # If no input value
# Debug Message:
if self.debug == 1:
print("Invalid value input. Set bit value.")
return False
elif value != 0 and value != 1: # Checks for valid input
# Debug Message:
if self.debug == 1:
print("Invalid value input. Value outside bounds.")
return False
else: # Use input 'value'
set_value = value
# Writes 'value' to the INVRT bit in the MODE 2 register
invrtMode = self.__writeBit__(mode2, invrt_bit, set_value)
self._i2c.writeByte(self.address, MODE2, invrtMode)
#----------------------------------------------
# Checks Value of LED Channel Bytes
def get_channel_word(self, channel = None, on_off = None):
"""!
Reads the ON/OFF timing for the specified PWM channel.
@param channel: PWM channel.
0 to 16
@param on_off: On or Off setting.
0- OFF Start timing (end of ON timing)
1- ON Start timing (anything greater than 0 is
considered a delay)
@return Word (2 bytes)
NOTE: There are two 12-bit registers per LED output. Both
registers will hold a value from 0 to 4095. One 12-bit register
will hold a value for the ON time and the other 12-bit register
will hold the value for the OFF time.
The ON and OFF times are compared with the value of a 12-bit
counter that will be running continuously from 0000h to 0FFFh (0
to 4095 decimal).