Small project to display some OBD values from the Toyota GT86/ Subaru BRZ/ Scion FRS on an OLED display.

Dependencies:   Adafruit_GFX MODSERIAL mbed-rtos mbed

main.cpp

Committer:
chrta
Date:
2014-04-22
Revision:
0:6b1f6139fb25
Child:
1:ca506b88b1d6

File content as of revision 0:6b1f6139fb25:

#include "mbed.h"
#include "rtos.h"
#include "IsoTpHandler.h"
 
DigitalOut led1(LED1);
DigitalOut led2(LED2);
CAN can2(p30, p29);
IsoTpHandler tpHandler(&can2);
 
void led2_thread(void const *args) {
    while (true) {
        led2 = !led2;
        Thread::wait(1000);
    }
}

Mail<CANMessage, 16> can_rx_queue;

void can_process_packets(void const *args) {
    while (true) {
        osEvent evt = can_rx_queue.get(osWaitForever);
        if (evt.status == osEventMail) {
            CANMessage *msg = (CANMessage*) evt.value.p;
            tpHandler.processCanMessage(msg);
            can_rx_queue.free(msg);
        }
    }
}

 
void can_rx_int_handler() {
    CANMessage* msg = can_rx_queue.alloc();
    if (!can2.read(*msg))
    {
        //this should not happen, because this function is called from the rx interrupt
        can_rx_queue.free(msg);
        return;
    }
    
    osStatus error_code = can_rx_queue.put(msg); 
    if (error_code != osOK) {
        error("Putting can message into mailbox failed with code %d!", error);
    }
}
 
int main() {
    can2.frequency(500000);
    can2.attach(can_rx_int_handler);
    Thread thread(led2_thread);
    Thread can_thread(can_process_packets);
    
    while (true) {
        led1 = !led1;
        Thread::wait(500);
    }
}