node-red-contrib-waveshare-shield-adda-11010
Loading...
Searching...
No Matches
gpio.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# gpio.py
3#
4# Copyright (c) 2020-2022 Spring City Solutions LLC and other contributors
5# Licensed under the Apache License, Version 2.0
6#
7# Need to have the rpi.gpio python library installed
8#
9# For development, need to install the pytest system.
10# sudo apt-get install python3-pytest
11# See also tests/gpio_test.py
12# Running 'python -m pytest' should result in no errors
13#
14# For development, need to install the flake8 system.
15# sudo apt-get install python3-flake8
16# Running flake8 should report no serious problems,
17# with E501 being ignored and excluding tests/
18#
19# Some notes on wiring:
20# Labeled header P22 is connected to physical pin 31 connected to BCM port 6
21# Labeled header P23 is connected to physical pin 33 connected to BCM port 13
22# Labeled header P24 is connected to physical pin 35 connected to BCM port 19
23# Labeled header P25 is connected to physical pin 37 connected to BCM port 26
24#
25# Hardware Links:
26#
27# Board Manufacturer Store:
28# https://www.waveshare.com/High-Precision-AD-DA-Board.htm
29#
30# Board Manufacturer Wiki:
31# https://www.waveshare.com/wiki/High-Precision_AD/DA_Board
32#
33# Links for ADS1256 chip:
34# https://www.ti.com/product/ADS1256
35# https://www.ti.com/lit/gpn/ads1256
36# Book "Fundamentals of Precision ADC Noise Analysis" https://www.ti.com/lit/pdf/slyy192
37# Journal Series "How delta-sigma ADCs work" https://www.ti.com/lit/pdf/slyt423 https://www.ti.com/lit/pdf/slyt438
38# App Note "Digital Filter Types in Delta-Sigma ADCs" https://www.ti.com/lit/pdf/sbaa230
39#
40# Link for LM285 chip, connected to the ADS1256:
41# https://www.ti.com/product/LM285-2.5-N
42# https://www.ti.com/lit/gpn/lm285-2.5-n
43#
44# Link for DAC8532 chip:
45# https://www.ti.com/product/DAC8532
46# https://www.ti.com/lit/gpn/dac8532
47#
48# Spring City Solutions Links:
49#
50# Spring City Solutions Node-RED project page:
51# https://www.springcitysolutions.com/nodered
52#
53# Spring City Solutions page for this node:
54# https://www.springcitysolutions.com/nodered-waveshare-adda-shield
55#
56# Spring City Solutions Node-RED project email:
57# nodered@springcitysolutions.com
58#
59# Spring City Solutions Gitlab for this node:
60# https://gitlab.com/SpringCitySolutionsLLC/waveshare-shield-adda-11010
61#
62# Patreon Page:
63# https://www.patreon.com/springcitysolutions_nodered
64#
65# Software Links:
66#
67# Spring City Solutions Gitlab for this node:
68# https://gitlab.com/SpringCitySolutionsLLC/waveshare-shield-adda-11010
69#
70# Doxygen docs for this node autogenerated by Gitlab CI/CD:
71# https://springcitysolutionsllc.gitlab.io/waveshare-shield-adda-11010/index.html
72#
73# npmjs for this node:
74# https://npmjs.org/package/node-red-contrib-waveshare-shield-adda-11010
75#
76# Node-RED flows for this node:
77# https://flows.nodered.org/node/node-red-contrib-waveshare-shield-adda-11010
78#
79# Documentation Links:
80#
81# Gitlab wiki for this node (the master copy of list of links is here):
82# https://gitlab.com/SpringCitySolutionsLLC/waveshare-shield-adda-11010/-/wikis/home
83#
84# Waveshare's libraries for this hardware:
85# https://github.com/waveshare/High-Precision-AD-DA-Board
86#
87# Youtube video "How to set up":
88# TODO
89#
90# Youtube video "How to use":
91# TODO
92#
93# Youtube video "Testing Results":
94# TODO
95#
96# Usage:
97#
98# Combine any of the following options:
99#
100# gpio.py -t
101# Returns a JSON of the register data for use with pytest
102#
103# gpio.py --pin "default P22"
104# Sets port to one of the following options:
105# P22, P23, P24, P25
106#
107# gpio.py --output
108# No output option means input mode; prints a 0 or 1 depending on port input
109# Specifying 0 or 1 as an output puts the port into output mode and sets the port to 0 or 1
110#
111# Note, if you have an open circuit unconnected port, and
112# output a 1, then run a input a few times, the physical wiring
113# has some capacitance and will input a 1 for the first
114# read, but later reads will read as 0.
115# It probably doesn't matter in normal operation, but it looks weird.
116#
117
118import argparse
119import json
120import sys
121
122import RPi.GPIO as GPIO
123
124register_data = dict([('pin', 0), ('output', 0)])
125
126
127# Functions that do things.
128
129
131 """Prints a JSON of the register_data for development and testing."""
132 print(json.dumps(register_data, sort_keys=True, indent=4))
133
134
135def parse_args(raw_args):
136 """Parses the args into ready to write register_data."""
137 # First parse the CLI.
138 parser = argparse.ArgumentParser()
139 parser.add_argument("--pin", type=str, default="P22",
140 help="Pin: P22, P23, P24, P25 default P22")
141 parser.add_argument("--output", type=str, default="-1",
142 help="Providing this puts the pin in output mode instead of input mode. Output: 0, 1")
143 parser.add_argument("-t", "--test", action="store_true",
144 help="Print JSON of register_data")
145 args = parser.parse_args(raw_args)
146
147 # Process pin selection
148 register_data['pin'] = 6
149 if (args.pin == 'P22'):
150 register_data['pin'] = 6
151 if (args.pin == 'P23'):
152 register_data['pin'] = 13
153 if (args.pin == 'P24'):
154 register_data['pin'] = 19
155 if (args.pin == 'P25'):
156 register_data['pin'] = 26
157
158 # Process output, which also selects input or output mode
159 register_data['output'] = -1
160 if (args.output == '0'):
161 register_data['output'] = 0
162 if (args.output == '1'):
163 register_data['output'] = 1
164
165 # Optionally output register state for development purposes.
166 if args.test:
168 exit()
169
170
171# Functions that read or write device
172
173
174def config():
175 """Configure the port mode"""
176 GPIO.setmode(GPIO.BCM)
177 GPIO.setwarnings(False)
178
179 if (register_data['output'] == -1):
180 GPIO.setup(register_data['pin'], GPIO.IN)
181 print(GPIO.input(register_data['pin']))
182
183 if (register_data['output'] != -1):
184 GPIO.setup(register_data['pin'], GPIO.OUT)
185 GPIO.output(register_data['pin'], register_data['output'])
186
187
188# Convert CLI options to register_data.
189parse_args(sys.argv[1:])
190
191# Configure the pin
192config()
parse_args(raw_args)
Definition gpio.py:135
output_json()
Definition gpio.py:130
config()
Definition gpio.py:174