prva verzija, poslednji bajt je sporan

main.cpp

Committer:
bosko001
Date:
2020-07-12
Revision:
1:dd44d6b64300
Parent:
0:c06c791ca345
Child:
2:190bae43eca9

File content as of revision 1:dd44d6b64300:

/* mbed Microcontroller Library
 * Copyright (c) 2019 ARM Limited
 * SPDX-License-Identifier: Apache-2.0
 */

#include "mbed.h"
#include "platform/mbed_thread.h"

#include "MiWiApp.h"
#include "SerialCom.h"
// Blinking rate in milliseconds
#define BLINKING_RATE_MS                                                    200

#define MINIMUM( A, B)  A<B ? A:B


DigitalIn b2(SW2);



char tx_niz[] = { 7,0,1,2,3, 4, 5 , 6, 7, 8};
char rx_niz[100];
int rx_br;

void consolafun( void );
Thread th_consola( consolafun );

void refreshMiWifun( void );
Thread th_refreshMiWi( refreshMiWifun );

int main()
{
    // Initialise the digital pin LED1 as an output
printf("\n\n\r ____________ POCETAK MiWI RX_______________\n\r");

    inic_MiWi( );
    
    DigitalOut led(LED1,1);
    DigitalOut ledB(LED3,1);
DigitalOut ledG(LED2,1);



   set_mode( 3 );   //prijem
 
 
 //for( int i=0; i<10; i++)
 {
      char r = readMiWiReg( 0x14 );  // read GCONFIG
  printf("rssi = %d\n\r", r );
//  print_regs();
}
 
int b = b2;
 
 wait(0.5);
   start_SerialCom( );   
   
    while (true) 
    {
 //       led = !led;

extern DigitalIn pin_irq0, pin_irq1;
//
//   unsigned char r = readMiWiReg( 0x14 );  // read GCONFIG
//  printf("rssi = %d   ", r );
//  printf(" irq0=%d  irq1=%d\n\r", pin_irq0.read(), pin_irq1.read() ); fflush(stdout);
////if( r> 50 )

    if( get_mode( ) == 3 )
    {
        if( pin_irq0.read() )
        {
              
             rx_br = 0;   
            //    print_regs();
                while( pin_irq0.read() )
                   {
                       rx_niz[rx_br++]= readMiWiData();
                      
                       
                    }
                        for(int i=0; i<rx_br;i++) { printf(" %d ", rx_niz[i]); } 
                       printf("\n\rrx_br %d\n\r", rx_br);
                    if( rx_br <= 4) { ledG=0; wait(0.1); ledG=1;}
                    else if(rx_br>3) {led = 0;sendToScom(rx_niz+2, rx_niz[0]-2/* MINIMUM(rx_br, rx_niz[0]-2)* */ ); }
                       rx_br = 0;
       }
        else        { if(!led )  led = 1; }
    }
//    else
    {
         
        if( b!= b2)
        {
            b = b2;
        if( b ) {/*set_mode( 1 );*/ ledB = 1;}
        else    { ledB = 0; txMiWi( tx_niz ); }
        }
   
    }



        thread_sleep_for(BLINKING_RATE_MS);
    }
}

char refrtx[10]={3,1,2, 3};
void refreshMiWifun( void )
{
    while(true)
    {
        wait(10);
       txMiWi(refrtx);
    }
}


void consolafun( void )
{
    while( true )
    {
     
     char uc = getchar( );
     switch( (unsigned char) uc )
     {
         case 'r': case 'R':  
         
            printf("\n\rcitanje registra, unesi broj:\n\r");
            int r;
            scanf("%x", &r);
            printf( "\n\rregistar %x ima vrednost %x\n\r", r, readMiWiReg(r));
        
            break;
         case 'w': case 'W':  
         {
            printf("\n\rupis registra, unesi broj registra:");
            int r;
            scanf("%x", &r);
            printf( " %x", r );
            printf("\n\runesi vrednost registra:");
            int v;
            scanf("%x", &v);
            printf( " %x", v );
            printf( "\n\rregistar %x ima vrednost %x\n\r", r, readMiWiReg(r));
        }
            break;
        case 's': case 'S':
            printf("aktivacija serijala\n\r");
             start_SerialCom( );   
             break;
     }
        
    }
}