zoukankan      html  css  js  c++  java
  • (2)小彩灯接收数据解析

    const size_t t_bright=1,t_color=2,t_frequency=3,t_switch=4;
    int red = 0,green = 0,blue = 0;
    int type = 2;//当前模式 1亮度 2颜色 3呼吸 4开关
    
    /**
         * @Desc 解析json
         * 有三种
         * 1.亮度控制页面(0 暗 1正常 2亮)
         * {
         *     "t": 1,
         *     "bb": 2
         * }
         * 2.颜色控制页面
         * {
         *     "t": 2,
         *     "cr": 154,
         *     "cg": 147,
         *     "cb": 255
         * }
         * 3.呼吸灯控制页面(0 慢呼吸 1正常 2快)
         * {
         *    "t": 3,
         *    "gf": 1
         * }
         * 4.开关控制(0关闭 1开启)
         * {
         *    "t": 4,
         *    "ss": 1
         * } 
         **/
    bool parseData(char* content) {
    
     
       String str(content); 
        
      type = JsontoString(str,"ledmode");//分割调用
      switch(type){ 
        case t_color:
             red = JsontoString(str,"cr");//分割调用
             green = JsontoString(str,"cg");//分割调用
             blue =  JsontoString(str,"cb");//分割调用
             colorRGB(red,green,blue);
             break;
       
      }
      return true;
    }
    
    /**
    * 控制RGB颜色
    */
    void colorRGB(int red, int green, int blue){
    
         analogWrite(PIN_RED,constrain(red,0,255));
    //Serial.println(constrain(red,0,255));
          analogWrite(PIN_GREEN,constrain(green,0,255));
       //Serial.println(constrain(green,0,255));
        analogWrite(PIN_BLUE,constrain(blue,0,255));
    //Serial.println(constrain(blue,0,255));
          
     
    }
    
    /*数说明: 从Json格式的String中,截取对应键值
    输入:  String 数据帧 String 键名
    输出:  int 类型的 键值
    示例;
      String m=  "{"ledmode":2,"cr":ff,"cg":a,"cb":1}";
      int a=JsontoString(m,"ledmode");//分割调用
    结果: a=2;
    */
    int  JsontoString(String zifuchuan,String fengefu)
      {
      fengefu="""+fengefu+"""; 
      int weizhi_KEY; //找查的位置
      int weizhi_DH; 
      String temps;//临时字符串
     weizhi_KEY = zifuchuan.indexOf(fengefu);//找到位置
     temps=zifuchuan.substring( weizhi_KEY+fengefu.length(), zifuchuan.length());//打印取第一个字符
     weizhi_DH = weizhi_KEY+fengefu.length()+ temps.indexOf(',');//找到位置
     if( temps.indexOf(',')==-1){ 
      // weizhi_DH = weizhi_KEY+fengefu.length()+ temps.indexOf('}');//找到位置
     weizhi_DH = zifuchuan.length()-1;
      }
     temps="";
      temps=zifuchuan.substring( weizhi_KEY+fengefu.length()+1,  weizhi_DH);//打印取第一个字符
    //  Serial.print(temps);
    //  Serial.print("---");
    
     return StringtoHex(temps);
      }
    
    /*说明 String 转 16进制对应的10进制数
    输入:  String
    输出:  int 
    示例:
    FF   255
    ff   255
    1    1
    */
    int StringtoHex(String temps)
      {
      int l=0;
      int p=1;
      for (int i = temps.length()-1; i >=0; i--) {
       if(temps[i]=='a'||temps[i]=='b'||temps[i]=='c'||temps[i]=='d'||temps[i]=='e'||temps[i]=='f'){
         l+=((int)(temps[i]-'a')+10)*p;
        p*=16;
       }
       else if(temps[i]=='A'||temps[i]=='B'||temps[i]=='C'||temps[i]=='D'||temps[i]=='E'||temps[i]=='F'){
         l+=((int)(temps[i]-'A')+10)*p;
        p*=16;
       }
       else if(temps[i]=='0'||temps[i]=='1'||temps[i]=='2'||temps[i]=='3'||temps[i]=='4'||temps[i]=='5'||temps[i]=='6'||temps[i]=='7'||temps[i]=='8'||temps[i]=='9')
       {
          l+=(int)(temps[i]-'0')*p;
        p*=16;
        
        }
       
     }
     // Serial.println(l);
      return  l;
        
        }
    
        /*
    说明 String 转 10进制对应的10进制数
    输入:  String
    输出:  int 
    示例:
    1023  1023
    1    1
    */
    int StringtoInt(String temps)
      {
    

      

  • 相关阅读:
    CTF---隐写术入门第二题 小苹果
    文件上传
    文件读取
    sqlmap之绕过waf思路
    【小技巧分享】如何通过微博图片进行社工Po主
    Windows 11恢复传统右键菜单-2021.10.5正式版
    sql注入之Oracle注入
    CTF之buuctf
    常见sql注入payload
    信息收集之Github
  • 原文地址:https://www.cnblogs.com/kekeoutlook/p/9122893.html
Copyright © 2011-2022 走看看