Skip to content

Commit 6f7ab73

Browse files
committed
updating content
1 parent 64c24c9 commit 6f7ab73

File tree

4 files changed

+190
-0
lines changed

4 files changed

+190
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Button Callback
2+
3+
```py
4+
import re
5+
6+
def extract_gpio_pin(input_string):
7+
# Use a regular expression to find the GPIO number
8+
match = re.search(r"GPIO(\d+)", input_string)
9+
if match:
10+
# Convert the extracted number to an integer to remove leading zeros
11+
return int(match.group(1))
12+
else:
13+
# Return None if no match is found (or raise an exception if that's preferable)
14+
return None
15+
16+
# Test the function with examples
17+
print(extract_gpio_pin("Pin(GPIO15, mode=IN, pull=PULL_UP)")) # Output: 15
18+
print(extract_gpio_pin("Pin(GPIO7, mode=IN, pull=PULL_UP)")) # Output: 7
19+
print(extract_gpio_pin("Pin(GPIO03, mode=IN, pull=PULL_UP)")) # Output: 3
20+
21+
```
22+
23+
24+
https://chat.openai.com/share/6e6d8123-ed4d-4dc6-a915-030fe2245dfe

src/20-sound-test.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import machine
2+
import utime
3+
from machine import Pin, ADC, SPI
4+
import ssd1306
5+
6+
# OLED display width and height
7+
WIDTH = 128
8+
HEIGHT = 64
9+
10+
# SPI pins for OLED
11+
clock = Pin(2) # SCL
12+
data = Pin(3) # SDA
13+
RES = Pin(4)
14+
DC = Pin(5)
15+
CS = Pin(6)
16+
17+
# Initialize SPI and OLED Display
18+
spi = SPI(0, sck=clock, mosi=data)
19+
display = ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, DC, RES, CS)
20+
21+
# Initialize ADC for GPIO26 (ADC0)
22+
adc = ADC(Pin(26))
23+
24+
min=64000
25+
def plot_signal():
26+
global min
27+
display.fill(0) # Clear the display
28+
old_x = 0
29+
old_y = HEIGHT // 2
30+
31+
# For simplicity, we're plotting every other pixel
32+
for x in range(0, WIDTH):
33+
# Read from ADC (values will be from 0 to 4095)
34+
val = adc.read_u16()
35+
if val < min:
36+
min = val
37+
# print(val-min)
38+
# Scale the ADC value to fit the OLED height
39+
y = int(((val-min) / 500) * HEIGHT)
40+
# Invert y to plot correctly on the OLED
41+
y = HEIGHT - y
42+
# Draw a line from the last point to the new point
43+
display.line(old_x, old_y, x, y, 1)
44+
old_x, old_y = x, y
45+
46+
display.show() # Update the display with the new data
47+
48+
while True:
49+
plot_signal()
50+
utime.sleep(0.1) # Small delay to reduce flickering
51+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import machine
2+
import utime
3+
from machine import Pin, ADC, SPI
4+
import ssd1306
5+
6+
# OLED display width and height
7+
WIDTH = 128
8+
HEIGHT = 64
9+
10+
# SPI pins for OLED
11+
clock = Pin(2) # SCL
12+
data = Pin(3) # SDA
13+
RES = Pin(4)
14+
DC = Pin(5)
15+
CS = Pin(6)
16+
17+
# Initialize SPI and OLED Display
18+
spi = SPI(0, sck=clock, mosi=data)
19+
display = ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, DC, RES, CS)
20+
21+
# Initialize ADC for sound input (GPIO26) and gain control (GPIO27)
22+
sound = ADC(Pin(26))
23+
gain = ADC(Pin(27))
24+
25+
def plot_signal_with_gain():
26+
display.fill(0) # Clear the display
27+
old_x = 0
28+
old_y = HEIGHT // 2
29+
30+
# Read gain control (potentiometer) value
31+
gain_value = gain.read_u16() + 1 # Adding 1 to avoid division by zero
32+
33+
for x in range(0, WIDTH, 2):
34+
# Read from ADC (sound input)
35+
val = sound.read_u16()
36+
37+
# Adjust the sound value based on the gain
38+
# Note: This scaling might need adjustment depending on your specific potentiometer and desired sensitivity
39+
adjusted_val = min(((val * gain_value) >> 16), 65535) # Ensure the adjusted value does not exceed ADC's max value
40+
41+
# Scale the adjusted value to fit the OLED height
42+
y = int((adjusted_val / 65535) * HEIGHT)
43+
# Invert y to plot correctly on the OLED
44+
y = HEIGHT - y
45+
# Draw a line from the last point to the new point
46+
display.line(old_x, old_y, x, y, 1)
47+
old_x, old_y = x, y
48+
49+
display.show() # Update the display with the new data
50+
51+
while True:
52+
plot_signal_with_gain()
53+
utime.sleep(0.1) # Small delay to reduce flickering
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import machine
2+
import utime
3+
from machine import Pin, ADC, SPI
4+
# https://docs.micropython.org/en/latest/esp8266/tutorial/ssd1306.html
5+
import ssd1306
6+
7+
# OLED display width and height
8+
WIDTH = 128
9+
HEIGHT = 64
10+
11+
# SPI pins for OLED
12+
clock = Pin(2) # SCL
13+
data = Pin(3) # SDA
14+
RES = Pin(4)
15+
DC = Pin(5)
16+
CS = Pin(6)
17+
18+
# Initialize SPI and OLED Display
19+
spi = SPI(0, sck=clock, mosi=data)
20+
display = ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, DC, RES, CS)
21+
22+
# Initialize ADC for GPIO26 (ADC0)
23+
adc = ADC(Pin(26))
24+
25+
26+
27+
def plot_signal(new_val, min):
28+
# Scroll the display content to the left by one pixel
29+
display.scroll(-1, 0)
30+
y = HEIGHT - new_val//4
31+
if y > HEIGHT:
32+
y = HEIGHT
33+
if y < 0:
34+
y = 0
35+
36+
for clear_y in range(HEIGHT):
37+
display.pixel(WIDTH - 2, y, 1)
38+
39+
# Update the display with the new data
40+
# display.fill_rect(0, 54, 100, 63, 0)
41+
# display.text(str(min), 0, 54, 1)
42+
# display.text(str(new_val), 0, 54, 1)
43+
display.show()
44+
45+
min = 53000
46+
avg = 50
47+
display.fill(0)
48+
while True:
49+
50+
# add up the ave number of points
51+
val=0
52+
for i in range(0,avg):
53+
val += (adc.read_u16() - min)
54+
avg_val = val//avg
55+
56+
# if the average val is negative, lower the minimum by that amoun
57+
if avg_val < 0:
58+
min += avg_val
59+
60+
plot_signal(avg_val, min)
61+
# utime.sleep(0.1) # Small delay to reduce flickering
62+

0 commit comments

Comments
 (0)