things / mosfet-xiao
README
H-Bridge Stepper
Software
import Thing from "../../../src/lib/thing";
export default class mosfet extends Thing {
async setGate(duty: number) {
if (duty > 1) duty = 1;
if (duty < 0) duty = 0;
await this.send("setGate", new Uint8Array([255 * duty]));
}
async pulseGate(duty: number, duration: number){
if (duty > 1) duty = 1;
if (duty < 0) duty = 0;
if (duration > 255) duration = 255;
if (duration < 0) duration = 0;
await this.send("pulseGate", new Uint8Array([255 * duty, duration]));
}
public api = [
{
name: "setGate",
args: [
"duty: 0 to 1"
],
}, {
name: "pulseGate",
args: [
"duty: 0 to 1",
"duration: 0 to 255 (ms)"
]
}
]
}
Firmware
#include <osap.h>
#define PIN_LED 27
#define PIN_THERM 28
#define PIN_GATE 29
OSAP_Runtime osap;
OSAP_Gateway_USBSerial serLink(&Serial);
OSAP_Port_DeviceNames namePort("mosfet");
void setGate(uint8_t* data, size_t len){
// we did the float -> int conversion in js
uint8_t duty = data[0];
analogWrite(PIN_GATE, duty);
digitalWrite(PIN_LED, duty > 0 ? HIGH : LOW);
}
OSAP_Port_Named setGate_port("setGate", setGate);
uint32_t pulseStart = 0;
uint32_t pulseDuration = 0;
uint32_t pulseDuty = 0;
void pulseGate(uint8_t* data, size_t len){
// duty (0-255), duration (0-255 ms)
pulseDuty = data[0];
pulseDuration = data[1];
// and begin,
pulseStart = millis();
analogWrite(PIN_GATE, pulseDuty);
digitalWrite(PIN_LED, pulseDuty > 0 ? HIGH : LOW);
}
OSAP_Port_Named pulseGate_port("pulseGate", pulseGate);
void setup() {
osap.begin();
analogWriteResolution(8);
pinMode(PIN_LED, OUTPUT);
pinMode(PIN_GATE, OUTPUT);
}
void loop() {
osap.loop();
if(pulseStart != 0){
if(pulseStart + pulseDuration < millis()){
analogWrite(PIN_GATE, 0);
digitalWrite(PIN_LED, LOW);
pulseStart = 0;
}
}
}