Check this one ! mbos setup changed, WTX hor pos changed, no font style check, no checksum check

Dependencies:   4DGL MODSERIAL mbed mbos

Fork of CDU_Mbed_21 by Engravity-CDU

main.cpp

Committer:
LvdK
Date:
2013-01-29
Revision:
6:e0bf3b244d7c
Parent:
5:99594f4ab659
Child:
7:6576a287e563

File content as of revision 6:e0bf3b244d7c:

// L. van der Kolk, ELVEDEKA, Holland //
// File:  main.cpp  Mbos O.S testversion  

#include "mbed.h"
#include "MODSERIAL.h"
#include "mbos.h"

//--------------------------------------------------------------------------------
#include "TextLCD.h"                    //  < LCD connected for testing only << !
TextLCD lcd(p8, p9, p10, p11, p12, p13); // rs, e, d4-d7 << for testing only << !
//--------------------------------------------------------------------------------

DigitalOut led1(LED1); // : LED 1 on Mbed board toggles when CDU is alive
 
extern MODSERIAL USB; 
extern int key_hit_ID;
int CDU_FS_interface = 0;   // : defines interface to FS: 0 = USB, 1 = Ethernet

void init_USB();
void set_initial_CDU_screen();
void Send_KEY_message(int button); 
void Send_ALIVE_message(int seconds);
void collect_FSdata();

//----------------------------------------------------------------
void init_buttons_Mbed(); // << for testing only << !
void scan_buttons_Mbed(); // << for testing only << !
//----------------------------------------------------------------

// ALIVE_TASK properties:
#define ALIVE_TASK_ID           1
#define ALIVE_TASK_PRIO         50
#define ALIVE_TASK_STACK_SZ     100    // : must be 100 to prevent stack overflow error!

// ALIVE_TIMER properties:
#define ALIVE_TIMER_ID          1
#define ALIVE_TIMER_PERIOD      500  // = 5 sec

// RECEIVE_DECODE_TASK properties:
#define RECEIVE_DECODE_TASK_ID       2
#define RECEIVE_DECODE_TASK_PRIO     80
#define RECEIVE_DECODE_TASK_STACK_SZ 256  // : must be 256 to prevent stack overflow error!

// SEND_KEYMESSAGE_TASK properties:
#define SEND_KEYMESSAGE_TASK_ID      3
#define SEND_KEYMESSAGE_TASK_PRIO    60
#define SEND_KEYMESSAGE_TASK_STACK_SZ 100   // : must be 100 to prevent stack overflow error!

//------------------------------ LvdK button scan --------------
// SCAN_KEYS_TASK properties:  
#define SCAN_KEYS_TASK_ID      4
#define SCAN_KEYS_TASK_PRIO    85
#define SCAN_KEYS_TASK_STACK_SZ 100

// SCAN_KEYS_TIMER properties: ( LvdK button scan )
#define SCAN_KEYS_TIMER_ID          2
#define SCAN_KEYS_TIMER_PERIOD      50  // = 50msec 
//----------------------------------------------------------------  

// Mbos EVENTS:
#define ALIVE_EVENT     1      
#define RX_EVENT        2    // : Event flag caused by serial RX interrupt
#define KEY_EVENT       4
#define SCAN_KEYS_TIMER_EVENT 8    // : LvdK button scan timer event

// Mbos resources:
#define USB_TX_RESOURCE      1     // : for locking USB TX buffer
#define USB_TX_PRIO         95     // : USB TX resource priority ( = highest! )


mbos CDU_OS( 8, 6, 5); //instantiate mbos O.S (tasks, timers, resources)

 // Task function prototypes:
 void ALIVE_TASK(void);
 void RECEIVE_DECODE_TASK(void);
 void SCAN_KEYS_TASK(void);
 void SEND_KEYMESSAGE_TASK(void);
 
 void char_received(MODSERIAL_IRQ_INFO *q) {
    // -- This functiom will be called on EACH rx interrupt --
    // Note: this is Rx interrupt handling, NOT a mbos task.
    // It only set an mbos Event.
    CDU_OS.SetEvent(RX_EVENT,RECEIVE_DECODE_TASK_ID); // : set event flag for Read Serial Task
}

//------------ USB error handling ------------------------------------------------------------
void rx_buf_overflow(MODSERIAL_IRQ_INFO *q) {
    // This functiom will be called when RX buffer overflows
    // Blue-Leds-of-Death will be showed, USB is dead so message itself will not be seen !
    error("RX OVERFLOW !"); 
}

void tx_buf_overflow(MODSERIAL_IRQ_INFO *q) {
    // This functiom will be called when TX buffer overflows
    // Blue-Leds-of-Death will be showed, USB is dead so message itself will not be seen !
    error("TX OVERFLOW !");  
}
//---------------------------------------------------------------------------------------------

void init_USB()
{   // -- This function controls the setup for the CDU-FS USB communication --
    // Function char_received() is attached to Rx interrupt.
    USB.baud(38400); // allways set default baudrate to 38400
    USB.attach(&char_received, MODSERIAL::RxIrq);    // : attach function char_received to RX inerrupt
    USB.attach(&rx_buf_overflow, MODSERIAL::RxOvIrq); //: attach function to call when RX buffer overflows
    USB.attach(&tx_buf_overflow, MODSERIAL::TxOvIrq); //: attach function to call when TX buffer overflows
}
 
main()
{
  init_USB();  // : setup USB communication 
  set_initial_CDU_screen();
  
  //------------------ for testing only ! ----------------------------------
  init_buttons_Mbed();   
  lcd.cls(); // : clear LCD display
  lcd.printf("CDU USB Mbos\n");
  USB.printf("\n--- CDU USB Mbos ---\n");
  //------------------------------------------------------------------------
    
  // Configure all tasks, timers and resources:
  CDU_OS.CreateTask(ALIVE_TASK_ID, ALIVE_TASK_PRIO, ALIVE_TASK_STACK_SZ, ALIVE_TASK);
  CDU_OS.CreateTask(RECEIVE_DECODE_TASK_ID, RECEIVE_DECODE_TASK_PRIO, RECEIVE_DECODE_TASK_STACK_SZ, RECEIVE_DECODE_TASK);
  CDU_OS.CreateTask(SEND_KEYMESSAGE_TASK_ID, SEND_KEYMESSAGE_TASK_PRIO, SEND_KEYMESSAGE_TASK_STACK_SZ, SEND_KEYMESSAGE_TASK);  
  CDU_OS.CreateTask(SCAN_KEYS_TASK_ID, SCAN_KEYS_TASK_PRIO, SCAN_KEYS_TASK_STACK_SZ, SCAN_KEYS_TASK );
  CDU_OS.CreateTimer(ALIVE_TIMER_ID, ALIVE_TASK_ID, ALIVE_EVENT);  
  CDU_OS.CreateTimer(SCAN_KEYS_TIMER_ID, SCAN_KEYS_TASK_ID, SCAN_KEYS_TIMER_EVENT);    
  CDU_OS.CreateResource(USB_TX_RESOURCE, USB_TX_PRIO);
  //CDU_OS.CreateTask(TASK4_ID, TASK4_PRIO, TASK4_STACK_SZ, task4);
     
  // Start mbos O.S with final statement in main() :
  CDU_OS.Start(); // : will never return!    
}        

void RECEIVE_DECODE_TASK(void)
{    // Task waits for RX_EVENT, then 
     // collects and decodes data from FS
    while(1){  
        CDU_OS.WaitEvent(RX_EVENT); // : wait for RX event
        collect_FSdata();
    } 
}

void SEND_KEYMESSAGE_TASK(void) 
{
    while(1){  
        CDU_OS.WaitEvent(KEY_EVENT); // : wait for KEY event ( = CDU key pressed )
        Send_KEY_message(key_hit_ID);
        key_hit_ID = 0;  // : reset Id flag
    }
}

void ALIVE_TASK(void)
{   // Mbos task started by mbos timer to send ALIVE message to FS every 5 sec.
    CDU_OS.SetTimer( ALIVE_TIMER_ID, ALIVE_TIMER_PERIOD, ALIVE_TIMER_PERIOD );
    static int seconds = 0;
    while(1){  // : loop forever because it is a mbos taskfunction
        CDU_OS.WaitEvent(ALIVE_EVENT ); // : wait for Mbos timer event (5 sec)
        seconds += 5 ; // : increase seconds by 5 
        if (seconds >= 60) seconds = 0 ;
        Send_ALIVE_message(seconds);
        led1 = !led1;
    }
}     

// ------------- LvdK button scan, test only -------------------------------------------------
void SCAN_KEYS_TASK(void) // : Test if button pressed
{     // Will be task written by W.B
      // Is now LvdK test Task, scans some buttons attached to Mbed board.
      // Task scans buttons every 50msec.
    CDU_OS.SetTimer( SCAN_KEYS_TIMER_ID, SCAN_KEYS_TIMER_PERIOD, SCAN_KEYS_TIMER_PERIOD );
  
    while(1){  // : loop forever because it is a mbos taskfunction
        CDU_OS.WaitEvent(SCAN_KEYS_TIMER_EVENT );   // : scan buttons every 50msec
        scan_buttons_Mbed(); 
    } 
}
// -------------------------------------------------------------------------------------------