Skip to content

Commit

Permalink
Adding servo command, startup name resolution fix
Browse files Browse the repository at this point in the history
  • Loading branch information
enesbcs committed May 23, 2020
1 parent b95c72d commit 69f1156
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 4 deletions.
7 changes: 6 additions & 1 deletion RPIEasy.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ def initprogram():
CPluginInit()
NPluginInit()
RulesInit()
signal.signal(signal.SIGINT, signal_handler) # avoid stoling by a plugin
timer100ms = millis()
timer20ms = timer100ms
timer1s = timer100ms
Expand All @@ -427,11 +428,15 @@ def initprogram():
except:
ports = [80,8080,8008,591] # check for usable ports
up = 0
try:
ownaddr = socket.gethostname()
except:
ownaddr = '127.0.0.1'
for p in ports:
up = p
try:
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind((socket.gethostname(), up))
serversocket.bind((ownaddr, up))
serversocket.close()
except:
up = 0
Expand Down
2 changes: 1 addition & 1 deletion _P001_Switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def plugin_write(self,cmd):
res = False
cmdarr = cmd.split(",")
cmdarr[0] = cmdarr[0].strip().lower()
if cmdarr[0].strip().lower() in ["gpio","pwm","pulse","longpulse","tone","rtttl","status"]:
if cmdarr[0].strip().lower() in gpiohelper.commandlist:
res = gpiohelper.gpio_commands(cmd)
return res

Expand Down
2 changes: 1 addition & 1 deletion _P029_DomoOutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,6 @@ def plugin_write(self,cmd):
res = False
cmdarr = cmd.split(",")
cmdarr[0] = cmdarr[0].strip().lower()
if cmdarr[0].strip().lower() in ["gpio","pwm","pulse","longpulse","tone","rtttl","status"]:
if cmdarr[0].strip().lower() in gpiohelper.commandlist:
res = gpiohelper.gpio_commands(cmd)
return res
39 changes: 39 additions & 0 deletions lib/lib_gpiohelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import lib.lib_rtttl as rtttllib
import threading

commandlist = ["gpio","pwm","pulse","longpulse","tone","rtttl","status","servo"]

def syncvalue(bcmpin,value):
for x in range(0,len(Settings.Tasks)):
if (Settings.Tasks[x]) and type(Settings.Tasks[x]) is not bool: # device exists
Expand Down Expand Up @@ -216,6 +218,36 @@ def gpio_commands(cmd):
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,str(e))
suc = False
res = True

elif cmdarr[0]=="servo":
snr = -1
pin = -1
pos = -1
gi = -1
logline = ""
try:
snr = int(cmdarr[1].strip())
pin = int(cmdarr[2].strip())
pos = int(cmdarr[3].strip())
except:
snr = -1
pin = -1
pos = 0
if snr>-1 and pin>-1 and pos>0:
suc = False
try:
suc = True
logline = "BCM"+str(pin)+" to servo "+str(pos)+" angle"
misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,logline)
setservoangle(pin,pos)
gi = gpios.GPIO_refresh_status(pin,pstate=0,pluginid=1,pmode="servo",logtext=logline)
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"BCM"+str(pin)+" Servo "+str(e))
suc = False
if gi>-1:
return gpios.GPIO_get_status(gi)
res = True

elif cmdarr[0] == "status":
pin = -1
subcmd = ""
Expand All @@ -233,6 +265,13 @@ def gpio_commands(cmd):

return res

def setservoangle(servopin,angle):
if angle>= 0 and angle<= 180:
try:
gpios.HWPorts.servo_pwm(servopin,angle)
except:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"Servo command is not supported on this hardware!")

def play_tone(pin,freq,delay):
gpios.HWPorts.output_pwm(pin,50,freq) # generate 'freq' sound
s = float(delay/1000)
Expand Down
49 changes: 49 additions & 0 deletions lib/lib_rpigpios.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,55 @@ def output_pwm(self,bcmpin,pprop,pfreq=1000): # default 1000Hz
self.pwmo[p]["o"].start(prop)
return True

def servo_pwm(self,bcmpin,angle):
pin = int(bcmpin)
freq = 50
startprop = 8
prop = angle / 18. + 3.
if pin in self.pwm: # hardpwm
pid = 0
if self.pwm[0] == pin:
pid = 0
else:
pid = 1
if prop<=0:
self.pwmo[pid]["o"].stop()
else:
self.pwmo[pid]["pin"]=pin
self.pwmo[pid]["o"].set_frequency(freq)
self.pwmo[pid]["o"].set_duty_prop(prop)
self.pwmo[pid]["o"].enable()
time.sleep(0.3)
self.pwmo[pid]["o"].stop()
return True
else: # softpwm
pfound = False
for p in range(len(Settings.Pinout)):
if int(Settings.Pinout[p]["BCM"])==pin :
if (int(Settings.Pinout[p]["startupstate"]) not in [4,5,6]):
return False # if not output skip

if len(self.pwmo)>2:
for p in range(2,len(self.pwmo)):
if int(self.pwmo[p]["pin"])==pin:
if (self.pwmo[p]["o"]):
if prop<=0:
self.pwmo[p]["o"].stop()
else:
self.pwmo[p]["o"].start(startprop)
self.pwmo[p]["o"].ChangeFrequency(freq)
self.pwmo[p]["o"].ChangeDutyCycle(prop)
pfound = True
break
if pfound==False:
self.pwmo[p]["pin"] = pin
self.pwmo[p]["o"] = GPIO.PWM(pin,freq)
self.pwmo[p]["o"].start(startprop)
self.pwmo[p]["o"].ChangeDutyCycle(prop)
time.sleep(0.3)
self.pwmo[p]["o"].stop()
return True

def pwm_get_func(self,pin):
func = 0
if pin==12 or pin==13:
Expand Down
2 changes: 1 addition & 1 deletion rpieGlobals.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Copyright (C) 2018-2020 by Alexander Nagy - https://bitekmindenhol.blog.hu/
#
PROGNAME = "RPIEasy"
BUILD = 20142
BUILD = 20144
PROGVER = str(BUILD)[:1]+"."+str(BUILD)[1:2]+"."+str(BUILD)[2:]

gpMenu = []
Expand Down
1 change: 1 addition & 0 deletions webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -3128,6 +3128,7 @@ def addFormPinSelect(label,fid,choice):
def addPinSelect(fori2c,name,choice):
global TXBuffer
addSelector_Head(name,False)
addSelector_Item("None",-1,(str(choice)==str(-1)),False,"")
for x in range(len(Settings.Pinout)):
try:
if int(Settings.Pinout[x]["altfunc"]==0) and int(Settings.Pinout[x]["canchange"])>0 and int(Settings.Pinout[x]["BCM"]>-1):
Expand Down

0 comments on commit 69f1156

Please sign in to comment.