mirror of
https://github.com/xtacocorex/CHIP_IO
synced 2025-07-20 12:53:22 +00:00
Compare commits
16 Commits
Author | SHA1 | Date | |
---|---|---|---|
f7bec3f891 | |||
48d9343dba | |||
bab94f7600 | |||
dab60b334d | |||
c3ead5d395 | |||
121ad398e4 | |||
c21c431b7b | |||
f66740b797 | |||
ca7d221b33 | |||
e179e52b5e | |||
fa3108544b | |||
c5dbede3c4 | |||
6bd2e61450 | |||
cd85e2b5eb | |||
6beacbb382 | |||
a498fc12bd |
@ -1,3 +1,28 @@
|
||||
0.5.2
|
||||
---
|
||||
* Updating Utilties to determine CHIP Pro better
|
||||
* Updating the README to fix things
|
||||
|
||||
0.5.0
|
||||
---
|
||||
* CHIP Pro Support
|
||||
* README Updates
|
||||
|
||||
0.4.0
|
||||
---
|
||||
* Software Servo code added
|
||||
- Only works on the LCD and CSI pins
|
||||
* Fixed cleanup() for the SOFTPWM and SERVO
|
||||
- The per pin cleanup for SOFTPWM doesn't work as stop() clears up the memory for the pin used
|
||||
- SERVO code was based on SOFTPWM, so it inherited this issue
|
||||
|
||||
0.3.5
|
||||
---
|
||||
* Merged in brettcvz's code to read a byte of data from the GPIO
|
||||
- Cleaned the code up and expanded it (in the low level C code) to read up to 32 bits of data
|
||||
- Presented 8 bit and 16 bits of data functions to the Python interface with brettcvz's read_byte() and my read_word()
|
||||
* I think I finally fixed the GPIO.cleanup() code one and for all
|
||||
|
||||
0.3.4.1
|
||||
---
|
||||
* Quick fix as I borked XIO setup as inputs with the latest change that enabled PUD
|
||||
|
@ -46,8 +46,10 @@ def toggle_debug():
|
||||
global DEBUG
|
||||
if DEBUG:
|
||||
DEBUG = False
|
||||
print("debug disabled")
|
||||
else:
|
||||
DEBUG = True
|
||||
print("debug enabled")
|
||||
|
||||
def setup(rate=250):
|
||||
# First we determine if the device exists
|
||||
|
@ -20,11 +20,11 @@
|
||||
import os
|
||||
import shutil
|
||||
import time
|
||||
import Utilities as UT
|
||||
|
||||
DEBUG = False
|
||||
|
||||
OVERLAYINSTALLPATH = "/lib/firmware/chip_io"
|
||||
SPIINSTALLPATH = "/lib/firmware/nextthingco/chip"
|
||||
OVERLAYINSTALLPATH = "/lib/firmware/nextthingco/chip"
|
||||
OVERLAYCONFIGPATH = "/sys/kernel/config/device-tree/overlays"
|
||||
CUSTOMOVERLAYFILEPATH = ""
|
||||
|
||||
@ -43,7 +43,7 @@ _LOADED = {
|
||||
|
||||
_OVERLAYS = {
|
||||
"SPI2" : "sample-spi.dtbo",
|
||||
"PWM0" : "chip-pwm0.dtbo",
|
||||
"PWM0" : "sample-pwm.dtbo",
|
||||
"CUST" : ""
|
||||
}
|
||||
|
||||
@ -57,8 +57,10 @@ def toggle_debug():
|
||||
global DEBUG
|
||||
if DEBUG:
|
||||
DEBUG = False
|
||||
print("debug disabled")
|
||||
else:
|
||||
DEBUG = True
|
||||
print("debug enabled")
|
||||
|
||||
def get_spi_loaded():
|
||||
"""
|
||||
@ -159,12 +161,14 @@ def load(overlay, path=""):
|
||||
print("Custom Overlay path does not exist")
|
||||
return 1
|
||||
|
||||
# DETERMINE IF WE ARE A CHIP PRO AND WE ARE COMMANDED TO LOAD PWM0
|
||||
if UT.is_chip_pro() and overlay.upper() == "PWM0":
|
||||
print("CHIP Pro supports PWM0 in base DTB, exiting")
|
||||
return 1
|
||||
|
||||
# SET UP THE OVERLAY PATH FOR OUR USE
|
||||
if overlay.upper() != "CUST":
|
||||
opath = OVERLAYINSTALLPATH
|
||||
# IF THE OVERLAY IS SPI, USE THE NTC PATH
|
||||
if overlay.upper() == "SPI2":
|
||||
opath = SPIINSTALLPATH
|
||||
opath += "/" + _OVERLAYS[overlay.upper()]
|
||||
else:
|
||||
opath = path
|
||||
@ -194,6 +198,12 @@ def unload(overlay):
|
||||
global _LOADED
|
||||
if DEBUG:
|
||||
print("UNLOAD OVERLAY: {0}".format(overlay))
|
||||
|
||||
# DETERMINE IF WE ARE A CHIP PRO AND WE ARE COMMANDED TO UNLOAD PWM0
|
||||
if UT.is_chip_pro() and overlay.upper() == "PWM0":
|
||||
print("CHIP Pro supports PWM0 in base DTB, exiting")
|
||||
return
|
||||
|
||||
# SEE IF OUR OVERLAY NAME IS IN THE KEYS
|
||||
if overlay.upper() in _OVERLAYS.keys():
|
||||
# BRUTE FORCE REMOVE AS THE DIRECTORY CONTAINS FILES
|
||||
|
@ -35,59 +35,74 @@ def toggle_debug():
|
||||
global DEBUG
|
||||
if DEBUG:
|
||||
DEBUG = False
|
||||
print("debug disabled")
|
||||
else:
|
||||
DEBUG = True
|
||||
print("debug enabled")
|
||||
|
||||
# Set the 1.8V-pin on the CHIP U13-header to given voltage
|
||||
# Return False on error
|
||||
def set_1v8_pin_voltage(voltage):
|
||||
if not isinstance(voltage, int) and not isinstance(voltage, float):
|
||||
return False
|
||||
if voltage < 1.8 or voltage > 3.3:
|
||||
return False
|
||||
if DEBUG:
|
||||
print("Setting 1.8V Pin voltage: {0}".format(voltage))
|
||||
voltage=int(round((voltage - 1.8) / 0.1)) << 4
|
||||
if subprocess.call(["/usr/sbin/i2cset", "-f", "-y" ,"0", "0x34", "0x90", "0x03"]):
|
||||
if not is_chip_pro():
|
||||
if not isinstance(voltage, int) and not isinstance(voltage, float):
|
||||
return False
|
||||
if voltage < 1.8 or voltage > 3.3:
|
||||
return False
|
||||
if DEBUG:
|
||||
print("Pin enable command failed")
|
||||
return False
|
||||
if subprocess.call(["/usr/sbin/i2cset", "-f", "-y", "0", "0x34", "0x91", str(voltage)]):
|
||||
if DEBUG:
|
||||
print("Pin set voltage command failed")
|
||||
return False
|
||||
return True
|
||||
print("Setting 1.8V Pin voltage: {0}".format(voltage))
|
||||
voltage=int(round((voltage - 1.8) / 0.1)) << 4
|
||||
if subprocess.call(["/usr/sbin/i2cset", "-f", "-y" ,"0", "0x34", "0x90", "0x03"]):
|
||||
if DEBUG:
|
||||
print("Pin enable command failed")
|
||||
return False
|
||||
if subprocess.call(["/usr/sbin/i2cset", "-f", "-y", "0", "0x34", "0x91", str(voltage)]):
|
||||
if DEBUG:
|
||||
print("Pin set voltage command failed")
|
||||
return False
|
||||
return True
|
||||
else:
|
||||
print("Set 1.8V Pin Voltage not supported on the CHIP Pro")
|
||||
|
||||
# Get the voltage the 1.8V-pin on the CHIP U13-header has been configured as
|
||||
# Return False on error
|
||||
def get_1v8_pin_voltage():
|
||||
p=subprocess.Popen(["/usr/sbin/i2cget", "-f", "-y", "0", "0x34", "0x90"], stdout=subprocess.PIPE)
|
||||
output=p.communicate()[0].decode("utf-8").strip()
|
||||
#Not configured as an output
|
||||
if output != "0x03":
|
||||
if not is_chip_pro():
|
||||
p=subprocess.Popen(["/usr/sbin/i2cget", "-f", "-y", "0", "0x34", "0x90"], stdout=subprocess.PIPE)
|
||||
output=p.communicate()[0].decode("utf-8").strip()
|
||||
#Not configured as an output
|
||||
if output != "0x03":
|
||||
if DEBUG:
|
||||
print("1.8V Pin is currently disabled")
|
||||
return False
|
||||
p=subprocess.Popen(["/usr/sbin/i2cget", "-f", "-y", "0", "0x34", "0x91"], stdout=subprocess.PIPE)
|
||||
output=p.communicate()[0].decode("utf-8").strip()
|
||||
voltage=round((int(output, 16) >> 4) * 0.1 + 1.8, 1)
|
||||
if DEBUG:
|
||||
print("1.8V Pin is currently disabled")
|
||||
return False
|
||||
p=subprocess.Popen(["/usr/sbin/i2cget", "-f", "-y", "0", "0x34", "0x91"], stdout=subprocess.PIPE)
|
||||
output=p.communicate()[0].decode("utf-8").strip()
|
||||
voltage=round((int(output, 16) >> 4) * 0.1 + 1.8, 1)
|
||||
if DEBUG:
|
||||
print("Current 1.8V Pin voltage: {0}".format(voltage))
|
||||
return voltage
|
||||
print("Current 1.8V Pin voltage: {0}".format(voltage))
|
||||
return voltage
|
||||
else:
|
||||
print("Get 1.8V Pin Voltage not supported on the CHIP Pro")
|
||||
|
||||
# Enable 1.8V Pin on CHIP U13 Header
|
||||
def enable_1v8_pin():
|
||||
set_1v8_pin_voltage(1.8)
|
||||
if not is_chip_pro():
|
||||
set_1v8_pin_voltage(1.8)
|
||||
else:
|
||||
print("Enable 1.8V Pin not supported on the CHIP Pro")
|
||||
|
||||
# Disable 1.8V Pin on CHIP U13 Header
|
||||
def disable_1v8_pin():
|
||||
if DEBUG:
|
||||
print("Disabling the 1.8V Pin")
|
||||
# CANNOT USE I2C LIB AS WE NEED TO FORCE THE COMMAND DUE TO THE KERNEL OWNING THE DEVICE
|
||||
# First we have to write 0x05 to AXP-209 Register 0x91
|
||||
subprocess.call('/usr/sbin/i2cset -f -y 0 0x34 0x91 0x05', shell=True)
|
||||
# Then we have to write 0x07 to AXP-209 Register 0x90
|
||||
subprocess.call('/usr/sbin/i2cset -f -y 0 0x34 0x90 0x07', shell=True)
|
||||
|
||||
if not is_chip_pro():
|
||||
if DEBUG:
|
||||
print("Disabling the 1.8V Pin")
|
||||
# CANNOT USE I2C LIB AS WE NEED TO FORCE THE COMMAND DUE TO THE KERNEL OWNING THE DEVICE
|
||||
# First we have to write 0x05 to AXP-209 Register 0x91
|
||||
subprocess.call('/usr/sbin/i2cset -f -y 0 0x34 0x91 0x05', shell=True)
|
||||
# Then we have to write 0x07 to AXP-209 Register 0x90
|
||||
subprocess.call('/usr/sbin/i2cset -f -y 0 0x34 0x90 0x07', shell=True)
|
||||
else:
|
||||
print("Disable 1.8V Pin not supported on the CHIP Pro")
|
||||
|
||||
# Unexport All
|
||||
def unexport_all():
|
||||
@ -101,3 +116,29 @@ def unexport_all():
|
||||
cmd = "echo " + num + " > /sys/class/gpio/unexport"
|
||||
subprocess.Popen(cmd,shell=True, stdout=subprocess.PIPE)
|
||||
|
||||
# Determine Processor
|
||||
def is_chip_pro():
|
||||
isgr8 = False
|
||||
if DEBUG:
|
||||
print("Determining if computer is CHIP or CHIP Pro")
|
||||
|
||||
# GET FIRST LINE FROM /proc/meminfo
|
||||
f = open("/proc/meminfo","r")
|
||||
fline = f.readline()
|
||||
f.close()
|
||||
|
||||
# FIGURE OUT OUR TOTAL MEMORY SIZE
|
||||
parts = fline.split()
|
||||
mem = float(parts[1]) / 1024
|
||||
|
||||
if mem > 380:
|
||||
isgr8 = False
|
||||
if DEBUG:
|
||||
print("found CHIP!")
|
||||
else:
|
||||
isgr8 = True
|
||||
if DEBUG:
|
||||
print("found CHIP Pro!")
|
||||
|
||||
# Return isgr8
|
||||
return isgr8
|
||||
|
@ -2,4 +2,3 @@ include distribute_setup.py
|
||||
include README.rst
|
||||
include CHANGELOG.rst
|
||||
recursive-include source *.h
|
||||
recursive-include overlays *.dts *.py
|
||||
|
8
Makefile
8
Makefile
@ -8,7 +8,10 @@ clean:
|
||||
rm -rf CHIP_IO.* build dist
|
||||
rm -f *.pyo *.pyc
|
||||
rm -f *.egg
|
||||
rm -f overlays/*.pyo overlays/*.pyc
|
||||
rm -rf __pycache__
|
||||
rm -rf debian/python-chip-io*
|
||||
rm -rf debian/python3-chip-io*
|
||||
|
||||
tests:
|
||||
py.test
|
||||
|
||||
@ -17,3 +20,6 @@ build:
|
||||
|
||||
install: build
|
||||
python setup.py install --force
|
||||
|
||||
debfile:
|
||||
dpkg-buildpackage -rfakeroot -uc -b
|
||||
|
303
README.rst
303
README.rst
@ -2,19 +2,12 @@ CHIP_IO
|
||||
============================
|
||||
A CHIP GPIO library
|
||||
|
||||
NOTE: Now requires the custom DTC to install the library
|
||||
|
||||
Manual::
|
||||
Manual Installation::
|
||||
|
||||
For Python2.7::
|
||||
|
||||
sudo apt-get update
|
||||
sudo apt-get install git build-essential python-dev python-pip flex bison chip-dt-overlays -y
|
||||
git clone https://github.com/atenart/dtc.git
|
||||
cd dtc
|
||||
make
|
||||
sudo make install PREFIX=/usr
|
||||
cd ..
|
||||
git clone git://github.com/xtacocorex/CHIP_IO.git
|
||||
cd CHIP_IO
|
||||
sudo python setup.py install
|
||||
@ -24,16 +17,25 @@ For Python3::
|
||||
|
||||
sudo apt-get update
|
||||
sudo apt-get install git build-essential python3-dev python3-pip flex bison chip-dt-overlays -y
|
||||
git clone https://github.com/atenart/dtc.git
|
||||
cd dtc
|
||||
make
|
||||
sudo make install PREFIX=/usr
|
||||
cd ..
|
||||
git clone git://github.com/xtacocorex/CHIP_IO.git
|
||||
cd CHIP_IO
|
||||
sudo python3 setup.py install
|
||||
cd ..
|
||||
|
||||
PyPi Installation::
|
||||
|
||||
For Python2.7::
|
||||
|
||||
sudo apt-get update
|
||||
sudo apt-get install git build-essential python-dev python-pip flex bison chip-dt-overlays -y
|
||||
sudo pip install CHIP-IO
|
||||
|
||||
For Python3::
|
||||
|
||||
sudo apt-get update
|
||||
sudo apt-get install git build-essential python3-dev python3-pip flex bison chip-dt-overlays -y
|
||||
sudo pip3 install CHIP-IO
|
||||
|
||||
**Usage**
|
||||
|
||||
Using the library is very similar to the excellent RPi.GPIO library used on the Raspberry Pi. Below are some examples.
|
||||
@ -44,113 +46,115 @@ All scripts that require GPIO, PWM (HW and/or SW), and Overlay Manager need to b
|
||||
|
||||
The following "table" is the allowable pin names that are able to be used by the library. The Name column is the normal name used on the CHIP Headers, the Alt Name column is the value used by the PocketCHIP header (if it's broken out), and the Key is the Header and Pin Number the the Pin is physically located. Either of these 3 means is able to specify a pin in CHIP_IO.
|
||||
|
||||
+------------------+--------------------------+--------+
|
||||
| CHIP (Name) | PocketCHIP (Alt Name) | Key |
|
||||
+------------------+--------------------------+--------+
|
||||
| TWI1-SDA | KPD-I2C-SDA | U13_9 |
|
||||
+------------------+--------------------------+--------+
|
||||
| TWI1-SCK | KPD-I2C-SCL | U13_11 |
|
||||
+------------------+--------------------------+--------+
|
||||
| LCD-D2 | LCD-D2 | U13_17 |
|
||||
+------------------+--------------------------+--------+
|
||||
| PWM0 | PWM0 | U13_18 |
|
||||
+------------------+--------------------------+--------+
|
||||
| LCD-D4 | LCD-D4 | U13_19 |
|
||||
+------------------+--------------------------+--------+
|
||||
| LCD-D3 | LCD-D3 | U13_20 |
|
||||
+------------------+--------------------------+--------+
|
||||
| LCD-D6 | LCD-D6 | U13_21 |
|
||||
+------------------+--------------------------+--------+
|
||||
| LCD-D5 | LCD-D5 | U13_22 |
|
||||
+------------------+--------------------------+--------+
|
||||
| LCD-D10 | LCD-D10 | U13_23 |
|
||||
+------------------+--------------------------+--------+
|
||||
| LCD-D7 | LCD-D7 | U13_24 |
|
||||
+------------------+--------------------------+--------+
|
||||
| LCD-D12 | LCD-D12 | U13_25 |
|
||||
+------------------+--------------------------+--------+
|
||||
| LCD-D11 | LCD-D11 | U13_26 |
|
||||
+------------------+--------------------------+--------+
|
||||
| LCD-D14 | LCD-D14 | U13_27 |
|
||||
+------------------+--------------------------+--------+
|
||||
| LCD-D13 | LCD-D13 | U13_28 |
|
||||
+------------------+--------------------------+--------+
|
||||
| LCD-D18 | LCD-D18 | U13_29 |
|
||||
+------------------+--------------------------+--------+
|
||||
| LCD-D15 | LCD-D15 | U13_30 |
|
||||
+------------------+--------------------------+--------+
|
||||
| LCD-D20 | LCD-D20 | U13_31 |
|
||||
+------------------+--------------------------+--------+
|
||||
| LCD-D19 | LCD-D19 | U13_32 |
|
||||
+------------------+--------------------------+--------+
|
||||
| LCD-D22 | LCD-D22 | U13_33 |
|
||||
+------------------+--------------------------+--------+
|
||||
| LCD-D21 | LCD-D21 | U13_34 |
|
||||
+------------------+--------------------------+--------+
|
||||
| LCD-CLK | LCD-CLK | U13_35 |
|
||||
+------------------+--------------------------+--------+
|
||||
| LCD-D23 | LCD-D23 | U13_36 |
|
||||
+------------------+--------------------------+--------+
|
||||
| LCD-VSYNC | LCD-VSYNC | U13_37 |
|
||||
+------------------+--------------------------+--------+
|
||||
| LCD-HSYNC | LCD-HSYNC | U13_38 |
|
||||
+------------------+--------------------------+--------+
|
||||
| LCD-DE | LCD-DE | U13_40 |
|
||||
+------------------+--------------------------+--------+
|
||||
| UART1-TX | UART-TX | U14_3 |
|
||||
+------------------+--------------------------+--------+
|
||||
| UART1-RX | UART-RX | U14_5 |
|
||||
+------------------+--------------------------+--------+
|
||||
| LRADC | ADC | U14_11 |
|
||||
+------------------+--------------------------+--------+
|
||||
| XIO-P0 | XIO-P0 | U14_13 |
|
||||
+------------------+--------------------------+--------+
|
||||
| XIO-P1 | XIO-P1 | U14_14 |
|
||||
+------------------+--------------------------+--------+
|
||||
| XIO-P2 | GPIO1 | U14_15 |
|
||||
+------------------+--------------------------+--------+
|
||||
| XIO-P3 | GPIO2 | U14_16 |
|
||||
+------------------+--------------------------+--------+
|
||||
| XIO-P4 | GPIO3 | U14_17 |
|
||||
+------------------+--------------------------+--------+
|
||||
| XIO-P5 | GPIO4 | U14_18 |
|
||||
+------------------+--------------------------+--------+
|
||||
| XIO-P6 | GPIO5 | U14_19 |
|
||||
+------------------+--------------------------+--------+
|
||||
| XIO-P7 | GPIO6 | U14_20 |
|
||||
+------------------+--------------------------+--------+
|
||||
| AP-EINT1 | KPD-INT | U14_23 |
|
||||
+------------------+--------------------------+--------+
|
||||
| AP-EINT3 | AP-INT3 | U14_24 |
|
||||
+------------------+--------------------------+--------+
|
||||
| TWI2-SDA | I2C-SDA | U14_25 |
|
||||
+------------------+--------------------------+--------+
|
||||
| TWI2-SCK | I2C-SCL | U14_26 |
|
||||
+------------------+--------------------------+--------+
|
||||
| CSIPCK | SPI-SEL | U14_27 |
|
||||
+------------------+--------------------------+--------+
|
||||
| CSICK | SPI-CLK | U14_28 |
|
||||
+------------------+--------------------------+--------+
|
||||
| CSIHSYNC | SPI-MOSI | U14_29 |
|
||||
+------------------+--------------------------+--------+
|
||||
| CSIVSYNC | SPI-MISO | U14_30 |
|
||||
+------------------+--------------------------+--------+
|
||||
| CSID0 | CSID0 | U14_31 |
|
||||
+------------------+--------------------------+--------+
|
||||
| CSID1 | CSID1 | U14_32 |
|
||||
+------------------+--------------------------+--------+
|
||||
| CSID2 | CSID2 | U14_33 |
|
||||
+------------------+--------------------------+--------+
|
||||
| CSID3 | CSID3 | U14_34 |
|
||||
+------------------+--------------------------+--------+
|
||||
| CSID4 | CSID4 | U14_35 |
|
||||
+------------------+--------------------------+--------+
|
||||
| CSID5 | CSID5 | U14_36 |
|
||||
+------------------+--------------------------+--------+
|
||||
| CSID6 | CSID6 | U14_37 |
|
||||
+------------------+--------------------------+--------+
|
||||
| CSID7 | CSID7 | U14_38 |
|
||||
+------------------+--------------------------+--------+
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| CHIP (Name) | PocketCHIP/CHIP Pro Name | CHIP Key | HW Support |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| TWI1-SDA | KPD-I2C-SDA | U13_9 | CHIP/CHIP PRO |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| TWI1-SCK | KPD-I2C-SCL | U13_11 | CHIP/CHIP PRO |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| LCD-D2 | UART2-TX | U13_17 | CHIP/CHIP PRO |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| PWM0 | PWM0 | U13_18 | CHIP/CHIP PRO |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| PWM1 | PWM1 | EINT13 | CHIP PRO |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| LCD-D4 | UART2-CTS | U13_19 | CHIP/CHIP PRO |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| LCD-D3 | UART2-RX | U13_20 | CHIP/CHIP PRO |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| LCD-D6 | LCD-D6 | U13_21 | CHIP |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| LCD-D5 | UART2-RTS | U13_22 | CHIP/CHIP PRO |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| LCD-D10 | LCD-D10 | U13_23 | CHIP |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| LCD-D7 | LCD-D7 | U13_24 | CHIP |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| LCD-D12 | LCD-D12 | U13_25 | CHIP |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| LCD-D11 | LCD-D11 | U13_26 | CHIP |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| LCD-D14 | LCD-D14 | U13_27 | CHIP |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| LCD-D13 | LCD-D13 | U13_28 | CHIP |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| LCD-D18 | LCD-D18 | U13_29 | CHIP |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| LCD-D15 | LCD-D15 | U13_30 | CHIP |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| LCD-D20 | LCD-D20 | U13_31 | CHIP |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| LCD-D19 | LCD-D19 | U13_32 | CHIP |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| LCD-D22 | LCD-D22 | U13_33 | CHIP |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| LCD-D21 | LCD-D21 | U13_34 | CHIP |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| LCD-CLK | LCD-CLK | U13_35 | CHIP |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| LCD-D23 | LCD-D23 | U13_36 | CHIP |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| LCD-VSYNC | LCD-VSYNC | U13_37 | CHIP |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| LCD-HSYNC | LCD-HSYNC | U13_38 | CHIP |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| LCD-DE | LCD-DE | U13_40 | CHIP |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| UART1-TX | UART-TX | U14_3 | CHIP/CHIP PRO |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| UART1-RX | UART-RX | U14_5 | CHIP/CHIP PRO |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| LRADC | ADC | U14_11 | CHIP/CHIP PRO |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| XIO-P0 | XIO-P0 | U14_13 | CHIP |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| XIO-P1 | XIO-P1 | U14_14 | CHIP |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| XIO-P2 | GPIO1 | U14_15 | CHIP |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| XIO-P3 | GPIO2 | U14_16 | CHIP |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| XIO-P4 | GPIO3 | U14_17 | CHIP |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| XIO-P5 | GPIO4 | U14_18 | CHIP |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| XIO-P6 | GPIO5 | U14_19 | CHIP |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| XIO-P7 | GPIO6 | U14_20 | CHIP |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| AP-EINT1 | KPD-INT | U14_23 | CHIP/CHIP PRO |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| AP-EINT3 | AP-INT3 | U14_24 | CHIP/CHIP PRO |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| TWI2-SDA | I2C-SDA | U14_25 | CHIP/CHIP PRO |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| TWI2-SCK | I2C-SCL | U14_26 | CHIP/CHIP PRO |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| CSIPCK | SPI-SEL | U14_27 | CHIP/CHIP PRO |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| CSICK | SPI-CLK | U14_28 | CHIP/CHIP PRO |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| CSIHSYNC | SPI-MOSI | U14_29 | CHIP/CHIP PRO |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| CSIVSYNC | SPI-MISO | U14_30 | CHIP/CHIP PRO |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| CSID0 | D0 | U14_31 | CHIP/CHIP PRO |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| CSID1 | D1 | U14_32 | CHIP/CHIP PRO |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| CSID2 | D2 | U14_33 | CHIP/CHIP PRO |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| CSID3 | D3 | U14_34 | CHIP/CHIP PRO |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| CSID4 | D4 | U14_35 | CHIP/CHIP PRO |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| CSID5 | D5 | U14_36 | CHIP/CHIP PRO |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| CSID6 | D6 | U14_37 | CHIP/CHIP PRO |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
| CSID7 | D7 | U14_38 | CHIP/CHIP PRO |
|
||||
+------------------+--------------------------+-------------+-----------------+
|
||||
|
||||
**GPIO Setup**
|
||||
|
||||
@ -167,12 +171,19 @@ You can also refer to the bin based upon its alternate name::
|
||||
|
||||
GPIO.setup("GPIO1", GPIO.IN)
|
||||
|
||||
**GPIO Debug**
|
||||
**GPIO Miscellaneous**
|
||||
|
||||
Debug can be enabled/disabled by the following command::
|
||||
|
||||
# Enable Debug
|
||||
GPIO.toggle_debug()
|
||||
|
||||
You can determine if the hardware is a CHIP/CHIP Pro using the following::
|
||||
|
||||
# Determine hardware
|
||||
# 0 For CHIP
|
||||
# 1 For CHIP Pro
|
||||
GPIO.is_chip_pro()
|
||||
|
||||
**GPIO Output**
|
||||
|
||||
@ -205,6 +216,15 @@ Polling inputs::
|
||||
else:
|
||||
print("LOW")
|
||||
|
||||
Read lots of data::
|
||||
|
||||
# Get 8 bits of data in one shot
|
||||
mybyte = GPIO.read_byte("LCD-D3")
|
||||
# Get 16 bits of data in one shot
|
||||
myword = GPIO.read_word("XIO-P4")
|
||||
|
||||
This code was initially added by brettcvz and I cleaned it up and expanded it.
|
||||
|
||||
The edge detection code below only works for the AP-EINT1, AP-EINT3, and XPO Pins on the CHIP.
|
||||
|
||||
Waiting for an edge (GPIO.RISING, GPIO.FALLING, or GPIO.BOTH::
|
||||
@ -250,6 +270,10 @@ To clean up the GPIO when done, do the following::
|
||||
Hardware PWM requires a DTB Overlay loaded on the CHIP to allow the kernel to know there is a PWM device available to use.
|
||||
::
|
||||
import CHIP_IO.PWM as PWM
|
||||
# Determine hardware
|
||||
# 0 For CHIP
|
||||
# 1 For CHIP Pro
|
||||
PWM.is_chip_pro()
|
||||
# Enable/Disable Debug
|
||||
PWM.toggle_debug()
|
||||
#PWM.start(channel, duty, freq=2000, polarity=0)
|
||||
@ -266,6 +290,10 @@ Hardware PWM requires a DTB Overlay loaded on the CHIP to allow the kernel to kn
|
||||
**SOFTPWM**::
|
||||
|
||||
import CHIP_IO.SOFTPWM as SPWM
|
||||
# Determine hardware
|
||||
# 0 For CHIP
|
||||
# 1 For CHIP Pro
|
||||
SPWM.is_chip_pro()
|
||||
# Enable/Disable Debug
|
||||
SPWM.toggle_debug()
|
||||
#SPWM.start(channel, duty, freq=2000, polarity=0)
|
||||
@ -276,10 +304,8 @@ Hardware PWM requires a DTB Overlay loaded on the CHIP to allow the kernel to kn
|
||||
SPWM.set_frequency("XIO-P7", 10)
|
||||
# To Stop SPWM
|
||||
SPWM.stop("XIO-P7")
|
||||
# Cleanup can have no argument to clean up all SoftPWM outputs
|
||||
# Cleanup
|
||||
SPWM.cleanup()
|
||||
# Or you can specify a single SoftPWM output to cleanup (keeping the rest intact)
|
||||
SPWM.cleanup("XIO-P7")
|
||||
#For specific polarity: this example sets polarity to 1 on start:
|
||||
SPWM.start("XIO-P7", 50, 2000, 1)
|
||||
|
||||
@ -287,6 +313,28 @@ Use SOFTPWM at low speeds (hundreds of Hz) for the best results. Do not use for
|
||||
|
||||
If using SOFTPWM and PWM at the same time, import CHIP_IO.SOFTPWM as SPWM or something different than PWM as to not confuse the library.
|
||||
|
||||
**SERVO**::
|
||||
|
||||
import CHIP_IO.SERVO as SERVO
|
||||
# Determine hardware
|
||||
# 0 For CHIP
|
||||
# 1 For CHIP Pro
|
||||
SERVO.is_chip_pro()
|
||||
# Enable/Disable Debug
|
||||
SERVO.toggle_debug()
|
||||
#SPWM.start(channel, angle=0, range=180)
|
||||
#angle values are between +/- range/2)
|
||||
#you can choose any pin except the XIO's
|
||||
SERVO.start("CSID4", 50)
|
||||
SERVO.set_angle("CSID4", 25.5)
|
||||
SERVO.set_range("CSID4", 90)
|
||||
# To Stop Servo
|
||||
SERVO.stop("CSID4")
|
||||
# Cleanup
|
||||
SERVO.cleanup()
|
||||
|
||||
The Software Servo control only works on the LCD and CSI pins. The XIO is too slow to control.
|
||||
|
||||
**LRADC**::
|
||||
|
||||
The LRADC was enabled in the 4.4.13-ntc-mlc. This is a 6 bit ADC that is 2 Volt tolerant.
|
||||
@ -323,9 +371,9 @@ SPI requires a DTB Overlay to access. CHIP_IO does not contain any SPI specific
|
||||
**Overlay Manager**::
|
||||
|
||||
The Overlay Manager enables you to quickly load simple Device Tree Overlays. The options for loading are:
|
||||
PWM0, SPI2, I2C1, CUST
|
||||
PWM0, SPI2, CUST. The Overlay Manager is smart enough to determine if you are trying to load PWM on a CHIP Pro and will fail due to the base DTB for the CHIP Pro supporting PWM0/1 out of the box.
|
||||
|
||||
Only one of each type of overlay can be loaded at a time, but all three options can be loaded simultaneously. So you can have SPI2 and I2C1 without PWM0, but you cannot have SPI2 loaded twice.
|
||||
Only one of each type of overlay can be loaded at a time, but all three options can be loaded simultaneously. So you can have SPI2 without PWM0, but you cannot have SPI2 loaded twice.
|
||||
::
|
||||
import CHIP_IO.OverlayManager as OM
|
||||
# The toggle_debug() function turns on/off debug printing
|
||||
@ -374,6 +422,9 @@ To use the utilities, here is sample code::
|
||||
UT.get_1v8_pin_voltage()
|
||||
# Unexport Everything
|
||||
UT.unexport_all()
|
||||
# Determine if you are running a CHIP/CHIP Pro
|
||||
# This returns True if the computer is a CHIP Pro and False if it is a CHIP
|
||||
UT.is_chip_pro()
|
||||
|
||||
**Running tests**
|
||||
|
||||
|
21
debian/changelog
vendored
Normal file
21
debian/changelog
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
chip-io (0.5.2-1) unstable; urgency=low
|
||||
|
||||
* Updating Utilities to determine CHIP Pro better
|
||||
* Updating Utilities to only run CHIP appropriate code on the CHIP and not CHIP Pro
|
||||
* Updated README
|
||||
|
||||
-- Robert Wolterman <robert.wolterman@gmail.com> Sun, 26 Feb 2017 13:56:00 -0600
|
||||
|
||||
chip-io (0.5.0-1) unstable; urgency=low
|
||||
|
||||
* CHIP Pro support for PWM1, reduced GPIO capability
|
||||
* New is_chip_pro() in each of the PWM, GPIO, SoftPWM, and Servo modules
|
||||
* Updated README
|
||||
|
||||
-- Robert Wolterman <robert.wolterman@gmail.com> Sat, 25 Feb 2017 19:00:00 -0600
|
||||
|
||||
chip-io (0.4.0-1) unstable; urgency=low
|
||||
|
||||
* source package automatically created by stdeb 0.8.2
|
||||
|
||||
-- Robert Wolterman <robert.wolterman@gmail.com> Sat, 18 Feb 2017 23:58:48 +0000
|
1
debian/compat
vendored
Normal file
1
debian/compat
vendored
Normal file
@ -0,0 +1 @@
|
||||
9
|
23
debian/control
vendored
Normal file
23
debian/control
vendored
Normal file
@ -0,0 +1,23 @@
|
||||
Source: chip-io
|
||||
Maintainer: Robert Wolterman <robert.wolterman@gmail.com>
|
||||
Section: python
|
||||
Priority: optional
|
||||
Build-Depends: python-setuptools (>= 0.6b3), python-all-dev (>= 2.6.6-3), debhelper (>= 9), dh-python,
|
||||
python3-all (>=3.2), python3-setuptools
|
||||
Standards-Version: 3.9.1
|
||||
|
||||
Package: python-chip-io
|
||||
Architecture: any
|
||||
Depends: ${misc:Depends}, ${python:Depends}, ${shlibs:Depends}
|
||||
Description: A module to control CHIP IO channels
|
||||
CHIP_IO
|
||||
============================
|
||||
A CHIP GPIO library
|
||||
|
||||
Package: python3-chip-io
|
||||
Architecture: any
|
||||
Depends: ${misc:Depends}, ${python3:Depends}, ${shlibs:Depends}
|
||||
Description: A module to control CHIP IO channels
|
||||
CHIP_IO
|
||||
============================
|
||||
A CHIP GPIO library
|
2
debian/files
vendored
Normal file
2
debian/files
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
python-chip-io_0.4.0-1_armhf.deb python optional
|
||||
python3-chip-io_0.4.0-1_armhf.deb python optional
|
9
debian/rules
vendored
Executable file
9
debian/rules
vendored
Executable file
@ -0,0 +1,9 @@
|
||||
#!/usr/bin/make -f
|
||||
|
||||
# This file was automatically generated by stdeb 0.8.2 at
|
||||
# Sat, 18 Feb 2017 23:58:48 +0000
|
||||
export PYBUILD_NAME=chip-io
|
||||
|
||||
%:
|
||||
dh $@ --with python2,python3 --buildsystem=pybuild
|
||||
|
1
debian/source/format
vendored
Normal file
1
debian/source/format
vendored
Normal file
@ -0,0 +1 @@
|
||||
3.0 (quilt)
|
@ -1,22 +0,0 @@
|
||||
from subprocess import call
|
||||
import os
|
||||
import shutil
|
||||
import glob
|
||||
import sys
|
||||
|
||||
def compile():
|
||||
print("Compiling DTS Files")
|
||||
call(["dtc", "-O", "dtb", "-o", "overlays/chip-pwm0.dtbo", "-b", "o", "-@", "overlays/chip-pwm0.dts"])
|
||||
|
||||
def copy():
|
||||
target_dir = os.environ.get('TARGET_DIR', '')
|
||||
overlay_path = target_dir + "/lib/firmware/chip_io"
|
||||
print("Checking for DTBO Install Path")
|
||||
if not os.path.exists(overlay_path):
|
||||
print("Path not found, creating "+overlay_path)
|
||||
os.makedirs(overlay_path)
|
||||
print("Removing old DTBO files (if applicable)")
|
||||
for fl in glob.glob(overlay_path+"/chip-*-.dtbo"):
|
||||
os.remove(fl)
|
||||
print("Moving DTBO files to "+overlay_path)
|
||||
shutil.move("overlays/chip-pwm0.dtbo", overlay_path+"/chip-pwm0.dtbo")
|
@ -1,61 +0,0 @@
|
||||
/*
|
||||
* Copyright 2016 Free Electrons
|
||||
* Copyright 2016 NextThing Co
|
||||
*
|
||||
* Maxime Ripard <maxime.ripard@free-electrons.com>
|
||||
*
|
||||
* This file is dual-licensed: you can use it either under the terms
|
||||
* of the GPL or the X11 license, at your option. Note that this dual
|
||||
* licensing only applies to this file, and not this project as a
|
||||
* whole.
|
||||
*
|
||||
* a) This file is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as
|
||||
* published by the Free Software Foundation; either version 2 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This file 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.
|
||||
*
|
||||
* Or, alternatively,
|
||||
*
|
||||
* b) 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.
|
||||
*/
|
||||
/dts-v1/;
|
||||
/plugin/;
|
||||
|
||||
/ {
|
||||
compatible = "nextthing,chip", "allwinner,sun5i-r8";
|
||||
|
||||
/* Enable the PWM */
|
||||
fragment@0 {
|
||||
target = <&pwm>;
|
||||
|
||||
__overlay__ {
|
||||
pinctrl-names = "default";
|
||||
pinctrl-0 = <&pwm0_pins>;
|
||||
status = "okay";
|
||||
};
|
||||
};
|
||||
};
|
14
setup.py
14
setup.py
@ -1,10 +1,3 @@
|
||||
try:
|
||||
from overlays import builder
|
||||
builder.compile()
|
||||
builder.copy()
|
||||
except:
|
||||
pass
|
||||
|
||||
import distribute_setup
|
||||
distribute_setup.use_setuptools()
|
||||
from setuptools import setup, Extension, find_packages
|
||||
@ -20,17 +13,18 @@ classifiers = ['Development Status :: 3 - Alpha',
|
||||
'Topic :: System :: Hardware']
|
||||
|
||||
setup(name = 'CHIP_IO',
|
||||
version = '0.3.4.1',
|
||||
version = '0.5.2',
|
||||
author = 'Robert Wolterman',
|
||||
author_email = 'robert.wolterman@gmail.com',
|
||||
description = 'A module to control CHIP IO channels',
|
||||
long_description = open('README.rst').read() + open('CHANGELOG.rst').read(),
|
||||
license = 'MIT',
|
||||
keywords = 'CHIP NextThingCo IO GPIO PWM ADC',
|
||||
keywords = 'CHIP NextThingCo IO GPIO PWM ADC GR8 R8',
|
||||
url = 'https://github.com/xtacocorex/CHIP_IO/',
|
||||
classifiers = classifiers,
|
||||
packages = find_packages(),
|
||||
ext_modules = [Extension('CHIP_IO.GPIO', ['source/py_gpio.c', 'source/event_gpio.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security']),
|
||||
Extension('CHIP_IO.PWM', ['source/py_pwm.c', 'source/c_pwm.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security']),
|
||||
Extension('CHIP_IO.SOFTPWM', ['source/py_softpwm.c', 'source/c_softpwm.c', 'source/constants.c', 'source/common.c', 'source/event_gpio.c'], extra_compile_args=['-Wno-format-security'])]) #,
|
||||
Extension('CHIP_IO.SOFTPWM', ['source/py_softpwm.c', 'source/c_softpwm.c', 'source/constants.c', 'source/common.c', 'source/event_gpio.c'], extra_compile_args=['-Wno-format-security']),
|
||||
Extension('CHIP_IO.SERVO', ['source/py_servo.c', 'source/c_softservo.c', 'source/constants.c', 'source/common.c', 'source/event_gpio.c'], extra_compile_args=['-Wno-format-security'])]) #,
|
||||
# Extension('CHIP_IO.ADC', ['source/py_adc.c', 'source/c_adc.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security']),
|
||||
|
104
source/c_pwm.c
104
source/c_pwm.c
@ -47,35 +47,14 @@ SOFTWARE.
|
||||
#define DISABLE 0
|
||||
|
||||
// Global variables
|
||||
int pwm_initialized = 0;
|
||||
|
||||
// pwm devices (future chip pro use)
|
||||
struct pwm_dev
|
||||
{
|
||||
char key[KEYLEN+1]; /* leave room for terminating NUL byte */
|
||||
int gpio;
|
||||
int initialized;
|
||||
struct pwm_dev *next;
|
||||
};
|
||||
struct pwm_dev *initialized_pwms = NULL;
|
||||
|
||||
struct pwm_dev *lookup_initialized_pwm(const char *key)
|
||||
{
|
||||
struct pwm_dev *dev = initialized_pwms;
|
||||
while (dev != NULL)
|
||||
{
|
||||
if (strcmp(dev->key, key) == 0) {
|
||||
return dev;
|
||||
}
|
||||
dev = dev->next;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// pwm exports
|
||||
struct pwm_exp
|
||||
{
|
||||
char key[KEYLEN+1]; /* leave room for terminating NUL byte */
|
||||
int iscpro;
|
||||
int gpio;
|
||||
int initialized;
|
||||
int period_fd;
|
||||
int duty_fd;
|
||||
int polarity_fd;
|
||||
@ -101,15 +80,12 @@ struct pwm_exp *lookup_exported_pwm(const char *key)
|
||||
return NULL; /* standard for pointers */
|
||||
}
|
||||
|
||||
int initialize_pwm(void)
|
||||
int initialize_pwm(int *initialized, int gpio)
|
||||
{
|
||||
int e_no;
|
||||
if (!pwm_initialized) {
|
||||
if (!*initialized) {
|
||||
int fd, len;
|
||||
char str_gpio[80];
|
||||
// Per https://github.com/NextThingCo/CHIP-linux/pull/4
|
||||
// we need to export 0 here to enable pwm0
|
||||
int gpio = 0;
|
||||
|
||||
if (DEBUG)
|
||||
printf(" ** initialize_pwm **\n");
|
||||
@ -134,7 +110,7 @@ int initialize_pwm(void)
|
||||
if (DEBUG)
|
||||
printf(" ** initialize_pwm: export pin: s = %d, len = %d\n", s, len);
|
||||
|
||||
pwm_initialized = 1;
|
||||
*initialized = 1;
|
||||
return 1;
|
||||
} else {
|
||||
if (DEBUG)
|
||||
@ -169,7 +145,7 @@ int pwm_set_frequency(const char *key, float freq) {
|
||||
len = snprintf(buffer, sizeof(buffer), "%lu", period_ns); BUF2SMALL(buffer);
|
||||
ssize_t s = write(pwm->period_fd, buffer, len); e_no = errno;
|
||||
if (DEBUG) {
|
||||
printf(" ** pwm_set_frequency: pwm_initialized = %d\n", pwm_initialized);
|
||||
printf(" ** pwm_set_frequency: pwm_initialized = %d\n", pwm->initialized);
|
||||
printf(" ** pwm_set_frequency: buffer: %s\n", buffer);
|
||||
printf(" ** pwm_set_frequency: s = %d, len = %d\n", s, len);
|
||||
}
|
||||
@ -212,7 +188,7 @@ int pwm_set_period_ns(const char *key, unsigned long period_ns) {
|
||||
len = snprintf(buffer, sizeof(buffer), "%lu", period_ns); BUF2SMALL(buffer);
|
||||
ssize_t s = write(pwm->period_fd, buffer, len); e_no = errno;
|
||||
if (DEBUG) {
|
||||
printf(" ** pwm_set_period_ns: pwm_initialized = %d\n", pwm_initialized);
|
||||
printf(" ** pwm_set_period_ns: pwm_initialized = %d\n", pwm->initialized);
|
||||
printf(" ** pwm_set_period_ns: buffer: %s\n", buffer);
|
||||
printf(" ** pwm_set_period_ns: s = %d, len = %d\n", s, len);
|
||||
}
|
||||
@ -280,7 +256,7 @@ int pwm_set_polarity(const char *key, int polarity) {
|
||||
}
|
||||
ssize_t s = write(pwm->polarity_fd, buffer, len); e_no = errno;
|
||||
if (DEBUG) {
|
||||
printf(" ** pwm_set_polarity: pwm_initialized = %d\n", pwm_initialized);
|
||||
printf(" ** pwm_set_polarity: pwm_initialized = %d\n", pwm->initialized);
|
||||
printf(" ** pwm_set_polarity: buffer: %s\n", buffer);
|
||||
printf(" ** pwm_set_polarity: s = %d, len = %d\n", s, len);
|
||||
}
|
||||
@ -320,7 +296,7 @@ int pwm_set_duty_cycle(const char *key, float duty) {
|
||||
len = snprintf(buffer, sizeof(buffer), "%lu", pwm->duty); BUF2SMALL(buffer);
|
||||
ssize_t s = write(pwm->duty_fd, buffer, len); e_no = errno;
|
||||
if (DEBUG) {
|
||||
printf(" ** pwm_set_duty_cycle: pwm_initialized = %d\n", pwm_initialized);
|
||||
printf(" ** pwm_set_duty_cycle: pwm_initialized = %d\n", pwm->initialized);
|
||||
printf(" ** pwm_set_duty_cycle: buffer: %s\n", buffer);
|
||||
printf(" ** pwm_set_duty_cycle: s = %d, len = %d\n", s, len);
|
||||
}
|
||||
@ -360,7 +336,7 @@ int pwm_set_pulse_width_ns(const char *key, unsigned long pulse_width_ns) {
|
||||
len = snprintf(buffer, sizeof(buffer), "%lu", pwm->duty); BUF2SMALL(buffer);
|
||||
ssize_t s = write(pwm->duty_fd, buffer, len); e_no = errno;
|
||||
if (DEBUG) {
|
||||
printf(" ** pwm_set_pulse_width_ns: pwm_initialized = %d\n", pwm_initialized);
|
||||
printf(" ** pwm_set_pulse_width_ns: pwm_initialized = %d\n", pwm->initialized);
|
||||
printf(" ** pwm_set_pulse_width_ns: buffer: %s\n", buffer);
|
||||
printf(" ** pwm_set_pulse_width_ns: s = %d, len = %d\n", s, len);
|
||||
}
|
||||
@ -405,7 +381,7 @@ int pwm_set_enable(const char *key, int enable)
|
||||
len = snprintf(buffer, sizeof(buffer), "%d", enable); BUF2SMALL(buffer);
|
||||
ssize_t s = write(pwm->enable_fd, buffer, len); e_no = errno;
|
||||
if (DEBUG) {
|
||||
printf(" ** pwm_set_enable: pwm_initialized = %d\n", pwm_initialized);
|
||||
printf(" ** pwm_set_enable: pwm_initialized = %d\n", pwm->initialized);
|
||||
printf(" ** pwm_set_enable: buffer: %s\n", buffer);
|
||||
printf(" ** pwm_set_enable: s = %d, len = %d\n", s, len);
|
||||
}
|
||||
@ -435,21 +411,35 @@ int pwm_start(const char *key, float duty, float freq, int polarity)
|
||||
char polarity_path[80];
|
||||
int period_fd, duty_fd, polarity_fd, enable_fd;
|
||||
struct pwm_exp *new_pwm, *pwm;
|
||||
int gpio = 0;
|
||||
int initialized = 0;
|
||||
int iscpro = 0;
|
||||
|
||||
if (DEBUG)
|
||||
printf(" ** pwm_start: pwm_initialized = %d\n", pwm_initialized);
|
||||
if(!pwm_initialized) {
|
||||
initialize_pwm();
|
||||
// Figure out if we are a CPro
|
||||
iscpro = is_this_chippro();
|
||||
|
||||
// Figure out which pin we are
|
||||
if (strcmp(key,"U13_18") == 0) {
|
||||
gpio = 0;
|
||||
} else if (strcmp(key,"EINT13") == 0) {
|
||||
gpio = 1;
|
||||
}
|
||||
|
||||
// Try to get the pwm
|
||||
pwm = lookup_exported_pwm(key);
|
||||
|
||||
if (pwm == NULL) {
|
||||
initialize_pwm(&initialized, gpio);
|
||||
if (DEBUG)
|
||||
printf(" ** pwm_start: pwm(%d) initialized = %d\n", gpio, initialized);
|
||||
} else {
|
||||
if (DEBUG)
|
||||
printf(" ** pwm_start: pwm already initialized, cleaning up **");
|
||||
printf(" ** pwm_start: pwm(%d) already initialized, cleaning up **", gpio);
|
||||
pwm_cleanup();
|
||||
}
|
||||
if (DEBUG)
|
||||
printf(" ** pwm_start: pwm_initialized = %d\n", pwm_initialized);
|
||||
|
||||
//setup the pwm base path, the chip only has one pwm
|
||||
snprintf(pwm_base_path, sizeof(pwm_base_path), "/sys/class/pwm/pwmchip0/pwm%d", 0); BUF2SMALL(pwm_base_path);
|
||||
snprintf(pwm_base_path, sizeof(pwm_base_path), "/sys/class/pwm/pwmchip0/pwm%d", gpio); BUF2SMALL(pwm_base_path);
|
||||
|
||||
//create the path for the period and duty
|
||||
snprintf(enable_path, sizeof(enable_path), "%s/enable", pwm_base_path); BUF2SMALL(enable_path);
|
||||
@ -515,6 +505,9 @@ int pwm_start(const char *key, float duty, float freq, int polarity)
|
||||
printf(" ** pwm_start: sysfs files opened successfully **\n");
|
||||
strncpy(new_pwm->key, key, KEYLEN); /* can leave string unterminated */
|
||||
new_pwm->key[KEYLEN] = '\0'; /* terminate string */
|
||||
new_pwm->gpio = gpio;
|
||||
new_pwm->iscpro = iscpro;
|
||||
new_pwm->initialized = initialized;
|
||||
new_pwm->period_fd = period_fd;
|
||||
new_pwm->duty_fd = duty_fd;
|
||||
new_pwm->polarity_fd = polarity_fd;
|
||||
@ -534,6 +527,10 @@ int pwm_start(const char *key, float duty, float freq, int polarity)
|
||||
}
|
||||
|
||||
int rtnval = 0;
|
||||
// Always set polarity first
|
||||
if (iscpro == 1) {
|
||||
rtnval = pwm_set_polarity(key, polarity);
|
||||
}
|
||||
rtnval = pwm_set_enable(key, ENABLE);
|
||||
// Fix for issue #53
|
||||
if (rtnval != -1) {
|
||||
@ -541,8 +538,6 @@ int pwm_start(const char *key, float duty, float freq, int polarity)
|
||||
rtnval = pwm_set_frequency(key, freq);
|
||||
if (rtnval != -1) {
|
||||
rtnval = 0;
|
||||
//rtnval = pwm_set_polarity(key, polarity);
|
||||
//rtnval = 0;
|
||||
rtnval = pwm_set_duty_cycle(key, duty);
|
||||
}
|
||||
}
|
||||
@ -555,15 +550,22 @@ int pwm_disable(const char *key)
|
||||
|
||||
int fd, len, e_no;
|
||||
char str_gpio[80];
|
||||
// Per https://github.com/NextThingCo/CHIP-linux/pull/4
|
||||
// we need to export 0 here to enable pwm0
|
||||
int gpio = 0;
|
||||
|
||||
pwm = lookup_exported_pwm(key);
|
||||
|
||||
if (pwm == NULL) {
|
||||
if (DEBUG)
|
||||
printf(" ** pwm_disable: pwm struct is null **\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Disable the PWM
|
||||
pwm_set_frequency(key, 0);
|
||||
//pwm_set_polarity(key, 0);
|
||||
pwm_set_duty_cycle(key, 0);
|
||||
pwm_set_enable(key, DISABLE);
|
||||
if (pwm->iscpro == 1) {
|
||||
pwm_set_polarity(key, 0);
|
||||
}
|
||||
|
||||
if ((fd = open("/sys/class/pwm/pwmchip0/unexport", O_WRONLY)) < 0)
|
||||
{
|
||||
@ -572,7 +574,7 @@ int pwm_disable(const char *key)
|
||||
add_error_msg(err);
|
||||
return -1;
|
||||
}
|
||||
len = snprintf(str_gpio, sizeof(str_gpio), "%d", gpio); BUF2SMALL(str_gpio);
|
||||
len = snprintf(str_gpio, sizeof(str_gpio), "%d", pwm->gpio); BUF2SMALL(str_gpio);
|
||||
ssize_t s = write(fd, str_gpio, len); e_no = errno;
|
||||
close(fd);
|
||||
if (s != len) {
|
||||
|
@ -39,7 +39,7 @@ SOFTWARE.
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
#include <time.h>
|
||||
#include "c_pwm.h"
|
||||
#include "c_softpwm.h"
|
||||
#include "common.h"
|
||||
#include "event_gpio.h"
|
||||
|
||||
|
355
source/c_softservo.c
Normal file
355
source/c_softservo.c
Normal file
@ -0,0 +1,355 @@
|
||||
/*
|
||||
Copyright (c) 2017 Robert Wolterman
|
||||
|
||||
Using CHIP_IO servo code from Brady Hurlburt as a basis for the servo code
|
||||
|
||||
Copyright (c) 2016 Brady Hurlburt
|
||||
|
||||
Original BBIO Author Justin Cooper
|
||||
Modified for CHIP_IO Author Brady Hurlburt
|
||||
|
||||
This file incorporates work covered by the following copyright and
|
||||
permission notice, all modified code adopts the original license:
|
||||
|
||||
Copyright (c) 2013 Adafruit
|
||||
Author: Justin Cooper
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <sys/types.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
#include <time.h>
|
||||
#include "c_softservo.h"
|
||||
#include "common.h"
|
||||
#include "event_gpio.h"
|
||||
|
||||
#define KEYLEN 7
|
||||
|
||||
#define PERIOD 0
|
||||
#define DUTY 1
|
||||
|
||||
#define MSTONS 1000000
|
||||
#define BLOCKNS 20 * MSTONS
|
||||
#define FUDGEFACTOR 450
|
||||
#define MSTOMICROS 1000
|
||||
#define MICROSTONS 1000
|
||||
#define MINMICROS 1000
|
||||
#define NEUTRAL 1500
|
||||
#define MAXMICROS 2000
|
||||
|
||||
int servo_initialized = 0;
|
||||
|
||||
struct servo_params
|
||||
{
|
||||
float range;
|
||||
float min_angle;
|
||||
float max_angle;
|
||||
float current_angle;
|
||||
bool enabled;
|
||||
bool stop_flag;
|
||||
};
|
||||
|
||||
struct servo
|
||||
{
|
||||
char key[KEYLEN+1]; /* leave room for terminating NUL byte */
|
||||
int gpio;
|
||||
struct servo_params params;
|
||||
pthread_mutex_t* params_lock;
|
||||
pthread_t thread;
|
||||
struct servo *next;
|
||||
};
|
||||
struct servo *exported_servos = NULL;
|
||||
|
||||
struct servo *lookup_exported_servo(const char *key)
|
||||
{
|
||||
struct servo *srv = exported_servos;
|
||||
|
||||
while (srv != NULL)
|
||||
{
|
||||
if (strcmp(srv->key, key) == 0) {
|
||||
return srv;
|
||||
}
|
||||
srv = srv->next;
|
||||
}
|
||||
|
||||
return NULL; /* standard for pointers */
|
||||
}
|
||||
|
||||
void *servo_thread_toggle(void *arg)
|
||||
{
|
||||
struct servo *srv = (struct servo *)arg;
|
||||
int gpio = srv->gpio;
|
||||
struct timespec tim_on;
|
||||
struct timespec tim_off;
|
||||
float on_time_microsec = 0;
|
||||
unsigned int on_ns;
|
||||
unsigned int off_ns;
|
||||
|
||||
/* Used to determine if something has
|
||||
* has changed
|
||||
*/
|
||||
float angle_local = 0;
|
||||
float range_local = 0;
|
||||
bool stop_flag_local = false;
|
||||
bool enabled_local = false;
|
||||
bool recalculate_timing = false;
|
||||
|
||||
while (!stop_flag_local) {
|
||||
/* Take a snapshot of the parameter block */
|
||||
pthread_mutex_lock(srv->params_lock);
|
||||
if ((angle_local != srv->params.current_angle) || (range_local != srv->params.range)) {
|
||||
recalculate_timing = true;
|
||||
}
|
||||
angle_local = srv->params.current_angle;
|
||||
range_local = srv->params.range;
|
||||
enabled_local = srv->params.enabled;
|
||||
stop_flag_local = srv->params.stop_flag;
|
||||
pthread_mutex_unlock(srv->params_lock);
|
||||
|
||||
/* If freq or duty has been changed, update the
|
||||
* sleep times
|
||||
*/
|
||||
if (recalculate_timing) {
|
||||
if (DEBUG)
|
||||
printf(" ** servo updating timing: new angle: (%.2f)\n",angle_local);
|
||||
on_time_microsec = (((MAXMICROS - MINMICROS) / range_local) * angle_local) + NEUTRAL;
|
||||
on_ns = (unsigned long)(on_time_microsec * MICROSTONS - FUDGEFACTOR);
|
||||
off_ns = BLOCKNS - on_ns;
|
||||
tim_on.tv_sec = 0;
|
||||
tim_on.tv_nsec = on_ns;
|
||||
tim_off.tv_sec = 0;
|
||||
tim_off.tv_nsec = off_ns;
|
||||
recalculate_timing = false;
|
||||
}
|
||||
|
||||
if (enabled_local)
|
||||
{
|
||||
/* Set gpio */
|
||||
gpio_set_value(gpio, HIGH);
|
||||
|
||||
nanosleep(&tim_on, NULL);
|
||||
|
||||
/* Unset gpio */
|
||||
gpio_set_value(gpio, LOW);
|
||||
|
||||
nanosleep(&tim_off, NULL);
|
||||
//printf("AFTER SECOND NANOSLEEP\n");
|
||||
} /* if enabled_local */
|
||||
} /* while !stop_flag_local */
|
||||
|
||||
gpio_set_value(gpio, LOW);
|
||||
|
||||
/* This servo has been disabled */
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
|
||||
int servo_start(const char *key, float angle, float range)
|
||||
{
|
||||
struct servo *new_srv, *srv;
|
||||
pthread_t new_thread;
|
||||
pthread_mutex_t *new_params_lock;
|
||||
int gpio;
|
||||
int ret;
|
||||
|
||||
if (get_gpio_number(key, &gpio) < 0) {
|
||||
if (DEBUG)
|
||||
printf(" ** servo_start: invalid gpio specified **\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (gpio_export(gpio) < 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "Error setting up servo on pin %d, maybe already exported? (%s)", gpio, get_error_msg());
|
||||
add_error_msg(err);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (DEBUG)
|
||||
printf(" ** servo_start: %d exported **\n", gpio);
|
||||
|
||||
if (gpio_set_direction(gpio, OUTPUT) < 0) {
|
||||
if (DEBUG)
|
||||
printf(" ** servo_start: gpio_set_direction failed **\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("c_softservo.c: servo_start(%d,%.2f,%.2f)\n",gpio,angle,range);
|
||||
|
||||
// add to list
|
||||
new_srv = malloc(sizeof(struct servo)); ASSRT(new_srv != NULL);
|
||||
new_params_lock = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
|
||||
if (new_srv == 0) {
|
||||
return -1; // out of memory
|
||||
}
|
||||
pthread_mutex_init(new_params_lock, NULL);
|
||||
pthread_mutex_lock(new_params_lock);
|
||||
|
||||
strncpy(new_srv->key, key, KEYLEN); /* can leave string unterminated */
|
||||
new_srv->key[KEYLEN] = '\0'; /* terminate string */
|
||||
new_srv->gpio = gpio;
|
||||
new_srv->params.enabled = true;
|
||||
new_srv->params.stop_flag = false;
|
||||
new_srv->params_lock = new_params_lock;
|
||||
new_srv->next = NULL;
|
||||
|
||||
if (exported_servos == NULL)
|
||||
{
|
||||
// create new list
|
||||
exported_servos = new_srv;
|
||||
} else {
|
||||
// add to end of existing list
|
||||
srv = exported_servos;
|
||||
while (srv->next != NULL)
|
||||
srv = srv->next;
|
||||
srv->next = new_srv;
|
||||
}
|
||||
pthread_mutex_unlock(new_params_lock);
|
||||
|
||||
if (DEBUG)
|
||||
printf(" ** servo_enable: setting servo parameters **\n");
|
||||
ASSRT(servo_set_range(new_srv->key, range) == 0);
|
||||
ASSRT(servo_set_angle(new_srv->key, angle) == 0);
|
||||
|
||||
pthread_mutex_lock(new_params_lock);
|
||||
// create thread for srv
|
||||
if (DEBUG)
|
||||
printf(" ** servo_enable: creating thread **\n");
|
||||
ret = pthread_create(&new_thread, NULL, servo_thread_toggle, (void *)new_srv);
|
||||
ASSRT(ret == 0);
|
||||
|
||||
new_srv->thread = new_thread;
|
||||
|
||||
pthread_mutex_unlock(new_params_lock);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int servo_disable(const char *key)
|
||||
{
|
||||
struct servo *srv, *temp, *prev_srv = NULL;
|
||||
|
||||
if (DEBUG)
|
||||
printf(" ** in servo_disable **\n");
|
||||
// remove from list
|
||||
srv = exported_servos;
|
||||
while (srv != NULL)
|
||||
{
|
||||
if (strcmp(srv->key, key) == 0)
|
||||
{
|
||||
if (DEBUG)
|
||||
printf(" ** servo_disable: found pin **\n");
|
||||
pthread_mutex_lock(srv->params_lock);
|
||||
srv->params.stop_flag = true;
|
||||
pthread_mutex_unlock(srv->params_lock);
|
||||
pthread_join(srv->thread, NULL); /* wait for thread to exit */
|
||||
|
||||
if (DEBUG)
|
||||
printf(" ** servo_disable: unexporting %d **\n", srv->gpio);
|
||||
gpio_unexport(srv->gpio);
|
||||
|
||||
if (prev_srv == NULL)
|
||||
{
|
||||
exported_servos = srv->next;
|
||||
prev_srv = srv;
|
||||
} else {
|
||||
prev_srv->next = srv->next;
|
||||
}
|
||||
|
||||
temp = srv;
|
||||
srv = srv->next;
|
||||
free(temp->params_lock);
|
||||
free(temp);
|
||||
} else {
|
||||
prev_srv = srv;
|
||||
srv = srv->next;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int servo_set_range(const char *key, float range)
|
||||
{
|
||||
struct servo *srv;
|
||||
float min_angle, max_angle;
|
||||
|
||||
srv = lookup_exported_servo(key);
|
||||
|
||||
if (srv == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Compute the min and max angle
|
||||
max_angle = range / 2.0;
|
||||
min_angle = -max_angle;
|
||||
|
||||
if (DEBUG)
|
||||
printf(" ** servo_set_range(%d,%.2f = %.2f,%.2f)\n",srv->gpio,range,min_angle,max_angle);
|
||||
pthread_mutex_lock(srv->params_lock);
|
||||
srv->params.range = range;
|
||||
srv->params.min_angle = min_angle;
|
||||
srv->params.max_angle = max_angle;
|
||||
pthread_mutex_unlock(srv->params_lock);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int servo_set_angle(const char *key, float angle)
|
||||
{
|
||||
struct servo *srv;
|
||||
|
||||
srv = lookup_exported_servo(key);
|
||||
|
||||
if (srv == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Make sure we're between the range of allowable angles
|
||||
if (angle < srv->params.min_angle || angle > srv->params.max_angle) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "Angle specified (%.2f) for pin %d, is outside allowable range (%.2f,%.2f)", angle, srv->gpio, srv->params.min_angle,srv->params.max_angle);
|
||||
add_error_msg(err);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (DEBUG)
|
||||
printf(" ** servo_set_angle(%d,%.2f)\n",srv->gpio,angle);
|
||||
pthread_mutex_lock(srv->params_lock);
|
||||
srv->params.current_angle = angle;
|
||||
pthread_mutex_unlock(srv->params_lock);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void servo_cleanup(void)
|
||||
{
|
||||
while (exported_servos != NULL) {
|
||||
servo_disable(exported_servos->key);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
40
source/c_softservo.h
Normal file
40
source/c_softservo.h
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
Copyright (c) 2017 Robert Wolterman
|
||||
|
||||
Using CHIP_IO SoftPWM code from Brady Hurlburt as a basis for the servo code
|
||||
|
||||
Copyright (c) 2016 Brady Hurlburt
|
||||
|
||||
Original BBIO Author Justin Cooper
|
||||
Modified for CHIP_IO Author Brady Hurlburt
|
||||
|
||||
This file incorporates work covered by the following copyright and
|
||||
permission notice, all modified code adopts the original license:
|
||||
|
||||
Copyright (c) 2013 Adafruit
|
||||
Author: Justin Cooper
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
int servo_start(const char *key, float angle, float range);
|
||||
int servo_disable(const char *key);
|
||||
int servo_set_range(const char *key, float range);
|
||||
int servo_set_angle(const char *key, float angle);
|
||||
void servo_cleanup(void);
|
172
source/common.c
172
source/common.c
@ -45,6 +45,7 @@ SOFTWARE.
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/sysinfo.h>
|
||||
|
||||
int setup_error = 0;
|
||||
int module_setup = 0;
|
||||
@ -52,6 +53,9 @@ int module_setup = 0;
|
||||
// Library Debug
|
||||
int DEBUG = 0;
|
||||
|
||||
// Is This a CHIP PRO
|
||||
int is_chip_pro = 0;
|
||||
|
||||
pins_t pins_info[] = {
|
||||
{ "GND", "GND", "U13_1", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "CHG-IN", "CHG-IN", "U13_2", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
@ -65,39 +69,40 @@ pins_t pins_info[] = {
|
||||
{ "PWRON", "PWRON", "U13_10", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "TWI1-SCK", "KPD-I2C-SCL", "U13_11", 47, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "GND", "GND", "U13_12", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "X1", "X1", "U13_13", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "X2", "X2", "U13_14", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "Y1", "Y1", "U13_15", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "Y2", "Y2", "U13_16", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "LCD-D2", "LCD-D2", "U13_17", 98, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "X1", "X1", "U13_13", -1, BASE_METHOD_AS_IS, -1, -1, CHIP},
|
||||
{ "X2", "X2", "U13_14", -1, BASE_METHOD_AS_IS, -1, -1, CHIP},
|
||||
{ "Y1", "Y1", "U13_15", -1, BASE_METHOD_AS_IS, -1, -1, CHIP},
|
||||
{ "Y2", "Y2", "U13_16", -1, BASE_METHOD_AS_IS, -1, -1, CHIP},
|
||||
{ "LCD-D2", "UART2-TX", "U13_17", 98, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "PWM0", "PWM0", "U13_18", 34, BASE_METHOD_AS_IS, 0, -1, BOTH},
|
||||
{ "LCD-D4", "LCD-D4", "U13_19", 100, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "LCD-D3", "LCD-D3", "U13_20", 99, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "LCD-D6", "LCD-D6", "U13_21", 102, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "LCD-D5", "LCD-D5", "U13_22", 101, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "LCD-D10", "LCD-D10", "U13_23", 106, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "LCD-D7", "LCD-D7", "U13_24", 103, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "LCD-D12", "LCD-D12", "U13_25", 108, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "LCD-D11", "LCD-D11", "U13_26", 107, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "LCD-D14", "LCD-D14", "U13_27", 110, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "LCD-D13", "LCD-D13", "U13_28", 109, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "LCD-D18", "LCD-D18", "U13_29", 114, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "LCD-D15", "LCD-D15", "U13_30", 111, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "LCD-D20", "LCD-D20", "U13_31", 116, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "LCD-D19", "LCD-D19", "U13_32", 115, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "LCD-D22", "LCD-D22", "U13_33", 118, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "LCD-D21", "LCD-D21", "U13_34", 117, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "LCD-CLK", "LCD-CLK", "U13_35", 120, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "LCD-D23", "LCD-D23", "U13_36", 119, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "LCD-VSYNC", "LCD-VSYNC", "U13_37", 123, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "LCD-HSYNC", "LCD-HSYNC", "U13_38", 122, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "PWM1", "PWM1", "EINT13", 205, BASE_METHOD_AS_IS, 0, -1, CHIPPRO},
|
||||
{ "LCD-D4", "UART2-CTS", "U13_19", 100, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "LCD-D3", "UART2-RX", "U13_20", 99, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "LCD-D6", "LCD-D6", "U13_21", 102, BASE_METHOD_AS_IS, -1, -1, CHIP},
|
||||
{ "LCD-D5", "UART2-RTS", "U13_22", 101, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "LCD-D10", "LCD-D10", "U13_23", 106, BASE_METHOD_AS_IS, -1, -1, CHIP},
|
||||
{ "LCD-D7", "LCD-D7", "U13_24", 103, BASE_METHOD_AS_IS, -1, -1, CHIP},
|
||||
{ "LCD-D12", "LCD-D12", "U13_25", 108, BASE_METHOD_AS_IS, -1, -1, CHIP},
|
||||
{ "LCD-D11", "LCD-D11", "U13_26", 107, BASE_METHOD_AS_IS, -1, -1, CHIP},
|
||||
{ "LCD-D14", "LCD-D14", "U13_27", 110, BASE_METHOD_AS_IS, -1, -1, CHIP},
|
||||
{ "LCD-D13", "LCD-D13", "U13_28", 109, BASE_METHOD_AS_IS, -1, -1, CHIP},
|
||||
{ "LCD-D18", "LCD-D18", "U13_29", 114, BASE_METHOD_AS_IS, -1, -1, CHIP},
|
||||
{ "LCD-D15", "LCD-D15", "U13_30", 111, BASE_METHOD_AS_IS, -1, -1, CHIP},
|
||||
{ "LCD-D20", "LCD-D20", "U13_31", 116, BASE_METHOD_AS_IS, -1, -1, CHIP},
|
||||
{ "LCD-D19", "LCD-D19", "U13_32", 115, BASE_METHOD_AS_IS, -1, -1, CHIP},
|
||||
{ "LCD-D22", "LCD-D22", "U13_33", 118, BASE_METHOD_AS_IS, -1, -1, CHIP},
|
||||
{ "LCD-D21", "LCD-D21", "U13_34", 117, BASE_METHOD_AS_IS, -1, -1, CHIP},
|
||||
{ "LCD-CLK", "LCD-CLK", "U13_35", 120, BASE_METHOD_AS_IS, -1, -1, CHIP},
|
||||
{ "LCD-D23", "LCD-D23", "U13_36", 119, BASE_METHOD_AS_IS, -1, -1, CHIP},
|
||||
{ "LCD-VSYNC", "LCD-VSYNC", "U13_37", 123, BASE_METHOD_AS_IS, -1, -1, CHIP},
|
||||
{ "LCD-HSYNC", "LCD-HSYNC", "U13_38", 122, BASE_METHOD_AS_IS, -1, -1, CHIP},
|
||||
{ "GND", "GND", "U13_39", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "LCD-DE", "LCD-DE", "U13_40", 121, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "LCD-DE", "LCD-DE", "U13_40", 121, BASE_METHOD_AS_IS, -1, -1, CHIP},
|
||||
{ "GND", "GND", "U14_1", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "VCC-5V", "VCC-5V", "U14_2", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "UART1-TX", "UART-TX", "U14_3", 195, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "UART1-TX", "UART-TX", "U14_3", 195, BASE_METHOD_AS_IS, -1, -1, BOTH}, /* THIS IS AP-EINT3 ON CHIP PRO */
|
||||
{ "HPL", "HPL", "U14_4", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "UART1-RX", "UART-RX", "U14_5", 196, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "UART1-RX", "UART-RX", "U14_5", 196, BASE_METHOD_AS_IS, -1, -1, BOTH}, /* THIS IS AP-EINT4 ON CHIP PRO */
|
||||
{ "HPCOM", "HPCOM", "U14_6", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "FEL", "FEL", "U14_7", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "HPR", "HPR", "U14_8", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
@ -123,14 +128,14 @@ pins_t pins_info[] = {
|
||||
{ "CSICK", "SPI-CLK", "U14_28", 129, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "CSIHSYNC", "SPI-MOSI", "U14_29", 130, BASE_METHOD_AS_IS, 1, -1, BOTH},
|
||||
{ "CSIVSYNC", "SPI-MISO", "U14_30", 131, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "CSID0", "CSID0", "U14_31", 132, BASE_METHOD_AS_IS, 1, -1, BOTH},
|
||||
{ "CSID1", "CSID1", "U14_32", 133, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "CSID2", "CSID2", "U14_33", 134, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "CSID3", "CSID3", "U14_34", 135, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "CSID4", "CSID4", "U14_35", 136, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "CSID5", "CSID5", "U14_36", 137, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "CSID6", "CSID6", "U14_37", 138, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "CSID7", "CSID7", "U14_38", 139, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "CSID0", "D0", "U14_31", 132, BASE_METHOD_AS_IS, 1, -1, BOTH},
|
||||
{ "CSID1", "D1", "U14_32", 133, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "CSID2", "D2", "U14_33", 134, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "CSID3", "D3", "U14_34", 135, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "CSID4", "D4", "U14_35", 136, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "CSID5", "D5", "U14_36", 137, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "CSID6", "D6", "U14_37", 138, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "CSID7", "D7", "U14_38", 139, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "GND", "GND", "U14_39", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ "GND", "GND", "U14_40", -1, BASE_METHOD_AS_IS, -1, -1, BOTH},
|
||||
{ NULL, NULL, NULL, -1, 0, -1, -1, -1}
|
||||
@ -216,12 +221,107 @@ int get_xio_base(void)
|
||||
return xio_base_address;
|
||||
} /* get_xio_base */
|
||||
|
||||
#define RAMDETERMINER 380.0
|
||||
|
||||
int is_this_chippro(void)
|
||||
{
|
||||
int is_pro = 0;
|
||||
struct sysinfo si;
|
||||
sysinfo (&si);
|
||||
const double megabyte = 1024 * 1024;
|
||||
|
||||
if (DEBUG)
|
||||
printf(" ** is_this_chippro: total system ram: %5.1f mb\n", si.totalram / megabyte);
|
||||
|
||||
if ((si.totalram/megabyte) > RAMDETERMINER) {
|
||||
is_pro = 0;
|
||||
if (DEBUG)
|
||||
printf(" ** is_this_chippro: we are a chip\n");
|
||||
} else {
|
||||
is_pro = 1;
|
||||
if (DEBUG)
|
||||
printf(" ** is_this_chippro: we are a chip pro\n");
|
||||
}
|
||||
|
||||
return is_pro;
|
||||
}
|
||||
|
||||
int gpio_allowed(int gpio)
|
||||
{
|
||||
int rtnval = -1;
|
||||
pins_t *p;
|
||||
int tmpgpio = -1;
|
||||
|
||||
// If the return is good, we should be good to go, so let's check the data
|
||||
|
||||
// Loop through the pins
|
||||
for (p = pins_info; p->key != NULL; ++p) {
|
||||
tmpgpio = gpio_number(p);
|
||||
if (tmpgpio == gpio) {
|
||||
if (DEBUG)
|
||||
printf(" ** gpio_allowed: found match\n");
|
||||
// We have a CHIP and the pin is for CHIP/BOTH
|
||||
if (((p->sbc_type == CHIP) || (p->sbc_type == BOTH)) && (is_this_chippro() == 0)) {
|
||||
if (DEBUG)
|
||||
printf(" ** gpio_allowed: pin allowed for chip or bth and we're a chip\n");
|
||||
rtnval = 1;
|
||||
// We have a CHIP Pro and the pin is for CHIPPRO/BOTH
|
||||
} else if (((p->sbc_type == CHIPPRO) || (p->sbc_type == BOTH)) && (is_this_chippro() == 1)) {
|
||||
if (DEBUG)
|
||||
printf(" ** gpio_allowed: pin allowed for chip pro or both and we're a chip pro\n");
|
||||
rtnval = 1;
|
||||
} else {
|
||||
if (DEBUG)
|
||||
printf(" ** gpio_allowed: pin is not allowed on hardware\n");
|
||||
rtnval = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return rtnval;
|
||||
}
|
||||
|
||||
int pwm_allowed(const char *key)
|
||||
{
|
||||
int rtnval = -1;
|
||||
pins_t *p;
|
||||
// Determine if we are CHIP Pro
|
||||
// Running because we cannot be sure if it was previously run
|
||||
|
||||
// If the return is good, we should be good to go, so let's check the data
|
||||
for (p = pins_info; p->key != NULL; ++p) {
|
||||
if (strcmp(p->key, key) == 0) {
|
||||
if (DEBUG)
|
||||
printf(" ** pwm_allowed: found match\n");
|
||||
// We have a CHIP and the pin is for CHIP/BOTH
|
||||
if ((p->sbc_type == BOTH) && (is_this_chippro() == 0)) {
|
||||
if (DEBUG)
|
||||
printf(" ** pwm_allowed: pwm allowed for chip or bth and we're a chip\n");
|
||||
rtnval = 1;
|
||||
// We have a CHIP Pro and the pin is for CHIPPRO/BOTH
|
||||
} else if (((p->sbc_type == CHIPPRO) || (p->sbc_type == BOTH)) && (is_this_chippro() == 1)) {
|
||||
if (DEBUG)
|
||||
printf(" ** pwm_allowed: pwm allowed for chip pro or both and we're a chip pro\n");
|
||||
rtnval = 1;
|
||||
} else {
|
||||
if (DEBUG)
|
||||
printf(" ** pwm_allowed: pwm is not allowed on hardware\n");
|
||||
rtnval = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return rtnval;
|
||||
}
|
||||
|
||||
void toggle_debug(void)
|
||||
{
|
||||
if (DEBUG) {
|
||||
DEBUG = 0;
|
||||
printf(" ** debug disabled\n");
|
||||
} else {
|
||||
DEBUG = 1;
|
||||
printf(" ** debug enabled\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -91,6 +91,7 @@ int module_setup;
|
||||
int DEBUG;
|
||||
|
||||
int get_xio_base(void);
|
||||
int is_this_chippro(void);
|
||||
int gpio_number(pins_t *pin);
|
||||
int gpio_pud_capable(pins_t *pin);
|
||||
int lookup_gpio_by_key(const char *key);
|
||||
@ -118,3 +119,5 @@ char *get_error_msg(void);
|
||||
void add_error_msg(char *msg);
|
||||
void toggle_debug(void);
|
||||
int compute_port_pin(const char *key, int gpio, int *port, int *pin);
|
||||
int gpio_allowed(int gpio);
|
||||
int pwm_allowed(const char *key);
|
||||
|
@ -84,10 +84,7 @@ void define_constants(PyObject *module)
|
||||
|
||||
bcm = Py_BuildValue("i", BCM);
|
||||
PyModule_AddObject(module, "BCM", bcm);
|
||||
|
||||
module_debug = Py_BuildValue("i", DEBUG ? Py_True: Py_False);
|
||||
PyModule_AddObject(module, "DEBUG", module_debug);
|
||||
|
||||
version = Py_BuildValue("s", "0.3.4.1");
|
||||
version = Py_BuildValue("s", "0.5.2");
|
||||
PyModule_AddObject(module, "VERSION", version);
|
||||
}
|
||||
|
@ -13,6 +13,5 @@ PyObject *version;
|
||||
PyObject *unknown;
|
||||
PyObject *board;
|
||||
PyObject *bcm;
|
||||
PyObject *module_debug;
|
||||
|
||||
void define_constants(PyObject *module);
|
||||
|
@ -486,8 +486,8 @@ int gpio_set_value(int gpio, unsigned int value)
|
||||
strncpy(vstr, "0", ARRAY_SIZE(vstr) - 1);
|
||||
}
|
||||
|
||||
if (DEBUG)
|
||||
printf(" ** gpio_set_value: writing %s **\n", vstr);
|
||||
//if (DEBUG)
|
||||
// printf(" ** gpio_set_value: writing %s **\n", vstr);
|
||||
|
||||
ssize_t s = write(fd, vstr, strlen(vstr)); e_no = errno;
|
||||
|
||||
@ -506,8 +506,7 @@ int gpio_get_value(int gpio, unsigned int *value)
|
||||
int fd = fd_lookup(gpio);
|
||||
char ch;
|
||||
|
||||
if (!fd)
|
||||
{
|
||||
if (!fd) {
|
||||
if ((fd = open_value_file(gpio)) == -1) {
|
||||
char err[256];
|
||||
snprintf(err, sizeof(err), "gpio_get_value: could not open GPIO %d value file", gpio);
|
||||
@ -547,6 +546,58 @@ int gpio_get_value(int gpio, unsigned int *value)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int gpio_get_more(int gpio, int bits, unsigned int *value)
|
||||
{
|
||||
int fd = fd_lookup(gpio);
|
||||
char ch;
|
||||
|
||||
if (!fd) {
|
||||
if ((fd = open_value_file(gpio)) == -1) {
|
||||
char err[256];
|
||||
snprintf(err, sizeof(err), "gpio_get_more: could not open GPIO %d value file", gpio);
|
||||
add_error_msg(err);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// Loop for our number of bits
|
||||
int i;
|
||||
for (i = 0; i < bits; i++) {
|
||||
|
||||
if (lseek(fd, 0, SEEK_SET) < 0) {
|
||||
char err[256];
|
||||
snprintf(err, sizeof(err), "gpio_get_more: could not seek GPIO %d (%s)", gpio, strerror(errno));
|
||||
add_error_msg(err);
|
||||
return -1;
|
||||
}
|
||||
ssize_t s = read(fd, &ch, sizeof(ch));
|
||||
if (s < 0) {
|
||||
char err[256];
|
||||
snprintf(err, sizeof(err), "gpio_get_more: could not read GPIO %d (%s)", gpio, strerror(errno));
|
||||
add_error_msg(err);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ch == '1') {
|
||||
*value |= (1 << i);
|
||||
} else if (ch == '0') {
|
||||
*value |= (0 << i);
|
||||
} else {
|
||||
char err[256];
|
||||
snprintf(err, sizeof(err), "gpio_get_more: unrecognized read GPIO %d (%c)", gpio, ch);
|
||||
add_error_msg(err);
|
||||
return -1;
|
||||
}
|
||||
if (DEBUG) {
|
||||
printf(" ** gpio_get_more: %c **\n", ch);
|
||||
printf(" ** gpio_get_more: current value: %u **\n", *value);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
int gpio_set_edge(int gpio, unsigned int edge)
|
||||
{
|
||||
int fd;
|
||||
|
@ -69,6 +69,7 @@ int gpio_set_direction(int gpio, unsigned int in_flag);
|
||||
int gpio_get_direction(int gpio, unsigned int *value);
|
||||
int gpio_set_value(int gpio, unsigned int value);
|
||||
int gpio_get_value(int gpio, unsigned int *value);
|
||||
int gpio_get_more(int gpio, int bits, unsigned int *value);
|
||||
int fd_lookup(int gpio);
|
||||
int open_value_file(int gpio);
|
||||
|
||||
|
321
source/py_gpio.c
321
source/py_gpio.c
@ -43,6 +43,7 @@ SOFTWARE.
|
||||
#include "event_gpio.h"
|
||||
|
||||
static int gpio_warnings = 1;
|
||||
static int r8_mem_setup = 0;
|
||||
|
||||
int max_gpio = -1;
|
||||
dyn_int_array_t *gpio_direction = NULL;
|
||||
@ -70,13 +71,6 @@ static PyObject *py_toggle_debug(PyObject *self, PyObject *args)
|
||||
static int init_module(void)
|
||||
{
|
||||
clear_error_msg();
|
||||
|
||||
if (map_pio_memory() < 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "init_module error (%s)", get_error_msg());
|
||||
PyErr_SetString(PyExc_RuntimeError, err);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// If we make it here, we're good to go
|
||||
if (DEBUG)
|
||||
@ -86,6 +80,37 @@ static int init_module(void)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int init_r8_gpio_mem(void)
|
||||
{
|
||||
clear_error_msg();
|
||||
|
||||
if (DEBUG)
|
||||
printf(" ** init_r8_gpio_mem: mapping memory **\n");
|
||||
|
||||
if (map_pio_memory() < 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "init_r8_gpio_mem error (%s)", get_error_msg());
|
||||
PyErr_SetString(PyExc_RuntimeError, err);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// If we make it here, we're good to go
|
||||
if (DEBUG)
|
||||
printf(" ** init_r8_gpio_mem: setup complete **\n");
|
||||
r8_mem_setup = 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// python function value = is_chip_pro
|
||||
static PyObject *py_is_chip_pro(PyObject *self, PyObject *args)
|
||||
{
|
||||
PyObject *py_value;
|
||||
|
||||
py_value = Py_BuildValue("i", is_this_chippro());
|
||||
|
||||
return py_value;
|
||||
}
|
||||
|
||||
static void remember_gpio_direction(int gpio, int direction)
|
||||
{
|
||||
@ -118,10 +143,7 @@ static PyObject *py_cleanup(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||
event_cleanup();
|
||||
} else {
|
||||
if (get_gpio_number(channel, &gpio) < 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "Invalid channel %s. (%s)", channel, get_error_msg());
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
event_cleanup();
|
||||
}
|
||||
gpio_unexport(gpio);
|
||||
}
|
||||
@ -133,6 +155,7 @@ static PyObject *py_cleanup(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||
static PyObject *py_setup_channel(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||
{
|
||||
int gpio;
|
||||
int allowed = -1;
|
||||
char *channel;
|
||||
int direction;
|
||||
int pud = PUD_OFF;
|
||||
@ -171,6 +194,26 @@ static PyObject *py_setup_channel(PyObject *self, PyObject *args, PyObject *kwar
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Check to see if GPIO is allowed on the hardware
|
||||
// A 1 means we're good to go
|
||||
allowed = gpio_allowed(gpio);
|
||||
if (allowed == -1) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
} else if (allowed == 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "GPIO %d not available on current Hardware", gpio);
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Only map /dev/mem if we're not an XIO
|
||||
if (!r8_mem_setup && !(gpio >= lookup_gpio_by_name("XIO-P0") && gpio <= lookup_gpio_by_name("XIO-P7"))) {
|
||||
init_r8_gpio_mem();
|
||||
}
|
||||
|
||||
if (gpio_export(gpio) < 0) {
|
||||
char err[2000];
|
||||
@ -222,6 +265,7 @@ static PyObject *py_output_gpio(PyObject *self, PyObject *args)
|
||||
int gpio;
|
||||
int value;
|
||||
char *channel;
|
||||
int allowed = -1;
|
||||
|
||||
clear_error_msg();
|
||||
|
||||
@ -233,6 +277,21 @@ static PyObject *py_output_gpio(PyObject *self, PyObject *args)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Check to see if GPIO is allowed on the hardware
|
||||
// A 1 means we're good to go
|
||||
allowed = gpio_allowed(gpio);
|
||||
if (allowed == -1) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
} else if (allowed == 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "GPIO %d not available on current Hardware", gpio);
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!module_setup || dyn_int_array_get(&gpio_direction, gpio, -1) != OUTPUT)
|
||||
{
|
||||
char err[2000];
|
||||
@ -260,6 +319,7 @@ static PyObject *py_input_gpio(PyObject *self, PyObject *args)
|
||||
char *channel;
|
||||
unsigned int value;
|
||||
PyObject *py_value;
|
||||
int allowed = -1;
|
||||
|
||||
clear_error_msg();
|
||||
|
||||
@ -271,6 +331,21 @@ static PyObject *py_input_gpio(PyObject *self, PyObject *args)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Check to see if GPIO is allowed on the hardware
|
||||
// A 1 means we're good to go
|
||||
allowed = gpio_allowed(gpio);
|
||||
if (allowed == -1) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
} else if (allowed == 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "GPIO %d not available on current Hardware", gpio);
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// check channel is set up as an input or output
|
||||
if (!module_setup || (dyn_int_array_get(&gpio_direction, gpio, -1) == -1))
|
||||
{
|
||||
@ -290,6 +365,115 @@ static PyObject *py_input_gpio(PyObject *self, PyObject *args)
|
||||
return py_value;
|
||||
}
|
||||
|
||||
//TODO: Come up with a way to merge py_read_byte_gpio and py_read_word_gpio
|
||||
// python function value = read_byte(channel)
|
||||
static PyObject *py_read_byte_gpio(PyObject *self, PyObject *args)
|
||||
{
|
||||
int gpio;
|
||||
char *channel;
|
||||
unsigned int value = 0;
|
||||
PyObject *py_value;
|
||||
int allowed = -1;
|
||||
|
||||
clear_error_msg();
|
||||
|
||||
if (!PyArg_ParseTuple(args, "s", &channel))
|
||||
return NULL;
|
||||
|
||||
if (get_gpio_number(channel, &gpio)) {
|
||||
PyErr_SetString(PyExc_ValueError, "Invalid channel");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Check to see if GPIO is allowed on the hardware
|
||||
// A 1 means we're good to go
|
||||
allowed = gpio_allowed(gpio);
|
||||
if (allowed == -1) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
} else if (allowed == 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "GPIO %d not available on current Hardware", gpio);
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// check channel is set up as an input or output
|
||||
if (!module_setup || (dyn_int_array_get(&gpio_direction, gpio, -1) == -1))
|
||||
{
|
||||
PyErr_SetString(PyExc_RuntimeError, "You must setup() the GPIO channel first");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// We only want to get a 8 bits here
|
||||
if (gpio_get_more(gpio, 8, &value) < 0) {
|
||||
char err[1024];
|
||||
snprintf(err, sizeof(err), "Could not get 8 bits of data ('%s')", get_error_msg());
|
||||
PyErr_SetString(PyExc_RuntimeError, err);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
py_value = Py_BuildValue("i", value);
|
||||
|
||||
return py_value;
|
||||
}
|
||||
|
||||
// python function value = read_word(channel)
|
||||
static PyObject *py_read_word_gpio(PyObject *self, PyObject *args)
|
||||
{
|
||||
int gpio;
|
||||
char *channel;
|
||||
unsigned int value = 0;
|
||||
PyObject *py_value;
|
||||
int allowed = -1;
|
||||
|
||||
clear_error_msg();
|
||||
|
||||
if (!PyArg_ParseTuple(args, "s", &channel))
|
||||
return NULL;
|
||||
|
||||
if (get_gpio_number(channel, &gpio)) {
|
||||
PyErr_SetString(PyExc_ValueError, "Invalid channel");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Check to see if GPIO is allowed on the hardware
|
||||
// A 1 means we're good to go
|
||||
allowed = gpio_allowed(gpio);
|
||||
if (allowed == -1) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
} else if (allowed == 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "GPIO %d not available on current Hardware", gpio);
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// check channel is set up as an input or output
|
||||
if (!module_setup || (dyn_int_array_get(&gpio_direction, gpio, -1) == -1))
|
||||
{
|
||||
PyErr_SetString(PyExc_RuntimeError, "You must setup() the GPIO channel first");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// We only want to get a 8 bits here
|
||||
if (gpio_get_more(gpio, 16, &value) < 0) {
|
||||
char err[1024];
|
||||
snprintf(err, sizeof(err), "Could not get 16 bits of data ('%s')", get_error_msg());
|
||||
PyErr_SetString(PyExc_RuntimeError, err);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
py_value = Py_BuildValue("i", value);
|
||||
|
||||
return py_value;
|
||||
}
|
||||
|
||||
static void run_py_callbacks(int gpio, void* data)
|
||||
{
|
||||
PyObject *result;
|
||||
@ -368,6 +552,7 @@ static PyObject *py_add_event_callback(PyObject *self, PyObject *args, PyObject
|
||||
{
|
||||
int gpio;
|
||||
char *channel;
|
||||
int allowed = -1;
|
||||
unsigned int bouncetime = 0;
|
||||
PyObject *cb_func;
|
||||
char *kwlist[] = {"gpio", "callback", "bouncetime", NULL};
|
||||
@ -388,6 +573,21 @@ static PyObject *py_add_event_callback(PyObject *self, PyObject *args, PyObject
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Check to see if GPIO is allowed on the hardware
|
||||
// A 1 means we're good to go
|
||||
allowed = gpio_allowed(gpio);
|
||||
if (allowed == -1) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
} else if (allowed == 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "GPIO %d not available on current Hardware", gpio);
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// check to ensure gpio is one of the allowed pins
|
||||
if (gpio != lookup_gpio_by_name("AP-EINT3")
|
||||
&& gpio != lookup_gpio_by_name("AP-EINT1")
|
||||
@ -422,6 +622,7 @@ static PyObject *py_add_event_detect(PyObject *self, PyObject *args, PyObject *k
|
||||
char *channel;
|
||||
int edge, result;
|
||||
unsigned int bouncetime = 0;
|
||||
int allowed = -1;
|
||||
PyObject *cb_func = NULL;
|
||||
char *kwlist[] = {"gpio", "edge", "callback", "bouncetime", NULL};
|
||||
|
||||
@ -441,6 +642,21 @@ static PyObject *py_add_event_detect(PyObject *self, PyObject *args, PyObject *k
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Check to see if GPIO is allowed on the hardware
|
||||
// A 1 means we're good to go
|
||||
allowed = gpio_allowed(gpio);
|
||||
if (allowed == -1) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
} else if (allowed == 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "GPIO %d not available on current Hardware", gpio);
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// check to ensure gpio is one of the allowed pins
|
||||
if (gpio != lookup_gpio_by_name("AP-EINT3")
|
||||
&& gpio != lookup_gpio_by_name("AP-EINT1")
|
||||
@ -490,6 +706,7 @@ static PyObject *py_remove_event_detect(PyObject *self, PyObject *args)
|
||||
struct py_callback *cb = py_callbacks;
|
||||
struct py_callback *temp;
|
||||
struct py_callback *prev = NULL;
|
||||
int allowed = -1;
|
||||
|
||||
clear_error_msg();
|
||||
|
||||
@ -501,6 +718,21 @@ static PyObject *py_remove_event_detect(PyObject *self, PyObject *args)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Check to see if GPIO is allowed on the hardware
|
||||
// A 1 means we're good to go
|
||||
allowed = gpio_allowed(gpio);
|
||||
if (allowed == -1) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
} else if (allowed == 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "GPIO %d not available on current Hardware", gpio);
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// check to ensure gpio is one of the allowed pins
|
||||
if (gpio != lookup_gpio_by_name("AP-EINT3")
|
||||
&& gpio != lookup_gpio_by_name("AP-EINT1")
|
||||
@ -538,6 +770,7 @@ static PyObject *py_event_detected(PyObject *self, PyObject *args)
|
||||
{
|
||||
int gpio;
|
||||
char *channel;
|
||||
int allowed = -1;
|
||||
|
||||
clear_error_msg();
|
||||
|
||||
@ -549,6 +782,21 @@ static PyObject *py_event_detected(PyObject *self, PyObject *args)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Check to see if GPIO is allowed on the hardware
|
||||
// A 1 means we're good to go
|
||||
allowed = gpio_allowed(gpio);
|
||||
if (allowed == -1) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
} else if (allowed == 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "GPIO %d not available on current Hardware", gpio);
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (event_detected(gpio))
|
||||
Py_RETURN_TRUE;
|
||||
else
|
||||
@ -562,6 +810,7 @@ static PyObject *py_wait_for_edge(PyObject *self, PyObject *args)
|
||||
int edge, result;
|
||||
char *channel;
|
||||
char error[81];
|
||||
int allowed = -1;
|
||||
|
||||
clear_error_msg();
|
||||
|
||||
@ -573,6 +822,21 @@ static PyObject *py_wait_for_edge(PyObject *self, PyObject *args)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Check to see if GPIO is allowed on the hardware
|
||||
// A 1 means we're good to go
|
||||
allowed = gpio_allowed(gpio);
|
||||
if (allowed == -1) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
} else if (allowed == 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "GPIO %d not available on current Hardware", gpio);
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// check to ensure gpio is one of the allowed pins
|
||||
if (gpio != lookup_gpio_by_name("AP-EINT3")
|
||||
&& gpio != lookup_gpio_by_name("AP-EINT1")
|
||||
@ -621,6 +885,7 @@ static PyObject *py_gpio_function(PyObject *self, PyObject *args)
|
||||
unsigned int value;
|
||||
PyObject *func;
|
||||
char *channel;
|
||||
int allowed = -1;
|
||||
|
||||
clear_error_msg();
|
||||
|
||||
@ -632,6 +897,21 @@ static PyObject *py_gpio_function(PyObject *self, PyObject *args)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Check to see if GPIO is allowed on the hardware
|
||||
// A 1 means we're good to go
|
||||
allowed = gpio_allowed(gpio);
|
||||
if (allowed == -1) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
} else if (allowed == 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "GPIO %d not available on current Hardware", gpio);
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (setup_error)
|
||||
{
|
||||
PyErr_SetString(PyExc_RuntimeError, "Module not imported correctly!");
|
||||
@ -819,6 +1099,7 @@ static PyObject *py_set_direction(PyObject *self, PyObject *args, PyObject *kwar
|
||||
int gpio;
|
||||
char *channel;
|
||||
int direction;
|
||||
int allowed = -1;
|
||||
static char *kwlist[] = { "channel", "direction", NULL };
|
||||
|
||||
clear_error_msg();
|
||||
@ -843,6 +1124,21 @@ static PyObject *py_set_direction(PyObject *self, PyObject *args, PyObject *kwar
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Check to see if GPIO is allowed on the hardware
|
||||
// A 1 means we're good to go
|
||||
allowed = gpio_allowed(gpio);
|
||||
if (allowed == -1) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
} else if (allowed == 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "GPIO %d not available on current Hardware", gpio);
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (gpio_set_direction(gpio, direction) < 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "Error setting direction %d on channel %s. (%s)", direction, channel, get_error_msg());
|
||||
@ -860,6 +1156,8 @@ PyMethodDef gpio_methods[] = {
|
||||
{"cleanup", (PyCFunction)py_cleanup, METH_VARARGS | METH_KEYWORDS, "Clean up by resetting all GPIO channels that have been used by this program to INPUT with no pullup/pulldown and no event detection"},
|
||||
{"output", py_output_gpio, METH_VARARGS, "Output to a GPIO channel\ngpio - gpio channel\nvalue - 0/1 or False/True or LOW/HIGH"},
|
||||
{"input", py_input_gpio, METH_VARARGS, "Input from a GPIO channel. Returns HIGH=1=True or LOW=0=False\ngpio - gpio channel"},
|
||||
{"read_byte", py_read_byte_gpio, METH_VARARGS, "Read a byte (8 bits) from a set of GPIO channels. Returns 8-bits of integer data\ngpio - gpio channel."},
|
||||
{"read_word", py_read_word_gpio, METH_VARARGS, "Read a word (16 bits) from a set of GPIO channels. Returns 16-bits of integer data\ngpio - gpio channel."},
|
||||
{"add_event_detect", (PyCFunction)py_add_event_detect, METH_VARARGS | METH_KEYWORDS, "Enable edge detection events for a particular GPIO channel.\nchannel - either board pin number or BCM number depending on which mode is set.\nedge - RISING, FALLING or BOTH\n[callback] - A callback function for the event (optional)\n[bouncetime] - Switch bounce timeout in ms for callback"},
|
||||
{"remove_event_detect", py_remove_event_detect, METH_VARARGS, "Remove edge detection for a particular GPIO channel\ngpio - gpio channel"},
|
||||
{"event_detected", py_event_detected, METH_VARARGS, "Returns True if an edge has occured on a given GPIO. You need to enable edge detection using add_event_detect() first.\ngpio - gpio channel"},
|
||||
@ -872,6 +1170,7 @@ PyMethodDef gpio_methods[] = {
|
||||
{"direction", (PyCFunction)py_set_direction, METH_VARARGS | METH_KEYWORDS, "Change direction of gpio channel. Either INPUT or OUTPUT\n" },
|
||||
{"setmode", (PyCFunction)py_setmode, METH_VARARGS, "Dummy function that does nothing but maintain compatibility with RPi.GPIO\n" },
|
||||
{"toggle_debug", py_toggle_debug, METH_VARARGS, "Toggles the enabling/disabling of Debug print output"},
|
||||
{"is_chip_pro", py_is_chip_pro, METH_VARARGS, "Is hardware a CHIP Pro? Boolean False for normal CHIP/PocketCHIP (R8 SOC)"},
|
||||
{NULL, NULL, 0, NULL}
|
||||
};
|
||||
|
||||
|
123
source/py_pwm.c
123
source/py_pwm.c
@ -52,6 +52,28 @@ static PyObject *py_toggle_debug(PyObject *self, PyObject *args)
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static int init_module(void)
|
||||
{
|
||||
clear_error_msg();
|
||||
|
||||
// If we make it here, we're good to go
|
||||
if (DEBUG)
|
||||
printf(" ** init_module: setup complete **\n");
|
||||
module_setup = 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// python function value = is_chip_pro
|
||||
static PyObject *py_is_chip_pro(PyObject *self, PyObject *args)
|
||||
{
|
||||
PyObject *py_value;
|
||||
|
||||
py_value = Py_BuildValue("i", is_this_chippro());
|
||||
|
||||
return py_value;
|
||||
}
|
||||
|
||||
// python function start(channel, duty_cycle, freq)
|
||||
static PyObject *py_start_channel(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||
{
|
||||
@ -60,6 +82,7 @@ static PyObject *py_start_channel(PyObject *self, PyObject *args, PyObject *kwar
|
||||
float frequency = 2000.0;
|
||||
float duty_cycle = 0.0;
|
||||
int polarity = 0;
|
||||
int allowed = -1;
|
||||
static char *kwlist[] = {"channel", "duty_cycle", "frequency", "polarity", NULL};
|
||||
|
||||
clear_error_msg();
|
||||
@ -68,11 +91,30 @@ static PyObject *py_start_channel(PyObject *self, PyObject *args, PyObject *kwar
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!module_setup) {
|
||||
init_module();
|
||||
}
|
||||
|
||||
if (!get_pwm_key(channel, key)) {
|
||||
PyErr_SetString(PyExc_ValueError, "Invalid PWM key or name.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Check to see if PWM is allowed on the hardware
|
||||
// A 1 means we're good to go
|
||||
allowed = pwm_allowed(key);
|
||||
if (allowed == -1) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
} else if (allowed == 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "PWM %s not available on current Hardware", key);
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (duty_cycle < 0.0 || duty_cycle > 100.0) {
|
||||
PyErr_SetString(PyExc_ValueError, "duty_cycle must have a value from 0.0 to 100.0");
|
||||
return NULL;
|
||||
@ -103,6 +145,7 @@ static PyObject *py_stop_channel(PyObject *self, PyObject *args, PyObject *kwarg
|
||||
{
|
||||
char key[8];
|
||||
char *channel;
|
||||
int allowed = -1;
|
||||
|
||||
clear_error_msg();
|
||||
|
||||
@ -114,6 +157,21 @@ static PyObject *py_stop_channel(PyObject *self, PyObject *args, PyObject *kwarg
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Check to see if PWM is allowed on the hardware
|
||||
// A 1 means we're good to go
|
||||
allowed = pwm_allowed(key);
|
||||
if (allowed == -1) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
} else if (allowed == 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "PWM %s not available on current Hardware", key);
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (pwm_disable(key) < 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "PWM: %s issue: (%s)", channel, get_error_msg());
|
||||
@ -129,6 +187,7 @@ static PyObject *py_set_duty_cycle(PyObject *self, PyObject *args, PyObject *kwa
|
||||
{
|
||||
char key[8];
|
||||
char *channel;
|
||||
int allowed = -1;
|
||||
float duty_cycle = 0.0;
|
||||
static char *kwlist[] = {"channel", "duty_cycle", NULL};
|
||||
|
||||
@ -147,6 +206,21 @@ static PyObject *py_set_duty_cycle(PyObject *self, PyObject *args, PyObject *kwa
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Check to see if PWM is allowed on the hardware
|
||||
// A 1 means we're good to go
|
||||
allowed = pwm_allowed(key);
|
||||
if (allowed == -1) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
} else if (allowed == 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "PWM %s not available on current Hardware", key);
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (pwm_set_duty_cycle(key, duty_cycle) == -1) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "PWM: %s issue: (%s)", channel, get_error_msg());
|
||||
@ -162,6 +236,7 @@ static PyObject *py_set_pulse_width_ns(PyObject *self, PyObject *args, PyObject
|
||||
{
|
||||
char key[8];
|
||||
char *channel;
|
||||
int allowed = -1;
|
||||
unsigned long pulse_width_ns = 0.0;
|
||||
unsigned long period_ns;
|
||||
static char *kwlist[] = {"channel", "pulse_width_ns", NULL};
|
||||
@ -176,6 +251,21 @@ static PyObject *py_set_pulse_width_ns(PyObject *self, PyObject *args, PyObject
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Check to see if PWM is allowed on the hardware
|
||||
// A 1 means we're good to go
|
||||
allowed = pwm_allowed(key);
|
||||
if (allowed == -1) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
} else if (allowed == 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "PWM %s not available on current Hardware", key);
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Get the period out of the data struct
|
||||
int rtn = pwm_get_period_ns(key, &period_ns);
|
||||
if (rtn == -1) {
|
||||
@ -203,6 +293,7 @@ static PyObject *py_set_frequency(PyObject *self, PyObject *args, PyObject *kwar
|
||||
{
|
||||
char key[8];
|
||||
char *channel;
|
||||
int allowed = -1;
|
||||
float frequency = 1.0;
|
||||
static char *kwlist[] = {"channel", "frequency", NULL};
|
||||
|
||||
@ -221,6 +312,21 @@ static PyObject *py_set_frequency(PyObject *self, PyObject *args, PyObject *kwar
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Check to see if PWM is allowed on the hardware
|
||||
// A 1 means we're good to go
|
||||
allowed = pwm_allowed(key);
|
||||
if (allowed == -1) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
} else if (allowed == 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "PWM %s not available on current Hardware", key);
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (pwm_set_frequency(key, frequency) < 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "PWM: %s issue: (%s)", channel, get_error_msg());
|
||||
@ -236,6 +342,7 @@ static PyObject *py_set_period_ns(PyObject *self, PyObject *args, PyObject *kwar
|
||||
{
|
||||
char key[8];
|
||||
char *channel;
|
||||
int allowed = -1;
|
||||
unsigned long period_ns = 2e6;
|
||||
static char *kwlist[] = {"channel", "period_ns", NULL};
|
||||
|
||||
@ -254,6 +361,21 @@ static PyObject *py_set_period_ns(PyObject *self, PyObject *args, PyObject *kwar
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Check to see if PWM is allowed on the hardware
|
||||
// A 1 means we're good to go
|
||||
allowed = pwm_allowed(key);
|
||||
if (allowed == -1) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
} else if (allowed == 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "PWM %s not available on current Hardware", key);
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (pwm_set_period_ns(key, period_ns) < 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "PWM: %s issue: (%s)", channel, get_error_msg());
|
||||
@ -275,6 +397,7 @@ PyMethodDef pwm_methods[] = {
|
||||
{"set_pulse_width_ns", (PyCFunction)py_set_pulse_width_ns, METH_VARARGS, "Change the period\npulse_width_ns - pulse width in nanoseconds" },
|
||||
{"cleanup", py_cleanup, METH_VARARGS, "Clean up by resetting all GPIO channels that have been used by this program to INPUT with no pullup/pulldown and no event detection"},
|
||||
{"toggle_debug", py_toggle_debug, METH_VARARGS, "Toggles the enabling/disabling of Debug print output"},
|
||||
{"is_chip_pro", py_is_chip_pro, METH_VARARGS, "Is hardware a CHIP Pro? Boolean False for normal CHIP/PocketCHIP (R8 SOC)"},
|
||||
{NULL, NULL, 0, NULL}
|
||||
};
|
||||
|
||||
|
349
source/py_servo.c
Normal file
349
source/py_servo.c
Normal file
@ -0,0 +1,349 @@
|
||||
/*
|
||||
Copyright (c) 2017 Robert Wolterman
|
||||
|
||||
Using CHIP_IO servo code from Brady Hurlburt as a basis for the servo code
|
||||
|
||||
Copyright (c) 2016 Brady Hurlburt
|
||||
|
||||
Original BBIO Author Justin Cooper
|
||||
Modified for CHIP_IO Author Brady Hurlburt
|
||||
|
||||
This file incorporates work covered by the following copyright and
|
||||
permission notice, all modified code adopts the original license:
|
||||
|
||||
Copyright (c) 2013 Adafruit
|
||||
Author: Justin Cooper
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
#include "Python.h"
|
||||
#include "constants.h"
|
||||
#include "common.h"
|
||||
#include "c_softservo.h"
|
||||
|
||||
// python function toggle_debug()
|
||||
static PyObject *py_toggle_debug(PyObject *self, PyObject *args)
|
||||
{
|
||||
// toggle debug printing
|
||||
toggle_debug();
|
||||
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
// python function cleanup()
|
||||
static PyObject *py_cleanup(PyObject *self, PyObject *args)
|
||||
{
|
||||
// unexport the Servo
|
||||
servo_cleanup();
|
||||
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static int init_module(void)
|
||||
{
|
||||
clear_error_msg();
|
||||
|
||||
// If we make it here, we're good to go
|
||||
if (DEBUG)
|
||||
printf(" ** init_module: setup complete **\n");
|
||||
module_setup = 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// python function value = is_chip_pro
|
||||
static PyObject *py_is_chip_pro(PyObject *self, PyObject *args)
|
||||
{
|
||||
PyObject *py_value;
|
||||
|
||||
py_value = Py_BuildValue("i", is_this_chippro());
|
||||
|
||||
return py_value;
|
||||
}
|
||||
|
||||
// python function start(channel, angle, range)
|
||||
static PyObject *py_start_channel(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||
{
|
||||
int gpio;
|
||||
char key[8];
|
||||
char *channel = NULL;
|
||||
float angle = 0.0;
|
||||
float range = 180.0;
|
||||
int allowed = -1;
|
||||
static char *kwlist[] = {"channel", "angle", "range", NULL};
|
||||
|
||||
clear_error_msg();
|
||||
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|ff", kwlist, &channel, &angle, &range)) {
|
||||
return NULL;
|
||||
}
|
||||
ASSRT(channel != NULL);
|
||||
|
||||
if (!module_setup) {
|
||||
init_module();
|
||||
}
|
||||
|
||||
if (!get_key(channel, key)) {
|
||||
PyErr_SetString(PyExc_ValueError, "Invalid Servo key or name.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// check to ensure gpio is one of the allowed pins
|
||||
// Not protecting the call as if the get_key() fails, we won't make it here
|
||||
get_gpio_number(channel, &gpio);
|
||||
if ((gpio >= lookup_gpio_by_name("XIO-P0") && gpio <= lookup_gpio_by_name("XIO-P7"))) {
|
||||
PyErr_SetString(PyExc_ValueError, "Servo currently not available on XIO-P0 to XIO-P7");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Check to see if GPIO is allowed on the hardware
|
||||
// A 1 means we're good to go
|
||||
allowed = gpio_allowed(gpio);
|
||||
if (allowed == -1) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
} else if (allowed == 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "GPIO %d not available on current Hardware", gpio);
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (servo_start(key, angle, range) < 0) {
|
||||
printf("servo_start failed");
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "Error starting servo on pin %s (%s)", key, get_error_msg());
|
||||
PyErr_SetString(PyExc_RuntimeError, err);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
// python function stop(channel)
|
||||
static PyObject *py_stop_channel(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||
{
|
||||
int gpio;
|
||||
char key[8];
|
||||
int allowed = -1;
|
||||
char *channel;
|
||||
|
||||
clear_error_msg();
|
||||
|
||||
if (!PyArg_ParseTuple(args, "s", &channel))
|
||||
return NULL;
|
||||
|
||||
if (!get_key(channel, key)) {
|
||||
PyErr_SetString(PyExc_ValueError, "Invalid key or name");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// check to ensure gpio is one of the allowed pins
|
||||
// Not protecting the call as if the get_key() fails, we won't make it here
|
||||
get_gpio_number(channel, &gpio);
|
||||
if ((gpio >= lookup_gpio_by_name("XIO-P0") && gpio <= lookup_gpio_by_name("XIO-P7"))) {
|
||||
PyErr_SetString(PyExc_ValueError, "Servo currently not available on XIO-P0 to XIO-P7");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Check to see if GPIO is allowed on the hardware
|
||||
// A 1 means we're good to go
|
||||
allowed = gpio_allowed(gpio);
|
||||
if (allowed == -1) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
} else if (allowed == 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "GPIO %d not available on current Hardware", gpio);
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
servo_disable(key);
|
||||
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
// python method SERVO.set_range(channel, duty_cycle)
|
||||
static PyObject *py_set_range(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||
{
|
||||
int gpio;
|
||||
char key[8];
|
||||
char *channel;
|
||||
float range = 180.0;
|
||||
int allowed = -1;
|
||||
static char *kwlist[] = {"channel", "range", NULL};
|
||||
|
||||
clear_error_msg();
|
||||
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|f", kwlist, &channel, &range))
|
||||
return NULL;
|
||||
|
||||
if (range > 360.0) {
|
||||
PyErr_SetString(PyExc_ValueError, "range can not be greater than 360.0");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (!get_key(channel, key)) {
|
||||
PyErr_SetString(PyExc_ValueError, "Invalid key or name.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// check to ensure gpio is one of the allowed pins
|
||||
// Not protecting the call as if the get_key() fails, we won't make it here
|
||||
get_gpio_number(channel, &gpio);
|
||||
if ((gpio >= lookup_gpio_by_name("XIO-P0") && gpio <= lookup_gpio_by_name("XIO-P7"))) {
|
||||
PyErr_SetString(PyExc_ValueError, "Servo currently not available on XIO-P0 to XIO-P7");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Check to see if GPIO is allowed on the hardware
|
||||
// A 1 means we're good to go
|
||||
allowed = gpio_allowed(gpio);
|
||||
if (allowed == -1) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
} else if (allowed == 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "GPIO %d not available on current Hardware", gpio);
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (servo_set_range(key, range) == -1) {
|
||||
PyErr_SetString(PyExc_RuntimeError, "You must start() the Servo channel first");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
// python method SERVO.set_angle(channel, duty_cycle)
|
||||
static PyObject *py_set_angle(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||
{
|
||||
int gpio;
|
||||
char key[8];
|
||||
char *channel;
|
||||
float angle = 0.0;
|
||||
int allowed = -1;
|
||||
static char *kwlist[] = {"channel", "angle", NULL};
|
||||
|
||||
clear_error_msg();
|
||||
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|f", kwlist, &channel, &angle))
|
||||
return NULL;
|
||||
|
||||
//if (range > 360.0) {
|
||||
// PyErr_SetString(PyExc_ValueError, "range can not be greater than 360.0");
|
||||
// return NULL;
|
||||
//}
|
||||
|
||||
if (!get_key(channel, key)) {
|
||||
PyErr_SetString(PyExc_ValueError, "Invalid key or name.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// check to ensure gpio is one of the allowed pins
|
||||
// Not protecting the call as if the get_key() fails, we won't make it here
|
||||
get_gpio_number(channel, &gpio);
|
||||
if ((gpio >= lookup_gpio_by_name("XIO-P0") && gpio <= lookup_gpio_by_name("XIO-P7"))) {
|
||||
PyErr_SetString(PyExc_ValueError, "Servo currently not available on XIO-P0 to XIO-P7");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Check to see if GPIO is allowed on the hardware
|
||||
// A 1 means we're good to go
|
||||
allowed = gpio_allowed(gpio);
|
||||
if (allowed == -1) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
} else if (allowed == 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "GPIO %d not available on current Hardware", gpio);
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (servo_set_angle(key, angle) == -1) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "Error setting servo angle on pin %s (%s)", key, get_error_msg());
|
||||
PyErr_SetString(PyExc_RuntimeError, err);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static const char moduledocstring[] = "Software Servo functionality of a CHIP using Python";
|
||||
|
||||
PyMethodDef servo_methods[] = {
|
||||
{"start", (PyCFunction)py_start_channel, METH_VARARGS | METH_KEYWORDS, "Set up and start the Servo. channel can be in the form of 'XIO-P0', or 'U14_13'"},
|
||||
{"stop", (PyCFunction)py_stop_channel, METH_VARARGS | METH_KEYWORDS, "Stop the Servo. channel can be in the form of 'XIO-P0', or 'U14_13'"},
|
||||
{"set_range", (PyCFunction)py_set_range, METH_VARARGS, "Change the servo range\nrange - max angular range of the servo" },
|
||||
{"set_angle", (PyCFunction)py_set_angle, METH_VARARGS, "Change the servo angle\nangle - angle of the servo between +/-(range/2)" },
|
||||
{"cleanup", (PyCFunction)py_cleanup, METH_VARARGS, "Clean up by resetting All or one Servo that have been used by this program."},
|
||||
{"toggle_debug", py_toggle_debug, METH_VARARGS, "Toggles the enabling/disabling of Debug print output"},
|
||||
{"is_chip_pro", py_is_chip_pro, METH_VARARGS, "Is hardware a CHIP Pro? Boolean False for normal CHIP/PocketCHIP (R8 SOC)"},
|
||||
{NULL, NULL, 0, NULL}
|
||||
};
|
||||
|
||||
#if PY_MAJOR_VERSION > 2
|
||||
static struct PyModuleDef chipservomodule = {
|
||||
PyModuleDef_HEAD_INIT,
|
||||
"SERVO", // name of module
|
||||
moduledocstring, // module documentation, may be NULL
|
||||
-1, // size of per-interpreter state of the module, or -1 if the module keeps state in global variables.
|
||||
servo_methods
|
||||
};
|
||||
#endif
|
||||
|
||||
#if PY_MAJOR_VERSION > 2
|
||||
PyMODINIT_FUNC PyInit_SERVO(void)
|
||||
#else
|
||||
PyMODINIT_FUNC initSERVO(void)
|
||||
#endif
|
||||
{
|
||||
PyObject *module = NULL;
|
||||
|
||||
#if PY_MAJOR_VERSION > 2
|
||||
if ((module = PyModule_Create(&chipservomodule)) == NULL)
|
||||
return NULL;
|
||||
#else
|
||||
if ((module = Py_InitModule3("SERVO", servo_methods, moduledocstring)) == NULL)
|
||||
return;
|
||||
#endif
|
||||
|
||||
define_constants(module);
|
||||
|
||||
|
||||
#if PY_MAJOR_VERSION > 2
|
||||
return module;
|
||||
#else
|
||||
return;
|
||||
#endif
|
||||
}
|
@ -43,34 +43,37 @@ static PyObject *py_toggle_debug(PyObject *self, PyObject *args)
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
// python function cleanup(channel=None)
|
||||
// python function cleanup()
|
||||
static PyObject *py_cleanup(PyObject *self, PyObject *args)
|
||||
{
|
||||
// unexport the PWM
|
||||
char key[8];
|
||||
char *channel = NULL;
|
||||
|
||||
clear_error_msg();
|
||||
|
||||
// Channel is optional
|
||||
if (!PyArg_ParseTuple(args, "|s", &channel))
|
||||
return NULL;
|
||||
|
||||
// The !channel fixes issue #50
|
||||
if (channel == NULL || strcmp(channel, "\0") == 0) {
|
||||
softpwm_cleanup();
|
||||
return NULL;
|
||||
} else {
|
||||
if (!get_key(channel, key)) {
|
||||
PyErr_SetString(PyExc_ValueError, "Invalid SOFTPWM key or name.");
|
||||
return NULL;
|
||||
}
|
||||
softpwm_disable(key);
|
||||
}
|
||||
softpwm_cleanup();
|
||||
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static int init_module(void)
|
||||
{
|
||||
clear_error_msg();
|
||||
|
||||
// If we make it here, we're good to go
|
||||
if (DEBUG)
|
||||
printf(" ** init_module: setup complete **\n");
|
||||
module_setup = 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// python function value = is_chip_pro
|
||||
static PyObject *py_is_chip_pro(PyObject *self, PyObject *args)
|
||||
{
|
||||
PyObject *py_value;
|
||||
|
||||
py_value = Py_BuildValue("i", is_this_chippro());
|
||||
|
||||
return py_value;
|
||||
}
|
||||
|
||||
// python function start(channel, duty_cycle, freq)
|
||||
static PyObject *py_start_channel(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||
{
|
||||
@ -79,6 +82,8 @@ static PyObject *py_start_channel(PyObject *self, PyObject *args, PyObject *kwar
|
||||
float frequency = 2000.0;
|
||||
float duty_cycle = 0.0;
|
||||
int polarity = 0;
|
||||
int gpio;
|
||||
int allowed = -1;
|
||||
static char *kwlist[] = {"channel", "duty_cycle", "frequency", "polarity", NULL};
|
||||
|
||||
clear_error_msg();
|
||||
@ -88,11 +93,34 @@ static PyObject *py_start_channel(PyObject *self, PyObject *args, PyObject *kwar
|
||||
}
|
||||
ASSRT(channel != NULL);
|
||||
|
||||
if (!module_setup) {
|
||||
init_module();
|
||||
}
|
||||
|
||||
if (!get_key(channel, key)) {
|
||||
PyErr_SetString(PyExc_ValueError, "Invalid SOFTPWM key or name.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// check to ensure gpio is one of the allowed pins
|
||||
// Not protecting the call as if the get_key() fails, we won't make it here
|
||||
get_gpio_number(channel, &gpio);
|
||||
|
||||
// Check to see if GPIO is allowed on the hardware
|
||||
// A 1 means we're good to go
|
||||
allowed = gpio_allowed(gpio);
|
||||
if (allowed == -1) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
} else if (allowed == 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "GPIO %d not available on current Hardware", gpio);
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (duty_cycle < 0.0 || duty_cycle > 100.0) {
|
||||
PyErr_SetString(PyExc_ValueError, "duty_cycle must have a value from 0.0 to 100.0");
|
||||
return NULL;
|
||||
@ -124,6 +152,8 @@ static PyObject *py_stop_channel(PyObject *self, PyObject *args, PyObject *kwarg
|
||||
{
|
||||
char key[8];
|
||||
char *channel;
|
||||
int gpio;
|
||||
int allowed = -1;
|
||||
|
||||
clear_error_msg();
|
||||
|
||||
@ -135,6 +165,25 @@ static PyObject *py_stop_channel(PyObject *self, PyObject *args, PyObject *kwarg
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// check to ensure gpio is one of the allowed pins
|
||||
// Not protecting the call as if the get_key() fails, we won't make it here
|
||||
get_gpio_number(channel, &gpio);
|
||||
|
||||
// Check to see if GPIO is allowed on the hardware
|
||||
// A 1 means we're good to go
|
||||
allowed = gpio_allowed(gpio);
|
||||
if (allowed == -1) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
} else if (allowed == 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "GPIO %d not available on current Hardware", gpio);
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
softpwm_disable(key);
|
||||
|
||||
Py_RETURN_NONE;
|
||||
@ -145,6 +194,8 @@ static PyObject *py_set_duty_cycle(PyObject *self, PyObject *args, PyObject *kwa
|
||||
{
|
||||
char key[8];
|
||||
char *channel;
|
||||
int gpio;
|
||||
int allowed = -1;
|
||||
float duty_cycle = 0.0;
|
||||
static char *kwlist[] = {"channel", "duty_cycle", NULL};
|
||||
|
||||
@ -163,6 +214,25 @@ static PyObject *py_set_duty_cycle(PyObject *self, PyObject *args, PyObject *kwa
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// check to ensure gpio is one of the allowed pins
|
||||
// Not protecting the call as if the get_key() fails, we won't make it here
|
||||
get_gpio_number(channel, &gpio);
|
||||
|
||||
// Check to see if GPIO is allowed on the hardware
|
||||
// A 1 means we're good to go
|
||||
allowed = gpio_allowed(gpio);
|
||||
if (allowed == -1) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
} else if (allowed == 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "GPIO %d not available on current Hardware", gpio);
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (softpwm_set_duty_cycle(key, duty_cycle) == -1) {
|
||||
PyErr_SetString(PyExc_RuntimeError, "You must start() the PWM channel first");
|
||||
return NULL;
|
||||
@ -176,6 +246,8 @@ static PyObject *py_set_frequency(PyObject *self, PyObject *args, PyObject *kwar
|
||||
{
|
||||
char key[8];
|
||||
char *channel;
|
||||
int gpio;
|
||||
int allowed = -1;
|
||||
float frequency = 1.0;
|
||||
static char *kwlist[] = {"channel", "frequency", NULL};
|
||||
|
||||
@ -194,6 +266,25 @@ static PyObject *py_set_frequency(PyObject *self, PyObject *args, PyObject *kwar
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// check to ensure gpio is one of the allowed pins
|
||||
// Not protecting the call as if the get_key() fails, we won't make it here
|
||||
get_gpio_number(channel, &gpio);
|
||||
|
||||
// Check to see if GPIO is allowed on the hardware
|
||||
// A 1 means we're good to go
|
||||
allowed = gpio_allowed(gpio);
|
||||
if (allowed == -1) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "Error determining hardware. (%s)", get_error_msg());
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
} else if (allowed == 0) {
|
||||
char err[2000];
|
||||
snprintf(err, sizeof(err), "GPIO %d not available on current Hardware", gpio);
|
||||
PyErr_SetString(PyExc_ValueError, err);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (softpwm_set_frequency(key, frequency) == -1) {
|
||||
PyErr_SetString(PyExc_RuntimeError, "You must start() the PWM channel first");
|
||||
return NULL;
|
||||
@ -211,6 +302,7 @@ PyMethodDef pwm_methods[] = {
|
||||
{"set_frequency", (PyCFunction)py_set_frequency, METH_VARARGS, "Change the frequency\nfrequency - frequency in Hz (freq > 0.0)" },
|
||||
{"cleanup", (PyCFunction)py_cleanup, METH_VARARGS, "Clean up by resetting all GPIO channels that have been used by this program to INPUT with no pullup/pulldown and no event detection"},
|
||||
{"toggle_debug", py_toggle_debug, METH_VARARGS, "Toggles the enabling/disabling of Debug print output"},
|
||||
{"is_chip_pro", py_is_chip_pro, METH_VARARGS, "Is hardware a CHIP Pro? Boolean False for normal CHIP/PocketCHIP (R8 SOC)"},
|
||||
{NULL, NULL, 0, NULL}
|
||||
};
|
||||
|
||||
|
@ -1,5 +1,3 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import CHIP_IO.LRADC as ADC
|
||||
|
||||
# == ENABLE DEBUG ==
|
71
test/servotest.py
Normal file
71
test/servotest.py
Normal file
@ -0,0 +1,71 @@
|
||||
import CHIP_IO.SERVO as SERVO
|
||||
import CHIP_IO.GPIO as GPIO
|
||||
import time
|
||||
import datetime
|
||||
import threading
|
||||
|
||||
class ServoTestReceiver(threading.Thread):
|
||||
def __init__(self,gpio,key,maxcount=20,sleeptime=0.005):
|
||||
self.gpio = gpio
|
||||
self.key = key
|
||||
self.counter = 0
|
||||
self.maxcount = maxcount
|
||||
self.sleeptime = sleeptime
|
||||
self.dead = False
|
||||
threading.Thread.__init__(self)
|
||||
|
||||
def kill(self):
|
||||
self.dead = True
|
||||
|
||||
def run(self):
|
||||
print("SETTING UP RECEIVER GPIO")
|
||||
self.gpio.cleanup()
|
||||
self.gpio.setup(self.key, self.gpio.IN)
|
||||
print("STARTING RECEIVE LOOP")
|
||||
try:
|
||||
#while self.counter < self.maxcount:
|
||||
while not self.dead:
|
||||
pwmval = self.gpio.input(self.key)
|
||||
print("SERVO VALUE: {0} @ {1}".format(pwmval, datetime.datetime.now()))
|
||||
time.sleep(self.sleeptime)
|
||||
self.counter += 1
|
||||
except KeyboardInterrupt:
|
||||
self.gpio.cleanup(self.key)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# SETUP VARIABLES
|
||||
SERVOGPIO = "CSID1" #"XIO-P7"
|
||||
RECEIVERGPIO = "CSID7"
|
||||
COUNT = 120
|
||||
SLEEPTIME = 0.0001
|
||||
RANGE = 180
|
||||
|
||||
# SETUP PWM
|
||||
try:
|
||||
print("SERVO START")
|
||||
SERVO.toggle_debug()
|
||||
SERVO.start(SERVOGPIO, 0, RANGE)
|
||||
|
||||
# SETUP SERVO RECEIVER
|
||||
#rcvr = ServoTestReceiver(GPIO, RECEIVERGPIO, COUNT, SLEEPTIME)
|
||||
#rcvr.start()
|
||||
|
||||
# LOOP THROUGH RANGE
|
||||
for i in range(-(RANGE/2),(RANGE/2),5):
|
||||
print("SETTING ANGLE: {0}".format(i))
|
||||
SERVO.set_angle(SERVOGPIO, i)
|
||||
time.sleep(1)
|
||||
|
||||
#rcvr.kill()
|
||||
raw_input("PRESS ENTER WHEN DONE")
|
||||
|
||||
except:
|
||||
raise
|
||||
finally:
|
||||
# CLEANUP
|
||||
print("CLEANUP")
|
||||
SERVO.stop(SERVOGPIO)
|
||||
SERVO.cleanup(SERVOGPIO)
|
||||
|
||||
|
Reference in New Issue
Block a user