Terminato gertione driver e implementato prima versione beta del driver spb16ch

This commit is contained in:
lejubila
2017-08-20 12:21:30 +02:00
parent 2056b30be0
commit 4322a83977
26 changed files with 739 additions and 10 deletions

7
drv/spb16ch/README.md Normal file
View File

@@ -0,0 +1,7 @@
# Driver per controllare la scheda "Smart Power Board 16 channel with RTC" (spb16ch)
Questo driver richiede l'interprete python e la libreria python-smbus. Inoltre l'utente pi deve fare parte del gruppo i2c
sudo apt-get install python python-smbus
sudo usermod -a -G i2c pi

View File

@@ -0,0 +1,20 @@
#
# Mapping rele spb16ch: l'indice indica il relè, ogni elemento deve essere lungo quattro caratteri,
# i primi due indicano il mux channel, mentre gli ultimi due caratteri indicano il numero del rele sul canale
#
SPB16CH_RELE_MAP[1]=" 0 1"
SPB16CH_RELE_MAP[2]=" 0 2"
SPB16CH_RELE_MAP[3]=" 0 3"
SPB16CH_RELE_MAP[4]=" 0 4"
SPB16CH_RELE_MAP[5]=" 0 5"
SPB16CH_RELE_MAP[6]=" 0 6"
SPB16CH_RELE_MAP[7]=" 0 7"
SPB16CH_RELE_MAP[8]=" 0 8"
SPB16CH_RELE_MAP[9]=" 1 1"
SPB16CH_RELE_MAP[10]=" 1 2"
SPB16CH_RELE_MAP[11]=" 1 3"
SPB16CH_RELE_MAP[12]=" 1 4"
SPB16CH_RELE_MAP[13]=" 1 5"
SPB16CH_RELE_MAP[14]=" 1 6"
SPB16CH_RELE_MAP[15]=" 1 7"
SPB16CH_RELE_MAP[16]=" 1 8"

View File

@@ -0,0 +1,13 @@
#
# Questa funzione viene inviocata dalla funzione "init" di piGarden se sono presenti elettrovalvole o sensori che utilizzano questo driver
#
function drv_spb16ch_init {
$DIR_SCRIPT/drv/spb16ch/scripts/mux_channel.py 72 0
$DIR_SCRIPT/drv/spb16ch/scripts/gpo_init.py 25 255 0
$DIR_SCRIPT/drv/spb16ch/scripts/mux_channel.py 72 1
$DIR_SCRIPT/drv/spb16ch/scripts/gpo_init.py 25 255 0
$DIR_SCRIPT/drv/spb16ch/scripts/mux_channel.py 72 0
}

View File

@@ -0,0 +1,23 @@
#
# Inizializza il sensore di rilevamento pioggia
#
# $i identificativo gpio del sensore di pioggia
#
function drv_spb16ch_rain_sensor_init {
local FOO="bar"
}
#
# Ritorna lo stato del sensore di rilevamento pioggia
#
# $i identificativo gpio del sensore di pioggia
#
function drv_spb16ch_rain_sensor_get {
local FOO="bar"
}

View File

@@ -0,0 +1,58 @@
#
# Inizializzazione rele
#
# $1 identificativo relè da inizializzare
#
function drv_spb16ch_rele_init {
drv_spb16ch_rele_open "$1"
}
#
# Apertura rele
#
# $1 identificativo relè da aprire
#
function drv_spb16ch_rele_open {
local rele_id=`echo $1 | $CUT -d':' -f3,3`
local rele_data=${SPB16CH_RELE_MAP[$rele_id]}
if [[ -z $rele_data ]]; then
local message="Error - Rele map not defined - rele_id=$rele_id - ($1)"
log_write "$message"
message_write "warning" "$message"
fi
local channel_num=${rele_data:0:2}
local rele_num=${rele_data:2:2}
echo channel_num=$channel_num
echo rele_num=$rele_num
$DIR_SCRIPT/drv/spb16ch/scripts/mux_channel.py 72 $channel_num
$DIR_SCRIPT/drv/spb16ch/scripts/gpo_active.py 72 $rele_num 0
}
#
# Chiusura rele
#
# $1 identificativo relè da chiudere
#
function drv_spb16ch_rele_close {
local rele_id=`echo $1 | $CUT -d':' -f3,3`
local rele_data=${SPB16CH_RELE_MAP[$rele_id]}
if [[ -z $rele_data ]]; then
local message="Error - Rele map not defined - rele_id=$rele_id - ($1)"
log_write "$message"
message_write "warning" "$message"
fi
local channel_num=${rele_data:0:2}
local rele_num=${rele_data:2:2}
echo channel_num=$channel_num
echo rele_num=$rele_num
$DIR_SCRIPT/drv/spb16ch/scripts/mux_channel.py 72 $channel_num
$DIR_SCRIPT/drv/spb16ch/scripts/gpo_active.py 72 $rele_num 1
}

37
drv/spb16ch/scripts/ee_read.py Executable file
View File

@@ -0,0 +1,37 @@
#!/usr/bin/python
# coding=utf-8
# Read the eeprom 24C16
# I2C Address: 0x50 (24C16)
# sudo ./ee_read.py 0x50 address
# Example: sudo ./ee_read.py 50 0
import time
import argparse
import RPi.GPIO as GPIO
import smbus
def I2C_setup(i2c_address, eeprom_address):
I2C_address = 0x50
if GPIO.RPI_REVISION in [2, 3]:
I2C_bus_number = 1
else:
I2C_bus_number = 0
bus = smbus.SMBus(I2C_bus_number)
#bus.read_byte_data(I2C_address, eeprom_address)
print("24C16 ADDRESS: {}".format(bin(eeprom_address)))
print("24C16 DATA: {}".format(bin(bus.read_byte_data(I2C_address, eeprom_address))))
print("24C16 DATA: {}".format(hex(bus.read_byte_data(I2C_address, eeprom_address))))
def menu():
parser = argparse.ArgumentParser(description='Select address to read data on eeprom 24C16 ')
parser.add_argument('i2c_address', type=int)
parser.add_argument('eeprom_address', type=int)
args = parser.parse_args()
I2C_setup(args.i2c_address, args.eeprom_address)
if __name__ == "__main__":
menu()

35
drv/spb16ch/scripts/ee_write.py Executable file
View File

@@ -0,0 +1,35 @@
#!/usr/bin/python
# coding=utf-8
# Write the eeprom 24C16
# I2C Address: 0x50 (24C16)
# sudo ./ee_write.py 0x50 address data
# Example: sudo ./ee_write.py 50 0 1
import time
import argparse
import RPi.GPIO as GPIO
import smbus
def I2C_setup(i2c_address, eeprom_address, eeprom_data):
I2C_address = 0x50
if GPIO.RPI_REVISION in [2, 3]:
I2C_bus_number = 1
else:
I2C_bus_number = 0
bus = smbus.SMBus(I2C_bus_number)
bus.write_byte_data(I2C_address, eeprom_address, eeprom_data)
def menu():
parser = argparse.ArgumentParser(description='Select address and data to write on eeprom 24C16 ')
parser.add_argument('i2c_address', type=int)
parser.add_argument('eeprom_address', type=int)
parser.add_argument('eeprom_data', type=int)
args = parser.parse_args()
I2C_setup(args.i2c_address, args.eeprom_address, args.eeprom_data)
if __name__ == "__main__":
menu()

View File

@@ -0,0 +1,44 @@
#!/usr/bin/python
# coding=utf-8
# Select address and channel of PCA9571 I2C general purpose outputs
# I2C Address: 0x25 Fixed
# sudo ./gpo_active.py CHANNEL
# Example: sudo ./gpo_active.py 25 255 1 #all relays activates
import time
import argparse
import RPi.GPIO as GPIO
import smbus
def I2C_setup(multiplexer_i2c_address, i2c_channel_setup, state):
I2C_address = 0x25
if GPIO.RPI_REVISION in [2, 3]:
I2C_bus_number = 1
else:
I2C_bus_number = 0
bus = smbus.SMBus(I2C_bus_number)
status_outputs=bus.read_byte(I2C_address)
if state == 1:
i2c_channel_setup=status_outputs|i2c_channel_setup
elif state == 0:
i2c_channel_setup=(-i2c_channel_setup-1)&status_outputs
elif state == -1:
i2c_channel_setup=0
bus.write_byte(I2C_address, i2c_channel_setup)
#time.sleep(0)
file = open("rl1", "r")
address=int(file.readline())
channel_outputs=int(file.readline())
state=int(file.readline())
def menu():
I2C_setup(address, channel_outputs, state)
if __name__ == "__main__":
menu()

44
drv/spb16ch/scripts/gpo_init.py Executable file
View File

@@ -0,0 +1,44 @@
#!/usr/bin/python
# coding=utf-8
# Select address and channel of PCA9571 I2C general purpose outputs
# I2C Address: 0x25 Fixed
# sudo ./gpo_active.py CHANNEL
# Example: sudo ./gpo_active.py 25 255 1 #all relays activates
import time
import argparse
import RPi.GPIO as GPIO
import smbus
def I2C_setup(multiplexer_i2c_address, i2c_channel_setup, state):
I2C_address = 0x25
if GPIO.RPI_REVISION in [2, 3]:
I2C_bus_number = 1
else:
I2C_bus_number = 0
bus = smbus.SMBus(I2C_bus_number)
status_outputs=bus.read_byte(I2C_address)
if state == 1:
i2c_channel_setup=status_outputs|i2c_channel_setup
elif state == 0:
i2c_channel_setup=(-i2c_channel_setup-1)&status_outputs
elif state == -1:
i2c_channel_setup=0
bus.write_byte(I2C_address, i2c_channel_setup)
#time.sleep(0)
def menu():
parser = argparse.ArgumentParser(description='Select channel outputs of PCA9571')
parser.add_argument('address', type=int)
parser.add_argument('channel_outputs', type=int)
parser.add_argument('state', type=int)
args = parser.parse_args()
I2C_setup(args.address, args.channel_outputs, args.state)
if __name__ == "__main__":
menu()

33
drv/spb16ch/scripts/gpo_read.py Executable file
View File

@@ -0,0 +1,33 @@
#!/usr/bin/python
# coding=utf-8
# Select address and channel of PCA9571 I2C general purpose outputs
# I2C Address: 0x25 Fixed
# sudo ./gpo_read.py CHANNEL
# Example: sudo ./gpo_read.py 25
import time
import argparse
import RPi.GPIO as GPIO
import smbus
def I2C_read(multiplexer_i2c_address):
I2C_address = 0x25
if GPIO.RPI_REVISION in [2, 3]:
I2C_bus_number = 1
else:
I2C_bus_number = 0
bus = smbus.SMBus(I2C_bus_number)
status_outputs=bus.read_byte(I2C_address)
time.sleep(0)
# print("PCA9571 sts:{}".format(bin(bus.read_byte(I2C_address))))
print("PCA9571 GPO sts:{}".format(hex(bus.read_byte(I2C_address))))
def menu():
I2C_read(0x25)
if __name__ == "__main__":
menu()

3
drv/spb16ch/scripts/gpo_send Executable file
View File

@@ -0,0 +1,3 @@
25
100
1

1
drv/spb16ch/scripts/gpo_states Executable file
View File

@@ -0,0 +1 @@
PCA9571 GPO sts:0x64

5
drv/spb16ch/scripts/init.sh Executable file
View File

@@ -0,0 +1,5 @@
./mux_channel.py 72 0
./gpo_init.py 25 255 0
./mux_channel.py 72 1
./gpo_init.py 25 255 0
./mux_channel.py 72 0

View File

@@ -0,0 +1,38 @@
#!/usr/bin/python
# coding=utf-8
# Select address and channel of PCA9547 I2C multiplexer
# I2C Address: 0xYY, where YY can be 70 through 77
# Multiplexer Channel: 1 - 8
# sudo ./mux_channel.py ADDRESS CHANNEL
# Example: sudo ./mux_channel.py 70 1
import time
import argparse
import RPi.GPIO as GPIO
import smbus
def I2C_setup(multiplexer_i2c_address, i2c_channel_setup):
I2C_address = 0x70 + multiplexer_i2c_address % 10
if GPIO.RPI_REVISION in [2, 3]:
I2C_bus_number = 1
else:
I2C_bus_number = 0
bus = smbus.SMBus(I2C_bus_number)
i2c_channel_setup=i2c_channel_setup + 0x08
bus.write_byte(I2C_address, i2c_channel_setup)
#time.sleep(0.1)
def menu():
parser = argparse.ArgumentParser(description='Select channel of PCA9547 I2C multiplexer')
parser.add_argument('address', type=int)
parser.add_argument('channel', type=int)
args = parser.parse_args()
I2C_setup(args.address, args.channel)
if __name__ == "__main__":
menu()

37
drv/spb16ch/scripts/mux_read.py Executable file
View File

@@ -0,0 +1,37 @@
#!/usr/bin/python
# coding=utf-8
# Select address and channel of PCA9547 I2C multiplexer
# I2C Address: 0xYY, where YY can be 70 through 77
# Multiplexer Channel: 1 - 8
# sudo ./mux_read.py ADDRESS
# Example: sudo ./mux_read.py 70
import time
import argparse
import RPi.GPIO as GPIO
import smbus
def I2C_setup(multiplexer_i2c_address):
I2C_address = 0x70 + multiplexer_i2c_address % 10
if GPIO.RPI_REVISION in [2, 3]:
I2C_bus_number = 1
else:
I2C_bus_number = 0
bus = smbus.SMBus(I2C_bus_number)
time.sleep(0.1)
# print("PCA9547 MUX sts:{}".format(bin(bus.read_byte(I2C_address))))
print("PCA9547 MUX sts:{}".format(hex(bus.read_byte(I2C_address))))
def menu():
parser = argparse.ArgumentParser(description='Select channel of PCA9547 I2C multiplexer')
parser.add_argument('address', type=int)
args = parser.parse_args()
I2C_setup(args.address)
if __name__ == "__main__":
menu()

1
drv/spb16ch/scripts/mux_states Executable file
View File

@@ -0,0 +1 @@
PCA9547 MUX sts:0x8

View File

@@ -0,0 +1,6 @@
#!/usr/bin/python
# coding=utf-8
file = open("mux", "r")
line1=file.readline()
print line1

33
drv/spb16ch/scripts/rele1_16 Executable file
View File

@@ -0,0 +1,33 @@
25
1
0
2
0
4
0
8
0
16
1
32
1
64
1
128
1
1
0
2
0
4
0
8
0
16
1
32
1
64
1
128
1

View File

@@ -0,0 +1,95 @@
#!/usr/bin/python
# coding=utf-8
# Select address and channel of PCA9571 I2C general purpose outputs
# I2C Address: 0x25 Fixed
# sudo ./gpo_active.py CHANNEL
# Example: sudo ./gpo_active.py 25 255 1 #all relays activates
import time
import argparse
import subprocess
import RPi.GPIO as GPIO
import smbus
def I2C_setup(multiplexer_i2c_address, i2c_channel_setup, state):
I2C_address = 0x25
if GPIO.RPI_REVISION in [2, 3]:
I2C_bus_number = 1
else:
I2C_bus_number = 0
bus = smbus.SMBus(I2C_bus_number)
status_outputs=bus.read_byte(I2C_address)
if state == 1:
i2c_channel_setup=status_outputs|i2c_channel_setup
elif state == 0:
i2c_channel_setup=(-i2c_channel_setup-1)&status_outputs
elif state == -1:
i2c_channel_setup=0
bus.write_byte(I2C_address, i2c_channel_setup)
#time.sleep(0)
def menu():
while 1==1:
subprocess.call('./mux_channel.py 72 0', shell=True)
#time.sleep(0.1)
file = open("rele1_16", "r")
address=int(file.readline())
channel_outputs1=int(file.readline())
state1=int(file.readline())
channel_outputs2=int(file.readline())
state2=int(file.readline())
channel_outputs3=int(file.readline())
state3=int(file.readline())
channel_outputs4=int(file.readline())
state4=int(file.readline())
channel_outputs5=int(file.readline())
state5=int(file.readline())
channel_outputs6=int(file.readline())
state6=int(file.readline())
channel_outputs7=int(file.readline())
state7=int(file.readline())
channel_outputs8=int(file.readline())
state8=int(file.readline())
channel_outputs9=int(file.readline())
state9=int(file.readline())
channel_outputs10=int(file.readline())
state10=int(file.readline())
channel_outputs11=int(file.readline())
state11=int(file.readline())
channel_outputs12=int(file.readline())
state12=int(file.readline())
channel_outputs13=int(file.readline())
state13=int(file.readline())
channel_outputs14=int(file.readline())
state14=int(file.readline())
channel_outputs15=int(file.readline())
state15=int(file.readline())
channel_outputs16=int(file.readline())
state16=int(file.readline())
I2C_setup(address, channel_outputs1, state1)
I2C_setup(address, channel_outputs2, state2)
I2C_setup(address, channel_outputs3, state3)
I2C_setup(address, channel_outputs4, state4)
I2C_setup(address, channel_outputs5, state5)
I2C_setup(address, channel_outputs6, state6)
I2C_setup(address, channel_outputs7, state7)
I2C_setup(address, channel_outputs8, state8)
subprocess.call('./mux_channel.py 72 1', shell=True)
#time.sleep(0.1)
I2C_setup(address, channel_outputs9, state9)
I2C_setup(address, channel_outputs10, state10)
I2C_setup(address, channel_outputs11, state11)
I2C_setup(address, channel_outputs12, state12)
I2C_setup(address, channel_outputs13, state13)
I2C_setup(address, channel_outputs14, state14)
I2C_setup(address, channel_outputs15, state15)
I2C_setup(address, channel_outputs16, state16)
subprocess.call('./mux_channel.py 72 0', shell=True)
if __name__ == "__main__":
menu()

View File

@@ -0,0 +1,34 @@
#
# Inizializza i rele che gestiscono l'alimentazione per le valvole bistabili
#
# $1 identificativo relè
#
function drv_spb16ch_supply_bistable_init {
drv_spb16ch_supply_negative "$1"
}
#
# Imposta l'alimentazione delle elettrovalvole con voltaggio positivo
#
# $1 identificativo relè
#
function drv_spb16ch_supply_positive {
drv_spb16ch_rele_open "$1"
}
#
# Imposta l'alimentazione delle elettrovalvole con voltaggio negativo
#
# $1 identificativo relè
#
function drv_spb16ch_supply_negative {
drv_spb16ch_rele_close "$1"
}