-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
171 lines (120 loc) · 3.67 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import numpy as np
import time
import h5py
import matplotlib.pyplot as plt
from scipy.stats import linregress
import os
from datetime import datetime
"""
# Example enter to exit loop
for i in range(5):
print("if pin %d is connected to OP %d" % (i, i))
answer = (input("Enter anything to continue (press e to exit): " ))
if str(answer) == 'e':
break
else:
pass
print(i)
exit()
"""
#
""" # Pin 1-op4
input_v_offset_grad = 0.03542 # 0.03579
input_v_offset_constant = 0.339 # 0.335 # 0.33 # 0.35 # voltage offset at input electrodes (used when scaling)
"""
# # Location
location = "mod_software/calibrate_data.hdf5"
# # save data
if os.path.isfile(location) == False:
with h5py.File(location, 'a') as hdf:
G_sub = hdf.create_group("Input_Offset")
G_sub.create_dataset('Constant', data=np.zeros(16))
G_sub.create_dataset('Gradient', data=np.zeros(16))
G_sub2 = hdf.create_group("Output_Offset")
G_sub2.create_dataset('Constant', data=np.zeros(4))
G_sub2.create_dataset('Gradient', data=np.zeros(4))
exit()
with h5py.File(location, 'r') as hdf:
InOffset_C_bias = np.array(hdf.get('/Input_Offset/Constant'))
InOffset_G_bias = np.array(hdf.get('/Input_Offset/Gradient'))
print(InOffset_C_bias)
print(InOffset_G_bias)
exit()
# # Make a Change
with h5py.File(location, 'r+') as hdf:
InOffset_C_bias = hdf.get('/Input_Offset/Constant')
InOffset_C_bias[:] = np.ones(16)
InOffset_C_bias[1] = 20
InOffset_G_bias = hdf.get('/Input_Offset/Gradient')
InOffset_G_bias[:] = np.ones(16)
OutOffset_C_bias = hdf.get('/Output_Offset/Constant')
OutOffset_C_bias[:] = np.ones(4)
OutOffset_G_bias = hdf.get('/Output_Offset/Gradient')
OutOffset_G_bias[:] = np.ones(4)
with h5py.File(location, 'r') as hdf:
InOffset_C_bias = np.array(hdf.get('/Input_Offset/Constant'))
InOffset_G_bias = np.array(hdf.get('/Input_Offset/Gradient'))
print(InOffset_C_bias)
print(InOffset_G_bias)
exit()
#
# ###########################
#
#Reading the MCP3008 analog input channels and printing them.
import time
#import SPI library (for hardware SPI) and MCP3008 library.
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008
# Software SPI configuration:
# CLK = 18
# MISO = 23
# MOSI = 24
# CS = 25
# mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI)
# Hardware SPI configuration:
SPI_PORT = 0
SPI_DEVICE = 0
mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))
print('Reading MCP3008 values, press Ctrl-C to quit...')
# Print nice channel column headers.
print('| {0:>4} | {1:>4} | {2:>4} | {3:>4} |'.format(*range(4)))
print('-' * 28)
# Main program loop.
while True:
# Read all the ADC channel values in a list.
values = [0]*4
for i in range(4):
# The read_adc function will get the value of the specified channel (0-3).
values[i] = mcp.read_adc(i)
# Print the ADC values.
print('| {0:>4} | {1:>4} | {2:>4} | {3:>4} |'.format(*values))
# Pause for half a second.
time.sleep(0.5)
#
# ######################
#
#!/usr/bin/env python
import time
import pigpio
LOOPS=10000
CHANNELS=4
def read_MCP3008(adc, channel):
count, data = pi.spi_xfer(adc, [1, (8+channel)<<4, 0])
value = ((data[1] << 8) | data[2]) & 0x3FF
return value
pi = pigpio.pi()
if not pi.connected:
exit()
adc = pi.spi_open(0, 1e6) # CE0
start = time.time()
for i in range(LOOPS):
for c in range(CHANNELS):
v = read_MCP3008(adc, c)
finish = time.time()
reads = LOOPS * CHANNELS
runtime = finish - start
print("did {} reads in {:.1f} seconds ({}sps)".
format(reads, runtime, int(reads/runtime)))
pi.spi_close(adc)
pi.stop()
# fin