本帖最后由 agdhun 于 2015-9-28 15:38 编辑
demo跑完后我就开始做电机的添加,期间遇到了复杂繁多的html代码,APP端数据发不出去等问题,好在现在已解决,现在发帖庆祝一下.
先说说APP端的修改,一开始准备用总动员的源码,但是一来html代码太多,混杂了好多页面在一个文件,虽说我以前微看过Html语法,但这实属复杂;其二,我按照APICloud实例所讲的方法,云编译后再下载,始终返回不到设备列表页面,果断放弃,没有那么多时间去弄代码,选了第一个例子,ReadRGB进行修改了.
首先找到html/rgbctrl.html文件,这个文件就是显示设备控制的页面,其他我们不用管,直接找到body标签,复制以下代码:
-
-
-
- Motor
-
-
-
复制代码
复制04devicename-dinaji-120.png,smallicon-8kaiguan.png,smallicon-9kaiguan.png这几个文件到image目录下;接着在script标签添加以下代码:
- $("#motorbtn").click(function() {
- var topic = device_id + '/in';
- var command;
- var mtbtn = $("#motorbtn");
- if (mtbtn.attr("src") == "../image/smallicon-9kaiguan.png") {
- mtbtn.attr("src", "//www.obk20.com/bbs/../image/smallicon-8kaiguan.png");
- command = '{"MOTOR_KEY":0}';
- } else {
- mtbtn.attr("src", "../image/smallicon-9kaiguan.png");
- command = '{"MOTOR_KEY":4800}';
- }
- publishcmd(topic, command);
- });
复制代码
代码稍微解释一下:mtbtn.attr就是获取mtbtn的src属性或者设置src属性,publishcmd就是通过topic和 command变量,发布标题和信息,然后设备可以通过订阅标题,获取到云端信息,之后就可以提交云端了,添加三个模块,云编译,这样APP端就定制完成。
接下来就开始介绍固件端的修改,一些必备的步骤就不介绍了,庆科人员在wiki上写的很清楚了。
代码也是借一个人,再此表示感谢。
user_main.c的user_main函数改成如下
- OSStatus user_main( app_context_t * const app_context )
- {
- user_log_trace();
- OSStatus err = kUnknownErr;
- fogcloud_msg_t *recv_msg = NULL;
- json_object *recv_json_object = NULL;
-
- /* rgb led control variants, use h*** color.
- * h -- hues
- * s -- saturation
- * b -- brightness
- */
- bool led_switch = false;
- int led_hues = 0;
- int led_saturation = 0;
- int led_brightness = 0;
- int motor_val = 0;
-
- require(app_context, exit);
-
- h***2rgb_led_init(); // rgb led init
-
- #if 0
- //err = MicoPwmInitialize((mico_pwm_t)MICO_PWM_IR_SEND, 38000, 50);
- err = MicoPwmInitialize(MICO_PWM_IR_SEND, 38000, 50);
- ewh_log("MicoPwmInitialized err =%d..",err);
- ewh_log("MicoPwmStart..");
- // err = MicoPwmStart((mico_pwm_t)MICO_PWM_IR_SEND);
- err = MicoPwmStart(MICO_PWM_IR_SEND);
- ewh_log("MicoPwmStart end err =%d..",err);
- //dc_motor_init();
- #else
- dc_pwm_init();
- #endif
- while(1){
- mico_thread_msleep(200);
-
- // check fogcloud connect status
- if(!app_context->appStatus.fogcloudStatus.isCloudConnected){
- continue;
- }
-
- /* get a msg pointer, points to the memory of a msg:
- * msg data format: recv_msg->data =
- */
- err = MiCOFogCloudMsgRecv(app_context, &recv_msg, 100);
- if(kNoErr == err){
- // debug log in MICO dubug uart
- user_log("Cloud => Module: topic[%d]=[%.*s]tdata[%d]=[%.*s]",
- recv_msg->topic_len, recv_msg->topic_len, recv_msg->data,
- recv_msg->data_len, recv_msg->data_len, recv_msg->data + recv_msg->topic_len);
-
- // parse json data from the msg, get led control value
- recv_json_object = json_tokener_parse((const char*)(recv_msg->data + recv_msg->topic_len));
- if (NULL != recv_json_object){
- json_object_object_foreach(recv_json_object, key, val) {
- if(!strcmp(key, "rgbled_switch")){
- led_switch = json_object_get_boolean(val);
- }
- else if(!strcmp(key, "rgbled_hues")){
- led_hues = json_object_get_int(val);
- }
- else if(!strcmp(key, "rgbled_saturation")){
- led_saturation = json_object_get_int(val);
- }
- else if(!strcmp(key, "rgbled_brightness")){
- led_brightness = json_object_get_int(val);
- }
- else if(!strcmp(key, "MOTOR_KEY"))
- motor_val = json_object_get_int(val);
- }
-
- // control led
- if(led_switch){
- h***2rgb_led_open(led_hues, led_saturation, led_brightness); // open rgb led
- OLED_ShowString(OLED_DISPLAY_COLUMN_START, OLED_DISPLAY_ROW_3, "LED on "); // show cmd on LCD
-
- }else{
- h***2rgb_led_close(); // close led
- OLED_ShowString(OLED_DISPLAY_COLUMN_START, OLED_DISPLAY_ROW_3, "LED off "); // show cmd on LCD
-
- }
-
-
- dc_pwm_set(motor_val);
- // free memory of json object
- json_object_put(recv_json_object);
- recv_json_object = NULL;
- }
-
- // NOTE: must free msg memory after been used.
- if(NULL != recv_msg){
- free(recv_msg);
- recv_msg = NULL;
- }
- }
- else{
- // update info on LCD
- OLED_ShowString(OLED_DISPLAY_COLUMN_START, OLED_DISPLAY_ROW_2, "Demo RGB LED "); // clean line2
- OLED_ShowString(OLED_DISPLAY_COLUMN_START, OLED_DISPLAY_ROW_3, "LED control "); // show led cmd
- OLED_ShowString(OLED_DISPLAY_COLUMN_START, OLED_DISPLAY_ROW_4, " "); // clean line4
- }
- }
- exit:
- user_log("ERROR: user_main exit with err=%d", err);
- return err;
- }
复制代码
dc_motor.c,dc_motor.h就以附件形式给吧,代码比较长;之后编译下载,复位就可以控制电机了。[qq]1417075248[/qq]
1
|
|
|
|