Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 90 additions & 2 deletions build/devices/esp32/targets/m5stick_c/host/provider.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Moddable Tech, Inc.
* Copyright (c) 2021-2026 Moddable Tech, Inc. ,Satoshi Tanaka
*
* This file is part of the Moddable SDK Runtime.
*
Expand Down Expand Up @@ -27,6 +27,11 @@ import PWM from "embedded:io/pwm";
import Serial from "embedded:io/serial";
import SMBus from "embedded:io/smbus";
import SPI from "embedded:io/spi";
import PulseWidth from "embedded:io/pulsewidth";
import AXP192 from "embedded:peripheral/Power/axp192";
import SH200Q from "embedded:sensor/Accelerometer-Gyroscope/SH200Q";
import MPU6886 from "embedded:sensor/Accelerometer-Gyroscope/MPU6886";
import RTC from "embedded:RTC/BM8563";

const device = {
I2C: {
Expand Down Expand Up @@ -68,12 +73,95 @@ const device = {
pin: 36
}
},
io: {Analog, Digital, DigitalBank, I2C, PulseCount, PWM, Serial, SMBus, SPI},
io: { Analog, Digital, DigitalBank, I2C, PulseCount, PWM, Serial, SMBus, SPI, PulseWidth },
pin: {
button: 37,
led: 10,
displayDC: 23,
displaySelect: 5
},
sensor: {
IMU: class {
constructor(options) {
let isMPU6886 = true;
let probe;
try {
probe = new SMBus({
...device.I2C.internal,
io: SMBus,
hz: 400_000,
address: 0x68, // MPU6886
});
probe.readUint8(0x74);
} catch {
isMPU6886 = false;
} finally {
probe?.close();
}

const IMUBase = isMPU6886 ? MPU6886 : SH200Q;
const IMUClass = class extends IMUBase {
sample() {
const result = super.sample();
result.accelerometer.x *= -1;
result.accelerometer.y *= -1;
result.gyroscope.y *= -1;
return result;
}
};

return new IMUClass({
...options,
sensor: {
...device.I2C.internal,
io: device.io.SMBus
}
});
}
}
},
peripheral: {
Power: class extends AXP192 {
constructor(options) {
super({
...options,
peripheral: {
...device.I2C.internal,
io: device.io.SMBus
}
});
this.writeByte(0x10, 0xff); // OLED VPP Enable
this.writeByte(0x28, 0xff); // Enable LDO2&LDO3, LED&TFT 3.3V
this.writeByte(0x82, 0xff); // Enable all the ADCs
this.writeByte(0x33, 0xc0); // Enable Charging, 100mA, 4.2V End at 0.9
this.writeByte(0xb8, 0x80); // Enable Colume Counter
this.writeByte(0x12, 0x4d); // Enable DC-DC1, OLED VDD, 5B V EXT
this.writeByte(0x36, 0x5c); // PEK
this.writeByte(0x91, 0xA0); // Set MIC voltage to 2.8V
this.writeByte(0x90, 0x02); // gpio0
}
// value 0 - 100 %
set brightness(value) {
if (value <= 0)
value = 7;
else if (value >= 100)
value = 15;
else
value = (value / 100) * 8 + 7;
this.writeByte(0x28, (this.readByte(0x28) & 0x0F) | (value << 4));
}
},
RTC: class {
constructor(options) {
return new RTC({
...options,
clock: {
...device.I2C.internal,
io: SMBus,
},
});
}
},
}
};

Expand Down
23 changes: 9 additions & 14 deletions build/devices/esp32/targets/m5stick_c/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
"DEBUGGER_SPEED": "1500000"
},
"include": [
"$(MODDABLE)/modules/drivers/st7735/manifest.json",
"$(MODDABLE)/modules/drivers/axp192/manifest.json",
"$(MODDABLE)/modules/pins/smbus/manifest.json"
"$(MODDABLE)/modules/io/manifest.json",
"$(MODULES)/pins/digital/monitor/manifest.json",
"$(MODULES)/drivers/st7735/manifest.json",
"$(MODULES)/drivers/sensors/mpu6886/manifest.json",
"$(MODULES)/drivers/sensors/sh200q/manifest.json",
"$(MODULES)/drivers/peripherals/axp192/manifest.json",
"$(MODULES)/drivers/peripherals/bm8563/manifest.json"
],
"config": {
"screen": "st7735",
"touch": ""
},
"defines": {
"i2c": {
"sda_pin": 21,
"scl_pin": 22
},
"spi": {
"mosi_pin": 15,
"sck_pin": 13
Expand Down Expand Up @@ -75,19 +75,14 @@
},
"modules": {
"*": [
"$(MODULES)/pins/digital/monitor/*",
"$(MODULES)/pins/digital/monitor/esp32/*",
"$(MODULES)/drivers/mpu6886/*",
"$(MODULES)/drivers/sh200q/*"

"../m5stack_fire/m5button"
],
"setup/target": "./setup-target"
},
"preload": [
"monitor",
"setup/target",
"mpu6886",
"sh200q"
"m5button"
],
"creation": {
"static": 0,
Expand Down
184 changes: 19 additions & 165 deletions build/devices/esp32/targets/m5stick_c/setup-target.js
Original file line number Diff line number Diff line change
@@ -1,183 +1,37 @@
import Digital from "pins/digital";
import Monitor from "monitor";
import AXP192 from "axp192";
import SH200Q from "sh200q";
import MPU6886 from "mpu6886";
import I2C from "pins/i2c";
import Timer from "timer";

import M5Button from "m5button";
import config from "mc/config";

const state = {
handleRotation: nop
};
import Timer from "timer";

export default function (done) {
global.button = {
a: new Monitor({
pin: 37,
mode: Digital.InputPullUp,
edge: Monitor.Rising | Monitor.Falling
}),
b: new Monitor({
pin: 39,
mode: Digital.InputPullUp,
edge: Monitor.Rising | Monitor.Falling
}),
globalThis.button = {
a: new M5Button(37),
b: new M5Button(39)
};
button.a.onChanged = button.b.onChanged = nop;

global.power = new Power();

state.accelerometerGyro = new IMU();

global.accelerometer = {
onreading: nop
}

global.gyro = {
onreading: nop
}

//trace('The Temp:', state.accelerometerGyro.sampleTemp(), '\n');

accelerometer.start = function (frequency) {
accelerometer.stop();
state.accelerometerTimerID = Timer.repeat(id => {
state.accelerometerGyro.configure({
operation: "accelerometer"
});
const sample = state.accelerometerGyro.sample();
if (sample) {
sample.y *= -1;
sample.z *= -1;
state.handleRotation(sample);
accelerometer.onreading(sample);
}
}, frequency);
}

gyro.start = function (frequency) {
gyro.stop();
state.gyroTimerID = Timer.repeat(id => {
state.accelerometerGyro.configure({
operation: "gyroscope"
});
const sample = state.accelerometerGyro.sample();
if (sample) {
let {
x,
y,
z
} = sample;
const temp = x;
x = y * -1;
y = temp * -1;
z *= -1;
gyro.onreading({
x,
y,
z
});
}
}, frequency);
}

accelerometer.stop = function () {
if (undefined !== state.accelerometerTimerID)
Timer.clear(state.accelerometerTimerID);
delete state.accelerometerTimerID;
}

gyro.stop = function () {
if (undefined !== state.gyroTimerID)
Timer.clear(state.gyroTimerID);
delete state.gyroTimerID;
}

if (config.autorotate && global.Application) {
state.handleRotation = function (reading) {
if (Math.abs(reading.y) > Math.abs(reading.x)) {
if (reading.y < -0.7 && application.rotation != 90) {
application.rotation = 90;
} else if (reading.y > 0.7 && application.rotation != 270) {
globalThis.power = new device.peripheral.Power();

if (config.autorotate && globalThis.Application) {
const imu = new device.sensor.IMU();
Timer.repeat(id => {
const sample = imu.sample();
const {x, y } = sample.accelerometer;
if (Math.abs(y) > Math.abs(x)) {
if (y < -0.7 && application.rotation !== 270) {
application.rotation = 270;
} else if (y > 0.7 && application.rotation !== 90) {
application.rotation = 90;
}
} else {
if (reading.x < -0.7 && application.rotation != 180) {
if (x < -0.7 && application.rotation !== 180) {
application.rotation = 180;
} else if (reading.x > 0.7 && application.rotation != 0) {
} else if (x > 0.7 && application.rotation !== 0) {
application.rotation = 0;
}
}
}
accelerometer.start(300);
}, 300);
}

done();
}

function nop() {}

class Power extends AXP192 {
constructor() {
super({
sda: 21,
scl: 22,
});
// TODO: Use class method rather than directly accessing register
this.write(0x10, 0xff); // OLED VPP Enable
this.write(0x28, 0xff); // Enable LDO2&LDO3, LED&TFT 3.3V
this.write(0x82, 0xff); // Enable all the ADCs
this.write(0x33, 0xc0); // Enable Charging, 100mA, 4.2V End at 0.9
this.write(0xb8, 0x80); // Enable Colume Counter
this.write(0x12, 0x4d); // Enable DC-DC1, OLED VDD, 5B V EXT
this.write(0x36, 0x5c); // PEK
this.write(0x91, 0xA0); // Set MIC voltage to 2.8V
this.write(0x90, 0x02); // gpio0
}

// value 0 - 100 %
set brightness(value) {
if (value <= 0)
value = 7;
else if (value >= 100)
value = 15;
else
value = (value / 100) * 8 + 7;
this.writeByte(0x28, (this.readByte(0x28) & 0x0F) | (value << 4));
}

/**
* sets the screen brightness
* @param {*} brightness brightness between 7-15
* @deprecated Use setter
*/
setBrightness(brightness) {
brightness=Math.floor((brightness-6)*12.5);
trace(`WARNING: AXP192#setBrightness is deprecated. use setter, range now 0-100, now ${brightness}\n`);
this.brightness = brightness;
}

/**
* @deprecated
*/
initialize() {
trace(
"WARNING: AXP192#initialize is deprecated. no need to initialize explicitly"
);
}
}

class IMU {
constructor() {
const probe = new I2C({
address: 0x68, // MPU6886
throw: false
});
const result = probe.write(0x75);
probe.close();

return (result instanceof Error) ? new SH200Q : new MPU6886;
}
}
Loading