Code:
#include <FlexCAN_T4.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ADC.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define NUMFLAKES 10 // Number of snowflakes in the animation example
unsigned long curr, prev, next, interval;
int TW =0;
#define LOGO_HEIGHT 16
#define LOGO_WIDTH 16
FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_16> can2;
static const unsigned char PROGMEM logo_bmp[] =
{ B00000000, B11000000,
B00000001, B11000000,
B00000001, B11000000,
B00000011, B11100000,
B11110011, B11100000,
B11111110, B11111000,
B01111110, B11111111,
B00110011, B10011111,
B00011111, B11111100,
B00001101, B01110000,
B00011011, B10100000,
B00111111, B11100000,
B00111111, B11110000,
B01111100, B11110000,
B01110000, B01110000,
B00000000, B00110000 };
int engineCoolantTemp = 0; // Save the data here
int engineRPM = 0;// Save the data here
void setup(){
while (!Serial && millis() < 5000);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
delay(2000);
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
Serial.begin(115200);
can2.begin();
can2.setBaudRate(500000);
can2.setMBFilter(REJECT_ALL);
// can2.setMBFilter(MB1, 0x7E8); // Standard CANID ENG ECU -> TESTER
can2.setMBFilter(MB7, 0x18DB33F1, EXT); // Extended CANID ENG ECU -> TESTER (Physical Addressing)
can2.setFIFOFilter(0, 0x18DAF111,EXT); // Extended CANID Target address F1(Tester), Sender 11(ENG ECU)
can2.setMaxMB(16);
can2.enableFIFO();
can2.enableFIFOInterrupt();
//can2.enableMBInterrupt(MB4);
can2.onReceive(canSniff);
}
const long loopDelay1 = 200; // Make a request every 500ms
unsigned long timeNow1 = 0;
void loop(){
can2.events();
if (millis() > timeNow1 + loopDelay1)
{
timeNow1 = millis();
canTx_OBD(); // Query data from CAN BUS via OBD
//canTx_UDS(); // Query data from CAN BUS via UDS
}
}
void canSniff (const CAN_message_t &msg){ // Global callback to catch any CAN frame coming in
if(msg.id == 0x18DAF111){
Serial.println(" ");
Serial.print("MB: ");
Serial.print(msg.mb);
Serial.print(" ID: 0x");
Serial.print(msg.id, HEX);
Serial.print(" EXT: ");
Serial.print(msg.flags.extended);
Serial.print(" LEN: ");
Serial.print(msg.len);
Serial.print(" DATA: ");
for (uint8_t i = 0; i < 8; i++)
{
Serial.print(msg.buf[i], HEX);
Serial.print(" ");
}
}
if (msg.buf[1] == 0x41 && msg.buf[2] == 0x0C){ // SID$01 PID$05 Engine Coolant Temperature, PID$0C Engine Speed
//engineCoolantTemp = msg.buf[3] - 40; // Offset -40(degC)
engineRPM = (msg.buf[3] * 0x100 + msg.buf[4])/4; // Engine Speed 2byte 1/4 bit per rpm
Serial.println(" ");
//Serial.printf("OBD: Engine Coolant Temp: %d c", engineCoolantTemp);
Serial.printf("OBD Engine Speed; %d rpm", engineRPM);
}
if(msg.id == 0x18DAF111){
display.setTextColor(WHITE);
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.println(F("CAN_ID:"));
display.setCursor(60,0);
display.setTextColor(WHITE);
display.print(msg.id, HEX);
for ( uint8_t i = 0; i < 8; i++ ) {
display.setCursor(15*i ,15);
display.print(msg.buf[i],HEX); Serial.print(" ");
}
}
display.setTextSize(2); // Normal 1:1 pixel scale
display.setCursor(10, 30);
//display.print(engineCoolantTemp,DEC);
display.print(engineRPM, DEC);
display.setCursor(70, 30);
//display.print("degC");
display.print("RPM");
display.display();
}
void canTx_OBD(){ // Request function to ask for Engine Coolant Temp via OBD request
CAN_message_t msgTx, msgRx;
msgTx.buf[0] = 0x02; // Data Length 2byte
msgTx.buf[1] = 0x01; // Service$01
msgTx.buf[2] = 0x0C; // PID $05 Engine Coolant Temperature
msgTx.buf[3] = 0x55; // Padding
msgTx.buf[4] = 0x55;
msgTx.buf[5] = 0x55;
msgTx.buf[6] = 0x55;
msgTx.buf[7] = 0x55;
msgTx.len = 8; // number of bytes in request
//msgTx.flags.extended = 0; // 11 bit header, not 29 bit
msgTx.flags.extended = 1; // 29 bit header, not 11 bit
msgTx.flags.remote = 0;
//msgTx.id = 0x7E0; // request header for OBD // Physical Addressing Request to Engine ECU
msgTx.id = 0x18DB33F1; // request header for OBD Functional Addressing request to all emission related ECUs.
can2.write(msgTx);
Serial.println("canTx_OBD REQ sent");
}
void testdrawchar(void) {
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.cp437(true); // Use full 256 char 'Code Page 437' font
// Not all the characters will fit on the display. This is normal.
// Library will draw what it can and the rest will be clipped.
for(int16_t i=0; i<256; i++) {
if(i == '\n') display.write(' ');
else display.write(i);
}
display.display();
delay(2000);
}