Merge pull request #14 from kovrus/master

EEPROM
This commit is contained in:
Tristan Hearn
2014-05-12 13:43:38 -04:00
3 changed files with 105 additions and 8 deletions

View File

@@ -24,7 +24,7 @@ def enumerate_serial_ports():
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, path) key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, path)
except WindowsError: except WindowsError:
raise Exception raise Exception
for i in itertools.count(): for i in itertools.count():
try: try:
val = winreg.EnumValue(key, i) val = winreg.EnumValue(key, i)
@@ -110,6 +110,7 @@ class Arduino(object):
self.sr = sr self.sr = sr
self.SoftwareSerial = SoftwareSerial(self) self.SoftwareSerial = SoftwareSerial(self)
self.Servos = Servos(self) self.Servos = Servos(self)
self.EEPROM = EEPROM(self)
def version(self): def version(self):
return get_version(self.sr) return get_version(self.sr)
@@ -556,3 +557,61 @@ class SoftwareSerial(object):
return response return response
else: else:
return False return False
class EEPROM(object):
"""
Class for reading and writing to EEPROM.
"""
def __init__(self, board):
self.board = board
self.sr = board.sr
def size(self):
"""
Returns size of EEPROM memory.
"""
cmd_str = build_cmd_str("sz")
try:
self.sr.write(cmd_str)
self.sr.flush()
response = self.sr.readline().replace("\r\n", "")
return int(response)
except:
return 0
def write(self, address, value=0):
""" Write a byte to the EEPROM.
:address: the location to write to, starting from 0 (int)
:value: the value to write, from 0 to 255 (byte)
"""
if value > 255:
value = 255
elif value < 0:
value = 0
cmd_str = build_cmd_str("eewr", (address, value))
try:
self.sr.write(cmd_str)
self.sr.flush()
except:
pass
def read(self, adrress):
""" Reads a byte from the EEPROM.
:address: the location to write to, starting from 0 (int)
"""
cmd_str = build_cmd_str("eer", (adrress,))
try:
self.sr.write(cmd_str)
self.sr.flush()
response = self.sr.readline().replace("\r\n", "")
if response:
return int(response)
except:
return 0

View File

@@ -81,17 +81,17 @@ If there are more than one Arduino boards connected,
the desired COM port can be also be passed as an optional argument: the desired COM port can be also be passed as an optional argument:
```python ```python
board = Arduino("9600", port = "COM3") #Windows example board = Arduino("9600", port="COM3") #Windows example
``` ```
```python ```python
board = Arduino("9600", port = "/dev/tty.usbmodemfa141") #OSX example board = Arduino("9600", port="/dev/tty.usbmodemfa141") #OSX example
``` ```
A time-out for reading from the Arduino can also be specified as an optional A time-out for reading from the Arduino can also be specified as an optional
argument: argument:
```python ```python
board = Arduino("9600", timeout = 2) #Serial reading functions will board = Arduino("9600", timeout=2) #Serial reading functions will
#wait for no more than 2 seconds #wait for no more than 2 seconds
``` ```
@@ -142,7 +142,7 @@ board.analogWrite(11) #Set analog value (PWM) based on analog measurement
**Servo Library Functionality** **Servo Library Functionality**
Support is included for up to 8 servos. Support is included for up to 8 servos.
- `Arduino.Servos.attach(pin, min = 544, max = 2400)` Create servo instance. Only 8 servos can be used at one time. - `Arduino.Servos.attach(pin, min=544, max=2400)` Create servo instance. Only 8 servos can be used at one time.
- `Arduino.Servos.read(pin)` Returns the angle of the servo attached to the specified pin - `Arduino.Servos.read(pin)` Returns the angle of the servo attached to the specified pin
- `Arduino.Servos.write(pin, angle)` Move an attached servo on a pin to a specified angle - `Arduino.Servos.write(pin, angle)` Move an attached servo on a pin to a specified angle
- `Arduino.Servos.writeMicroseconds(pin, uS)` Write a value in microseconds to the servo on a specified pin - `Arduino.Servos.writeMicroseconds(pin, uS)` Write a value in microseconds to the servo on a specified pin
@@ -158,7 +158,7 @@ board.Servos.detach(9) #free pin 9
**Software Serial Functionality** **Software Serial Functionality**
- `Arduino.SoftwareSerial.begin(ss_rxPin,ss_txPin,ss_device_baud)` initialize software serial device on - `Arduino.SoftwareSerial.begin(ss_rxPin, ss_txPin, ss_device_baud)` initialize software serial device on
specified pins. specified pins.
Only one sofware serial device can be used at a time. Existing software serial instance will Only one sofware serial device can be used at a time. Existing software serial instance will
be be overwritten by calling this method, both in Python and on the arduino board. be be overwritten by calling this method, both in Python and on the arduino board.
@@ -168,11 +168,27 @@ serial connection.
```python ```python
#Software serial example #Software serial example
board.SoftwareSerial.begin(0,7,"19200") # Start software serial for transmit only (tx on pin 7) board.SoftwareSerial.begin(0, 7, "19200") # Start software serial for transmit only (tx on pin 7)
board.SoftwareSerial.write(" test ") #Send some data board.SoftwareSerial.write(" test ") #Send some data
response_char = board.SoftwareSerial.read() #read response character response_char = board.SoftwareSerial.read() #read response character
``` ```
**EEPROM**
- `Arduino.EEPROM.read(address)` reads a byte from the EEPROM
- `Arduino.EEPROM.write(address, value)` writes a byte to the EEPROM
- `Arduino.EEPROM.size()` returns size of the EEPROM
```python
#EEPROM read and write examples
location = 42
value = 10 # 0-255(byte)
board.EEPROM.write(location, 10)
print(board.EEPROM.read(location))
print('EEPROM size {size}'.format(size=board.EEPROM.size()))
```
**Misc** **Misc**
- `Arduino.close()` closes serial connection to the Arduino. - `Arduino.close()` closes serial connection to the Arduino.

View File

@@ -1,6 +1,7 @@
#include <SoftwareSerial.h> #include <SoftwareSerial.h>
#include <Wire.h> #include <Wire.h>
#include <Servo.h> #include <Servo.h>
#include <EEPROM.h>
SoftwareSerial *sserial = NULL; SoftwareSerial *sserial = NULL;
Servo servos[8]; Servo servos[8];
@@ -301,6 +302,19 @@ void SV_write_ms(String data) {
servos[pos].writeMicroseconds(uS); servos[pos].writeMicroseconds(uS);
} }
void sizeEEPROM() {
Serial.println(E2END + 1);
}
void EEPROMHandler(int mode, String data) {
String sdata[2];
split(sdata, 2, data, '%');
if (mode == 0) {
EEPROM.write(Str2int(sdata[0]), Str2int(sdata[1]));
} else {
Serial.println(EEPROM.read(Str2int(sdata[0])));
}
}
void SerialParser(void) { void SerialParser(void) {
char readChar[64]; char readChar[64];
@@ -377,7 +391,15 @@ void SerialParser(void) {
else if (cmd == "si") { else if (cmd == "si") {
shiftInHandler(data); shiftInHandler(data);
} }
else if (cmd == "eewr") {
EEPROMHandler(0, data);
}
else if (cmd == "eer") {
EEPROMHandler(1, data);
}
else if (cmd == "sz") {
sizeEEPROM();
}
} }
void setup() { void setup() {