Feng Hong / Mbed OS Nucleo_rtos_basic

main.cpp

Committer:
hi1000
Date:
2019-03-23
Revision:
7:e0c7e624c5fa
Parent:
6:a9a03663fa23
Child:
8:6105ffbaf237

File content as of revision 7:e0c7e624c5fa:

#include "mbed.h"
#include <HX711.h>
#include <eeprom.h>
#include "eeprom_cust.h"
//#include "digitLCD.h"
#include "SB1602E.h"
extern void analyzePayload();
extern void scaleCalibration();
void init_scale();
extern HX711 hx711;

unsigned char rx[8], tx[8];
unsigned char rx_length, tx_length;

#define LCD_1602
SB1602E lcd( PB_9, PB_8 );  //  SDA, SCL
CAN can1(PD_0, PD_1);
CAN can2(PB_5, PB_6);
DigitalOut led1(LED1);
DigitalOut led2(LED2);
//FlashIAP flashIAP;

//#define LCD_1621
//digitLCD lcd(PA_5,PA_4,PB_5); // WO, CS, DATA



extern EEPROM ep;
int init_id = 0x537;  // first 8 bit is the address

int a = 0;
int b = 0;

void print_char(char c = '*')
{
    printf("%c\r\n", c);
    fflush(stdout);
}

Thread thread;


CANMessage  msg;


InterruptIn button1(USER_BUTTON);
volatile bool button1_pressed = false; // Used in the main loop
volatile bool button1_enabled = true; // Used for debouncing
Timeout button1_timeout; // Used for debouncing

// Enables button when bouncing is over
void button1_enabled_cb(void)
{
    button1_enabled = true;
}

// ISR handling button pressed event
void button1_onpressed_cb(void)
{
    if (button1_enabled) { // Disabled while the button is bouncing
        button1_enabled = false;
        button1_pressed = true; // To be read by the main loop
        button1_timeout.attach(callback(button1_enabled_cb), 0.3); // Debounce time 300 ms
    }
}

void print_thread()
{
    while (true) {
 #if 1       
        if(can1.read(msg)) {
            print_char();
            printf("got message id=%d 0x%08x\r\n", msg.id, msg.id);
//            b = *reinterpret_cast<int*>(msg.data);
            b = msg.data[0];
            printf("got data %d 0x%08x \r\n", b, b);
            if(msg.id == 1337) {
                led2 = !led2;
 
                b = *reinterpret_cast<int*>(msg.data);
                printf("got message %d\r\n", b);
                if(b % 5 == 0)
                    led2 = !led2;
             }
        }
//        wait(0.2);
#endif
    }
}

int main()
{
    wait(1);
 #ifdef LCD_1621
    lcd.clear();            // clears display
    lcd.allsegson();
   
 //   lcd.printf("ABCDEFGHI"); // Standard printf function, All ASCII characters will display
#endif  
    printf("\n\n*** RTOS basic example ***\r\n");
    analyzePayload();
#ifdef LCD_1602
    lcd.printf( 0, "Hello world!" );    //  line# (0 or 1), string
    lcd.printf( 1, "pi = %.6f", 3.14159265 );
    lcd.putcxy(0x55, 5, 1);
    lcd.printf(5, 0, "UUU");
#endif    
    init_scale();
    thread.start(print_thread);

//    flashIAP.init();
//    printf("Flash start address: 0x%08x Flash Size: %d\r\n", flashIAP.get_flash_start(), flashIAP.get_flash_size());
//    can1.reset();
//    can2.reset();
    can1.frequency(100000);
//    can2.frequency(100000);
    //button1.mode(PullUp); // Activate pull-up
    button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event
//    eeprom_test();

    int idx = 0; // Just for printf below

    while(1) {
        if (button1_pressed) { // Set when button is pressed
            printf("scale value %f. \r\n", hx711.getGram());
            button1_pressed = false;
            printf("Button pressed %d\r\n", idx++);
            printf("ID=%d. \r\n", init_id + idx%10);
            can1.write(CANMessage((init_id + idx%10), reinterpret_cast<char*>(&a), 1));            
            led1 = !led1;
            a++;
        }
    }
 #if 0
    while(1) {
 //       can1.write(CANMessage(1337, reinterpret_cast<char*>(&a), sizeof(a)));
#if  
        can1.write(CANMessage(1337, reinterpret_cast<char*>(&a), 1));
#endif        
        printf("loop a=%d\n", a);
        led1 = !led1;
        a++;
        wait(0.2);
    }
#endif    
}