#include <Adafruit_GFX.h>
#include <Adafruit_SSD1351.h>
#include <SD.h>
#include <mcp_can.h>
#include <SPI.h>
//CAN config
unsigned int rxId;
unsigned char rxLen = 0;
unsigned char rxBuf[8];
volatile unsigned char canData = 0;
int led = 23;
int boostvalue_li = 0;
float afrvalue_f = 0;
MCP_CAN CAN0(17); // Set CS to pin 17
//OLED display config
// If we are using the hardware SPI interface, these are the pins (for future ref)
#define sclk 13
#define mosi 11
#define OLED_CS 5
#define rst 6
#define dc 4
// Color definitions
#define BLACK 0x0000
#define RED 0xF800
#define GREEN 0x07E0
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
// to draw images from the SD card, we will share the hardware SPI interface
Adafruit_SSD1351 tft = Adafruit_SSD1351(OLED_CS, dc, rst);
//chip select for sd card
#define SD_CS 10
File bmpFile;
int bmpWidth, bmpHeight;
uint8_t bmpDepth, bmpImageoffset;
void setup()
{
pinMode(led, OUTPUT);
Serial.begin(9600);
delay(5000);
int serialattempt = 0;
// while (!Serial && serialattempt<10) {
// delay(1000);
// serialattempt++; // wait 10 secs for serial port to connect. Needed for Leonardo only
// }
Serial.println("Serial port up!");
initOLED();
delay(1000);
drawGTRlogo();
//show logo for2 secs and then wipe screen
delay(2000);
tft.fillScreen(BLACK);
drawboostlabel(WHITE);
drawafrlabel(WHITE);
//initialise CAN bus
//keep retrying indefinitley
while(CAN_OK!=CAN0.begin(CAN_125KBPS))
{
//Serial.println("CAN init failed - retry..");
delay(100);
}
// *********************************************************
// Bytes 1 and 2 of mask/filter apply to CAN ID
CAN0.init_Mask(0, 0, 0xFFFC); // 0b1111 1111 1111 1100
CAN0.init_Filt(0, 0, 0x3E8); // 0b0000 0011 1110 1000 - canid1000 (16bit (motorola) rpm, 16bit (motorola)boostpresoffset -100,8bit water temp,8bit post IC temp, 8bit battery volts,8bit oil tmep
CAN0.init_Filt(1, 0, 0xFFFF); // 1111 1111 1111 1111 - FAIL
//CAN0.init_Filt(1, 0, 0x3EA); // 0b0000 0011 1110 1000 - canid1002 (16bit (motorola) lambda x1000)
CAN0.init_Mask(1, 0, 0xFFFF); // 1111 1111 1111 1111
CAN0.init_Filt(2, 0, 0x0001); // 0000 0000 0000 0001 - FAIL
CAN0.init_Filt(3, 0, 0x0001); // 0000 0000 0000 0001 - FAIL
CAN0.init_Filt(4, 0, 0x0001); // 0000 0000 0000 0001 - FAIL
CAN0.init_Filt(5, 0, 0x0001); // 0000 0000 0000 0001 - FAIL
}
void loop()
{
if(CAN_MSGAVAIL == CAN0.checkReceive()) // check if data waiting
{
//Serial.println("CAN");
CAN0.readMsgBuf(&rxLen, rxBuf); // Read data: len = data length, buf = data byte(s)
rxId = CAN0.getCanId(); // Get message ID
//if canid 1000 read byte numbers as follows
//0,1 rpm, 2,3 boost, 4 water temp, 5 IC temp, 6 bat volt,
if(rxId==1000)
{
//offset is -100 to give kPa
boostvalue_li=word(rxBuf[2],rxBuf[3]);
boostvalue_li=boostvalue_li-100;
drawboostvalue(boostvalue_li);
Serial.print(word(rxBuf[2],rxBuf[3]),HEX);
Serial.print(" kpa");
Serial.println(boostvalue_li);
}
//if canid 1002 read byte
//1,2 lambda
if(rxId==1002)
{
drawafrvalue(word(rxBuf[0],rxBuf[1]));
// Serial.print(" afr");
// Serial.println(afrvalue_i);
}
}
else
{
//drawboostvalue(0);
//drawafrvalue(147);
// Serial.println("noCAN");
}
}
void drawboostlabel(uint16_t color){
tft.setCursor(0, 5);
tft.setTextColor(color);
tft.setTextSize(2);
tft.println("kPa");
}
void drawafrlabel(uint16_t color){
tft.setCursor(0, 69);
tft.setTextColor(color);
tft.setTextSize(2);
tft.println("AFR");
}
void drawafrvalue(word afrvalue){
afrvalue_f=afrvalue; //div by 1000 to give lambda then *14.68 for AFR
afrvalue_f=(afrvalue_f/1000)*14.7;
tft.setCursor(0, 85);
tft.setTextColor(GREEN,BLACK);
if (afrvalue_f >13.0) {
tft.setTextColor(YELLOW,BLACK);
}
if (afrvalue_f >14.0) {
tft.setTextColor(RED,BLACK);
}
tft.setTextSize(4);
tft.println(afrvalue_f);
}
void drawboostvalue(int boostvalue){
tft.setCursor(0, 23);
tft.setTextColor(WHITE,BLACK);
if (boostvalue >80) {
tft.setTextColor(GREEN,BLACK);
}
if (boostvalue >160) {
tft.setTextColor(YELLOW,BLACK);
}
if (boostvalue >200) {
tft.setTextColor(RED,BLACK);
}
tft.setTextSize(4);
tft.println(boostvalue);
}
#define BUFFPIXEL 20
void bmpDraw(char *filename, uint8_t x, uint8_t y) {
File bmpFile;
int bmpWidth, bmpHeight; // W+H in pixels
uint8_t bmpDepth; // Bit depth (currently must be 24)
uint32_t bmpImageoffset; // Start of image data in file
uint32_t rowSize; // Not always = bmpWidth; may have padding
uint8_t sdbuffer[3*BUFFPIXEL]; // pixel buffer (R+G+B per pixel)
uint8_t buffidx = sizeof(sdbuffer); // Current position in sdbuffer
boolean goodBmp = false; // Set to true on valid header parse
boolean flip = true; // BMP is stored bottom-to-top
int w, h, row, col;
uint8_t r, g, b;
uint32_t pos = 0, startTime = millis();
if((x >= tft.width()) || (y >= tft.height())) return;
// Open requested file on SD card
if ((bmpFile = SD.open(filename)) == NULL) {
//Serial.print("File not found");
return;
}
// Parse BMP header
if(read16(bmpFile) == 0x4D42) { // BMP signature
Serial.print("File size: "); Serial.println(read32(bmpFile));
(void)read32(bmpFile); // Read & ignore creator bytes
bmpImageoffset = read32(bmpFile); // Start of image data
Serial.print("Image Offset: "); Serial.println(bmpImageoffset, DEC);
// Read DIB header
Serial.print("Header size: "); Serial.println(read32(bmpFile));
bmpWidth = read32(bmpFile);
bmpHeight = read32(bmpFile);
if(read16(bmpFile) == 1) { // # planes -- must be '1'
bmpDepth = read16(bmpFile); // bits per pixel
Serial.print("Bit Depth: "); Serial.println(bmpDepth);
if((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = uncompressed
goodBmp = true; // Supported BMP format -- proceed!
// BMP rows are padded (if needed) to 4-byte boundary
rowSize = (bmpWidth * 3 + 3) & ~3;
// If bmpHeight is negative, image is in top-down order.
// This is not canon but has been observed in the wild.
if(bmpHeight < 0) {
bmpHeight = -bmpHeight;
flip = false;
}
// Crop area to be loaded
w = bmpWidth;
h = bmpHeight;
if((x+w-1) >= tft.width()) w = tft.width() - x;
if((y+h-1) >= tft.height()) h = tft.height() - y;
for (row=0; row<h; row++) { // For each scanline...
tft.goTo(x, y+row);
// Seek to start of scan line. It might seem labor-
// intensive to be doing this on every line, but this
// method covers a lot of gritty details like cropping
// and scanline padding. Also, the seek only takes
// place if the file position actually needs to change
// (avoids a lot of cluster math in SD library).
if(flip) // Bitmap is stored bottom-to-top order (normal BMP)
pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
else // Bitmap is stored top-to-bottom
pos = bmpImageoffset + row * rowSize;
if(bmpFile.position() != pos) { // Need seek?
bmpFile.seek(pos);
buffidx = sizeof(sdbuffer); // Force buffer reload
}
// optimize by setting pins now
for (col=0; col<w; col++) { // For each pixel...
// Time to read more pixel data?
if (buffidx >= sizeof(sdbuffer)) { // Indeed
bmpFile.read(sdbuffer, sizeof(sdbuffer));
buffidx = 0; // Set index to beginning
}
// Convert pixel from BMP to TFT format, push to display
b = sdbuffer[buffidx++];
g = sdbuffer[buffidx++];
r = sdbuffer[buffidx++];
tft.drawPixel(x+col, y+row, tft.Color565(r,g,b));
// optimized!
//tft.pushColor(tft.Color565(r,g,b));
} // end pixel
} // end scanline
Serial.print("Loaded in ");
Serial.print(millis() - startTime);
Serial.println(" ms");
} // end goodBmp
}
}
bmpFile.close();
if(!goodBmp) Serial.println("BMP format not recognized.");
}
uint16_t read16(File f) {
uint16_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read(); // MSB
return result;
}
uint32_t read32(File f) {
uint32_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read();
((uint8_t *)&result)[2] = f.read();
((uint8_t *)&result)[3] = f.read(); // MSB
return result;
}
void initOLED()
{
// initialize the OLED
pinMode(OLED_CS, OUTPUT);
digitalWrite(OLED_CS, HIGH);
Serial.println("init OLED");
tft.begin();
tft.fillScreen(BLACK);
}
void drawGTRlogo()
{
// initialize the SD
//Serial.print("Initializing SD card...");
if (!SD.begin(SD_CS)) {
return;
}
bmpDraw("gtr.bmp", 0, 0);
}