Added display support

This commit is contained in:
Morten Kals
2019-05-02 16:27:49 -07:00
parent c1e4a8703d
commit 87ed24da88
5 changed files with 128 additions and 170 deletions

View File

@@ -12,7 +12,7 @@ if sys.platform.startswith('win'):
else: else:
import glob import glob
libraryVersion = 'V0.4' libraryVersion = 'V0.5'
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@@ -499,7 +499,7 @@ class Arduino(object):
# Bryan's attempt at extending this package's functionality to include the ability to write text to the Arduino screen. # Bryan's attempt at extending this package's functionality to include the ability to write text to the Arduino screen.
# I will ignore drawing anything fancy, and just focus on displaying text. If fancy drawings are seen to be useful, they can be added later. # I will ignore drawing anything fancy, and just focus on displaying text. If fancy drawings are seen to be useful, they can be added later.
# There will be several functions. One to clear and reset the display, # There will be several functions. One to clear and reset the display,
# one to set up the display to draw things, and one to actually draw the text (the most resource-intensive, so should be called at the end) # one to set up the display to draw things, and one to actually draw the text (the most resource-intensive, so should be called at the end)
@@ -507,7 +507,7 @@ class Arduino(object):
def setupDisplay(self): def setupDisplay(self):
""" """
Sets up a I2C-connected SSD1306 display to receive data. This sends Sets up a I2C-connected SSD1306 display to receive data. This sends
the command 'scs' to the Arduino (SCreen Setup). the command 'scs' to the Arduino (SCreen Setup).
@@ -552,12 +552,12 @@ class Arduino(object):
def displayText(self, text, fontsize=1): def displayText(self, text, fontsize=1):
""" """
Sets a string of text to be displayed on the connected SSD1306 Sets a string of text to be displayed on the connected SSD1306
display. It sends the command 'dst' to the Arduino. display. It sends the command 'dst' to the Arduino.
Inputs: Inputs:
text: A string, containing the characters to be displayed. text: A string, containing the characters to be displayed.
fontsize: A single integer value, adjusts the size of the fontsize: A single integer value, adjusts the size of the
characters. Please only pass numbers between 1 and 9. characters. Please only pass numbers between 1 and 9.
""" """

View File

@@ -192,6 +192,10 @@ print(board.EEPROM.read(location))
print('EEPROM size {size}'.format(size=board.EEPROM.size())) print('EEPROM size {size}'.format(size=board.EEPROM.size()))
``` ```
**Screen**
Display text on an LCD screen.
Use the function displayText(text, fontSize = 1) to display a string on the screen.
**DHT** **DHT**
- `Arduino.dht(pin, module)` reads sensor values from the DHT sensor connected at the specified pin. - `Arduino.dht(pin, module)` reads sensor values from the DHT sensor connected at the specified pin.

View File

@@ -5,7 +5,7 @@ with open("README.md", "r") as fh:
setuptools.setup( setuptools.setup(
name="arduino-python3", name="arduino-python3",
version="0.4.1", version="0.5",
install_requires=['pyserial'], install_requires=['pyserial'],
author="Morten Kals", author="Morten Kals",
author_email="morten@kals.no", author_email="morten@kals.no",

View File

@@ -9,7 +9,7 @@
#include <Adafruit_SSD1306.h> #include <Adafruit_SSD1306.h>
void Version(){ void Version(){
Serial.println(F("V0.4")); Serial.println(F("V0.5"));
} }
@@ -88,7 +88,7 @@ uint8_t readCapacitivePin(String data) {
else if (*pin & bitmask) { cycles = 16;} else if (*pin & bitmask) { cycles = 16;}
// Discharge the pin again by setting it low and output // Discharge the pin again by setting it low and output
// It's important to leave the pins low if you want to // It's important to leave the pins low if you want to
// be able to touch more than 1 sensor at a time - if // be able to touch more than 1 sensor at a time - if
// the sensor is left pulled high, when you touch // the sensor is left pulled high, when you touch
// two sensors, your body will transfer the charge between // two sensors, your body will transfer the charge between
@@ -118,12 +118,12 @@ void Tone(String data){
delay(pause); delay(pause);
noTone(pin); noTone(pin);
} }
} }
void ToneNo(String data){ void ToneNo(String data){
int pin = Str2int(data); int pin = Str2int(data);
noTone(pin); noTone(pin);
} }
void DigitalHandler(int mode, String data){ void DigitalHandler(int mode, String data){
int pin = Str2int(data); int pin = Str2int(data);
@@ -161,7 +161,7 @@ void ConfigurePinHandler(String data){
} }
} }
void shiftOutHandler(String data) { void shiftOutHandler(String data) {
String sdata[4]; String sdata[4];
split(sdata, 4, data, '%'); split(sdata, 4, data, '%');
int dataPin = sdata[0].toInt(); int dataPin = sdata[0].toInt();
@@ -207,10 +207,10 @@ void SS_write(String data) {
char buffer[len]; char buffer[len];
data.toCharArray(buffer,len); data.toCharArray(buffer,len);
Serial.println("ss OK"); Serial.println("ss OK");
sserial->write(buffer); sserial->write(buffer);
} }
void SS_read(String data) { void SS_read(String data) {
char c = sserial->read(); char c = sserial->read();
Serial.println(c); Serial.println(c);
} }
@@ -219,10 +219,10 @@ void pulseInHandler(String data){
long duration; long duration;
if(pin <=0){ if(pin <=0){
pinMode(-pin, INPUT); pinMode(-pin, INPUT);
duration = pulseIn(-pin, LOW); duration = pulseIn(-pin, LOW);
}else{ }else{
pinMode(pin, INPUT); pinMode(pin, INPUT);
duration = pulseIn(pin, HIGH); duration = pulseIn(pin, HIGH);
} }
Serial.println(duration); Serial.println(duration);
} }
@@ -238,7 +238,7 @@ void pulseInSHandler(String data){
delayMicroseconds(5); delayMicroseconds(5);
digitalWrite(-pin, HIGH); digitalWrite(-pin, HIGH);
pinMode(-pin, INPUT); pinMode(-pin, INPUT);
duration = pulseIn(-pin, LOW); duration = pulseIn(-pin, LOW);
}else{ }else{
pinMode(pin, OUTPUT); pinMode(pin, OUTPUT);
digitalWrite(pin, LOW); digitalWrite(pin, LOW);
@@ -247,7 +247,7 @@ void pulseInSHandler(String data){
delayMicroseconds(5); delayMicroseconds(5);
digitalWrite(pin, LOW); digitalWrite(pin, LOW);
pinMode(pin, INPUT); pinMode(pin, INPUT);
duration = pulseIn(pin, HIGH); duration = pulseIn(pin, HIGH);
} }
Serial.println(duration); Serial.println(duration);
} }
@@ -315,8 +315,8 @@ void sizeEEPROM() {
void EEPROMHandler(int mode, String data) { void EEPROMHandler(int mode, String data) {
String sdata[2]; String sdata[2];
split(sdata, 2, data, '%'); split(sdata, 2, data, '%');
if (mode == 0) { if (mode == 0) {
EEPROM.write(Str2int(sdata[0]), Str2int(sdata[1])); EEPROM.write(Str2int(sdata[0]), Str2int(sdata[1]));
} else { } else {
Serial.println(EEPROM.read(Str2int(sdata[0]))); Serial.println(EEPROM.read(Str2int(sdata[0])));
} }
@@ -326,11 +326,11 @@ int dhtSensorPin = -1;
DHT dhtSensor(dhtSensorPin, DHT11); DHT dhtSensor(dhtSensorPin, DHT11);
void dht(String data) { void dht(String data) {
String sdata[2]; String sdata[2];
int dataPin = sdata[0].toInt(); int dataPin = sdata[0].toInt();
int sensorNumber = sdata[1].toInt(); int sensorNumber = sdata[1].toInt();
int sensorType = DHT11; // assume DHT11 as default int sensorType = DHT11; // assume DHT11 as default
if (sensorNumber == 1) { if (sensorNumber == 1) {
// split(sdata, 2, data, '%'); // split(sdata, 2, data, '%');
@@ -355,12 +355,12 @@ void dht(String data) {
float h = dhtSensor.readHumidity(); float h = dhtSensor.readHumidity();
// Read temperature as Celsius (the default) // Read temperature as Celsius (the default)
float t = dhtSensor.readTemperature(); float t = dhtSensor.readTemperature();
if (isnan(h) || isnan(t)) { if (isnan(h) || isnan(t)) {
Serial.println("0&0&0"); Serial.println("0&0&0");
return; return;
} }
float hic = dhtSensor.computeHeatIndex(t, h, false); float hic = dhtSensor.computeHeatIndex(t, h, false);
Serial.println(String(h) + "&" + String(t) + "&" + String(hic)); Serial.println(String(h) + "&" + String(t) + "&" + String(hic));
} }
@@ -371,26 +371,26 @@ void dht(String data) {
// A large function to set up the display, clear it from previously, set a line(s) of text, and write it. // A large function to set up the display, clear it from previously, set a line(s) of text, and write it.
// TODO: I was unable to break this apart into different functions to play around with in Python, due to issues with variable scope. I will come back to this. // TODO: I was unable to break this apart into different functions to play around with in Python, due to issues with variable scope. I will come back to this.
void displayText(String data) { void displayText(String data) {
int screen_height = 32; int screen_height = 32;
int screen_width = 128; int screen_width = 128;
// The analog pin number connected to the reset pin of the screen (SDA). // The analog pin number connected to the reset pin of the screen (SDA).
int reset_pin = 4; int reset_pin = 4;
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins). // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins).
Adafruit_SSD1306 display(screen_width, screen_height, &Wire, 4); Adafruit_SSD1306 display(screen_width, screen_height, &Wire, 4);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
// Clears previously displayed data and resets the cursor and text colour. // Clears previously displayed data and resets the cursor and text colour.
display.clearDisplay(); display.clearDisplay();
display.setCursor(0,0); display.setCursor(0,0);
display.setTextColor(WHITE); display.setTextColor(WHITE);
// The input data string contains the text to be written, along with %#, // The input data string contains the text to be written, along with %#,
// where # is the font size. This sets the font size by reading the last // where # is the font size. This sets the font size by reading the last
// character of the input data (by default it is 1), and converting it // character of the input data (by default it is 1), and converting it
// to an int. Once that is done, the last two characters are deleted. // to an int. Once that is done, the last two characters are deleted.
int font_size = data[data.length() - 1] - 48; int font_size = data[data.length() - 1] - 48;
display.setTextSize(font_size); display.setTextSize(font_size);
@@ -414,64 +414,64 @@ void SerialParser(void) {
// separate command from associated data // separate command from associated data
String cmd = read_.substring(1,idx1); String cmd = read_.substring(1,idx1);
String data = read_.substring(idx1+1,idx2); String data = read_.substring(idx1+1,idx2);
// determine command sent // determine command sent
if (cmd == "dw") { if (cmd == "dw") {
DigitalHandler(1, data); DigitalHandler(1, data);
} }
else if (cmd == "dr") { else if (cmd == "dr") {
DigitalHandler(0, data); DigitalHandler(0, data);
} }
else if (cmd == "aw") { else if (cmd == "aw") {
AnalogHandler(1, data); AnalogHandler(1, data);
} }
else if (cmd == "ar") { else if (cmd == "ar") {
AnalogHandler(0, data); AnalogHandler(0, data);
} }
else if (cmd == "pm") { else if (cmd == "pm") {
ConfigurePinHandler(data); ConfigurePinHandler(data);
} }
else if (cmd == "ps") { else if (cmd == "ps") {
pulseInSHandler(data); pulseInSHandler(data);
} }
else if (cmd == "pi") { else if (cmd == "pi") {
pulseInHandler(data); pulseInHandler(data);
} }
else if (cmd == "ss") { else if (cmd == "ss") {
SS_set(data); SS_set(data);
} }
else if (cmd == "sw") { else if (cmd == "sw") {
SS_write(data); SS_write(data);
} }
else if (cmd == "sr") { else if (cmd == "sr") {
SS_read(data); SS_read(data);
} }
else if (cmd == "sva") { else if (cmd == "sva") {
SV_add(data); SV_add(data);
} }
else if (cmd == "svr") { else if (cmd == "svr") {
SV_read(data); SV_read(data);
} }
else if (cmd == "svw") { else if (cmd == "svw") {
SV_write(data); SV_write(data);
} }
else if (cmd == "svwm") { else if (cmd == "svwm") {
SV_write_ms(data); SV_write_ms(data);
} }
else if (cmd == "svd") { else if (cmd == "svd") {
SV_remove(data); SV_remove(data);
} }
else if (cmd == "version") { else if (cmd == "version") {
Version(); Version();
} }
else if (cmd == "to") { else if (cmd == "to") {
Tone(data); Tone(data);
} }
else if (cmd == "nto") { else if (cmd == "nto") {
ToneNo(data); ToneNo(data);
} }
else if (cmd == "cap") { else if (cmd == "cap") {
readCapacitivePin(data); readCapacitivePin(data);
} }
else if (cmd == "so") { else if (cmd == "so") {
shiftOutHandler(data); shiftOutHandler(data);
@@ -480,12 +480,12 @@ void SerialParser(void) {
shiftInHandler(data); shiftInHandler(data);
} }
else if (cmd == "eewr") { else if (cmd == "eewr") {
EEPROMHandler(0, data); EEPROMHandler(0, data);
} }
else if (cmd == "eer") { else if (cmd == "eer") {
EEPROMHandler(1, data); EEPROMHandler(1, data);
} }
else if (cmd == "sz") { else if (cmd == "sz") {
sizeEEPROM(); sizeEEPROM();
} }
else if (cmd == "dht") { else if (cmd == "dht") {
@@ -498,7 +498,7 @@ void SerialParser(void) {
} }
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
while (!Serial) { while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only ; // wait for serial port to connect. Needed for Leonardo only
} }

View File

@@ -2,10 +2,9 @@
#include <Wire.h> #include <Wire.h>
#include <Servo.h> #include <Servo.h>
#include <EEPROM.h> #include <EEPROM.h>
#include <DHT.h>
void Version(){ void Version(){
Serial.println(F("V0.4")); Serial.println(F("V0.5"));
} }
@@ -84,7 +83,7 @@ uint8_t readCapacitivePin(String data) {
else if (*pin & bitmask) { cycles = 16;} else if (*pin & bitmask) { cycles = 16;}
// Discharge the pin again by setting it low and output // Discharge the pin again by setting it low and output
// It's important to leave the pins low if you want to // It's important to leave the pins low if you want to
// be able to touch more than 1 sensor at a time - if // be able to touch more than 1 sensor at a time - if
// the sensor is left pulled high, when you touch // the sensor is left pulled high, when you touch
// two sensors, your body will transfer the charge between // two sensors, your body will transfer the charge between
@@ -114,12 +113,12 @@ void Tone(String data){
delay(pause); delay(pause);
noTone(pin); noTone(pin);
} }
} }
void ToneNo(String data){ void ToneNo(String data){
int pin = Str2int(data); int pin = Str2int(data);
noTone(pin); noTone(pin);
} }
void DigitalHandler(int mode, String data){ void DigitalHandler(int mode, String data){
int pin = Str2int(data); int pin = Str2int(data);
@@ -157,7 +156,7 @@ void ConfigurePinHandler(String data){
} }
} }
void shiftOutHandler(String data) { void shiftOutHandler(String data) {
String sdata[4]; String sdata[4];
split(sdata, 4, data, '%'); split(sdata, 4, data, '%');
int dataPin = sdata[0].toInt(); int dataPin = sdata[0].toInt();
@@ -203,10 +202,10 @@ void SS_write(String data) {
char buffer[len]; char buffer[len];
data.toCharArray(buffer,len); data.toCharArray(buffer,len);
Serial.println("ss OK"); Serial.println("ss OK");
sserial->write(buffer); sserial->write(buffer);
} }
void SS_read(String data) { void SS_read(String data) {
char c = sserial->read(); char c = sserial->read();
Serial.println(c); Serial.println(c);
} }
@@ -215,10 +214,10 @@ void pulseInHandler(String data){
long duration; long duration;
if(pin <=0){ if(pin <=0){
pinMode(-pin, INPUT); pinMode(-pin, INPUT);
duration = pulseIn(-pin, LOW); duration = pulseIn(-pin, LOW);
}else{ }else{
pinMode(pin, INPUT); pinMode(pin, INPUT);
duration = pulseIn(pin, HIGH); duration = pulseIn(pin, HIGH);
} }
Serial.println(duration); Serial.println(duration);
} }
@@ -234,7 +233,7 @@ void pulseInSHandler(String data){
delayMicroseconds(5); delayMicroseconds(5);
digitalWrite(-pin, HIGH); digitalWrite(-pin, HIGH);
pinMode(-pin, INPUT); pinMode(-pin, INPUT);
duration = pulseIn(-pin, LOW); duration = pulseIn(-pin, LOW);
}else{ }else{
pinMode(pin, OUTPUT); pinMode(pin, OUTPUT);
digitalWrite(pin, LOW); digitalWrite(pin, LOW);
@@ -243,7 +242,7 @@ void pulseInSHandler(String data){
delayMicroseconds(5); delayMicroseconds(5);
digitalWrite(pin, LOW); digitalWrite(pin, LOW);
pinMode(pin, INPUT); pinMode(pin, INPUT);
duration = pulseIn(pin, HIGH); duration = pulseIn(pin, HIGH);
} }
Serial.println(duration); Serial.println(duration);
} }
@@ -311,55 +310,13 @@ void sizeEEPROM() {
void EEPROMHandler(int mode, String data) { void EEPROMHandler(int mode, String data) {
String sdata[2]; String sdata[2];
split(sdata, 2, data, '%'); split(sdata, 2, data, '%');
if (mode == 0) { if (mode == 0) {
EEPROM.write(Str2int(sdata[0]), Str2int(sdata[1])); EEPROM.write(Str2int(sdata[0]), Str2int(sdata[1]));
} else { } else {
Serial.println(EEPROM.read(Str2int(sdata[0]))); Serial.println(EEPROM.read(Str2int(sdata[0])));
} }
} }
int dhtSensorPin = -1;
DHT dhtSensor(dhtSensorPin, DHT11);
void dht(String data) {
String sdata[2];
split(sdata, 2, data, '%');
int dataPin = sdata[0].toInt();
int sensorNumber = sdata[1].toInt();
int sensorType = DHT11; // assume DHT11 as default
if (sensorNumber == 1) {
sensorType = DHT12;
} else if (sensorNumber == 2) {
sensorType = DHT21;
} else if (sensorNumber == 2) {
sensorType = DHT22;
} else if (sensorNumber == 2) {
sensorType = AM2301;
}
// do not initialize new sensor if we are reading repeatedly from same sensor
if (dataPin != dhtSensorPin) {
dhtSensorPin = dataPin;
dhtSensor = DHT(dataPin, sensorType);
dhtSensor.begin();
}
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dhtSensor.readHumidity();
// Read temperature as Celsius (the default)
float t = dhtSensor.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("0&0&0");
return;
}
float hic = dhtSensor.computeHeatIndex(t, h, false);
Serial.println(String(h) + "&" + String(t) + "&" + String(hic));
}
void SerialParser(void) { void SerialParser(void) {
char readChar[64]; char readChar[64];
@@ -371,64 +328,64 @@ void SerialParser(void) {
// separate command from associated data // separate command from associated data
String cmd = read_.substring(1,idx1); String cmd = read_.substring(1,idx1);
String data = read_.substring(idx1+1,idx2); String data = read_.substring(idx1+1,idx2);
// determine command sent // determine command sent
if (cmd == "dw") { if (cmd == "dw") {
DigitalHandler(1, data); DigitalHandler(1, data);
} }
else if (cmd == "dr") { else if (cmd == "dr") {
DigitalHandler(0, data); DigitalHandler(0, data);
} }
else if (cmd == "aw") { else if (cmd == "aw") {
AnalogHandler(1, data); AnalogHandler(1, data);
} }
else if (cmd == "ar") { else if (cmd == "ar") {
AnalogHandler(0, data); AnalogHandler(0, data);
} }
else if (cmd == "pm") { else if (cmd == "pm") {
ConfigurePinHandler(data); ConfigurePinHandler(data);
} }
else if (cmd == "ps") { else if (cmd == "ps") {
pulseInSHandler(data); pulseInSHandler(data);
} }
else if (cmd == "pi") { else if (cmd == "pi") {
pulseInHandler(data); pulseInHandler(data);
} }
else if (cmd == "ss") { else if (cmd == "ss") {
SS_set(data); SS_set(data);
} }
else if (cmd == "sw") { else if (cmd == "sw") {
SS_write(data); SS_write(data);
} }
else if (cmd == "sr") { else if (cmd == "sr") {
SS_read(data); SS_read(data);
} }
else if (cmd == "sva") { else if (cmd == "sva") {
SV_add(data); SV_add(data);
} }
else if (cmd == "svr") { else if (cmd == "svr") {
SV_read(data); SV_read(data);
} }
else if (cmd == "svw") { else if (cmd == "svw") {
SV_write(data); SV_write(data);
} }
else if (cmd == "svwm") { else if (cmd == "svwm") {
SV_write_ms(data); SV_write_ms(data);
} }
else if (cmd == "svd") { else if (cmd == "svd") {
SV_remove(data); SV_remove(data);
} }
else if (cmd == "version") { else if (cmd == "version") {
Version(); Version();
} }
else if (cmd == "to") { else if (cmd == "to") {
Tone(data); Tone(data);
} }
else if (cmd == "nto") { else if (cmd == "nto") {
ToneNo(data); ToneNo(data);
} }
else if (cmd == "cap") { else if (cmd == "cap") {
readCapacitivePin(data); readCapacitivePin(data);
} }
else if (cmd == "so") { else if (cmd == "so") {
shiftOutHandler(data); shiftOutHandler(data);
@@ -437,21 +394,18 @@ void SerialParser(void) {
shiftInHandler(data); shiftInHandler(data);
} }
else if (cmd == "eewr") { else if (cmd == "eewr") {
EEPROMHandler(0, data); EEPROMHandler(0, data);
}
else if (cmd == "eer") {
EEPROMHandler(1, data);
}
else if (cmd == "sz") {
sizeEEPROM();
} }
else if (cmd == "dht") { else if (cmd == "eer") {
dht(data); EEPROMHandler(1, data);
}
else if (cmd == "sz") {
sizeEEPROM();
} }
} }
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
while (!Serial) { while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only ; // wait for serial port to connect. Needed for Leonardo only
} }