Feng Hong / Mbed OS Nucleo_rtos_basic

main.cpp

Committer:
hi1000
Date:
2019-07-20
Revision:
18:e6ed582f7022
Parent:
17:faa4d4976d22
Child:
19:0356e54240cc

File content as of revision 18:e6ed582f7022:

#include "mbed.h"
#include <HX711.h>
#include <eeprom.h>
#include "eeprom_cust.h"
//#include "digitLCD.h"
#include "SB1602E.h"
#include "yoda2.h"

EventFlags button_event_flags;
EventFlags LCD_update_flags;
extern void main_menu();
extern void analyzePayload();
#if 1
#ifdef STM32F207xx
HX711 hx711(PB_11, PB_10);// data, clk
#endif

#ifdef STM32F303xE
//HX711 hx711(D8, D9);// data, clk
HX711 hx711(PA_14, PA_15);
#endif
extern void scaleCalibration(bool release_led);
extern void init_scale();
Thread scale_thread;
extern void scale_reading();
#endif
unsigned char rx_buffer[8], tx_buffer[8];
unsigned char rx_length, tx_length;

#define LCD_1602
#ifdef STM32F207xx
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
#endif
#ifdef STM32F303xE
SB1602E lcd(D14, D15 );  //  SDA, SCL
CAN can1(PA_11, PA_12); // RD, TD
DigitalOut led1(LED1);  // only one LED PA_5
DigitalOut led2(LED2);  // only one LED PA_5
#endif

DigitalOut output1(PC_3);
DigitalOut output2(PC_2);
DigitalOut output3(PB_7);
DigitalIn input1(PC_6);
DigitalIn input2(PC_8);
int current_weight;
EEPROM ep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C256);
extern void eeprom_test(void);
int device_address = DEVICE_DEFAULT_ADDRESS;  // address 11 bits: last 8 bits is device_address
int device_type;  //address 11 bits: first 3 bits is device_type
int init_id = 0x00300000;  // first 11 bit is the address
int init_filter_handle, broadcast_filter_handle;
int broadcast_id = 0x1ffC0000;

uint8_t can_tx_data[8];
uint8_t can_rx_data[8];

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


Thread can_receivethread;
Thread can_handlethread;
Thread mainmenu_thread;

CANMessage  msg;
MemoryPool<CANMessage, 16> can_mpool;
Queue<CANMessage, 16> can_queue;

InterruptIn button0(USER_BUTTON);
volatile bool button0_pressed = false; // Used in the main loop
volatile bool button0_enabled = true; // Used for debouncing
Timeout button0_timeout; // Used for debouncing
InterruptIn button1(PB_4);
volatile bool button1_pressed = false; // Used in the main loop
volatile bool button1_enabled = true; // Used for debouncing
Timeout button1_timeout; // Used for debouncing
InterruptIn button2(PC_1);
volatile bool button2_pressed = false; // Used in the main loop
volatile bool button2_enabled = true; // Used for debouncing
Timeout button2_timeout; // Used for debouncing
InterruptIn button3(PC_7);
volatile bool button3_pressed = false; // Used in the main loop
volatile bool button3_enabled = true; // Used for debouncing
Timeout button3_timeout; // Used for debouncing
InterruptIn button4(PA_9);
volatile bool button4_pressed = false; // Used in the main loop
volatile bool button4_enabled = true; // Used for debouncing
Timeout button4_timeout; // Used for debouncing


// Enables button when bouncing is over
//button0
void button0_enabled_cb(void)
{
    int button_status;
    button_status = button0.read();
    if (button_status == 0)
    {    
        printf("button0 down\r\n");
        scaleCalibration(true);
        button_status = button0.read();
        if (button_status == 0)
        {
            printf("button0 hold\r\n");
        }
        else
        {
            printf("button0 press hold and release\r\n");
        }
    }    
    else
        printf("button0 released\r\n");
    button0_enabled = true;
}

// ISR handling button pressed event
void button0_onpressed_cb(void)
{
    if (button0_enabled) { // Disabled while the button is bouncing
        button0_enabled = false;
        button0_pressed = true; // To be read by the main loop
        button0_timeout.attach(callback(button0_enabled_cb), 0.3); // Debounce time 300 ms
    }
}
//button0--
//button1
void button1_enabled_cb(void)
{
    int button_status;
    button_status = button1.read();
    if (button_status == 0)
    {    
        printf("button1 down\r\n");
        button_event_flags.set(BUTTON1_HOLD_EVENT);
    }    
    else
    {
        printf("button1 released\r\n");
        button_event_flags.set(BUTTON1_PRESSED_EVENT);
    }
    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
    }
}
//button1--
//button2
void button2_enabled_cb(void)
{
    int button_status;
    button_status = button2.read();
    if (button_status == 0)
    {    
        printf("button2 down\r\n");
        button_event_flags.set(BUTTON2_HOLD_EVENT);
    }    
    else
    {
        printf("button2 released\r\n");
        button_event_flags.set(BUTTON2_PRESSED_EVENT);        
    }
    button2_enabled = true;
}

// ISR handling button pressed event
void button2_onpressed_cb(void)
{
    if (button2_enabled) { // Disabled while the button is bouncing
        button2_enabled = false;
        button2_pressed = true; // To be read by the main loop
        button2_timeout.attach(callback(button2_enabled_cb), 0.3); // Debounce time 300 ms
    }
}
//button2--
//button3
void button3_enabled_cb(void)
{
    int button_status;
    button_status = button3.read();
    if (button_status == 0)
    {    
        printf("button3 down\r\n");
        button_event_flags.set(BUTTON3_HOLD_EVENT);        
    }    
    else
    {
        printf("button3 released\r\n");
        button_event_flags.set(BUTTON3_PRESSED_EVENT);  
    }
    button3_enabled = true;
}

// ISR handling button pressed event
void button3_onpressed_cb(void)
{
    if (button3_enabled) { // Disabled while the button is bouncing
        button3_enabled = false;
        button3_pressed = true; // To be read by the main loop
        button3_timeout.attach(callback(button3_enabled_cb), 0.3); // Debounce time 300 ms
    }
}
//button3--
//button4
void button4_enabled_cb(void)
{
    int button_status;
    button_status = button4.read();
    if (button_status == 0)
    {    
        printf("button4 down\r\n");
        button_event_flags.set(BUTTON4_HOLD_EVENT);         
    }    
    else
    {
        printf("button4 released\r\n");
        button_event_flags.set(BUTTON4_PRESSED_EVENT);   
    }
    button4_enabled = true;
}

// ISR handling button pressed event
void button4_onpressed_cb(void)
{
    if (button4_enabled) { // Disabled while the button is bouncing
        button4_enabled = false;
        button4_pressed = true; // To be read by the main loop
        button4_timeout.attach(callback(button4_enabled_cb), 0.3); // Debounce time 300 ms
    }
}
//button4--
void can_sendData(int can_id, uint8_t *tx_data, int length)
{
    CANMessage txmsg;

    txmsg.format = CANExtended;
    txmsg.id = can_id;
    txmsg.len = length;
    txmsg.data[0] = tx_data[0];
    txmsg.data[1] = tx_data[1];
    txmsg.data[2] = tx_data[2];
    txmsg.data[3] = tx_data[3];
    txmsg.data[4] = tx_data[4];
    txmsg.data[5] = tx_data[5];
    txmsg.data[6] = tx_data[6];
    txmsg.data[7] = tx_data[7];                            
    
    printf("can_sendData can_id=0x%08x \r\n", can_id);
    can1.write(txmsg);    
}

void can_rxthread()
{
    int loop;
    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);
            for (loop = 0; loop < msg.len; loop++)
            {
                can_rx_data[loop] = msg.data[loop];
            }
            
            printf("got data: length:%d\r\n", msg.len);
            for (loop = 0; loop < msg.len; loop++)
            {
                printf("data[%d]=%d\r\n", loop, can_rx_data[loop]);
            }           
//            if(msg.id == 1337) 
            {
                //only queue the message belongs to you
                CANMessage *can_message = can_mpool.alloc();
                memcpy((void *)can_message, (void *)&msg, sizeof(msg)); 
                if (!can_queue.full())
                    can_queue.put(can_message);
                else
                {
                    printf("message queue is full. \r\n");    
                }                
                led2 = !led2;
             }
        }
#endif
//        wait(0.2);
    }
}

int main()
{
    int loop = 0;
    int8_t ival;
    printf("\n\n*** RTOS starts ***\r\n");
  
//    wait(1);
    ep.read((uint32_t)EEPROM_DEVICE_ADDRESS_ADDRESS,device_address); 
    printf("EEPROM: read device address:%d 0x%08x\r\n", device_address, device_address);
    if ((device_address == 0) || (device_address == 0xFFFFFFFF))
        device_address = DEVICE_DEFAULT_ADDRESS;
    device_type = (device_address & 0x00000700) >> 8;
    
#ifdef LCD_1621
    lcd.clear();            // clears display
    lcd.allsegson();
   
 //   lcd.printf("ABCDEFGHI"); // Standard printf function, All ASCII characters will display
#endif  

#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");
//    lcd.printf(0, 0, "pressed!" );      
#endif   

    input1.mode(PullUp);
    input2.mode(PullUp);

//    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();
    printf("device_address =0x%08x \r\n", (device_address<<18));
    can1.frequency(100000);
    can1.filter((device_address<<18), 0x1FFC0000, CANExtended, init_filter_handle);  // 0x1FFC0000 to filter the last 18bits 0-17
    device_address = (device_address & 0x000000FF);
//only support one filter
//    can1.filter(broadcast_id, 0x1FFC0000, CANExtended, broadcast_filter_handle); // the broadcast id
//    can2.frequency(100000);
    //button0.mode(PullUp); // Activate pull-up
    can_receivethread.start(can_rxthread);  
    can_handlethread.start(analyzePayload);   

    button0.fall(callback(button0_onpressed_cb)); // Attach ISR to handle button press event
    button0.rise(callback(button0_onpressed_cb)); // Attach ISR to handle button press event
    button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event
    button1.rise(callback(button1_onpressed_cb)); // Attach ISR to handle button press event
    button2.fall(callback(button2_onpressed_cb)); // Attach ISR to handle button press event
    button2.rise(callback(button2_onpressed_cb)); // Attach ISR to handle button press event
    button3.fall(callback(button3_onpressed_cb)); // Attach ISR to handle button press event
    button3.rise(callback(button3_onpressed_cb)); // Attach ISR to handle button press event
    button4.fall(callback(button4_onpressed_cb)); // Attach ISR to handle button press event
    button4.rise(callback(button4_onpressed_cb)); // Attach ISR to handle button press event    
//    eeprom_test();

#if 1  
//    scaleCalibration(true);
    init_scale();
    scale_thread.start(scale_reading);
    mainmenu_thread.start(main_menu);

#endif
    while(1) {
        wait(1);
    }

#if 0
    int idx = 0; // Just for printf below
    can_tx_data[0] = 0;
    while(1) {
        if (button0_pressed) { // Set when button is pressed
#if 0        
            printf("scale value %f. \r\n", hx711.getGram());
#endif
            can_tx_data[1] = can_tx_data[0]+1;
            can_tx_data[2] = can_tx_data[1]+1;
            can_tx_data[3] = can_tx_data[2]+1;
            can_tx_data[4] = can_tx_data[3]+1;
            can_tx_data[5] = can_tx_data[4]+1;
            can_tx_data[6] = can_tx_data[5]+1;
            can_tx_data[7] = can_tx_data[6]+1; 
            button0_pressed = false;
            printf("Button pressed %d\r\n", idx++);
            printf("ID=%d data[0]=%d. \r\n", init_id + idx%10, can_tx_data[0]);
#ifdef LCD_1602
            lcd.printf(0, 0, "%d ", idx );    //  line# (0 or 1), string
#endif   
            can1.write(CANMessage((init_id + idx%10), reinterpret_cast<char*>(can_tx_data), 8, CANData,CANExtended));            
            led1 = !led1;
            can_tx_data[0]++;
        }
    }
#endif
  
}