Skip to content

Commit

Permalink
updating content
Browse files Browse the repository at this point in the history
  • Loading branch information
dmccreary committed May 5, 2024
1 parent 64c24c9 commit 6f7ab73
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 0 deletions.
24 changes: 24 additions & 0 deletions docs/advanced-labs/21-botton-callback.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Button Callback

```py
import re

def extract_gpio_pin(input_string):
# Use a regular expression to find the GPIO number
match = re.search(r"GPIO(\d+)", input_string)
if match:
# Convert the extracted number to an integer to remove leading zeros
return int(match.group(1))
else:
# Return None if no match is found (or raise an exception if that's preferable)
return None

# Test the function with examples
print(extract_gpio_pin("Pin(GPIO15, mode=IN, pull=PULL_UP)")) # Output: 15
print(extract_gpio_pin("Pin(GPIO7, mode=IN, pull=PULL_UP)")) # Output: 7
print(extract_gpio_pin("Pin(GPIO03, mode=IN, pull=PULL_UP)")) # Output: 3

```


https://chat.openai.com/share/6e6d8123-ed4d-4dc6-a915-030fe2245dfe
51 changes: 51 additions & 0 deletions src/20-sound-test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import machine
import utime
from machine import Pin, ADC, SPI
import ssd1306

# OLED display width and height
WIDTH = 128
HEIGHT = 64

# SPI pins for OLED
clock = Pin(2) # SCL
data = Pin(3) # SDA
RES = Pin(4)
DC = Pin(5)
CS = Pin(6)

# Initialize SPI and OLED Display
spi = SPI(0, sck=clock, mosi=data)
display = ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, DC, RES, CS)

# Initialize ADC for GPIO26 (ADC0)
adc = ADC(Pin(26))

min=64000
def plot_signal():
global min
display.fill(0) # Clear the display
old_x = 0
old_y = HEIGHT // 2

# For simplicity, we're plotting every other pixel
for x in range(0, WIDTH):
# Read from ADC (values will be from 0 to 4095)
val = adc.read_u16()
if val < min:
min = val
# print(val-min)
# Scale the ADC value to fit the OLED height
y = int(((val-min) / 500) * HEIGHT)
# Invert y to plot correctly on the OLED
y = HEIGHT - y
# Draw a line from the last point to the new point
display.line(old_x, old_y, x, y, 1)
old_x, old_y = x, y

display.show() # Update the display with the new data

while True:
plot_signal()
utime.sleep(0.1) # Small delay to reduce flickering

53 changes: 53 additions & 0 deletions src/advanced/spectrum-analyzer/02-pot-gain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import machine
import utime
from machine import Pin, ADC, SPI
import ssd1306

# OLED display width and height
WIDTH = 128
HEIGHT = 64

# SPI pins for OLED
clock = Pin(2) # SCL
data = Pin(3) # SDA
RES = Pin(4)
DC = Pin(5)
CS = Pin(6)

# Initialize SPI and OLED Display
spi = SPI(0, sck=clock, mosi=data)
display = ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, DC, RES, CS)

# Initialize ADC for sound input (GPIO26) and gain control (GPIO27)
sound = ADC(Pin(26))
gain = ADC(Pin(27))

def plot_signal_with_gain():
display.fill(0) # Clear the display
old_x = 0
old_y = HEIGHT // 2

# Read gain control (potentiometer) value
gain_value = gain.read_u16() + 1 # Adding 1 to avoid division by zero

for x in range(0, WIDTH, 2):
# Read from ADC (sound input)
val = sound.read_u16()

# Adjust the sound value based on the gain
# Note: This scaling might need adjustment depending on your specific potentiometer and desired sensitivity
adjusted_val = min(((val * gain_value) >> 16), 65535) # Ensure the adjusted value does not exceed ADC's max value

# Scale the adjusted value to fit the OLED height
y = int((adjusted_val / 65535) * HEIGHT)
# Invert y to plot correctly on the OLED
y = HEIGHT - y
# Draw a line from the last point to the new point
display.line(old_x, old_y, x, y, 1)
old_x, old_y = x, y

display.show() # Update the display with the new data

while True:
plot_signal_with_gain()
utime.sleep(0.1) # Small delay to reduce flickering
62 changes: 62 additions & 0 deletions src/advanced/spectrum-analyzer/23-average-plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import machine
import utime
from machine import Pin, ADC, SPI
# https://docs.micropython.org/en/latest/esp8266/tutorial/ssd1306.html
import ssd1306

# OLED display width and height
WIDTH = 128
HEIGHT = 64

# SPI pins for OLED
clock = Pin(2) # SCL
data = Pin(3) # SDA
RES = Pin(4)
DC = Pin(5)
CS = Pin(6)

# Initialize SPI and OLED Display
spi = SPI(0, sck=clock, mosi=data)
display = ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, DC, RES, CS)

# Initialize ADC for GPIO26 (ADC0)
adc = ADC(Pin(26))



def plot_signal(new_val, min):
# Scroll the display content to the left by one pixel
display.scroll(-1, 0)
y = HEIGHT - new_val//4
if y > HEIGHT:
y = HEIGHT
if y < 0:
y = 0

for clear_y in range(HEIGHT):
display.pixel(WIDTH - 2, y, 1)

# Update the display with the new data
# display.fill_rect(0, 54, 100, 63, 0)
# display.text(str(min), 0, 54, 1)
# display.text(str(new_val), 0, 54, 1)
display.show()

min = 53000
avg = 50
display.fill(0)
while True:

# add up the ave number of points
val=0
for i in range(0,avg):
val += (adc.read_u16() - min)
avg_val = val//avg

# if the average val is negative, lower the minimum by that amoun
if avg_val < 0:
min += avg_val

plot_signal(avg_val, min)
# utime.sleep(0.1) # Small delay to reduce flickering

0 comments on commit 6f7ab73

Please sign in to comment.