things / potentiometer-xiao
README
Software
import Thing from "../../../src/lib/thing";
import Serializers from "../../../src/lib/osapjs/utils/serializers"
const readUint16 = Serializers.readUint16;
// the name given to us here is the "uniqueName" of the matched
// device, we use this as a kind of address
export default class button 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 getPotentiometer() {
let data = await this.send("getPotentiometerState", new Uint8Array([]));
let x = readUint16(data, 0) / 1023;
return x;
}
// then we can docs our API,
api = [
{
name: "getPotentiometer",
args: [],
return: "0 to 1"
}
]
}
Firmware
#include <osap.h>
#define PIN_POT 26
OSAP_Runtime osap;
OSAP_Gateway_USBSerial serLink(&Serial);
OSAP_Port_DeviceNames namePort("potentiometer");
uint8_t rgb[3] = {0, 0, 255};
boolean ledState = false;
uint16_t value = 0;
size_t onPotentiometerReq(uint8_t* data, size_t len, uint8_t* reply){
reply[0] = value & 0xFF;
reply[1] = value >> 8 & 0xFF;
return 2;
}
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();
}
OSAP_Port_Named getPotentiometerState("getPotentiometerState", onPotentiometerReq);
OSAP_Port_Named setRGB("setRGB", onRGBPacket);
OSAP_Port_Named setLED("setLED", onLEDPacket);
void setup() {
osap.begin();
analogWriteResolution(8);
pinMode(PIN_LED_R, OUTPUT);
pinMode(PIN_LED_G, OUTPUT);
pinMode(PIN_LED_B, OUTPUT);
updateRGB();
pinMode(PIN_POT, INPUT);
}
uint32_t debounceDelay = 5;
uint32_t lastValueCheck = 0;
void loop() {
osap.loop();
if (millis() > lastValueCheck + debounceDelay) {
value = analogRead(PIN_POT);
lastValueCheck = millis();
}
}