From ea124942c804b6f362d3be524b6ae085b9e179a3 Mon Sep 17 00:00:00 2001 From: RusKovv Date: Wed, 30 Apr 2014 23:47:16 +0200 Subject: [PATCH 1/3] * added EEPROM read and write methods. --- Arduino/arduino.py | 61 +++++++++++++++++++++++++++++++- sketches/prototype/prototype.ino | 26 ++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) diff --git a/Arduino/arduino.py b/Arduino/arduino.py index 58bc020..0213f17 100755 --- a/Arduino/arduino.py +++ b/Arduino/arduino.py @@ -24,7 +24,7 @@ def enumerate_serial_ports(): key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, path) except WindowsError: raise Exception - + for i in itertools.count(): try: val = winreg.EnumValue(key, i) @@ -110,6 +110,7 @@ class Arduino(object): self.sr = sr self.SoftwareSerial = SoftwareSerial(self) self.Servos = Servos(self) + self.EEPROM = EEPROM(self) def version(self): return get_version(self.sr) @@ -556,3 +557,61 @@ class SoftwareSerial(object): return response else: 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 + \ No newline at end of file diff --git a/sketches/prototype/prototype.ino b/sketches/prototype/prototype.ino index 867c360..d02e98e 100644 --- a/sketches/prototype/prototype.ino +++ b/sketches/prototype/prototype.ino @@ -1,6 +1,7 @@ #include #include #include +#include SoftwareSerial *sserial = NULL; Servo servos[8]; @@ -136,6 +137,7 @@ void AnalogHandler(int mode, String data){ if(mode<=0){ //read int pin = Str2int(data); Serial.println(analogRead(pin)); + }else{ String sdata[2]; split(sdata,2,data,'%'); @@ -147,6 +149,7 @@ void AnalogHandler(int mode, String data){ void ConfigurePinHandler(String data){ int pin = Str2int(data); + if(pin <=0){ pinMode(-pin,INPUT); }else{ @@ -155,6 +158,7 @@ void ConfigurePinHandler(String data){ } void shiftOutHandler(String data) { + String sdata[4]; split(sdata, 4, data, '%'); int dataPin = sdata[0].toInt(); @@ -301,6 +305,19 @@ void SV_write_ms(String data) { 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) { char readChar[64]; @@ -377,6 +394,15 @@ void SerialParser(void) { else if (cmd == "si") { shiftInHandler(data); } + else if (cmd == "eewr") { + EEPROMHandler(0, data); + } + else if (cmd == "eer") { + EEPROMHandler(1, data); + } + else if (cmd == "sz") { + sizeEEPROM(); + } } From 26a11807a17eb247c71408b4bbfb5b60192ec535 Mon Sep 17 00:00:00 2001 From: Ruslan Kovalov Date: Thu, 1 May 2014 00:11:28 +0200 Subject: [PATCH 2/3] added EEPROM description, pepification --- README.md | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index be6fad6..fd32c46 100644 --- a/README.md +++ b/README.md @@ -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: ```python -board = Arduino("9600", port = "COM3") #Windows example +board = Arduino("9600", port="COM3") #Windows example ``` ```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 argument: ```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 ``` @@ -142,7 +142,7 @@ board.analogWrite(11) #Set analog value (PWM) based on analog measurement **Servo Library Functionality** 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.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 @@ -158,7 +158,7 @@ board.Servos.detach(9) #free pin 9 **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. 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. @@ -168,11 +168,27 @@ serial connection. ```python #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 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** - `Arduino.close()` closes serial connection to the Arduino. From cbfc6a6a3dc54afbb80107ce1e6648d50ef99c6e Mon Sep 17 00:00:00 2001 From: RusKovv Date: Thu, 1 May 2014 00:24:02 +0200 Subject: [PATCH 3/3] small editing --- sketches/prototype/prototype.ino | 4 ---- 1 file changed, 4 deletions(-) diff --git a/sketches/prototype/prototype.ino b/sketches/prototype/prototype.ino index d02e98e..cee57ab 100644 --- a/sketches/prototype/prototype.ino +++ b/sketches/prototype/prototype.ino @@ -137,7 +137,6 @@ void AnalogHandler(int mode, String data){ if(mode<=0){ //read int pin = Str2int(data); Serial.println(analogRead(pin)); - }else{ String sdata[2]; split(sdata,2,data,'%'); @@ -149,7 +148,6 @@ void AnalogHandler(int mode, String data){ void ConfigurePinHandler(String data){ int pin = Str2int(data); - if(pin <=0){ pinMode(-pin,INPUT); }else{ @@ -158,7 +156,6 @@ void ConfigurePinHandler(String data){ } void shiftOutHandler(String data) { - String sdata[4]; split(sdata, 4, data, '%'); int dataPin = sdata[0].toInt(); @@ -403,7 +400,6 @@ void SerialParser(void) { else if (cmd == "sz") { sizeEEPROM(); } - } void setup() {