-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuiltInSensor.py
170 lines (143 loc) · 4.92 KB
/
BuiltInSensor.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
from time import sleep_ms
import neopixel
from machine import Pin, ADC, PWM, SoftI2C
from micropython import const
BMP280_I2C_ADDR = const(0x76)
class BMP280():
def __init__(self, i2c):
self.i2c = i2c
self.dig_T1 = self.get2Reg(0x88)
self.dig_T2 = self.short(self.get2Reg(0x8A))
self.dig_T3 = self.short(self.get2Reg(0x8C))
self.dig_P1 = self.get2Reg(0x8E)
self.dig_P2 = self.short(self.get2Reg(0x90))
self.dig_P3 = self.short(self.get2Reg(0x92))
self.dig_P4 = self.short(self.get2Reg(0x94))
self.dig_P5 = self.short(self.get2Reg(0x96))
self.dig_P6 = self.short(self.get2Reg(0x98))
self.dig_P7 = self.short(self.get2Reg(0x9A))
self.dig_P8 = self.short(self.get2Reg(0x9C))
self.dig_P9 = self.short(self.get2Reg(0x9E))
self.mode = 3
self.osrs_p = 3
self.osrs_t = 1
self.setReg(0xF4, 0x2F)
self.setReg(0xF5, 0x0C)
self.filter = 3
self.T = 0
self.P = 0
self.version = '1.0'
def short(self, dat):
if dat > 32767:
return dat - 65536
else:
return dat
# set reg
def setReg(self, reg, dat):
self.i2c.writeto(BMP280_I2C_ADDR, bytearray([reg, dat]))
# get reg
def getReg(self, reg):
self.i2c.writeto(BMP280_I2C_ADDR, bytearray([reg]))
t = self.i2c.readfrom(BMP280_I2C_ADDR, 1)
return t[0]
# get two reg
def get2Reg(self, reg):
self.i2c.writeto(BMP280_I2C_ADDR, bytearray([reg]))
t = self.i2c.readfrom(BMP280_I2C_ADDR, 2)
return t[0] + t[1] * 256
def get(self):
adc_T = (self.getReg(0xFA) << 12) + (self.getReg(0xFB) << 4) + (self.getReg(0xFC) >> 4)
var1 = (((adc_T >> 3) - (self.dig_T1 << 1)) * self.dig_T2) >> 11
var2 = (((((adc_T >> 4) - self.dig_T1) * ((adc_T >> 4) - self.dig_T1)) >> 12) * self.dig_T3) >> 14
t = var1 + var2
self.T = ((t * 5 + 128) >> 8) / 100
var1 = (t >> 1) - 64000
var2 = (((var1 >> 2) * (var1 >> 2)) >> 11) * self.dig_P6
var2 = var2 + ((var1 * self.dig_P5) << 1)
var2 = (var2 >> 2) + (self.dig_P4 << 16)
var1 = (((self.dig_P3 * ((var1 >> 2) * (var1 >> 2)) >> 13) >> 3) + (((self.dig_P2) * var1) >> 1)) >> 18
var1 = ((32768 + var1) * self.dig_P1) >> 15
if var1 == 0:
return # avoid exception caused by division by zero
adc_P = (self.getReg(0xF7) << 12) + (self.getReg(0xF8) << 4) + (self.getReg(0xF9) >> 4)
p = ((1048576 - adc_P) - (var2 >> 12)) * 3125
if p < 0x80000000:
p = (p << 1) // var1
else:
p = (p // var1) * 2
var1 = (self.dig_P9 * (((p >> 3) * (p >> 3)) >> 13)) >> 12
var2 = (((p >> 2)) * self.dig_P8) >> 13
self.P = p + ((var1 + var2 + self.dig_P7) >> 4)
return [self.T, self.P]
# get Temperature in Celsius
def getTemp(self):
self.get()
return self.T
# get Pressure in Pa
def getPress(self):
self.get()
return self.P
# Calculating absolute altitude
def getAltitude(self):
return '%.2f' % (44330 * (1 - (self.getPress() / 101325) ** (1 / 5.256)))
# sleep mode
def poweroff(self):
self.setReg(0xF4, 0)
# normal mode
def poweron(self):
self.setReg(0xF4, 0x2F)
# RGB
p_rgb = Pin(23, Pin.OUT)
n = neopixel.NeoPixel(p_rgb, 3)
def RGB(num, rgb):
for i in range(len(num)):
if num[i] == 0:
num[i] = 2
elif num[i] == 2:
num[i] = 0
if type(rgb) == list:
for i in num:
n[i] = (rgb[0], rgb[1], rgb[2])
n.write()
elif type(rgb) == str:
if rgb == "blue":
for i in num:
n[i] = (0, 0, 255)
n.write()
elif rgb == "red":
for i in num:
n[i] = (255, 0, 0)
n.write()
elif rgb == "green":
for i in num:
n[i] = (0, 255, 0)
n.write()
elif rgb == "off":
for i in num:
n[i] = (0, 0, 0)
n.write()
class read_atmos(object):
def __init__(self):
self.i2c = SoftI2C(scl=Pin(22), sda=Pin(21))
self.bmp_sensor = BMP280(self.i2c)
def read(self):
return self.bmp_sensor.getPress()
class read_light(object):
def __init__(self):
self.s_pin = Pin(34, mode=Pin.IN, pull=None)
self.s_adc = ADC(self.s_pin)
self.s_adc.width(ADC.WIDTH_12BIT)
self.s_adc.atten(ADC.ATTN_11DB)
def read(self):
return self.s_adc.read()
class play_tone(object):
def __init__(self):
self.pwm = PWM(Pin(19, Pin.OUT))
self.pwm.duty(0)
def play(self, melodies, delay=150, duty=512):
for note in melodies:
if note:
self.pwm.freq(note)
self.pwm.duty(duty)
sleep_ms(delay)
self.pwm.duty(0)