Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
theycallmeswift committed Mar 12, 2013
0 parents commit f78d77f
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_Store
*~
*.sw[a-z]
*.pyc
venv/
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# breakfast_serial

breakfast_serial is a Firmata based framework for interacting with Arduinos over
serial.

## Arduino Setup

In order to use breakfast_serial, you need to have an arduino running the
standard firmata.

1. Download the Arduino IDE from the arduino website

- [OSX](http://arduino.googlecode.com/files/arduino-1.0-macosx.zip)
- [Linux 32 bit](http://arduino.googlecode.com/files/arduino-1.0-linux.tgz)
- [Linux 64 bit](http://arduino.googlecode.com/files/arduino-1.0-linux64.tgz)
- Windows support coming soon.

2. Plug in your Arduino or Arduino compatible microcontroller via USB
3. Open the Arduino IDE, select: File > Examples > Firmata > StandardFirmata
4. Click the "Upload" button.

## Installation

Using PyPi

``` bash
pip install breakfast_serial
```

#### From Source

``` bash
git clone git://github.com/theycallmeswift/breakfast_serial.git && cd breakfast_serial

python setup.py install
```

## Usage

The breakfast_serial library provides a simple abstraction for a number of
common components. Make sure your arduino is plugged in and is running firmata.

4 changes: 4 additions & 0 deletions breakfast_serial/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
__version__ = '0.0.0'

from breakfast_serial import *
from sensors import *
20 changes: 20 additions & 0 deletions breakfast_serial/breakfast_serial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import os, re, code
from pyfirmata import Arduino

class ArduinoNotFoundException(Exception):
pass

class FirmataNotOnBoardException(Exception):
pass

rport = re.compile('usb|acm')
ports = filter(rport.search, os.listdir('/dev'))

if len(ports) == 0:
raise ArduinoNotFoundException

print "Connecting to /dev/%s" % ports[0]
__board__ = Arduino("/dev/%s" % ports[0])

if not __board__.get_firmata_version():
raise FirmataNotOnBoardException
32 changes: 32 additions & 0 deletions breakfast_serial/sensors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from breakfast_serial import __board__
from util import setInterval

class Led(object):

def __init__(self, pin):
self._pin = __board__.digital[pin]
self._isOn = False
self._interval = None

def on(self):
self._pin.write(1)
self._isOn = True
return self

def off(self, clear=True):
self._pin.write(0)
self._isOn = False

if self._interval and clear:
self._interval.clear()

return self

def toggle(self):
if self._isOn:
return self.off(clear=False)
else:
return self.on()

def blink(self, millis):
self._interval = setInterval(self.toggle, millis)
24 changes: 24 additions & 0 deletions breakfast_serial/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import threading
from time import sleep

class setInterval(threading.Thread):

def __init__(self, func, millis):
threading.Thread.__init__(self)
self.event = threading.Event()
self.func = func
self.seconds = millis / 1000.0
self.shouldRun = True

self.setDaemon(True)
self.start()

def run(self):
self.func()
sleep(self.seconds)

if self.shouldRun:
self.run()

def clear(self):
self.shouldRun = False
13 changes: 13 additions & 0 deletions examples/led.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#! /usr/bin/env python
"""
This is an example that demonstrates how to blink an
led using breakfast_serial. It assumes you have an
led wired up to pin 13.
"""
from breakfast_serial import Led

led = Led(13)
led.blink(200)

import code
code.InteractiveConsole(locals=globals()).interact()
17 changes: 17 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from breakfast_serial import __version__
from setuptools import setup, find_packages

with open('README.md') as f:
long_description = f.read()

setup(
name = "breakfast_serial",
version = __version__,
description = "Python Framework for interacting with Arduino",
author = "Swift",
author_email = "[email protected]",
packages = find_packages(),
install_requires=['pyfirmata'],
url = "http://github.com/theycallmeswift/breakfast_serial/",
keywords = ["arduino","firmata"],
long_description = long_description )

0 comments on commit f78d77f

Please sign in to comment.