149 lines
3.7 KiB
C
149 lines
3.7 KiB
C
#include "mgos.h"
|
|
#include "WEMOS_Motor.h"
|
|
|
|
#include "mgos_rpc.h"
|
|
#include "common/cs_dbg.h"
|
|
#include "common/json_utils.h"
|
|
#include "common/platform.h"
|
|
#include "frozen/frozen.h"
|
|
#include "mgos_app.h"
|
|
#include "mgos_gpio.h"
|
|
#include "mgos_net.h"
|
|
#include "mgos_sys_config.h"
|
|
#include "mgos_timers.h"
|
|
|
|
static float pwm;
|
|
static float flashLightSpeed = 50; /* 0.0 .. 100.0 */
|
|
|
|
// Motor numbers
|
|
//Motor shiled I2C Address: 0x30
|
|
//PWM frequency: 1000Hz(1kHz)
|
|
#define M1 0
|
|
#define M1_addr 0x30
|
|
#define M1_freq 5000
|
|
|
|
static void motor_timer_cb(void *arg) {
|
|
if (pwm > flashLightSpeed + 5.0) {
|
|
pwm = 0.0; // start again
|
|
} else {
|
|
wemos_motor_setmotor(M1, _CW, pwm);
|
|
LOG(LL_INFO, ("M1, pwm=%f", pwm));
|
|
|
|
pwm += 0.1;
|
|
|
|
if (pwm > flashLightSpeed) {
|
|
wemos_motor_setmotor(M1, _STOP, 0.0);
|
|
LOG(LL_INFO, ("Stopped"));
|
|
}
|
|
}
|
|
(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);
|
|
|
|
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,
|
|
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 OFF\n");
|
|
wemos_motor_setmotor(M1, _STOP, 0);
|
|
|
|
mg_rpc_send_responsef(ri, "%.*s", fb.len, fb.buf);
|
|
ri = NULL;
|
|
mbuf_free(&fb);
|
|
|
|
(void) cb_arg;
|
|
(void) fi;
|
|
(void) args;
|
|
}
|
|
|
|
|
|
static void 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, 40);
|
|
|
|
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);
|
|
json_printf(&out, "{result: 0, resultString: %Q, speed: %d}", "OK", speed);
|
|
} else {
|
|
json_printf(&out, "{error: %Q}", "speed is required");
|
|
}
|
|
|
|
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();
|
|
(void) evd;
|
|
(void) arg;
|
|
}
|
|
|
|
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);
|
|
mgos_event_add_group_handler(MGOS_EVENT_GRP_NET, net_changed, NULL);
|
|
|
|
wemos_motor_init();
|
|
wemos_motor_initMotor(M1, M1_addr, M1_freq);
|
|
LOG(LL_INFO, ("\r\nTest PWM 30 to 100, step 0.1,CW\r\n"));
|
|
pwm = 30.0;
|
|
mgos_set_timer(200, MGOS_TIMER_REPEAT, motor_timer_cb, NULL);
|
|
return MGOS_APP_INIT_SUCCESS;
|
|
}
|
|
|