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:
1:ca506b88b1d6
Parent:
0:6b1f6139fb25
Child:
2:d3d61d9d323e

File content as of revision 1:ca506b88b1d6:

#include "mbed.h"
#include "rtos.h"
#include "IsoTpHandler.h"

Serial pc(USBTX, USBRX); // tx, rx 
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);
    }
}

void serial_int_handler() {
    if (!pc.readable()) {
        return;
    }
    uint8_t character = pc.getc();
    printf("Received '%c'\n", character);
    
    char can_msg[8] = {0};
    can_msg[0] = 0x02;  
    can_msg[1] = 0x01;
    char pid = 0;
    switch (character)
    {
        case '1':
            pid = 0x0C; //engine rpm
            break;
        case '2':
            pid = 0x11; //throttle
            break;
        case '3': //oil 1
            can_msg[1] = 0x21; //endian
            pid = 1;
            break;
        case '4': //oil 2
            can_msg[1] = 1; //endian
            pid = 0x21;
            break;
        default:
            pid = 0x05; //engine coolant temp
    }
    can_msg[2] = pid;
    
    printf("Can write %d\n", can2.write(CANMessage(0x7DF, can_msg, 8)));
 }
 
int main() {
    pc.baud(115200);
    pc.attach(serial_int_handler);
    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);
    }
}