diff --git a/TC1/http_server/app_httpd.c b/TC1/http_server/app_httpd.c index 32453e2..b0a8115 100644 --- a/TC1/http_server/app_httpd.c +++ b/TC1/http_server/app_httpd.c @@ -46,7 +46,7 @@ static bool is_http_init; static bool is_handlers_registered; struct httpd_wsgi_call g_app_handlers[]; -char power_info_json[1130] = { 0 }; +char power_info_json[1140] = { 0 }; static int HttpGetIndexPage(httpd_request_t *req) { @@ -99,9 +99,16 @@ exit: static int HttpGetPowerInfo(httpd_request_t *req) { - char* powers = GetPowerRecord(); - sprintf(power_info_json, POWER_INFO_JSON, power_record.idx, powers); OSStatus err = kNoErr; + char buf[4]; + err = httpd_get_data(req, buf, 4); + require_noerr(err, exit); + + int idx = 0; + sscanf(buf, "%d", &idx); + + char* powers = GetPowerRecord(idx); + sprintf(power_info_json, POWER_INFO_JSON, power_record.idx, PW_NUM, powers); send_http(power_info_json, strlen(power_info_json), exit, &err); exit: return err; @@ -172,7 +179,7 @@ struct httpd_wsgi_call g_app_handlers[] = { {"/", HTTPD_HDR_DEFORT, 0, HttpGetIndexPage, NULL, NULL, NULL}, {"/socket", HTTPD_HDR_DEFORT, 0, NULL, HttpSetSocketStatus, NULL, NULL}, {"/status", HTTPD_HDR_DEFORT, 0, HttpGetTc1Status, NULL, NULL, NULL}, - {"/power", HTTPD_HDR_DEFORT, 0, HttpGetPowerInfo, NULL, NULL, NULL}, + {"/power", HTTPD_HDR_DEFORT, 0, NULL, HttpGetPowerInfo, NULL, NULL}, {"/wifi/config", HTTPD_HDR_DEFORT, 0, HttpGetWifiConfig, HttpSetWifiConfig, NULL, NULL}, {"/wifi/scan", HTTPD_HDR_DEFORT, 0, HttpGetWifiScan, HttpSetWifiScan, NULL, NULL}, }; diff --git a/TC1/http_server/app_httpd.h b/TC1/http_server/app_httpd.h index b45494f..798e2cb 100644 --- a/TC1/http_server/app_httpd.h +++ b/TC1/http_server/app_httpd.h @@ -56,7 +56,7 @@ 'gateway':'%s'\ }" -#define POWER_INFO_JSON "{'idx':%d, 'powers:[%s]'}" +#define POWER_INFO_JSON "{'idx':%d,'len':%d,'powers:[%s]'}" int AppHttpdStart(void); int AppHttpdStop(); diff --git a/TC1/http_server/index.html b/TC1/http_server/index.html index 2caa689..62e3d5c 100644 --- a/TC1/http_server/index.html +++ b/TC1/http_server/index.html @@ -251,16 +251,21 @@ HttpGet("/status", function (re) { } }); +var first_load_power = true; +var power_idx = 1; +var position = 10; function GetPowerRecord() { - HttpGet("/power", function (re) { - var power = JSON.parse(re); - var html = ""; - for (var i = 0; i < power.powers.length; i++) { - html += "
"; - power_line.innerHTML = html; + HttpPost("/power", function (re) { + var power = JSON.parse(re); + power_idx = power.idx + 1; + var html = ""; + for (var i = 0; i <= power.idx; i++) { + html += "
"; + position += 10; + } + power_line.innerHTML += html; //滑动到最后 - } -}); + }, idx.toString()); } function SetOK(i) { diff --git a/TC1/user_power.c b/TC1/user_power.c index 3456c64..1d5d815 100644 --- a/TC1/user_power.c +++ b/TC1/user_power.c @@ -10,7 +10,7 @@ mico_timer_t power_timer; -PowerRecord power_record = { 0, { 50,55,60,65,70,75,80,85,90,95,90,85,80,75,70,65,60,65,70,75,80,85,90,95,90,85,80,75,70,65,60,65,70,75,80,85,90,95,90,85,80,75,70,65,60 } }; +PowerRecord power_record = { 44, { 50,55,60,65,70,75,80,85,90,95,90,85,80,75,70,65,60,65,70,75,80,85,90,95,90,85,80,75,70,65,60,65,70,75,80,85,90,95,90,85,80,75,70,65,60 } }; static uint32_t clock_count_last = 0; static uint32_t clock_count = 0; @@ -21,20 +21,18 @@ char power_record_str[1101] = { 0 }; void SetPowerRecord(PowerRecord* pr, uint32_t pw) { - if (pr->idx >= PW_NUM) - { - pr->idx = 0; - } - pr->powers[pr->idx++] = pw; + pr->powers[(++pr->idx)% PW_NUM] = pw; } -char* GetPowerRecord() +char* GetPowerRecord(int idx) { - int i = 0; + if (idx > power_record.idx) return NULL; + + int i = idx > 0 ? idx : (power_record.idx - PW_NUM - 1); char* tmp = power_record_str; - for (; i < PW_NUM; i++) + for (; i <= power_record.idx; i++) { - sprintf(tmp, "%d,", power_record.powers[i]); + sprintf(tmp, "%d,", power_record.powers[i%PW_NUM]); tmp += strlen(tmp); } *(--tmp) = 0; diff --git a/TC1/user_power.h b/TC1/user_power.h index 6be3209..af9c888 100644 --- a/TC1/user_power.h +++ b/TC1/user_power.h @@ -10,7 +10,7 @@ typedef struct extern PowerRecord power_record; -char* GetPowerRecord(); +char* GetPowerRecord(int idx); void PowerInit(void); void SetPowerRecord(PowerRecord* pr, uint32_t pw);