forked from enesbcs/rpieasy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_P022_PCA9685.py
141 lines (134 loc) · 4.43 KB
/
_P022_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
#!/usr/bin/env python3
#############################################################################
################ PCA9685 PWM extender plugin for RPIEasy ####################
#############################################################################
#
# Available commands:
# PCAPWM,<pin>,<value> - Control PCA9685 pwm level 0..4095
# PCAFRQ,<frequency> - Set all PWM channel frequency
#
# https://github.com/voidpp/PCA9685-driver/blob/master/pca9685_driver/device.py
#
# Copyright (C) 2018-2019 by Alexander Nagy - https://bitekmindenhol.blog.hu/
#
import plugin
import webserver
import rpieGlobals
import rpieTime
import misc
import gpios
import time
from pca9685_driver import Device # sudo pip3 install PCA9685-driver
class Plugin(plugin.PluginProto):
PLUGIN_ID = 22
PLUGIN_NAME = "Extra IO - PCA9685"
PLUGIN_VALUENAME1 = "PWM"
MAX_PINS = 15
MAX_PWM = 4095
MIN_FREQUENCY = 23.0 # Min possible PWM cycle frequency
MAX_FREQUENCY = 1500.0 # Max possible PWM cycle frequency
def __init__(self,taskindex): # general init
plugin.PluginProto.__init__(self,taskindex)
self.dtype = rpieGlobals.DEVICE_TYPE_I2C
self.vtype = rpieGlobals.SENSOR_TYPE_NONE
self.ports = 0
self.valuecount = 0
self.senddataoption = False
self.timeroption = False
self.pca = None
def plugin_init(self,enableplugin=None):
plugin.PluginProto.plugin_init(self,enableplugin)
self.initialized = False
self.pca = None
if self.enabled:
try:
i2cl = self.i2c
except:
i2cl = -1
try:
i2cport = gpios.HWPorts.geti2clist()
if i2cl==-1:
i2cl = int(i2cport[0])
except:
i2cport = []
if len(i2cport)>0 and i2cl>-1 and int(self.taskdevicepluginconfig[0])>0:
try:
freq = int(self.taskdevicepluginconfig[1])
if freq<self.MIN_FREQUENCY or freq>self.MAX_FREQUENCY:
freq = self.MAX_FREQUENCY
except:
freq = self.MAX_FREQUENCY
try:
self.pca = Device(address=int(self.taskdevicepluginconfig[0]),bus_number=int(i2cl))
if self.pca is not None:
self.initialized = True
self.pca.set_pwm_frequency(freq)
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"PCA9685 device init failed: "+str(e))
self.pca = None
if self.pca is not None:
pass
def webform_load(self): # create html page for settings
choice1 = int(float(self.taskdevicepluginconfig[0])) # store i2c address
optionvalues = []
for i in range(0x40,0x78):
optionvalues.append(i)
options = []
for i in range(len(optionvalues)):
options.append(str(hex(optionvalues[i])))
webserver.addFormSelector("Address","p022_adr",len(options),options,optionvalues,None,choice1)
webserver.addFormNote("Enable <a href='pinout'>I2C bus</a> first, than <a href='i2cscanner'>search for the used address</a>!")
webserver.addFormNumericBox("Frequency","p022_freq",self.taskdevicepluginconfig[2],self.MIN_FREQUENCY,self.MAX_FREQUENCY)
webserver.addUnit("Hz")
return True
def webform_save(self,params): # process settings post reply
cha = False
par = webserver.arg("p022_adr",params)
if par == "":
par = 0x40
if self.taskdevicepluginconfig[0] != int(par):
cha = True
self.taskdevicepluginconfig[0] = int(par)
par = webserver.arg("p022_freq",params)
if par == "":
par = self.MAX_FREQUENCY
if self.taskdevicepluginconfig[2] != int(par):
cha = True
self.taskdevicepluginconfig[2] = int(par)
if cha:
self.plugin_init()
return True
def plugin_write(self,cmd): # handle incoming commands
res = False
cmdarr = cmd.split(",")
cmdarr[0] = cmdarr[0].strip().lower()
if self.pca is not None:
if cmdarr[0] == "pcapwm":
pin = -1
val = -1
try:
pin = int(cmdarr[1].strip())
val = int(cmdarr[2].strip())
except:
pin = -1
if pin>-1 and val>=0 and val<=self.MAX_PWM:
misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,"PCAPWM"+str(pin)+" set to "+str(val))
try:
self.pca.set_pwm(pin, val)
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"PCAPWM"+str(pin)+": "+str(e))
return True
if cmdarr[0] == "pcafrq":
freq = -1
try:
freq = int(cmdarr[1].strip())
except:
freq = -1
if (freq>-1) and (freq>=self.MIN_FREQUENCY) and (freq<=self.MAX_FREQUENCY):
misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,"PCAFRQ"+str(freq))
try:
self.pca.set_pwm_frequency(freq)
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"PCAFRQ"+str(e))
return True
return res