From e88c87e73e3e2d3df35c46850ac66e4504991099 Mon Sep 17 00:00:00 2001 From: Bryan Poteryko Date: Thu, 2 May 2019 16:02:38 -0700 Subject: [PATCH] Added SSD1306 display support. Can now send text to a connected display via displayText(text, fontsize).I have commented out the servo control code so that it doesn't fill up the Arduino Nano's memory. --- Arduino/arduino.py | 70 ++++++++++++++++ sketches/prototype/prototype.ino | 137 +++++++++++++++++++++---------- 2 files changed, 162 insertions(+), 45 deletions(-) diff --git a/Arduino/arduino.py b/Arduino/arduino.py index fcfe3d3..3bcc0fb 100755 --- a/Arduino/arduino.py +++ b/Arduino/arduino.py @@ -496,7 +496,77 @@ def dht(self, pin, module = 0): except: return None + # 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. + # 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) + + + # Let's do this. + + def setupDisplay(self): + """ + Sets up a I2C-connected SSD1306 display to receive data. This sends + the command 'scs' to the Arduino (SCreen Setup). + + + Inputs: (TODO) + width: width of the display, in pixels. + height: height of the display, in pixels. + + """ + + width = 128 + height = 32 + + cmd_str = build_cmd_str("scs", (width, height)) + try: + self.sr.write(str.encode(cmd_str)) + self.sr.flush() + except: + pass + # not sure what this does + # rd = self.sr.readline().decode("utf-8").replace("\r\n", "") + # try: + # return int(rd) + # except: + # return 0 + + + def clearDisplay(self): + """ + Clears the connected display from its previously-set values. Should + be called before writing anything new to the display. + + This sends the command 'scc' to the Arduino (SCreen Clear). + """ + + cmd_str = build_cmd_str("scc") + try: + self.sr.write(str.encode(cmd_str)) + self.sr.flush() + except: + pass + + + def displayText(self, text, fontsize=1): + """ + Sets a string of text to be displayed on the connected SSD1306 + display. It sends the command 'dst' to the Arduino. + + Inputs: + text: A string, containing the characters to be displayed. + fontsize: A single integer value, adjusts the size of the + characters. Please only pass numbers between 1 and 9. + """ + + cmd_str = build_cmd_str("dst", (text, fontsize)) + try: + self.sr.write(str.encode(cmd_str)) + self.sr.flush() + except: + pass class Shrimp(Arduino): diff --git a/sketches/prototype/prototype.ino b/sketches/prototype/prototype.ino index 418c297..870b443 100644 --- a/sketches/prototype/prototype.ino +++ b/sketches/prototype/prototype.ino @@ -1,16 +1,20 @@ #include #include -#include +//#include #include #include +// NOTE: Requires new libraries for screens: +#include +#include + void Version(){ Serial.println(F("V0.4")); } SoftwareSerial *sserial = NULL; -Servo servos[8]; +//Servo servos[8]; int servo_pins[] = {0, 0, 0, 0, 0, 0, 0, 0}; boolean connected = false; @@ -249,59 +253,59 @@ void pulseInSHandler(String data){ } void SV_add(String data) { - String sdata[3]; - split(sdata,3,data,'%'); - int pin = Str2int(sdata[0]); - int min = Str2int(sdata[1]); - int max = Str2int(sdata[2]); - int pos = -1; - for (int i = 0; i<8;i++) { - if (servo_pins[i] == pin) { //reset in place - servos[pos].detach(); - servos[pos].attach(pin, min, max); - servo_pins[pos] = pin; - Serial.println(pos); - return; - } - } - for (int i = 0; i<8;i++) { - if (servo_pins[i] == 0) {pos = i;break;} // find spot in servo array - } - if (pos == -1) {;} //no array position available! - else { - servos[pos].attach(pin, min, max); - servo_pins[pos] = pin; - Serial.println(pos); - } +// String sdata[3]; +// split(sdata,3,data,'%'); +// int pin = Str2int(sdata[0]); +// int min = Str2int(sdata[1]); +// int max = Str2int(sdata[2]); +// int pos = -1; +// for (int i = 0; i<8;i++) { +// if (servo_pins[i] == pin) { //reset in place +// servos[pos].detach(); +// servos[pos].attach(pin, min, max); +// servo_pins[pos] = pin; +// Serial.println(pos); +// return; +// } +// } +// for (int i = 0; i<8;i++) { +// if (servo_pins[i] == 0) {pos = i;break;} // find spot in servo array +// } +// if (pos == -1) {;} //no array position available! +// else { +// servos[pos].attach(pin, min, max); +// servo_pins[pos] = pin; +// Serial.println(pos); +// } } void SV_remove(String data) { - int pos = Str2int(data); - servos[pos].detach(); - servo_pins[pos] = 0; +// int pos = Str2int(data); +// servos[pos].detach(); +// servo_pins[pos] = 0; } void SV_read(String data) { - int pos = Str2int(data); - int angle; - angle = servos[pos].read(); - Serial.println(angle); +// int pos = Str2int(data); +// int angle; +// angle = servos[pos].read(); +// Serial.println(angle); } void SV_write(String data) { - String sdata[2]; - split(sdata,2,data,'%'); - int pos = Str2int(sdata[0]); - int angle = Str2int(sdata[1]); - servos[pos].write(angle); +// String sdata[2]; +// split(sdata,2,data,'%'); +// int pos = Str2int(sdata[0]); +// int angle = Str2int(sdata[1]); +// servos[pos].write(angle); } void SV_write_ms(String data) { - String sdata[2]; - split(sdata,2,data,'%'); - int pos = Str2int(sdata[0]); - int uS = Str2int(sdata[1]); - servos[pos].writeMicroseconds(uS); +// String sdata[2]; +// split(sdata,2,data,'%'); +// int pos = Str2int(sdata[0]); +// int uS = Str2int(sdata[1]); +// servos[pos].writeMicroseconds(uS); } void sizeEEPROM() { @@ -324,13 +328,13 @@ 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; + // split(sdata, 2, data, '%'); + sensorType = DHT12; } else if (sensorNumber == 2) { sensorType = DHT21; } else if (sensorNumber == 2) { @@ -361,6 +365,45 @@ void dht(String data) { Serial.println(String(h) + "&" + String(t) + "&" + String(hic)); } + +// TODO: Fix the stuttering problem being caused by the program calling the display parameter multiple times. + +// 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. +void displayText(String data) { + + int screen_height = 32; + int screen_width = 128; + + // The analog pin number connected to the reset pin of the screen (SDA). + int reset_pin = 4; + + // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins). + Adafruit_SSD1306 display(screen_width, screen_height, &Wire, 4); + + display.begin(SSD1306_SWITCHCAPVCC, 0x3C); + + // Clears previously displayed data and resets the cursor and text colour. + display.clearDisplay(); + display.setCursor(0,0); + display.setTextColor(WHITE); + + // 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 + // 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. + int font_size = data[data.length() - 1] - 48; + display.setTextSize(font_size); + data.remove(data.length() - 2); + + display.print(data); + + // Prints the above to the display. Relatively resource-intensive. + display.display(); + delay(50); +} + +// TODO: try a switch statement, might save memory. void SerialParser(void) { char readChar[64]; Serial.readBytesUntil(33,readChar,64); @@ -448,6 +491,10 @@ void SerialParser(void) { else if (cmd == "dht") { dht(data); } + // screen display functions go here. + else if (cmd == "dst") { + displayText(data); + } } void setup() {