things / display-OLED-xiao

layout
schematic
preview

README

Software

import Thing from "../../../src/lib/thing";

// the name given to us here is the "uniqueName" of the matched
// device, we use this as a kind of address
export default class display extends Thing {
  // we can define methods that interact with the device,
  // using the 'send' primitive, which writes data (bytes) and gets data (bytes)
  async setRGB(r, g, b) {
    let datagram = new Uint8Array(3);
    datagram[0] = r * 255;
    datagram[1] = g * 255;
    datagram[2] = b * 255;
    await this.send("setRGB", datagram);
  }

  async setLED(state) {
    let datagram = new Uint8Array([state > 0]);
    await this.send("setLED", datagram);
  }

  async setText(text, textSize=2) {
    try {
      const utf8Encode = new TextEncoder();
      const datagram_txt = utf8Encode.encode(text);
      const datagram = new Uint8Array(datagram_txt.length+1);
      datagram[0] = textSize;
      for (let i=0; i < datagram_txt.length; i++) {
        datagram[i+1] = datagram_txt[i];
      }
      await this.send("setText", datagram);
    } catch (err) {
      console.error(err);
    }
  }

  api = [
    {
      name: "setText",
      args: [
      "text: string",
      "textSize=2: 1 to 16"
      ]
    }
  ]
}

Firmware

#include <osap.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

#define X_POS 0
#define Y_POS 0
#define TXT_SIZE 1 //Define Size 

#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT);

OSAP_Runtime osap;

OSAP_Gateway_USBSerial serLink(&Serial);

OSAP_Port_DeviceNames namePort("display");

boolean buttonStateA = false;
boolean buttonStateB = false;
uint8_t rgb[3] = {0, 0, 255};
boolean ledState = false;

void updateRGB() {
  if (ledState) {
    analogWrite(PIN_LED_R, 255-rgb[0]);
    analogWrite(PIN_LED_G, 255-rgb[1]);
    analogWrite(PIN_LED_B, 255-rgb[2]);
  } else {
    analogWrite(PIN_LED_R, 255);
    analogWrite(PIN_LED_G, 255);
    analogWrite(PIN_LED_B, 255);
  }
}

void onRGBPacket(uint8_t* data, size_t len){
  rgb[0] = data[0];
  rgb[1] = data[1];
  rgb[2] = data[2];
  ledState = true;
  updateRGB();
}

void onLEDPacket(uint8_t* data, size_t len){
  ledState = data[0] > 0;
  updateRGB();
}

void ontextPacket(uint8_t* data, size_t len) {
  // first byte is the text size
  uint8_t txt_size = data[0];
  // the rest is the text
  // add null terminator
  char* txt = (char*) malloc(len);
  memcpy(txt, data+1, len-1);
  txt[len-1] = '\0';
  
  display.clearDisplay();
  display.setCursor(X_POS, Y_POS);
  display.setTextSize(txt_size);
  display.print(txt);
  display.display();
}

OSAP_Port_Named setRGB("setRGB", onRGBPacket);
OSAP_Port_Named setLED("setLED", onLEDPacket);
OSAP_Port_Named setText("setText", ontextPacket);

void setup() {
  osap.begin();
  analogWriteResolution(8);
  pinMode(PIN_LED_R, OUTPUT);
  pinMode(PIN_LED_G, OUTPUT);
  pinMode(PIN_LED_B, OUTPUT);
  updateRGB();
  
  display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);

  display.clearDisplay();
  display.display();

  display.clearDisplay();
  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);
  display.setTextWrap(true);
}

void loop() {
  osap.loop();
}