-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·173 lines (139 loc) · 3.82 KB
/
main.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
172
173
import gc
gc.collect()
import board
gc.collect()
import busio as io
gc.collect()
import adafruit_ssd1306
gc.collect()
import bitmapfont
gc.collect()
import time
gc.collect()
import adafruit_bmp280
gc.collect()
#import gfx
#gc.collect()
# needed?
#import framebuf
#gc.collect()0
import pulseio
gc.collect()
# debug mode
debug = False
# init I2c on board
i2c = io.I2C(board.SCL, board.SDA)
gc.collect()
## BMP280 ##
sensor = adafruit_bmp280.Adafruit_BMP280_I2C(i2c)
gc.collect()
## OLED/ssd1306 ##
oled = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c, addr=0x3c)
gc.collect()
## Piezo
# tones to play
TONE_FREQ = [ 262, # C4
294, # D4
330, # E4
#349, # F4
#392, # G4
440, # A4
#494, # B4
262, # C3
530, ] # C4
buzzer = pulseio.PWMOut(board.D4, variable_frequency=True)
buzzer.frequency = 440
DUTY_OFF = 0
DUTY_ON = 2**15
# pressure at sea level in San Jose, CA
# https://aviationweather.gov/adds/metars/index?submit=1&station_ids=KSJC&chk_metars=on&hoursStr=8&std_trans=translated
sensor.sea_level_pressure = 1019.40
gc.collect()
# for console output
if debug:
print('\nTemperature: {} degrees C'.format(sensor.temperature))
print('Pressure: {}hPa'.format(sensor.pressure))
print('Altitude: {} meters'.format(sensor.altitude))
gc.collect()
### OLED bounds - bottom left/right, top left/right
oled.pixel(0, 31, 100)
oled.pixel(127, 31, 100)
oled.pixel(0, 0, 100)
oled.pixel(127, 0, 100)
gc.collect()
# free mem
print("\nfree mem:", gc.mem_free())
bf = bitmapfont.BitmapFont(128, 32, oled.pixel)
bf.init()
def welcome():
oled.fill(0)
bf.text(" - MicroVario - ", 0, 0, 100)
bf.text("version 0.1", 0, 12, 100)
bf.text("bmosley - 2018", 0, 22, 100)
oled.show()
# Startup tune
buzzer.duty_cycle = DUTY_ON
for i in range(len(TONE_FREQ)):
buzzer.frequency = TONE_FREQ[i]
time.sleep(.1)
buzzer.duty_cycle = DUTY_OFF
time.sleep(3)
gc.collect
return
# welcome to your doom
welcome()
# set timer to 0
oled_timer = 0
def data_display():
global oled_timer
oled_timer = 0
# BMP280 stores current value in mem. for more accurate readings, read once to dump to console
#sensor_dump = sensor.altitude
# meters to feet
altitude_feet = sensor.altitude / .3048
altitude_oled = 'Alt: {0:.3f} ft'.format(altitude_feet)
temp_oled = 'Tmp: {0:.2f} C'.format(sensor.temperature)
pressure_oled = 'Prs: {0:.0f} hPa'.format(sensor.pressure)
oled.fill(0)
bf.text(altitude_oled, 0, 0, 100)
bf.text(temp_oled, 0, 13, 100)
bf.text(pressure_oled, 0, 25, 100)
oled.show()
gc.collect
return
# Init display first
data_display()
current_alt = sensor.altitude
TONE = TONE_FREQ[3]
while True:
print(TONE)
while sensor.altitude > (current_alt + 0.2):
TONE = TONE + 2
buzzer.duty_cycle = DUTY_ON
buzzer.frequency = TONE
current_alt = sensor.altitude
lift = True
while sensor.altitude < (current_alt - 0.2):
TONE = TONE - 2
buzzer.duty_cycle = DUTY_ON
buzzer.frequency = TONE
current_alt = sensor.altitude
while (sensor.altitude - .1) <= sensor.altitude <= (sensor.altitude + .1):
buzzer.duty_cycle = DUTY_OFF
'''
while True:
oled_timer = oled_timer + 1
if oled_timer == 60:
data_display()
if (sensor.altitude) > (current_alt):
print("{}------UP".format(current_alt))
elif (sensor.altitude) < (current_alt):
print("{}------DOWN".format(current_alt))
else:
print("{}~".format(current_alt))
current_alt = round(sensor.altitude, 1)
#print(oled_timer)
#print(sensor.altitude)
'''
# free mem
print("\nfree mem:", gc.mem_free())