Skip to content

Commit 2e4b9c4

Browse files
author
Thomas Preston
committed
Finished adding analogue and pwm
1 parent 95b5284 commit 2e4b9c4

File tree

6 files changed

+80
-6
lines changed

6 files changed

+80
-6
lines changed

CHANGELOG

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ v0.7.0
55
------
66
- Fixed bytes error in serial_channel_device -- and/or/bulk commands
77
work again.
8-
- Added support for analogue inputs, PWM and added music helper methods.
8+
- Added support for analogue inputs and PWM.
99

1010
v0.6.1
1111
------

codebug_tether/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22
IO_DIGITAL_OUTPUT,
33
IO_DIGITAL_INPUT,
44
IO_ANALOGUE_INPUT,
5-
IO_PWM_OUTPUT)
5+
IO_PWM_OUTPUT,
6+
T2_PS_1_1,
7+
T2_PS_1_4,
8+
T2_PS_1_16)

codebug_tether/core.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@
6565

6666
UART_DEFAULT_BAUD = 9600
6767

68+
T2_PS_1_1 = 0
69+
T2_PS_1_4 = 1
70+
T2_PS_1_16 = 2
71+
6872

6973
class InvalidBaud(Exception):
7074
pass
@@ -184,16 +188,30 @@ def pwm_on(self, t2_prescale, full_period, on_period):
184188
"""
185189
# full period
186190
self.set(CHANNEL_INDEX_PWM_CONF_0, full_period)
187-
self.set(CHANNEL_INDEX_PWM_CONF_1, on_period | 0xff)
191+
self.set(CHANNEL_INDEX_PWM_CONF_1, on_period & 0xff)
188192
go_busy = 1
189193
top_two_bit_on_period = (on_period >> 8) & 0b11
190194
conf = go_busy << 4 | t2_prescale << 2 | top_two_bit_on_period
191195
self.set(CHANNEL_INDEX_PWM_CONF_2, conf)
192196

193197
def pwm_freq(self, frequency):
194-
"""Turns on the PWM generator with the given frequency."""
198+
"""Turns on the PWM generator with the given frequency. For example:
199+
200+
>>> codebug = CodeBug()
201+
>>> codebug.set_leg_io(0, IO_PWM_OUTPUT)
202+
>>> codebug.pwm_freq(1046)
203+
>>> time.sleep(2)
204+
>>> codebug.pwm_off()
205+
206+
"""
195207
# calculate pwm settings
196-
# self.pwm_on(settings)
208+
# 4 MHz / 16 = 250k ticks per second
209+
full_period = int(250000 / frequency) - 1
210+
# for 50% duty cycle: shift up by 2 then /(2 i.e. 50% duty cycle)
211+
# on_period = (full_period << 2) / 2;
212+
# this is quicker
213+
on_period = full_period << 1
214+
self.pwm_on(T2_PS_1_16, full_period, on_period)
197215
pass
198216

199217
def pwm_off(self):

codebug_tether/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.6.1'
1+
__version__ = '0.7.0'

docs/example.rst

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,59 @@ You can also change the direction text is written in::
107107
>>> bottom_to_top_msg = StringSprite('Hello CodeBug!', direction='U')
108108

109109

110+
Analogue Input
111+
==============
112+
You can read analogue inputs from all 8 of CodeBug's I/O legs/extension
113+
pins::
114+
115+
>>> import codebug_tether
116+
>>> from codebug_tether import (IO_DIGITAL_INPUT,
117+
... IO_ANALOGUE_INPUT,
118+
... IO_PWM_OUTPUT,
119+
... IO_DIGITAL_OUTPUT)
120+
...
121+
>>> codebug = codebug_tether.CodeBug()
122+
>>> codebug.set_leg_io(0, IO_ANALOGUE_INPUT)
123+
>>> codebug.read_analogue(0)
124+
128
125+
126+
127+
PWM Output
128+
==========
129+
You can drive one synchronised PWM (Pulse Width Modulation) signal out
130+
of the first three legs on CodeBug. That is, the same PWM signal will
131+
be driven out of legs configured as PWM output::
132+
133+
>>> import codebug_tether
134+
>>> from codebug_tether import (IO_DIGITAL_INPUT,
135+
... IO_ANALOGUE_INPUT,
136+
... IO_PWM_OUTPUT,
137+
... IO_DIGITAL_OUTPUT,
138+
... T2_PS_1_1,
139+
... T2_PS_1_4,
140+
... T2_PS_1_16)
141+
142+
>>> codebug = codebug_tether.CodeBug()
143+
>>> # configure legs 0 and 1 to be PWM output
144+
>>> codebug.set_leg_io(0, IO_PWM_OUTPUT)
145+
>>> codebug.set_leg_io(1, IO_PWM_OUTPUT)
146+
>>> # shortcut method to specify a frequency (the note C == 1046 Hz)
147+
>>> codebug.pwm_freq(1046)
148+
>>> time.sleep(2)
149+
>>> codebug.pwm_off()
150+
151+
Or you can be more specific with the duty cycle and timing::
152+
153+
>>> # pwm on with 1:4 prescaler and 75% duty cycle @ ~977Hz
154+
>>> # Timer 2 prescale: 4Mhz clock / 4 = 1MHz timer speed
155+
>>> # full_period: 255 << 2 = 1024 (timer resets at this count; PWM = 1)
156+
>>> # on_period: 765 (PWM goes to zero at this count; PWM = 0)
157+
>>> # therefore duty cycle here is 75%
158+
>>> codebug.pwm_on(T2_PS_1_4, 255, 765)
159+
>>> time.sleep(2)
160+
>>> codebug.pwm_off()
161+
162+
110163
Colour Tail
111164
===========
112165
You can control Colour Tails (WS2812's) attached to CodeBug. By default,

firmware/codebug_tether_v0.7.0.cbg

15 KB
Binary file not shown.

0 commit comments

Comments
 (0)