Skip to content

Commit 3891e56

Browse files
committed
updating content
1 parent 31eaf03 commit 3891e56

File tree

6 files changed

+308
-0
lines changed

6 files changed

+308
-0
lines changed

src/kits/pwm-display/01-pot-print.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from machine import Pin, ADC
2+
from utime import sleep
3+
POT_PIN = ADC(26)
4+
5+
while True:
6+
print(POT_PIN.read_u16())
7+
sleep(.1)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Display a PWM waveform on a 128x64 OLED Display
2+
3+
from machine import Pin, ADC, SPI
4+
from utime import sleep
5+
import ssd1306
6+
7+
# this is the built-in LED on the Pico
8+
led = Pin('LED', Pin.OUT)
9+
10+
WIDTH = 128
11+
HEIGHT = 64
12+
HALF_HEIGHT = HEIGHT // 2
13+
clock=Pin(2)
14+
data=Pin(3)
15+
RES = Pin(4)
16+
DC = Pin(5)
17+
CS = Pin(6)
18+
19+
spi=SPI(0, sck=clock, mosi=data)
20+
oled = ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, DC, RES, CS)
21+
22+
POT_PIN = ADC(26)
23+
POT_MAX = 65536
24+
25+
# map a value from one rante into another range
26+
# checks for divide by zero
27+
def map(value, istart, istop, ostart, ostop):
28+
# check ff (istop - istart)
29+
if (istop - istart) == 0:
30+
return ostop
31+
else:
32+
return int(ostart + (ostop - ostart) * ((value - istart) / (istop - istart)))
33+
34+
# draw a horizontal bar
35+
def draw_hbar(inval, yPos, barHeight):
36+
oled.fill_rect(0, yPos, inval, barHeight, 1) # fill with 1s
37+
38+
def update_display(xPos):
39+
oled.fill(0)
40+
oled.text('Display Pot', 0, 0)
41+
# draw a hbar at the top yPos, height
42+
draw_hbar(xPos, 10, 2)
43+
oled.text("X Pos: " + str(xPos), 0, 56)
44+
oled.show()
45+
46+
counter = 0
47+
# repeat forever
48+
while True:
49+
potVal = int(POT_PIN.read_u16())
50+
xPos = map(potVal, 0, POT_MAX, 0, WIDTH)
51+
update_display(xPos)
52+
led.toggle()
53+
sleep(.1)
54+
counter += 1
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Display a PWM waveform on a 128x64 OLED Display
2+
3+
from machine import Pin, ADC, SPI
4+
from utime import sleep
5+
import ssd1306
6+
7+
# this is the built-in LED on the Pico
8+
led = Pin('LED', Pin.OUT)
9+
10+
WIDTH = 128
11+
HEIGHT = 64
12+
HALF_HEIGHT = HEIGHT // 2
13+
clock=Pin(2)
14+
data=Pin(3)
15+
RES = Pin(4)
16+
DC = Pin(5)
17+
CS = Pin(6)
18+
19+
spi=SPI(0, sck=clock, mosi=data)
20+
oled = ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, DC, RES, CS)
21+
22+
POT_PIN = ADC(26)
23+
POT_MAX = 65536
24+
25+
# map a value from one rante into another range
26+
# checks for divide by zero
27+
def map(value, istart, istop, ostart, ostop):
28+
# check ff (istop - istart)
29+
if (istop - istart) == 0:
30+
return ostop
31+
else:
32+
return int(ostart + (ostop - ostart) * ((value - istart) / (istop - istart)))
33+
34+
# draw a horizontal bar
35+
def draw_hbar(inval, yPos, barHeight):
36+
oled.fill_rect(0, yPos, inval, barHeight, 1) # fill with 1s
37+
38+
def update_display(xPos):
39+
oled.fill(0)
40+
oled.text('Display Pot', 0, 0)
41+
# draw a hbar at the top yPos, height
42+
draw_hbar(xPos, 10, 2)
43+
oled.text("X Pos: " + str(xPos), 0, 56)
44+
oled.show()
45+
46+
counter = 0
47+
# repeat forever
48+
while True:
49+
potVal = int(POT_PIN.read_u16())
50+
xPos = map(potVal, 0, POT_MAX, 0, WIDTH)
51+
update_display(xPos)
52+
led.toggle()
53+
sleep(.1)
54+
counter += 1

src/kits/pwm-display/display-pwm.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Display a PWM waveform on a 128x64 OLED Display
2+
3+
from machine import Pin, ADC, SPI
4+
from utime import sleep
5+
import ssd1306
6+
7+
# this is the built-in LED on the Pico
8+
led = Pin('LED', Pin.OUT)
9+
10+
WIDTH = 128
11+
HEIGHT = 64
12+
HALF_HEIGHT = HEIGHT // 2
13+
clock=Pin(2)
14+
data=Pin(3)
15+
RES = Pin(4)
16+
DC = Pin(5)
17+
CS = Pin(6)
18+
19+
spi=SPI(0, sck=clock, mosi=data)
20+
oled = ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, DC, RES, CS)
21+
22+
POT_PIN = ADC(26)
23+
POT_MAX = 65536
24+
25+
# map a value from one rante into another range
26+
# checks for divide by zero
27+
def map(value, istart, istop, ostart, ostop):
28+
# check ff (istop - istart)
29+
if (istop - istart) == 0:
30+
return ostop
31+
else:
32+
return int(ostart + (ostop - ostart) * ((value - istart) / (istop - istart)))
33+
34+
# draw a horizontal bar
35+
def draw_hbar(inval, yPos, barHeight):
36+
oled.fill_rect(0, yPos, inval, barHeight, 1) # fill with 1s
37+
38+
# draw a square wave
39+
def draw_pulse_wave(dutyCycle, yOffset):
40+
pulses = 5
41+
square_wave_height = 20
42+
sq_wave_period = WIDTH // pulses
43+
high_length = int(dutyCycle * sq_wave_period / 100)
44+
# half the square wave period
45+
hswp = sq_wave_period // 2
46+
47+
48+
for i in range(0,pulses+1):
49+
# top bar
50+
oled.hline(i*sq_wave_period, yOffset-square_wave_height, high_length, 1)
51+
# down line
52+
oled.vline(i*sq_wave_period + high_length, yOffset-square_wave_height, square_wave_height, 1)
53+
# up line
54+
oled.vline(i*sq_wave_period + yOffset-square_wave_height-2, yOffset-square_wave_height+1, square_wave_height, 1)
55+
# bottem bar
56+
oled.hline(i*sq_wave_period - (sq_wave_period - high_length), yOffset, sq_wave_period-high_length, 1)
57+
58+
# oled.text(str(high_length), 64, 56)
59+
60+
def update_display(counter, xPos, dutyCycle):
61+
oled.fill(0)
62+
oled.text('Pulse Width Modu', 0, 0)
63+
# draw a hbar at the top yPos, height
64+
draw_hbar(xPos, 10, 2)
65+
draw_pulse_wave(dutyCycle, HALF_HEIGHT+15)
66+
# oled.hline(0, HALF_HEIGHT+15, WIDTH, 1)
67+
oled.text("Duty Cycle: " + str(round(dutyCycle)), 0, 56)
68+
oled.show()
69+
70+
counter = 0
71+
# repeat forever
72+
while True:
73+
potVal = int(POT_PIN.read_u16())
74+
xPos = map(potVal, 0, POT_MAX, 0, WIDTH)
75+
# duty cycle is a float
76+
dutyCycle = int(xPos / WIDTH * 100)
77+
update_display(counter, xPos, dutyCycle)
78+
led.toggle()
79+
sleep(.1)
80+
counter += 1
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from machine import Pin
2+
from utime import sleep
3+
import ssd1306
4+
5+
# this is the built-in LED on the Pico
6+
led = Pin('LED', Pin.OUT)
7+
8+
WIDTH = 128
9+
HEIGHT = 64
10+
clock=Pin(2)
11+
data=Pin(3)
12+
RES = machine.Pin(4)
13+
DC = machine.Pin(5)
14+
CS = machine.Pin(6)
15+
16+
spi=machine.SPI(0, sck=clock, mosi=data)
17+
oled = ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, DC, RES, CS)
18+
19+
def update_display(counter):
20+
oled.fill(0)
21+
oled.text('Running', 0, 0)
22+
oled.text("Dan McCreary's", 0, 10)
23+
oled.text('Robot Lab 47', 0, 20)
24+
oled.text(str(counter), 0, 30)
25+
oled.show()
26+
27+
counter = 0
28+
# repeat forever
29+
while True:
30+
update_display(counter)
31+
led.toggle()
32+
sleep(.5)
33+
counter += 1

src/kits/pwm-display/main.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Display a PWM waveform on a 128x64 OLED Display
2+
3+
from machine import Pin, ADC
4+
from utime import sleep
5+
import ssd1306
6+
7+
# this is the built-in LED on the Pico
8+
led = Pin('LED', Pin.OUT)
9+
10+
WIDTH = 128
11+
HEIGHT = 64
12+
HALF_HEIGHT = HEIGHT // 2
13+
clock=Pin(2)
14+
data=Pin(3)
15+
RES = machine.Pin(4)
16+
DC = machine.Pin(5)
17+
CS = machine.Pin(6)
18+
19+
spi=machine.SPI(0, sck=clock, mosi=data)
20+
oled = ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, DC, RES, CS)
21+
22+
POT_PIN = ADC(26)
23+
POT_MAX = 65536
24+
25+
# map a value from one rante into another range
26+
# checks for divide by zero
27+
def map(value, istart, istop, ostart, ostop):
28+
# check ff (istop - istart)
29+
if (istop - istart) == 0:
30+
return ostop
31+
else:
32+
return int(ostart + (ostop - ostart) * ((value - istart) / (istop - istart)))
33+
34+
# draw a horizontal bar
35+
def draw_hbar(inval, yPos, barHeight):
36+
oled.fill_rect(0, yPos, inval, barHeight, 1) # fill with 1s
37+
38+
# draw a square wave
39+
def draw_pulse_wave(dutyCycle, yOffset):
40+
pulses = 5
41+
square_wave_height = 20
42+
sq_wave_period = WIDTH // pulses
43+
high_length = int(dutyCycle * sq_wave_period / 100)
44+
# half the square wave period
45+
hswp = sq_wave_period // 2
46+
47+
48+
for i in range(0,pulses+1):
49+
# top bar
50+
oled.hline(i*sq_wave_period, yOffset-square_wave_height, high_length, 1)
51+
# down line
52+
oled.vline(i*sq_wave_period + high_length, yOffset-square_wave_height, square_wave_height, 1)
53+
# up line
54+
oled.vline(i*sq_wave_period + yOffset-square_wave_height-2, yOffset-square_wave_height+1, square_wave_height, 1)
55+
# bottem bar
56+
oled.hline(i*sq_wave_period - (sq_wave_period - high_length), yOffset, sq_wave_period-high_length, 1)
57+
58+
# oled.text(str(high_length), 64, 56)
59+
60+
def update_display(counter, xPos, dutyCycle):
61+
oled.fill(0)
62+
oled.text('Pulse Width Modu', 0, 0)
63+
# draw a hbar at the top yPos, height
64+
draw_hbar(xPos, 10, 2)
65+
draw_pulse_wave(dutyCycle, HALF_HEIGHT+15)
66+
# oled.hline(0, HALF_HEIGHT+15, WIDTH, 1)
67+
oled.text("Duty Cycle: " + str(round(dutyCycle)), 0, 56)
68+
oled.show()
69+
70+
counter = 0
71+
# repeat forever
72+
while True:
73+
potVal = int(POT_PIN.read_u16())
74+
xPos = map(potVal, 0, POT_MAX, 0, WIDTH)
75+
# duty cycle is a float
76+
dutyCycle = int(xPos / WIDTH * 100)
77+
update_display(counter, xPos, dutyCycle)
78+
led.toggle()
79+
sleep(.1)
80+
counter += 1

0 commit comments

Comments
 (0)