Finished remote control features.
Fixed: Cope with bug in motor board firmware (Wemos): Do not stop sending data through i2c, otherwise the receiving chip stalls.
This commit is contained in:
parent
a96438b0f1
commit
b619e72476
14
mos.yml
14
mos.yml
|
@ -28,15 +28,15 @@ config_schema:
|
|||
# - ["my_app.string_value", "s", "", {title: "Some string value"}]
|
||||
# - ["my_app.int_value", "i", 123, {title: "Some integer value"}]
|
||||
- ["i2c.enable", true]
|
||||
- ["i2c.freq", 1000]
|
||||
- ["i2c.freq", 400]
|
||||
- ["i2c.sda_gpio", 4] # D2
|
||||
- ["i2c.scl_gpio", 5] # D1
|
||||
- ["flashLight", "o", {title: "Flash light / alarm light settings"}]
|
||||
- ["flashLight.address", "i", 0x30, {title: "i2c address of motor controller TB6612 (e.g. WEMOS)"}]
|
||||
- ["flashLight.motorFrequency", "i", 100, {title: "Frequency of PWM in kHz"}]
|
||||
- ["flashLight.motorFrequency", "i", 500, {title: "Frequency of PWM in Hz"}]
|
||||
- ["flashLight.motorUpdateTime", "i", 50, {title: "Time between motor updates in msec"}]
|
||||
- ["flashLight.mqttCtrlTopic", "s", "flashLight/%s/ctrl", {title: "MQTT channel to subscribe to receive commands; %s is replaced by clientId"}]
|
||||
- ["flashLight.mqttStatusTopic", "s", "flashLight/%s/status", {title: "MQTT channel to publish to send status change infos; %s is replaced by clientId"}]
|
||||
|
||||
- ["mqtt.enable", true]
|
||||
- ["mqtt.server", "mqtt.pmpark.de:1883"]
|
||||
- ["mqtt.user", "default"]
|
||||
|
@ -44,6 +44,10 @@ config_schema:
|
|||
- ["mqtt.will_message", "offline"]
|
||||
- ["mqtt.will_topic", "flashLight/"]
|
||||
|
||||
- ["sntp.enable", true]
|
||||
- ["sntp.server", "time.google.com"]
|
||||
|
||||
|
||||
# These settings get compiled into the C structure, and can be accessed
|
||||
# from the C code this way:
|
||||
#
|
||||
|
@ -75,6 +79,10 @@ libs:
|
|||
- origin: https://github.com/mongoose-os-libs/rpc-loopback
|
||||
- origin: https://github.com/mongoose-os-libs/rpc-mqtt
|
||||
- origin: https://github.com/mongoose-os-libs/rpc-service-ota
|
||||
- origin: https://github.com/mongoose-os-libs/rpc-service-cron
|
||||
- origin: https://github.com/mongoose-os-libs/crontab
|
||||
- origin: https://github.com/mongoose-os-libs/dash
|
||||
- origin: https://github.com/mongoose-os-libs/sntp
|
||||
|
||||
# Used by the mos tool to catch mos binaries incompatible with this file format
|
||||
manifest_version: 2017-05-18
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
#include "mgos.h"
|
||||
#include "mgos_system.h"
|
||||
#include "mgos_i2c.h"
|
||||
#include "WEMOS_Motor.h"
|
||||
|
||||
#define SUPPRESS_SENDING_UNCHANGED_VALUSE 0
|
||||
|
||||
static struct mgos_i2c *i2c;
|
||||
static uint32_t motor_freq[MAX_NUM_MOTORS];
|
||||
|
@ -22,16 +24,17 @@ freq: PWM's frequency
|
|||
void wemos_motor_set_freq(uint8_t motorNumber, uint32_t freq) {
|
||||
uint8_t data[4];
|
||||
|
||||
wemos_motor_init();
|
||||
if (motorNumber < MAX_NUM_MOTORS) {
|
||||
LOG(LL_INFO, ("Writing frequency %d to controller of motor %d (addr=%d)", freq, motorNumber, motor_i2c_address[motorNumber]));
|
||||
if (motorNumber < MAX_NUM_MOTORS || !initialized) {
|
||||
data[0] = (uint8_t) (freq >> 16) & 0x0f;
|
||||
data[1] = (uint8_t) (freq >> 16);
|
||||
data[2] = (uint8_t) (freq >> 8);
|
||||
data[3] = (uint8_t) freq;
|
||||
mgos_i2c_write(i2c, motor_i2c_address[motorNumber], data, 4, true);
|
||||
LOG(LL_INFO, ("mgos_i2c_write(i2c, addr=%d, data={%d,%d,%d,%d}, 4, stop=true)", motor_i2c_address[motorNumber], data[0], data[1], data[2], data[3]));
|
||||
// mgos_msleep(100);
|
||||
} else {
|
||||
// LOG ERROR
|
||||
LOG(LL_ERROR, ("Invalid motor number %d, maximum %d motors known", motorNumber, MAX_NUM_MOTORS));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -42,13 +45,13 @@ freq: PWM's frequency
|
|||
*/
|
||||
void wemos_motor_initMotor(uint8_t motorNumber, uint8_t address, uint32_t freq) {
|
||||
wemos_motor_init();
|
||||
|
||||
LOG(LL_INFO, ("wemos_motor_initMotor(motorNumber=%d, address=%d, freq=%d", motorNumber, address, freq));
|
||||
if (motorNumber < MAX_NUM_MOTORS) {
|
||||
motor_freq[motorNumber] = freq;
|
||||
motor_i2c_address[motorNumber] = address;
|
||||
wemos_motor_set_freq(motorNumber, freq);
|
||||
} else {
|
||||
// LOG ERROR
|
||||
LOG(LL_ERROR, ("Invalid motor number %d, maximum %d motors known", motorNumber, MAX_NUM_MOTORS));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -57,7 +60,7 @@ void wemos_motor_initMotor(uint8_t motorNumber, uint8_t address, uint32_t freq)
|
|||
motorNumber: 0..n
|
||||
dir:
|
||||
_SHORT_BRAKE 0
|
||||
_CCW 1
|
||||
_CW 1
|
||||
_CCW 2
|
||||
_STOP 3
|
||||
_STANDBY 4
|
||||
|
@ -70,7 +73,17 @@ void wemos_motor_setmotor(uint8_t motorNumber, uint8_t dir, float pwm_val)
|
|||
uint16_t _pwm_val;
|
||||
uint8_t data[4];
|
||||
|
||||
#if SUPPRESS_SENDING_UNCHANGED_VALUSE
|
||||
static int lastDir = -1;
|
||||
static int lastPwm = -1.0;
|
||||
|
||||
if (lastDir == dir && lastPwm == pwm_val) return;
|
||||
lastDir = dir;
|
||||
lastPwm = pwm_val;
|
||||
#endif
|
||||
|
||||
wemos_motor_init();
|
||||
|
||||
if (motorNumber < MAX_NUM_MOTORS) {
|
||||
data[0] = (uint8_t) (motorNumber & 0x01) | (uint8_t) 0x10;
|
||||
data[1] = dir;
|
||||
|
@ -81,8 +94,16 @@ void wemos_motor_setmotor(uint8_t motorNumber, uint8_t dir, float pwm_val)
|
|||
data[2] = (uint8_t) (_pwm_val >> 8);
|
||||
data[3] = (uint8_t) _pwm_val;
|
||||
mgos_i2c_write(i2c, motor_i2c_address[motorNumber], data, 4, true);
|
||||
// mgos_msleep(100);
|
||||
// LOG(LL_INFO, ("mgos_i2c_write(Mot=%d, adr=%d, data={%d,%d,%d,%d}, 4, stop=true", motorNumber, motor_i2c_address[motorNumber], data[0], data[1], data[2], data[3]));
|
||||
} else {
|
||||
// LOG ERROR
|
||||
LOG(LL_ERROR, ("Invalid motor number %d, maximum %d motors known", motorNumber, MAX_NUM_MOTORS));
|
||||
}
|
||||
}
|
||||
|
||||
void wemos_motor_stop(uint8_t motorNumber) {
|
||||
wemos_motor_setmotor(motorNumber, _STOP, 0.0);
|
||||
}
|
||||
|
||||
void wemos_motor_standby(uint8_t motorNumber) {
|
||||
wemos_motor_setmotor(motorNumber, _STANDBY, 0.0);
|
||||
}
|
|
@ -8,6 +8,8 @@ extern void wemos_motor_init();
|
|||
extern void wemos_motor_initMotor(uint8_t motorNumber, uint8_t address, uint32_t freq);
|
||||
extern void wemos_motor_set_freq(uint8_t motorNumber, uint32_t freq);
|
||||
extern void wemos_motor_setmotor(uint8_t motorNumber, uint8_t dir, float pwm_val);
|
||||
extern void wemos_motor_stop(uint8_t motorNumber);
|
||||
extern void wemos_motor_standby(uint8_t motorNumber);
|
||||
|
||||
#define _SHORT_BRAKE 0
|
||||
#define _CCW 1
|
||||
|
|
225
src/main.c
225
src/main.c
|
@ -5,6 +5,7 @@
|
|||
#include "common/cs_dbg.h"
|
||||
#include "common/json_utils.h"
|
||||
#include "common/platform.h"
|
||||
#include "common/mg_str.h"
|
||||
#include "frozen/frozen.h"
|
||||
#include "mgos_app.h"
|
||||
#include "mgos_gpio.h"
|
||||
|
@ -13,19 +14,20 @@
|
|||
#include "mgos_timers.h"
|
||||
#include "mgos_mqtt.h"
|
||||
#include "common/str_util.h"
|
||||
#include "mgos_crontab.h"
|
||||
|
||||
static float pwm = 30.0;
|
||||
static float flashLightSpeed = 50.0; /* 0.0 .. 100.0 */
|
||||
static double pwm = 30.0;
|
||||
static double flashLightSpeed = 50.0; /* 0.0 .. 100.0 */
|
||||
static double flashLightTargetSpeed = 50.0; // used to rampdown to a specific speed
|
||||
static uint16_t motorRampUpTime_msec = 2000;
|
||||
static uint16_t motorRampDownTime_msec = 1000;
|
||||
static uint16_t motorUpdateTime_msec = 100;
|
||||
static float flashLightRampUp_deltaSpeed = 0;
|
||||
static float flashLightRampDown_deltaSpeed = 0;
|
||||
static double flashLightRampUp_deltaSpeed = 0;
|
||||
static double flashLightRampDown_deltaSpeed = 0;
|
||||
static uint8_t motorDirection = _CW;
|
||||
static const char * clientId = "";
|
||||
static char commandTopic[256] = {'\0'};
|
||||
#if 0
|
||||
#endif
|
||||
|
||||
static char pubStatusTopic[256] = {'\0'};
|
||||
static bool mqttConnected = false;
|
||||
enum MotorStatus { MotorStatus_Off = 0, MotorStatus_RampUp, MotorStatus_RampDown, MotorStatus_On, MotorStatus_DemoMode };
|
||||
|
@ -36,7 +38,7 @@ static uint8_t motorStatus = MotorStatus_DemoMode;
|
|||
//PWM frequency: 1000Hz(1kHz)
|
||||
#define M1 0
|
||||
#define M1_addr 0x30
|
||||
#define M1_freq 5000
|
||||
#define M1_freq 3000
|
||||
static uint8_t motorAddress = M1_addr;
|
||||
static uint32_t motorFrequency = M1_freq;
|
||||
|
||||
|
@ -45,7 +47,7 @@ static void recalcTimings() {
|
|||
flashLightRampDown_deltaSpeed = (flashLightSpeed * motorUpdateTime_msec) / motorRampDownTime_msec;
|
||||
}
|
||||
|
||||
static void pubStatus(const char *statusString, float percentage) {
|
||||
static void pubStatus(const char *statusString, double percentage) {
|
||||
struct mbuf fb;
|
||||
struct json_out out = JSON_OUT_MBUF(&fb);
|
||||
|
||||
|
@ -64,31 +66,43 @@ static void motor_timer_cb(void *arg) {
|
|||
|
||||
switch (motorStatus) {
|
||||
case MotorStatus_Off:
|
||||
wemos_motor_stop(M1);
|
||||
break;
|
||||
case MotorStatus_On:
|
||||
wemos_motor_setmotor(M1, motorDirection, pwm);
|
||||
break;
|
||||
case MotorStatus_RampUp:
|
||||
pwm += flashLightRampUp_deltaSpeed;
|
||||
if (pwm >= flashLightSpeed) {
|
||||
LOG(LL_INFO, ("MotorStatus_RampUp: Speed target reached"));
|
||||
pwm = flashLightSpeed;
|
||||
motorStatus = MotorStatus_On;
|
||||
}
|
||||
wemos_motor_setmotor(M1, motorDirection, pwm);
|
||||
// LOG(LL_INFO, ("M1, dir=%d, pwm=%f", motorDirection, pwm));
|
||||
break;
|
||||
case MotorStatus_RampDown:
|
||||
pwm -= flashLightRampDown_deltaSpeed;
|
||||
if (pwm <= 0.0) {
|
||||
pwm = 0;
|
||||
motorStatus = MotorStatus_Off;
|
||||
wemos_motor_setmotor(M1, _STOP, 0.0);
|
||||
// change direction for next time, when motor turns on again
|
||||
if (motorDirection == _CW) {
|
||||
motorDirection = _CCW;
|
||||
if (pwm <= flashLightTargetSpeed) {
|
||||
if (pwm <= 0.0) {
|
||||
motorStatus = MotorStatus_Off;
|
||||
pwm = 0.0;
|
||||
wemos_motor_stop(M1);
|
||||
// change direction for next time, when motor turns on again
|
||||
if (motorDirection == _CW) {
|
||||
motorDirection = _CCW;
|
||||
} else {
|
||||
motorDirection = _CW;
|
||||
}
|
||||
} else {
|
||||
motorDirection = _CW;
|
||||
pwm = flashLightTargetSpeed;
|
||||
LOG(LL_INFO, ("MotorStatus_RampDown: Speed target reached"));
|
||||
motorStatus = MotorStatus_On;
|
||||
wemos_motor_setmotor(M1, motorDirection, pwm);
|
||||
}
|
||||
} else {
|
||||
wemos_motor_setmotor(M1, motorDirection, pwm);
|
||||
// LOG(LL_INFO, ("M1, dir=%d, pwm=%f", motorDirection, pwm));
|
||||
}
|
||||
break;
|
||||
case MotorStatus_DemoMode:
|
||||
|
@ -100,13 +114,14 @@ static void motor_timer_cb(void *arg) {
|
|||
pwm += 0.1;
|
||||
if (!stopped) {
|
||||
wemos_motor_setmotor(M1, motorDirection, pwm);
|
||||
LOG(LL_INFO, ("M1, pwm=%f", pwm));
|
||||
LOG(LL_INFO, ("M1, dir=%d, pwm=%f", motorDirection, pwm));
|
||||
pubStatus("on", pwm);
|
||||
|
||||
if (pwm > flashLightSpeed) {
|
||||
wemos_motor_setmotor(M1, _STOP, 0.0);
|
||||
// wemos_motor_setmotor(M1, _STOP, 0.0);
|
||||
wemos_motor_setmotor(M1, _STANDBY, pwm);
|
||||
stopped = true;
|
||||
LOG(LL_INFO, ("Stopped"));
|
||||
LOG(LL_INFO, ("Stopped/Standby"));
|
||||
pubStatus("off", 0);
|
||||
}
|
||||
}
|
||||
|
@ -116,60 +131,21 @@ static void motor_timer_cb(void *arg) {
|
|||
(void) arg;
|
||||
}
|
||||
|
||||
static void flashLightOn(struct mg_rpc_request_info *ri, void *cb_arg,
|
||||
struct mg_rpc_frame_info *fi, struct mg_str args) {
|
||||
struct mbuf fb;
|
||||
struct json_out out = JSON_OUT_MBUF(&fb);
|
||||
|
||||
mbuf_init(&fb, 30);
|
||||
|
||||
/*
|
||||
int num = 0;
|
||||
if (json_scanf(args.p, args.len, ri->args_fmt, &num) == 1) {
|
||||
json_printf(&out, "{num: %d}", num + 1);
|
||||
} else {
|
||||
json_printf(&out, "{error: %Q}", "num is required");
|
||||
}
|
||||
*/
|
||||
json_printf(&out, "{result: 0, resultString: %Q}", "OK");
|
||||
|
||||
printf("FlashLight ON\n");
|
||||
// wemos_motor_setmotor(M1, _CW, flashLightSpeed);
|
||||
static void flashLightOn() {
|
||||
LOG(LL_INFO, ("FlashLight ON\n"));
|
||||
flashLightTargetSpeed = flashLightSpeed;
|
||||
motorStatus = MotorStatus_RampUp; // this starts the motor on next timer callback
|
||||
pubStatus("on", flashLightSpeed);
|
||||
|
||||
mg_rpc_send_responsef(ri, "%.*s", fb.len, fb.buf);
|
||||
ri = NULL;
|
||||
mbuf_free(&fb);
|
||||
|
||||
(void) cb_arg;
|
||||
(void) fi;
|
||||
(void) args;
|
||||
}
|
||||
|
||||
static void flashLightOff(struct mg_rpc_request_info *ri, void *cb_arg,
|
||||
static void rpc_flashLightOn(struct mg_rpc_request_info *ri, void *cb_arg,
|
||||
struct mg_rpc_frame_info *fi, struct mg_str args) {
|
||||
struct mbuf fb;
|
||||
struct json_out out = JSON_OUT_MBUF(&fb);
|
||||
|
||||
mbuf_init(&fb, 30);
|
||||
|
||||
/*
|
||||
int num = 0;
|
||||
if (json_scanf(args.p, args.len, ri->args_fmt, &num) == 1) {
|
||||
json_printf(&out, "{num: %d}", num + 1);
|
||||
} else {
|
||||
json_printf(&out, "{error: %Q}", "num is required");
|
||||
}
|
||||
*/
|
||||
flashLightOn();
|
||||
mbuf_init(&fb, 100);
|
||||
json_printf(&out, "{result: 0, resultString: %Q}", "OK");
|
||||
|
||||
printf("FlashLight OFF\n");
|
||||
// wemos_motor_setmotor(M1, _STOP, 0);
|
||||
motorStatus = MotorStatus_RampDown; // this stops the motor on next timer callback
|
||||
|
||||
pubStatus("off", 0);
|
||||
|
||||
mg_rpc_send_responsef(ri, "%.*s", fb.len, fb.buf);
|
||||
ri = NULL;
|
||||
mbuf_free(&fb);
|
||||
|
@ -179,20 +155,74 @@ static void flashLightOff(struct mg_rpc_request_info *ri, void *cb_arg,
|
|||
(void) args;
|
||||
}
|
||||
|
||||
static void cron_flashLightOn(struct mg_str action, struct mg_str payload, void *userdata) {
|
||||
LOG(LL_INFO, ("Crontab flashLightOn fired"));
|
||||
flashLightOn();
|
||||
(void) action;
|
||||
(void) payload;
|
||||
(void) userdata;
|
||||
}
|
||||
|
||||
static void flashLightSetSpeed(struct mg_rpc_request_info *ri, void *cb_arg,
|
||||
static void flashLightOff() {
|
||||
LOG(LL_INFO, ("FlashLight OFF\n"));
|
||||
flashLightTargetSpeed = 0.0;
|
||||
motorStatus = MotorStatus_RampDown; // this stops the motor on next timer callback
|
||||
pubStatus("off", 0);
|
||||
}
|
||||
|
||||
static void rpc_flashLightOff(struct mg_rpc_request_info *ri, void *cb_arg,
|
||||
struct mg_rpc_frame_info *fi, struct mg_str args) {
|
||||
struct mbuf fb;
|
||||
struct json_out out = JSON_OUT_MBUF(&fb);
|
||||
|
||||
mbuf_init(&fb, 40);
|
||||
flashLightOff();
|
||||
mbuf_init(&fb, 100);
|
||||
json_printf(&out, "{result: 0, resultString: %Q}", "OK");
|
||||
mg_rpc_send_responsef(ri, "%.*s", fb.len, fb.buf);
|
||||
ri = NULL;
|
||||
mbuf_free(&fb);
|
||||
|
||||
(void) cb_arg;
|
||||
(void) fi;
|
||||
(void) args;
|
||||
}
|
||||
|
||||
static void cron_flashLightOff(struct mg_str action, struct mg_str payload, void *userdata) {
|
||||
LOG(LL_INFO, ("Crontab flashLightOff fired"));
|
||||
flashLightOff();
|
||||
(void) action;
|
||||
(void) payload;
|
||||
(void) userdata;
|
||||
}
|
||||
|
||||
static void cron_init() {
|
||||
mgos_crontab_register_handler(mg_mk_str("FlashLightOn"), cron_flashLightOn, NULL);
|
||||
mgos_crontab_register_handler(mg_mk_str("FlashLightOff"), cron_flashLightOff, NULL);
|
||||
}
|
||||
|
||||
static void flashLightSetSpeed(uint16_t newSpeed) {
|
||||
flashLightTargetSpeed = (double) newSpeed;
|
||||
flashLightSpeed = flashLightTargetSpeed;
|
||||
if (pwm < flashLightTargetSpeed) {
|
||||
motorStatus = MotorStatus_RampUp;
|
||||
} else if (pwm > flashLightTargetSpeed) {
|
||||
motorStatus = MotorStatus_RampDown;
|
||||
}
|
||||
}
|
||||
|
||||
static void rpc_flashLightSetSpeed(struct mg_rpc_request_info *ri, void *cb_arg,
|
||||
struct mg_rpc_frame_info *fi, struct mg_str args) {
|
||||
struct mbuf fb;
|
||||
struct json_out out = JSON_OUT_MBUF(&fb);
|
||||
|
||||
mbuf_init(&fb, 100);
|
||||
|
||||
int speed = 0;
|
||||
|
||||
if (json_scanf(args.p, args.len, ri->args_fmt, &speed) == 1) {
|
||||
flashLightSpeed = (float) speed;
|
||||
printf("FlashLight set speed tp %d\n", speed);
|
||||
printf("FlashLight set speed to %d\n", speed);
|
||||
json_printf(&out, "{result: 0, resultString: %Q, speed: %d}", "OK", speed);
|
||||
flashLightSetSpeed(speed);
|
||||
} else {
|
||||
json_printf(&out, "{error: %Q}", "speed is required");
|
||||
}
|
||||
|
@ -212,14 +242,14 @@ static void flashLightSetRampupTime(struct mg_rpc_request_info *ri, void *cb_arg
|
|||
struct mbuf fb;
|
||||
struct json_out out = JSON_OUT_MBUF(&fb);
|
||||
|
||||
mbuf_init(&fb, 40);
|
||||
mbuf_init(&fb, 100);
|
||||
|
||||
uint16_t rampUpTime_msec = 0;
|
||||
|
||||
if (json_scanf(args.p, args.len, ri->args_fmt, &rampUpTime_msec) == 1) {
|
||||
motorRampUpTime_msec = rampUpTime_msec;
|
||||
printf("FlashLight set motor ramp up time to %d\n", rampUpTime_msec);
|
||||
json_printf(&out, "{result: 0, resultString: %Q, rampUpTime_msec: %d}", "OK", rampUpTime_msec);
|
||||
json_printf(&out, "{result: 0, resultString: %Q, rampUpTime_ms: %d}", "OK", rampUpTime_msec);
|
||||
} else {
|
||||
json_printf(&out, "{error: %Q}", "rampUpTime_msec is required");
|
||||
}
|
||||
|
@ -239,14 +269,14 @@ static void flashLightSetRampdownTime(struct mg_rpc_request_info *ri, void *cb_a
|
|||
struct mbuf fb;
|
||||
struct json_out out = JSON_OUT_MBUF(&fb);
|
||||
|
||||
mbuf_init(&fb, 40);
|
||||
mbuf_init(&fb, 100);
|
||||
|
||||
uint16_t rampDownTime_msec = 0;
|
||||
|
||||
if (json_scanf(args.p, args.len, ri->args_fmt, &rampDownTime_msec) == 1) {
|
||||
motorRampDownTime_msec = rampDownTime_msec;
|
||||
printf("FlashLight set motor ramp up time to %d\n", rampDownTime_msec);
|
||||
json_printf(&out, "{result: 0, resultString: %Q, rampDownTime_msec: %d}", "OK", rampDownTime_msec);
|
||||
json_printf(&out, "{result: 0, resultString: %Q, rampDownTime_ms: %d}", "OK", rampDownTime_msec);
|
||||
} else {
|
||||
json_printf(&out, "{error: %Q}", "rampDownTime_msec is required");
|
||||
}
|
||||
|
@ -266,14 +296,14 @@ static void flashLightSetMotorUpdateTime(struct mg_rpc_request_info *ri, void *c
|
|||
struct mbuf fb;
|
||||
struct json_out out = JSON_OUT_MBUF(&fb);
|
||||
|
||||
mbuf_init(&fb, 40);
|
||||
mbuf_init(&fb, 100);
|
||||
|
||||
uint16_t updateTime = 0;
|
||||
|
||||
if (json_scanf(args.p, args.len, ri->args_fmt, &updateTime) == 1) {
|
||||
motorUpdateTime_msec = updateTime;
|
||||
printf("FlashLight set updateTime_msec tp %d\n", updateTime);
|
||||
json_printf(&out, "{result: 0, resultString: %Q, motorUpdateTime_msec: %d}", "OK", updateTime);
|
||||
json_printf(&out, "{result: 0, resultString: %Q, udateTime_ms: %d}", "OK", updateTime);
|
||||
} else {
|
||||
json_printf(&out, "{error: %Q}", "motorUpdateTime_msec is required");
|
||||
}
|
||||
|
@ -288,6 +318,26 @@ static void flashLightSetMotorUpdateTime(struct mg_rpc_request_info *ri, void *c
|
|||
(void) args;
|
||||
}
|
||||
|
||||
static void flashLightGetSettings(struct mg_rpc_request_info *ri, void *cb_arg,
|
||||
struct mg_rpc_frame_info *fi, struct mg_str args) {
|
||||
struct mbuf fb;
|
||||
struct json_out out = JSON_OUT_MBUF(&fb);
|
||||
|
||||
mbuf_init(&fb, 1024);
|
||||
|
||||
json_printf(&out, "{pwm: %f, speed: %f, rampupTime_ms: %d, rampdownTime_ms: %d, updateTime_ms: %d, rampupDeltaSpeed: %f, rampdownDeltaSpeed: %f, motorDirection: %d, clientId: %Q, commandTopic: %Q, statusTopic: %Q, mqttConnected: %Q, motorStatus: %d}",
|
||||
pwm, flashLightSpeed, motorRampUpTime_msec, motorRampDownTime_msec, motorUpdateTime_msec, flashLightRampUp_deltaSpeed, flashLightRampDown_deltaSpeed,
|
||||
motorDirection, clientId, commandTopic, pubStatusTopic, mqttConnected ? "true" : "false", motorStatus);
|
||||
|
||||
mg_rpc_send_responsef(ri, "%.*s", fb.len, fb.buf);
|
||||
ri = NULL;
|
||||
mbuf_free(&fb);
|
||||
|
||||
(void) cb_arg;
|
||||
(void) fi;
|
||||
(void) args;
|
||||
}
|
||||
|
||||
void net_changed(int ev, void *evd, void *arg) {
|
||||
if (ev != MGOS_NET_EV_IP_ACQUIRED) return;
|
||||
// call_peer();
|
||||
|
@ -324,14 +374,18 @@ void onMqttConnection(struct mg_connection *c, const char *client_id, struct mg_
|
|||
|
||||
enum mgos_app_init_result mgos_app_init(void) {
|
||||
struct mg_rpc *c = mgos_rpc_get_global();
|
||||
mg_rpc_add_handler(c, "FlashLight.On", NULL, flashLightOn, NULL);
|
||||
mg_rpc_add_handler(c, "FlashLight.Off", NULL, flashLightOff, NULL);
|
||||
mg_rpc_add_handler(c, "FlashLight.Speed", "{num: %d}", flashLightSetSpeed, NULL);
|
||||
mg_rpc_add_handler(c, "FlashLight.RampUpTime_msec", "{num: %d}", flashLightSetRampupTime, NULL);
|
||||
mg_rpc_add_handler(c, "FlashLight.RampDownTime_msec", "{num: %d}", flashLightSetRampdownTime, NULL);
|
||||
mg_rpc_add_handler(c, "FlashLight.MotorUpdateTime_msec", "{num: %d}", flashLightSetMotorUpdateTime, NULL);
|
||||
mg_rpc_add_handler(c, "FlashLight.On", NULL, rpc_flashLightOn, NULL);
|
||||
mg_rpc_add_handler(c, "FlashLight.Off", NULL, rpc_flashLightOff, NULL);
|
||||
mg_rpc_add_handler(c, "FlashLight.Speed", "{speed: %d}", rpc_flashLightSetSpeed, NULL);
|
||||
mg_rpc_add_handler(c, "FlashLight.RampUpTime_msec", "{rampUpTime_ms: %d}", flashLightSetRampupTime, NULL);
|
||||
mg_rpc_add_handler(c, "FlashLight.RampDownTime_msec", "{rampDownTime_ms: %d}", flashLightSetRampdownTime, NULL);
|
||||
mg_rpc_add_handler(c, "FlashLight.MotorUpdateTime_msec", "{uptdateTime_ms: %d}", flashLightSetMotorUpdateTime, NULL);
|
||||
mg_rpc_add_handler(c, "FlashLight.GetSettings", NULL, flashLightGetSettings, NULL);
|
||||
mgos_event_add_group_handler(MGOS_EVENT_GRP_NET, net_changed, NULL);
|
||||
|
||||
// enable crontab
|
||||
cron_init();
|
||||
|
||||
// add MQTT cmd subscription
|
||||
LOG(LL_INFO, ("Initializing MQTT"));
|
||||
clientId = mgos_sys_config_get_mqtt_client_id();
|
||||
|
@ -341,14 +395,23 @@ enum mgos_app_init_result mgos_app_init(void) {
|
|||
LOG(LL_INFO, ("pubStatusTopic=%s", mgos_sys_config_get_flashLight_mqttStatusTopic()));
|
||||
c_snprintf(commandTopic, sizeof(commandTopic), mgos_sys_config_get_flashLight_mqttCtrlTopic(), clientId);
|
||||
c_snprintf(pubStatusTopic, sizeof(pubStatusTopic), mgos_sys_config_get_flashLight_mqttStatusTopic(), clientId);
|
||||
LOG(LL_INFO, ("cmdTopic=%s", commandTopic));
|
||||
LOG(LL_INFO, ("pubStatusTopic=%s", pubStatusTopic));
|
||||
mgos_mqtt_set_connect_fn(onMqttConnection, NULL);
|
||||
|
||||
LOG(LL_INFO, ("Initializing motor controller"));
|
||||
motorFrequency = mgos_sys_config_get_flashLight_motorFrequency();
|
||||
LOG(LL_INFO, ("motorFrequency=%d Hz", motorFrequency));
|
||||
motorAddress = mgos_sys_config_get_flashLight_address();
|
||||
LOG(LL_INFO, ("motorAddress=%d", motorAddress));
|
||||
LOG(LL_INFO, ("Initializing motor controller"));
|
||||
recalcTimings();
|
||||
wemos_motor_init();
|
||||
LOG(LL_INFO, ("Initializing motor M1"));
|
||||
wemos_motor_initMotor(M1, motorAddress, motorFrequency);
|
||||
LOG(LL_INFO, ("Setting up timer"));
|
||||
motorUpdateTime_msec = mgos_sys_config_get_flashLight_motorUpdateTime();
|
||||
mgos_set_timer(motorUpdateTime_msec, MGOS_TIMER_REPEAT, motor_timer_cb, NULL);
|
||||
LOG(LL_INFO, ("Initialization done"));
|
||||
return MGOS_APP_INIT_SUCCESS;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue