Skip to content

Commit

Permalink
Adding experimental support for FTDI GPIO
Browse files Browse the repository at this point in the history
  • Loading branch information
enesbcs committed May 6, 2020
1 parent 625ed59 commit b8ca368
Show file tree
Hide file tree
Showing 13 changed files with 1,672 additions and 46 deletions.
17 changes: 13 additions & 4 deletions RPIEasy.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ def hardwareInit():
if opv:
pinout = opv["pins"]
misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,str(opv["name"])+" "+str(opv["pins"])+" pins")
hwtype = rpieGlobals.ossubtype
if pinout=="0":
try:
import lib.lib_ftdigpios as ftdigpio # check for ftdi hwtype?
if ftdigpio.get_ftdi_devices(0)>0:
hwtype = 19
pinout = "ftdi"
except Exception as e:
print(e)
print("Load network settings...")
Settings.loadnetsettings()
Settings.NetMan.networkinit()
Expand All @@ -77,22 +86,22 @@ def hardwareInit():
if pinout != "0":
Settings.loadpinout()
try:
gpios.preinit(rpieGlobals.ossubtype) # create HWPorts variable
gpios.preinit(hwtype) # create HWPorts variable
except Exception as e:
print("init",e)
if (("40" in pinout) and (len(Settings.Pinout)<41)) or (("26" in pinout) and (len(Settings.Pinout)<27)):
if (("40" in pinout) and (len(Settings.Pinout)<41)) or (("26" in pinout) and (len(Settings.Pinout)<27)) or (pinout=="ftdi" and len(Settings.Pinout)<1):
print("Creating new pinout")
try:
gpios.HWPorts.createpinout(pinout)
except Exception as e:
print("cp:",e)
print("Pinout creation error:",e)
perror = False
try:
gpios.HWPorts.readconfig()
except Exception as e:
# print(e) # debug
perror = True
if perror or len(Settings.Pinout)<8:
if perror or len(Settings.Pinout)<1:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"Your GPIO can not be identified!")
Settings.Pinout = []
else:
Expand Down
37 changes: 31 additions & 6 deletions Settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ def get_i2c_pins(): # get list of enabled i2c pin numbers
gplist = []
try:
for p in range(len(Pinout)):
if int(Pinout[p]["altfunc"])==1:
if "I2C" in Pinout[p]["name"][1]:
gplist.append(Pinout[p]["name"][0]+"/"+Pinout[p]["name"][1])
if int(Pinout[p]["altfunc"])!=0:
n = Pinout[p]["name"]
for i in range(len(n)):
if "I2C" in Pinout[p]["name"][i]:
gplist.append(Pinout[p]["name"][0]+"/"+Pinout[p]["name"][i])
except:
pass
return gplist
Expand All @@ -105,18 +107,41 @@ def savesettings():
f = open(settingsfile,'w')
settingjson = jsonpickle.encode(Settings)
f.write(settingjson)
except:
except Exception as e:
success = 0
return success

def savetasks():
global Tasks, tasksfile
success = 1
try:
import copy
Tasks_Shadow = copy.copy(Tasks) # make a copy of original tasks
except Exception as e:
Tasks_Shadow = Tasks.copy() # this method is not working well
tasktoinit = []
try:
if len(Tasks)>0: # debug
for T in range(len(Tasks_Shadow)):
try:
for i in Tasks_Shadow[T].__dict__:
try:
test = jsonpickle.encode(Tasks_Shadow[T].__dict__[i]) # check if jsonpickle is needed
except:
Tasks_Shadow[T].__dict__[i]=None
if not T in tasktoinit:
tasktoinit.append(T)
except Exception as e:
pass
f = open(tasksfile,'w')
settingjson = jsonpickle.encode(Tasks)
settingjson = jsonpickle.encode(Tasks_Shadow)
f.write(settingjson)
except:
for t in tasktoinit:
try:
Tasks[t].plugin_init()
except:
pass
except Exception as e:
success = 0
return success

Expand Down
30 changes: 23 additions & 7 deletions _P001_Switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@
import rpieTime
import time
import misc
import gpios
import lib.lib_gpiohelper as gpiohelper
import webserver
try:
import gpios
gpioinit = True
except:
gpioinit = False

class Plugin(plugin.PluginProto):
PLUGIN_ID = 1
Expand Down Expand Up @@ -56,8 +60,16 @@ def plugin_exit(self):
def plugin_init(self,enableplugin=None):
plugin.PluginProto.plugin_init(self,enableplugin)
self.decimals[0]=0
if int(self.taskdevicepin[0])>=0 and self.enabled:
self.set_value(1,int(gpios.HWPorts.input(int(self.taskdevicepin[0]))),True) # Sync plugin value with real pin state
self.initialized = False
try:
gpioinit = gpios.HWPorts is not None
except:
gpioinit = False
if int(self.taskdevicepin[0])>=0 and self.enabled and gpioinit:
try:
self.set_value(1,int(gpios.HWPorts.input(int(self.taskdevicepin[0]))),True) # Sync plugin value with real pin state
except:
pass
try:
if int(self.taskdevicepluginconfig[3])<1:
self.taskdevicepluginconfig[3] = gpios.BOTH # for compatibility
Expand All @@ -78,6 +90,7 @@ def plugin_init(self,enableplugin=None):
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"Event can not be added, register backup timer "+str(e))
self.timer100ms = True
self.initialized = True

def webform_load(self):
webserver.addFormNote("Please make sure to select <a href='pinout'>pin configured</a> for input for default (or output to report back its state)!")
Expand All @@ -88,10 +101,13 @@ def webform_load(self):
optionvalues = [0,1,2]
webserver.addFormSelector("Switch Button Type","p001_button",len(optionvalues),options,optionvalues,None,self.taskdevicepluginconfig[2])
webserver.addFormNote("Use only normal switch for output type, i warned you!")
options = ["BOTH","RISING","FALLING"]
optionvalues = [gpios.BOTH,gpios.RISING,gpios.FALLING]
webserver.addFormSelector("Event detection type","p001_det",len(optionvalues),options,optionvalues,None,self.taskdevicepluginconfig[3])
webserver.addFormNote("Only valid if event detection activated")
try:
options = ["BOTH","RISING","FALLING"]
optionvalues = [gpios.BOTH,gpios.RISING,gpios.FALLING]
webserver.addFormSelector("Event detection type","p001_det",len(optionvalues),options,optionvalues,None,self.taskdevicepluginconfig[3])
webserver.addFormNote("Only valid if event detection activated")
except:
pass
return True

def webform_save(self,params):
Expand Down
2 changes: 1 addition & 1 deletion _P012_LCD.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def plugin_init(self,enableplugin=None):
self.__del__()
return False
if self.enabled:
if int(self.taskdevicepin[0])>=0:
if int(self.taskdevicepin[0])>0:
try:
gpios.HWPorts.remove_event_detect(int(self.taskdevicepin[0]))
except:
Expand Down
2 changes: 2 additions & 0 deletions _P036_FrameOLED.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ def plugin_init(self,enableplugin=None):
if self.device is not None:
try:
lc = int(self.taskdevicepluginconfig[4])
self.width = self.device.width
self.height = self.device.height
except:
lc = 1
if lc < 1:
Expand Down
7 changes: 7 additions & 0 deletions gpios.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,12 @@ def preinit(gpiotype):
RISING=GPIOHW.RISING
FALLING=GPIOHW.FALLING
HWPorts = hwports()
elif gpiotype==19: # FTDI
import lib.lib_ftdigpios as GPIOHW
from lib.lib_ftdigpios import hwports
BOTH=GPIOHW.BOTH
RISING=GPIOHW.RISING
FALLING=GPIOHW.FALLING
HWPorts = hwports()

#HWPorts = None
79 changes: 79 additions & 0 deletions lib/ftdi/smbus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import gpios

class SMBus:

def __init__(self,busnum):
self.busnum = busnum
try:
self.bus = gpios.HWPorts.get_i2c_ctrl(busnum)
except:
self.bus = None

def write_quick(self,addr):
pass # not supported

def read_byte(self,addr):
try:
i2c = self.bus.get_port(addr)
res = i2c.read(1)
except:
res = None
return res

def write_byte(self,addr,val):
try:
i2c = self.bus.get_port(addr)
i2c.write(val)
except:
pass

def read_byte_data(self,addr,cmd):
try:
i2c = self.bus.get_port(addr)
res = i2c.read_from(int(cmd),readlen=1)
except:
res = None
return res

def write_byte_data(self,addr,cmd,val):
try:
i2c = self.bus.get_port(addr)
i2c.write_to(int(cmd),val)
except:
pass

def read_word_data(self,addr,cmd):
try:
i2c = self.bus.get_port(addr)
res = i2c.read_from(int(cmd),readlen=2)
except:
res = None
return res

def write_word_data(self,addr,cmd,val):
try:
i2c = self.bus.get_port(addr)
i2c.write_to(int(cmd),val)
except:
pass

def read_block_data(self,addr,cmd,dlen=0):
try:
i2c = self.bus.get_port(addr)
res = i2c.read_from(int(cmd),readlen=dlen)
except:
res = None
return res

def read_i2c_block_data(self,addr,cmd,dlen=0):
self.read_block_data(addr,cmd,dlen)

def write_block_data(self,addr,cmd,vals):
try:
i2c = self.bus.get_port(addr)
i2c.write_to(int(cmd),vals)
except:
pass

def write_i2c_block_data(self,addr,cmd,vals):
self.write_block_data(addr,cmd,vals)
111 changes: 111 additions & 0 deletions lib/ftdi/smbus2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import gpios
import sys

class SMBus:

def open(self):
pass

def close(self):
pass

def __init__(self,busnum):
self.busnum = busnum
try:
self.bus = gpios.HWPorts.get_i2c_ctrl(busnum)
except:
self.bus = None

def write_quick(self,addr):
pass # not supported

def read_byte(self,addr):
try:
i2c = self.bus.get_port(addr)
res = i2c.read(1)
except:
res = None
return res

def write_byte(self,addr,val):
try:
i2c = self.bus.get_port(addr)
i2c.write(val)
except:
pass

def read_byte_data(self,addr,cmd):
try:
i2c = self.bus.get_port(addr)
res = i2c.read_from(int(cmd),readlen=1)
except:
res = None
return res

def write_byte_data(self,addr,cmd,val):
try:
i2c = self.bus.get_port(addr)
i2c.write_to(int(cmd),val)
except:
pass

def read_word_data(self,addr,cmd):
try:
i2c = self.bus.get_port(addr)
res = i2c.read_from(int(cmd),readlen=2)
except:
res = None
return res

def write_word_data(self,addr,cmd,val):
try:
i2c = self.bus.get_port(addr)
i2c.write_to(int(cmd),val)
except:
pass

def read_block_data(self,addr,cmd,dlen=0):
try:
i2c = self.bus.get_port(addr)
res = i2c.read_from(int(cmd),readlen=dlen)
except:
res = None
return res

def read_i2c_block_data(self,addr,cmd,dlen=0):
self.read_block_data(addr,cmd,dlen)

def write_block_data(self,addr,cmd,vals):
try:
i2c = self.bus.get_port(addr)
i2c.write_to(int(cmd),vals)
except:
pass

def write_i2c_block_data(self,addr,cmd,vals):
self.write_block_data(addr,cmd,vals)

def i2c_rdwr(self,datas):
self.write_block_data(datas[0],datas[1][0],datas[1][1:])

class i2c_msg():
@staticmethod
def write(address, buf):
"""
Prepares an i2c write transaction.
:param address: Slave address.
:type address: int
:param buf: Bytes to write. Either list of values or str.
:type buf: list
:return: New :py:class:`i2c_msg` instance for write operation.
:rtype: :py:class:`i2c_msg`
"""
if sys.version_info.major >= 3:
if type(buf) is str:
buf = bytes(map(ord, buf))
else:
buf = bytes(buf)
else:
if type(buf) is not str:
buf = ''.join([chr(x) for x in buf])
return address, buf
Loading

0 comments on commit b8ca368

Please sign in to comment.