node-red-contrib-waveshare-shield-adda-11010
Loading...
Searching...
No Matches
dac8532 Namespace Reference

Functions

 output_json ()
 
 parse_args (raw_args)
 
 write ()
 

Variables

int cs = 23
 
 register_data = dict([('control', 0x00), ('data_h', 0x00), ('data_l', 0x00)])
 

Function Documentation

◆ output_json()

dac8532.output_json ( )
Prints a JSON of the register_data for development and testing.

Definition at line 123 of file dac8532.py.

123def output_json():
124 """Prints a JSON of the register_data for development and testing."""
125 print(json.dumps(register_data, sort_keys=True, indent=4))
126
127

◆ parse_args()

dac8532.parse_args ( raw_args)
Parses the args into ready to write register_data.

Definition at line 128 of file dac8532.py.

128def parse_args(raw_args):
129 """Parses the args into ready to write register_data."""
130 # First parse the CLI.
131 parser = argparse.ArgumentParser()
132 parser.add_argument("--port", type=str, default="A",
133 help="Output Port: A, B default A")
134 parser.add_argument("--output", type=str, default="32767",
135 help="Output Voltage: 0 to 65535 default 32767")
136 parser.add_argument("-t", "--test", action="store_true",
137 help="Print JSON of register_data")
138 args = parser.parse_args(raw_args)
139
140 # Process the control register
141 register_data['control'] = 0x30
142 if (args.port == "A"):
143 register_data['control'] = 0x30
144 if (args.port == "B"):
145 register_data['control'] = 0x34
146
147 # Process the data registers
148 register_data['data_h'] = 0x00
149 register_data['data_l'] = 0x00
150 raw_output = int(args.output)
151 if (raw_output < 0):
152 raw_output = 0
153 if (raw_output > 65535):
154 raw_output = 65535
155 register_data['data_h'] = raw_output >> 8
156 register_data['data_l'] = raw_output & 0xFF
157
158 # Optionally output register state for development purposes
159 if args.test:
160 output_json()
161 exit()
162
163
164# Functions that read or write device registers
165
166

◆ write()

dac8532.write ( )
Writes to the DAC using SPI

Definition at line 167 of file dac8532.py.

167def write():
168 """Writes to the DAC using SPI"""
169
170 GPIO.setmode(GPIO.BCM)
171 GPIO.setwarnings(False)
172 GPIO.setup(cs, GPIO.OUT)
173 GPIO.output(cs, 1)
174
175 spi = spidev.SpiDev()
176 spi.open(0, 0)
177 spi.max_speed_hz = 20000
178 spi.mode = 0b01
179
180 GPIO.output(cs, 0)
181 spi.writebytes([register_data['control'], register_data['data_h'], register_data['data_l']])
182 GPIO.output(cs, 1)
183
184
185# Convert CLI options to register_data

Variable Documentation

◆ cs

int dac8532.cs = 23

Definition at line 115 of file dac8532.py.

◆ register_data

dac8532.register_data = dict([('control', 0x00), ('data_h', 0x00), ('data_l', 0x00)])

Definition at line 117 of file dac8532.py.