160 lines
3.3 KiB
C
160 lines
3.3 KiB
C
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <rtthread.h>
|
|
|
|
#include "chrg_led.h"
|
|
#include "chrg_thread.h"
|
|
#include "chrg_roll_sou.h"
|
|
#include "chrg_eload.h"
|
|
#include "chrg_utils.h"
|
|
|
|
#define LOG_TAG "chrg.rollsou"
|
|
#include <rtdbg.h>
|
|
|
|
rt_uint8_t sou_run = 1;
|
|
rt_uint8_t sou_ch = IDX_SOU_CH1;
|
|
rt_uint8_t sou_pt = MB_R_PART1;
|
|
|
|
void chrg_roll_sou_thread_entry (void *data)
|
|
{
|
|
struct chrg_thread_t *pTHR = (struct chrg_thread_t *)data;
|
|
|
|
if (RT_NULL == pTHR) {
|
|
return ;
|
|
}
|
|
|
|
rt_uint8_t index = 0, idx = 0, part = 0, pt = 0, manu = 0;
|
|
rt_err_t ret = RT_EOK;
|
|
struct chrg_south_t *pSOU = &chrgsouth;
|
|
struct chrg_thread_t *pTS = &chrgthr[IDX_THR_SOUTH];
|
|
struct mb_msg_t *pMB_R = RT_NULL;
|
|
|
|
while (1) {
|
|
rt_mutex_take(pTHR->mutex, RT_WAITING_FOREVER);
|
|
manu = pSOU->manual;
|
|
rt_mutex_release(pTHR->mutex);
|
|
|
|
if (1 == manu) {
|
|
chrg_south_switch((eIDX_SOU_CH)sou_ch);
|
|
chrg_eload_refresh(ID_ELOAD, sou_pt);
|
|
|
|
} else { // 轮巡4个通道,每个通道3条读取命令
|
|
if (0 == sou_run) {
|
|
rt_thread_mdelay(1000);
|
|
continue;
|
|
}
|
|
|
|
part = index%(TOTAL_SOU_CHS*TOTAL_MB_R); // [0, 11]
|
|
idx = part/TOTAL_MB_R; // [0, 3]
|
|
pt = part%TOTAL_MB_R;
|
|
|
|
if (0 == pSOU->enable[idx]) {
|
|
pSOU->idx = (eIDX_SOU_CH)idx;
|
|
index++;
|
|
rt_thread_mdelay(10);
|
|
continue;
|
|
} else {
|
|
chrg_led_flashing(IDX_LED3, TIMES_ONE, PULSE_TIME*2, PULSE_TIME*2);
|
|
if (idx != (rt_uint8_t)pSOU->idx) {
|
|
pSOU->idx = (eIDX_SOU_CH)idx;
|
|
chrg_south_switch(pSOU->idx);
|
|
}
|
|
|
|
chrg_eload_refresh(ID_ELOAD, pt);
|
|
}
|
|
|
|
index++;
|
|
}
|
|
|
|
ret = rt_mb_recv(pTS->mb, (rt_uint32_t *)&pMB_R, 200); // 超时 200ms
|
|
if ((RT_EOK == ret)&&(RT_NULL != pMB_R)) {
|
|
if (0 == mb_crc16(pMB_R->payload, pMB_R->length)) {
|
|
if (1 == manu) {
|
|
rt_mutex_take(pTHR->mutex, RT_WAITING_FOREVER);
|
|
pSOU->manual = 0;
|
|
rt_mutex_release(pTHR->mutex);
|
|
} else {
|
|
chrg_eload_parse(idx, pt, pMB_R->payload, pMB_R->length);
|
|
}
|
|
} else {
|
|
LOG_E("crc failed.");
|
|
}
|
|
|
|
if (RT_NULL != pMB_R->payload) {
|
|
rt_free(pMB_R->payload);
|
|
pMB_R->payload = RT_NULL;
|
|
}
|
|
|
|
if (RT_NULL != pMB_R) {
|
|
rt_free(pMB_R);
|
|
pMB_R = RT_NULL;
|
|
}
|
|
|
|
rt_thread_mdelay(50); // 间隔 50ms
|
|
} else {
|
|
rt_mutex_take(pTHR->mutex, RT_WAITING_FOREVER);
|
|
pSOU->manual = 0;
|
|
rt_mutex_release(pTHR->mutex);
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#if 1
|
|
static int south_send(rt_uint8_t argc, char **argv)
|
|
{
|
|
struct chrg_south_t *pSOU = &chrgsouth;
|
|
int ch = 0, pt = 0;
|
|
|
|
if (3 != argc) {
|
|
rt_kprintf("help : %s [0-3] [0-2]\r\n", argv[0]);
|
|
return -1;
|
|
}
|
|
|
|
ch = (rt_uint8_t)atoi(argv[1]);
|
|
if ((ch < 0)||(ch >= TOTAL_SOU_CHS)) {
|
|
rt_kprintf("help : %s [0-3] [0-2]\r\n", argv[0]);
|
|
return -1;
|
|
}
|
|
|
|
pt = (rt_uint8_t)atoi(argv[2]);
|
|
if ((pt < 0)||(pt >= TOTAL_MB_R)) {
|
|
rt_kprintf("help : %s [0-3] [0-2]\r\n", argv[0]);
|
|
return -1;
|
|
}
|
|
|
|
sou_ch = (rt_uint8_t)ch;
|
|
sou_pt = (rt_uint8_t)pt;
|
|
pSOU->manual = 1;
|
|
|
|
return 0;
|
|
}
|
|
|
|
MSH_CMD_EXPORT(south_send, south send test);
|
|
|
|
static int south_run (int argc, char **argv)
|
|
{
|
|
if (2 != argc) {
|
|
rt_kprintf("help : %s start|stop\r\n", argv[0]);
|
|
return -1;
|
|
}
|
|
|
|
if (rt_strcmp(argv[1], "start") == 0) {
|
|
sou_run = 1;
|
|
} else if (rt_strcmp(argv[1], "stop") == 0) {
|
|
sou_run = 0;
|
|
} else {
|
|
rt_kprintf("help : %s start|stop\r\n", argv[0]);
|
|
return -1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
MSH_CMD_EXPORT(south_run, south auto run);
|
|
|
|
#endif
|
|
|
|
|