test

Dependencies:   mbed-STM32F103C8T6 mbed-rtos mbed-dev

main.cpp

Committer:
bbw
Date:
2020-05-05
Revision:
10:ef9fa7e34eff
Parent:
8:a32b83084287
Child:
11:9dc8bbb8dda3

File content as of revision 10:ef9fa7e34eff:

#include "mbed.h"
#include "stm32f103c8t6.h"
#include <string>
#include "stdio.h"
#include "stdlib.h"
#include "rtos.h"

DigitalOut led1(PC_13);

RawSerial debug_uart(PB_10, PB_11, 115200);
RawSerial wifi_uart(PA_2, PA_3, 115200);

Timer timer;

char      rxBuf[32];
char      wifi_rxBuf[32];  //receive msg from xiaomi cloud
short     g_isCloud = 0;   //flag for connected xiaomi cloud

//char      set_property[] = {"down set_properties "}
//char      get_property[] = {"down get_properties "}
int       i = 0;
//---------------------------------------------------

string a("");
string b("");

int flag = 0;

#if 0
void led1_thread(void const *argument) {
    
    while (true) 
    {
        wifi_uart.printf("get_down\r\n");       
        while(1){
            if(wifi_uart.readable())
            {
                char ch2 = wifi_uart.getc();
                //f((ch2=='\n')||(ch2=='\r'))
                if(ch2=='\r')
                {                   
                    debug_uart.printf("got string from wifi uart: '%s' \r\n", b.c_str());
                    if(!strncmp(b.c_str(),"down none",9))
                    {
                        //debug_uart.printf("--- none\r\n");
                    }
                    if(!(strncmp(b.c_str(),"down set_properties",19)))  
                    {
                        debug_uart.printf("--- set_properties\r\n");
                        wifi_uart.printf("result 2 7 0\r\n");
                    }       
                    if(!(strncmp(b.c_str(),"down get_properties",19)))  
                    {
                        debug_uart.printf("--- get_properties\r\n");
                        //wifi_uart.printf("result 2 7 0\r\n");
                    }               
                    if(!strncmp(b.c_str(),"down MIIO_net_change",20))
                    {
                        if((!strncmp(b.c_str()+21, "offline", 7)))
                        {
                            debug_uart.printf("offline\r\n");
                        }
                        if((!strncmp(b.c_str()+21, "local", 5)))
                        {
                            debug_uart.printf("local\r\n");
                        }
                        if((!strncmp(b.c_str()+21, "cloud", 5)))
                        {
                            debug_uart.printf("cloud\r\n");
                        }
                    }   
                    b.clear();  
                    break;                  
                }else{
                    b += ch2;
                }               
            }   
        }   
        //wait_ms(400);
    }
}

#endif

#if 0
void led0_thread(void const *argument) {
    while (1) {
        if(debug_uart.readable()){
            char ch1 = debug_uart.getc();
            if(ch1=='\n'){                   
                debug_uart.printf("sent string to wifi uart: '%s' \r\n", a.c_str());
                wifi_uart.printf("%s\r\n",a.c_str());
                a.clear();              
            }else{
                a += ch1;
            }            
        }
        if(wifi_uart.readable())
        {
            char ch2 = wifi_uart.getc();
            if((ch2=='\n')||(ch2=='\r')){                   
                debug_uart.printf("got string from wifi uart: '%s' \r\n", b.c_str());
                b.clear();              
            }else{
                b += ch2;
            }               
        }
        //wait(0.5);
    }
}
#endif

void led0_thread(void const *argument) {
    int err;
    char rxBuf[32];
    char wifi_rxBuf[32];
    char i; 
        int position;
        char dat[2];
    
    while (1) {
        wifi_uart.printf("get_down\r");
                timer.reset();  
                b.clear();
        while(timer.read_ms()<5000){
            if(wifi_uart.readable()){
                char ch = wifi_uart.getc();
                if(ch!=0x0d){
                    b += ch;
                }else{
                    debug_uart.printf("get string: %s \r\n", b.c_str());
                                        #if 1
                                        if(!strncmp(b.c_str(),"down none",9))
                    {
                        //debug_uart.printf("--- none\r\n");
                    }
                    if(!(strncmp(b.c_str(),"down set_properties",19)))  
                    {
                        debug_uart.printf("--- set_properties\r\n");
                                                b.erase(0,b.length()-3);
                                                position = atoi(b.c_str());
                                                debug_uart.printf("position = %d\r\n",position);
                        wifi_uart.printf("result 2 7 0\r\n");
                                                b.clear();
                                                continue;
                    }       
                    if(!(strncmp(b.c_str(),"down get_properties",19)))  
                    {
                        debug_uart.printf("--- get_properties\r\n");
                                                wifi_uart.printf("result 2 4 0 10 2 6 0 %d 2 7 30\r\n", position);
                                                b.clear();
                                                continue;
                    }               
                    if(!strncmp(b.c_str(),"down MIIO_net_change",20))
                    {
                        if((!strncmp(b.c_str()+21, "offline", 7)))
                        {
                            debug_uart.printf("offline\r\n");
                        }
                        if((!strncmp(b.c_str()+21, "local", 5)))
                        {
                            debug_uart.printf("local\r\n");
                        }
                        if((!strncmp(b.c_str()+21, "cloud", 5)))
                        {
                            debug_uart.printf("cloud\r\n");
                        }
                    }
                                        #endif                                      
                    break;
                }
            }
        }
                while(wifi_uart.readable()){wifi_uart.getc();}  
        wait_ms(400);
    }
}

void wifi_debug(void const *argument){
  while(1){
    if(wifi_uart.readable()){
      debug_uart.putc(wifi_uart.getc());
    }
    if(debug_uart.readable()){
      wifi_uart.putc(debug_uart.getc());
    }
  }
}

int main() {

    debug_uart.printf("hello world\r\n");
    led1 = 1; 
        flag = 0;
        
        wait(3);

    Thread thread1(led0_thread, NULL, osPriorityHigh, DEFAULT_STACK_SIZE); /*check the real-time current*/
    
        
    timer.start(); 

    while(1)
    {
            wait(1);
#if 0           
            if(flag){
                debug_uart.printf("get string: '%s'\r\n", b.c_str());
                b.clear();
                while(wifi_uart.readable()){wifi_uart.getc();}      
                flag = 0;
                debug_uart.printf("sent get_down to module\r\n");
                wifi_uart.printf("get_down\r\n");               
            }
#endif          
    }
}