Skip to content

Commit

Permalink
Added RGBLed component and example
Browse files Browse the repository at this point in the history
  • Loading branch information
theycallmeswift committed Apr 17, 2013
1 parent de04569 commit 9c2d8cb
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 6 deletions.
52 changes: 49 additions & 3 deletions BreakfastSerial/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,52 @@ def blink(self, millis):

self._interval = setInterval(self.toggle, millis)

class RGBLed(EventEmitter):

def __init__(self, board, pins):
if not board:
raise ArduinoNotSuppliedException

# TODO: Check that pins is dict

super(RGBLed, self).__init__()

self._red = Led(board, pins["red"])
self._green = Led(board, pins["green"])
self._blue = Led(board, pins["blue"])

def off(self):
self._red.off(); self._green.off(); self._blue.off()
return self

def red(self):
self._red.on(); self._green.off(); self._blue.off()
return self

def green(self):
self._red.off(); self._green.on(); self._blue.off()
return self

def blue(self):
self._red.off(); self._green.off(); self._blue.on()
return self

def yellow(self):
self._red.on(); self._green.on(); self._blue.off()
return self

def cyan(self):
self._red.off(); self._green.on(); self._blue.on()
return self

def purple(self):
self._red.on(); self._green.off(); self._blue.on()
return self

def white(self):
self._red.on(); self._green.on(); self._blue.on()
return self

class Buzzer(Led):
pass

Expand Down Expand Up @@ -116,10 +162,10 @@ def hold(self, cb):
self.on('hold', cb)

class Servo(Component):

def __init__(self, board, pin):
super(Servo, self).__init__(board, pin)
self._pin.mode = pyfirmata.SERVO
self._pin.mode = pyfirmata.SERVO

def set_position(self, degrees):
if int(degrees) > 180 or int(degrees) < 0:
Expand All @@ -128,7 +174,7 @@ def set_position(self, degrees):

def move(self, degrees):
self.set_position(self.value + int(degrees))

def center(self):
self.set_position(90)

Expand Down
41 changes: 38 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ led = Led(board, pin)
led.on()
sleep(2)
led.off()
sleep(2)
```

You can also use the `blink` method and pass it a number of milliseconds to automate the blinking process
Expand Down Expand Up @@ -106,10 +105,46 @@ component emits the following events:
- `down` - The button is pressed
- `up` - The button is not being pressed
- `hold` - The button was held for at least 1 second


### Use an RGB Led

The `RGBLed` component lets us change the colors of an RGB Led without having to
interact with the three underlying leds.

```python
from BreakfastSerial import Arduino, RGBLed
from time import sleep

board = Arduino()
led = RGBLed(board, { "red": 10, "green": 9, "blue": 8 })

led.red()
sleep(1)

led.green()
sleep(1)

led.blue()
sleep(1)

led.yellow()
sleep(1)

led.cyan()
sleep(1)

led.purple()
sleep(1)

led.white()
sleep(1)

led.off()
```

### Read a sensor

The `Sensor` component let's us read in data from a sensor (analog or digital). The constructor takes in
The `Sensor` component lets us read in data from a sensor (analog or digital). The constructor takes in
an Arduino object and a pin number.

``` python
Expand Down
47 changes: 47 additions & 0 deletions examples/rgb_led.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#! /usr/bin/env python
"""
This is an example that demonstrates how to use an
RGB led with BreakfastSerial. It assumes you have an
RGB led wired up with red on pin 10, green on pin 9,
and blue on pin 8.
"""
from BreakfastSerial import RGBLed, Arduino
from time import sleep

board = Arduino()
led = RGBLed(board, { "red": 10, "green": 9, "blue": 8 })

# Red (R: on, G: off, B: off)
led.red()
sleep(1)

# Green (R: off, G: on, B: off)
led.green()
sleep(1)

# Blue (R: off, G: off, B: on)
led.blue()
sleep(1)

# Yellow (R: on, G: on, B: off)
led.yellow()
sleep(1)

# Cyan (R: off, G: on, B: on)
led.cyan()
sleep(1)

# Purple (R: on, G: off, B: on)
led.purple()
sleep(1)

# White (R: on, G: on, B: on)
led.white()
sleep(1)

# Off (R: off, G: off, B: off)
led.off()

# Run an interactive shell so you can play (not required)
import code
code.InteractiveConsole(locals=globals()).interact()

0 comments on commit 9c2d8cb

Please sign in to comment.