xxxxxxxxxx
148
var scrollValues = [];
var state = 0;
function setup() {
for (let i = 0; i < 5; i++) {
scrollValues.push(new uiFloat(500));
}
noStroke();
noScrolling();
createCanvas(windowWidth, windowHeight);
loadGoogleFont("Droid Sans");
textFont("Droid Sans");
frameRate(30);
serialConnect(true);
}
//let interval = options.dmx_speed ? (1000 / options.dmx_speed) : 46;
const dmxData = new Uint8Array(30);
let timer = 0;
function draw() {
background(200);
uiUpdateSimple();
if (!portIsOpen && uiButton("Connect", 255, 200, 30, 50, 100).clicked) {
serialConnect();
}
if (portIsOpen) {
for (let i = 0; i < scrollValues.length; i++) {
if (
uiScrollbar(i + 1, 0, 1000, scrollValues[i], 200, 50, 130 + i * 30)
.clicked
) {
let timeMove = 500;
sendCmd(
i + 1,
SERVO_MOVE_TIME_WRITE,
lowerByte(Math.round(scrollValues[i].get())), higherByte(Math.round(scrollValues[i].get())),
lowerByte(timeMove),higherByte(timeMove)
);
}
}
}
if (
portIsOpen &&
uiButton("Read servo 1 pos", 255, 200, 30, 50, 50).clicked
) {
//sendCmd(1, SERVO_POS_READ);
getResponse(1,SERVO_POS_READ).then(responseValue => {
print('value:', responseValue);
})
}
}
function lowerByte(value) {
return value & 0xFF;
}
function higherByte(value) {
return (value >> 8) & 0xFF;
}
function word(low, high) {
return (high << 8) | low;
}
readyToWrite = false;
let SERVO_ID_ALL = 0xfe;
let SERVO_MOVE_TIME_WRITE = 1;
let SERVO_MOVE_TIME_READ = 2;
let SERVO_MOVE_TIME_WAIT_WRITE = 7;
let SERVO_MOVE_TIME_WAIT_READ = 8;
let SERVO_MOVE_START = 11;
let SERVO_MOVE_STOP = 12;
let SERVO_ID_WRITE = 13;
let SERVO_ID_READ = 14;
let SERVO_ANGLE_OFFSET_ADJUST = 17;
let SERVO_ANGLE_OFFSET_WRITE = 18;
let SERVO_ANGLE_OFFSET_READ = 19;
let SERVO_ANGLE_LIMIT_WRITE = 20;
let SERVO_ANGLE_LIMIT_READ = 21;
let SERVO_VIN_LIMIT_WRITE = 22;
let SERVO_VIN_LIMIT_READ = 23;
let SERVO_TEMP_MAX_LIMIT_WRITE = 24;
let SERVO_TEMP_MAX_LIMIT_READ = 25;
let SERVO_TEMP_READ = 26;
let SERVO_VIN_READ = 27;
let SERVO_POS_READ = 28;
let SERVO_OR_MOTOR_MODE_WRITE = 29;
let SERVO_OR_MOTOR_MODE_READ = 30;
let SERVO_LOAD_OR_UNLOAD_WRITE = 31;
let SERVO_LOAD_OR_UNLOAD_READ = 32;
let SERVO_LED_CTRL_WRITE = 33;
let SERVO_LED_CTRL_READ = 34;
let SERVO_LED_ERROR_WRITE = 35;
let SERVO_LED_ERROR_READ = 36;
let SERVO_ERROR_OVER_TEMPERATURE = 1;
let SERVO_ERROR_OVER_VOLTAGE = 2;
let SERVO_ERROR_LOCKED_ROTOR = 4;
function sendCmd(servoId, command, params) {
const length = 3 + params.length;
const checksum =
255 -
((servoId + length + command + params.reduce((a, b) => a + b, 0)) % 256);
const buffer = new Uint8Array([
0x55,
0x55,
servoId,
length,
command,
params,
checksum,
]);
serialWriter.write(buffer);
}
async function getResponse(servoId, command, params) {
sendCmd(servoId, command, params);
print("hep");
if (port.readable && portIsOpen) {
reader = port.readable.getReader();
try {
const { value, done } = await reader.read();
await reader.releaseLock();
return value[5] | value[6] << 8 ; //value[6]+value[5];
} catch (error) {
print(error);
} finally {
// Allow the serial port to be closed later.
await reader.releaseLock();
}
}
}
function keyReleased() {
if (key == "f") {
fullScreenToggle();
}
}