[文章]

基于HiSpark_WiFi_IoT Hi3861板子项目实战3-触摸彩屏LCD

2020-11-29 13:46:46  372 WiFi IoT 板子 项目 触摸彩屏
分享

基于HiSpark_WiFi_IoT Hi3861板子项目实战2-彩屏LCD

这个例子是基于HiSpark Wi-Fi物联网套件Hi3861板子项目实战3-触摸彩屏LCD,从本例程中,在HarmonyOS中Hi3861学习到
      1。和谐liteOS Hi3861 WIFI设置和操作
      2。和谐LiteOS Hi3861 UDP长链接控制方式
      3。通过UDP控制彩屏LCD输出和触摸输入操作
      4。附上实战的原始码和个人开发的虚拟彩屏LCD带触摸,方便Hi3861,Hi3518EV300等没有彩屏驱动的MCU,也能通过彩屏LCD调试1。原始代码如下: #include“ ohos_init.h” #include“ cmsis_os2.h” #include“ hi_wifi_api.h” #include“ lwip / ip_addr.h” #include“ lwip / netifapi.h” #include“ lwip / sockets.h” #include” MQTTPacket.h“ #include” transport.h“ #include <stdio.h> #include <string.h> #include <unistd.h> #include”
LCD.png


#include“ wifiiot_gpio_ex.h”
#include“ lwip / netifapi.h”
#include“ lwip / api_shell.h”
#include“ net_common.h”
#include“ cJSON.h”

#define PARAM_HOTSPOT_SSID“ Honor” //您的AP SSID
#定义PARAM_HOTSPOT_PSK “12345678” //你的AP PSK
的#define PARAM_SERVER_ADDR “192.168.43.36”
的#define PARAM_SERVER_PORT 6201

#定义PARAM_HOTSPOT_TYPE WIFI_SEC_TYPE_PSK //定义在wifi_device_config.h

的#define R_LED WIFI_IOT_GPIO_IDX_10
的#define B_LED WIFI_IOT_GPIO_IDX_12
的#define G_LED WIFI_IOT_GPIO_IDX_11



的#define DOWN 0×01
#define UP 0x02
#define MOVE 0x03

静态字符响应[128] =“”;

static int sockfd = 0;
static int sockfd_isConect = 0;
static struct sockaddr_in toAddr = {0};
static int lcdTouch[3] ={0};

void UdpClient(const char* host, unsigned short port);
static void APPTask(void *arg);
static void PrintLinkedInfo(WifiLinkedInfo* info)
{
    if (!info) return;

    static char macAddress[32] = {0};
    unsigned char* mac = info->bssid;
    snprintf(macAddress, sizeof(macAddress), "%02X:%02X:%02X:%02X:%02X:%02X",
        mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
    printf("bssid: %s, rssi: %d, connState: %d, reason: %d, ssid: %s\r\n",
        macAddress, info->rssi, info->connState, info->disconnectedReason, info->ssid);
}

static volatile int g_connected = 0;

static void OnWifiConnectionChanged(int state, WifiLinkedInfo* info)
{
    if (!info) return;

    printf("%s %d, state = %d, info = \r\n", __FUNCTION__, __LINE__, state);
    PrintLinkedInfo(info);

    if (state == WIFI_STATE_AVALIABLE) {
        g_connected = 1;
    } else {
        g_connected = 0;
    }
}

static void OnWifiScanStateChanged(int state, int size)
{
    printf("%s %d, state = %X, size = %d\r\n", __FUNCTION__, __LINE__, state, size);
}

static WifiEvent g_defaultWifiEventListener = {
    .OnWifiConnectionChanged = OnWifiConnectionChanged,
    .OnWifiScanStateChanged = OnWifiScanStateChanged
};

static struct netif* g_iface = NULL;

int ConnectToHotspot(WifiDeviceConfig* apConfig)
{
    WifiErrorCode errCode;
    int netId = -1;

    errCode = RegisterWifiEvent(&g_defaultWifiEventListener);
    printf("RegisterWifiEvent: %d\r\n", errCode);

    errCode = EnableWifi();
    printf("EnableWifi: %d\r\n", errCode);

    errCode = AddDeviceConfig(apConfig, &netId);
    printf("AddDeviceConfig: %d\r\n", errCode);

    g_connected = 0;
    errCode = ConnectTo(netId);
    printf("ConnectTo(%d): %d\r\n", netId, errCode);

    while (!g_connected) { // wait until connect to AP
        osDelay(10);
    }
    printf("g_connected: %d\r\n", g_connected);

    g_iface = netifapi_netif_find("wlan0");
    if (g_iface) {
        err_t ret = netifapi_dhcp_start(g_iface);
        printf("netifapi_dhcp_start: %d\r\n", ret);

        osDelay(100); // wait DHCP server give me IP
        ret = netifapi_netif_common(g_iface, dhcp_clients_info_show, NULL);
        printf("netifapi_netif_common: %d\r\n", ret);
    }
    return netId;
}

void DisconnectWithHotspot(int netId)
{
    if (g_iface) {
        err_t ret = netifapi_dhcp_stop(g_iface);
        printf("netifapi_dhcp_stop: %d\r\n", ret);
    }

    WifiErrorCode errCode = Disconnect(); // disconnect with your AP
    printf("Disconnect: %d\r\n", errCode);

    errCode = UnRegisterWifiEvent(&g_defaultWifiEventListener);
    printf("UnRegisterWifiEvent: %d\r\n", errCode);

    RemoveDevice(netId); // remove AP config
    printf("RemoveDevice: %d\r\n", errCode);

    errCode = DisableWifi();
    printf("DisableWifi: %d\r\n", errCode);
}


static void NetTask(void *arg)
{
    (void)arg;
    WifiDeviceConfig config = {0};

    // 准备AP的配置参数
    strcpy(config.ssid, PARAM_HOTSPOT_SSID);
    strcpy(config.preSharedKey, PARAM_HOTSPOT_PSK);
    config.securityType = PARAM_HOTSPOT_TYPE;
for(;;){
    osDelay(10);

    int netId = ConnectToHotspot(&config);

    int timeout = 2;
    while (timeout--) {
        printf("After %d seconds, I will start\r\n", timeout);
        osDelay(100);
    }
    UdpClient(PARAM_SERVER_ADDR, PARAM_SERVER_PORT);
    // mqtt_connect();
    printf("disconnect to AP ...\r\n");
    DisconnectWithHotspot(netId);
    printf("disconnect to AP done!\r\n");
  }
}



int udpsend(char *msg,int len){
    if(sockfd_isConect==0)return -1;
    ssize_t retval = sendto(sockfd, msg, len, 0, (struct sockaddr *)&toAddr, sizeof(toAddr));
        if (retval < 0) {
            printf("sendto failed!\r\n");
            return -1;
        }
        return 1;
}

void UdpClient(const char* host, unsigned short port)
{
    int counts=0;
    ssize_t retval = 0;
    char* con = "connect;pc";
    while (g_connected){
    sockfd = socket(AF_INET, SOCK_DGRAM, 0); // UDP socket
    printf("ip%s:%d\r\n",host,port);
    toAddr.sin_family = AF_INET;
    toAddr.sin_port = htons(port);
    if (inet_pton(AF_INET, host, &toAddr.sin_addr) <= 0) {
        printf("inet_pton failed!\r\n");
        goto do_cleanup;
    }

    retval = sendto(sockfd, con, sizeof(con), 0, (struct sockaddr *)&toAddr, sizeof(toAddr));
    if (retval < 0) {
        printf("sendto failed!\r\n");
        goto do_cleanup;
    }

    GpioSetOutputVal(B_LED, 1);
    printf("request connect\r\n");
    sockfd_isConect =1;
    struct sockaddr_in fromAddr = {0};
    socklen_t fromLen = sizeof(fromAddr);

    struct timeval timeOut;
    timeOut.tv_sec = 5;                 //设置2s超时
    timeOut.tv_usec = 0;
    if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeOut, sizeof(timeOut)) < 0)
      {
             printf("time out setting failed\n");
      }
    for(;;){
    retval = recvfrom(sockfd, &response, sizeof(response), 0, (struct sockaddr *)&fromAddr, &fromLen);
    if (retval == -1 && errno == EAGAIN)      //阻塞接收超时
           {
               printf("timeout..\n");
               if(++counts>=5){
              // goto do_cleanup;
                  break;
               }
           }
    else{
         int x=0 ,y=0;
         counts=0;
         response[retval] = '\0';


         cJSON *json = cJSON_Parse((char *)response);

         char *mouse = cJSON_GetObjectItem((cJSON *)json,"mouse")->valuestring;

         if(strncmp(mouse, (char *)"mouseMove", 9) == 0){
             x = cJSON_GetObjectItem((cJSON *)json,"x")->valueint;
            y = cJSON_GetObjectItem((cJSON *)json,"y")->valueint;

             lcdTouch[0]=MOVE;
             lcdTouch[1]=x;
             lcdTouch[2]=y;

         }
         else if(strncmp(mouse, (char *)"mouseDown", 9) == 0){
             x = cJSON_GetObjectItem((cJSON *)json,"x")->valueint;
             y = cJSON_GetObjectItem((cJSON *)json,"y")->valueint;

             lcdTouch[0]=DOWN;
             lcdTouch[1]=x;
             lcdTouch[2]=y;

         }
         else if(strncmp(mouse, (char *)"mouseUp", 7) == 0){
            x = cJSON_GetObjectItem((cJSON *)json,"x")->valueint;
            y = cJSON_GetObjectItem((cJSON *)json,"y")->valueint;

             lcdTouch[0]=UP;
             lcdTouch[1]=x;
             lcdTouch[2]=y;

         }

         cJSON_Delete(json);


        }

    }
sockfd_isConect=0;
GpioSetOutputVal(B_LED, 0);

do_cleanup:
    printf("do_cleanup...\r\n");
    close(sockfd);
    }
}



static void MainEntry(void) //主程入口
{

    GpioInit();

    //设置红色,蓝 色,绿色 LED IO为输出状态
    IoSetFunc(WIFI_IOT_IO_NAME_GPIO_10, WIFI_IOT_IO_FUNC_GPIO_10_GPIO);
    GpioSetDir(WIFI_IOT_IO_NAME_GPIO_10, WIFI_IOT_GPIO_DIR_OUT);

    IoSetFunc(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_IO_FUNC_GPIO_11_GPIO);
    GpioSetDir(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_GPIO_DIR_OUT);

    IoSetFunc(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_IO_FUNC_GPIO_12_GPIO);
    GpioSetDir(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_GPIO_DIR_OUT);

    osThreadAttr_t attr;

    attr.name = "NetTask";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 30960;
    attr.priority = osPriorityNormal;

    if (osThreadNew(NetTask, NULL, &attr) == NULL) {  //Wifi udp task
        printf("[NetEntry] Falied to create NetTask!\n");
    }

    attr.name = "APPTask";
    attr.attr_bits = 0U;
    attr.stack_size = 30960;
    //attr.priority = osPriorityBelowNormal7;

    if (osThreadNew(APPTask, NULL, &attr) == NULL) {  //app
         printf("[NetEntry] Falied to create APPTask!\n");
     }

}

SYS_RUN(MainEntry);


//=========================================================
//RBG LCD绘图API接口 , LCD Color = RRBBGG(0XFFFFFF)

static char str[250] ={0};
void drawPixel(int x,int y, unsigned int color ){
    int len = sprintf((char *)str,"drawPixel;%d;%d;#%06x",x,y,color);
    udpsend((char *)str,len);
}

void drawLine(int x0,int y0,int x1,int y1, unsigned int color ){
    int len = sprintf((char *)str,"drawLine;%d;%d;%d;%d;#%06x",x0,y0,x1,y1,color);
    udpsend((char *)str,len);
}

void drawArc(int x, int y, int radius, int startAngle, int endAngle,unsigned int color){
    int len = sprintf((char *)str,"drawArc;%d;%d;%d;%d;%d;#%06x",x,y,radius,startAngle,endAngle,color);
    udpsend((char *)str,len);
}

void drawCircle(int x,int y, int radius,unsigned int color,int fill ){
    int len = sprintf((char *)str,"drawCircle;%d;%d;%d;#%06x;%d",x,y,radius,color,fill);
    udpsend((char *)str,len);
}


void drawRect(int x,int y, int width,int hight,unsigned int color,int fill ){
    int len = sprintf((char *)str,"drawRect;%d;%d;%d;%d;#%06x;%d",x,y,width,hight,color,fill);
    udpsend((char *)str,len);
}

void drawText(int x,int y, unsigned int color,const char* text,int size ){
    int len = sprintf((char *)str,"drawText;%d;%d;#%06x;%s;%d",x,y,color,text,size);
    udpsend((char *)str,len);
}

void drawImage(int x,int y, const char* fileName ){
    int len = sprintf((char *)str,"drawImage;%d;%d;%s",x,y,fileName);
    udpsend((char *)str,len);
}

void drawClean(unsigned int color ){
    int len = sprintf((char *)str,"drawClean;#%06x",color);
    udpsend((char *)str,len);
}

//===========================这个任务是写自已应用===============================

static void APPTask(void *arg)
{
    (void)arg;

    int x=0,y=0;

    while(!sockfd_isConect){
        osDelay(100);
    }

    drawClean(0xff0000); //red

    osDelay(100);

    drawClean(0x00ff00); //green

    osDelay(100);

    drawClean(0x0000ff);//blue

   osDelay(100);
    //drawImage(50,50, "test" );
    drawImage(0,0,"back");

    osDelay(100);

    drawText(60,100, 0x00ff00,"hello word!",20);

    osDelay(100);

    drawText(60,60, 0x00ff00,"中文测试",30);

    drawText(200,60, 0x0000ff,"中文测试",14);

    osDelay(100);

    drawCircle(300,70, 80,0xffffff,1);

    drawRect(200,100,100,150,0x0000ff,1);

for(;;){
        osDelay(10);
        if(lcdTouch[0]){


             if(lcdTouch[0]==DOWN) {
                 x=lcdTouch[1];
                 y=lcdTouch[2];
             }
             else  if(lcdTouch[0]==UP) {

             }
             else  if(lcdTouch[0]==MOVE) {

                 if((x==0)&&(y==0)){
                     x=lcdTouch[1];
                     y=lcdTouch[2];
                 }

                 drawLine(x,y,lcdTouch[1],lcdTouch[2],0xff0000);

                 x=lcdTouch[1];
                 y=lcdTouch[2];

             }
             lcdTouch [0] = 0;
        }
    }
}
2.
二进制附件[attach] 980057 [/ attach]
3.虚拟UDP彩屏LCD   
[附件] 980055 [/ attach]
[附件] 980056 [/ attach]





3

app.zip

6.28 KB, 下载次数: 5, 下载积分: 积分 -1 分

Hi3861-LCD 1.0.1.zip.002

27.51 MB, 下载次数: 6, 下载积分: 积分 -1 分

Hi3861-LCD 1.0.1.zip.001

29 MB, 下载次数: 5, 下载积分: 积分 -1 分

评分

参与人数 1积分 +6 收起 理由
emitter + 6 原创技术先锋,加分鼓励!

查看全部评分

2020-11-29 13:46:46   评论 分享淘帖
2 个讨论
厉害了,还能这么玩,赞
2020-11-30 09:11:24 评论

举报

有视频,有代码!赞
2020-11-30 11:34:23 评论

举报

您需要登录后才可以回帖 登录 | 注册

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容图片侵权或者其他问题,请联系本站作侵删。 侵权投诉
发表新帖