2021年01月09日
下記にソースコードを示します。エディタにコピペして、適当な名前を付けて、拡張子を”ino"にして保存してください。その後、Arduino IDEで開けば、コンパイル通るはずです。なお、実際に設置した水温計のソースでは、起動ロゴと水温計マークを別ファイルにしてたのを、一つのソースに修正したので、100%動く保証はできないんですが、たぶん大丈夫かと。
次回は、コンパイルの仕方説明します。
↓この下からソースです。
!2/28追記!
ようやくワーニングの問題が解決しました。原因は、CANの送信ピンとして、12番ピンを使用していましたが、そちらの初期状態の設定がLow出力になっていたために、CANの出力ピンとして初期化されるまでの間に、CANバスにドミナントの波形を出力していたことが原因の様です。(まっとうなオシロがないので、自宅でドミナント波形を確認したわけではないですが、様々な試行錯誤の結果としてそう判断しました。)
結果として、CANの送信ピンを15番に変更することで、ワーニングを発生させず使用することが可能になりました。
#include // Only needed for Arduino 1.6.5 and earlier
#include "SSD1306Wire.h" // legacy include: `#include "SSD1306.h"`
#include
#include
CAN_device_t CAN_cfg;
unsigned long prev_time;
byte loop_counter;
int last_rpm;
//unsigned byte test;
// Initialize the OLED display using Wire library
SSD1306Wire display(0x3c, 5, 4);
#define Freed_width 124
#define Freed_height 64
const unsigned char Image2 [] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,
0x00,0xfe,0x07,0xfe,0x03,0xc0,0xff,0x03,0xff,0x0f,0xfc,0x0f,
0x00,0x00,0x00,0xf0,0x00,0xff,0x07,0xff,0x07,0xe0,0xff,0x83,
0xff,0x0f,0xfe,0x1f,0x00,0x00,0x00,0xf0,0x00,0xff,0x03,0xff,
0x0f,0xe0,0xff,0x81,0xff,0x07,0x1e,0x1c,0x00,0x00,0x00,0xf0,
0x80,0xff,0x81,0x07,0x0e,0xf0,0x00,0xc0,0x03,0x00,0x0f,0x1c,
0x00,0x00,0x00,0xf0,0x80,0x07,0x80,0x07,0x06,0xf0,0x00,0xc0,
0x03,0x00,0x0f,0x1c,0x00,0x00,0x00,0xf0,0xc0,0x07,0xc0,0x03,
0x07,0x78,0x00,0xe0,0x01,0x80,0x07,0x1c,0x00,0x00,0x00,0xf0,
0xc0,0x03,0xc0,0x83,0x03,0x78,0x00,0xe0,0x01,0x80,0x07,0x1c,
0x00,0x00,0x00,0xf0,0xe0,0x03,0xe0,0x81,0x03,0xfc,0x3f,0xf0,
0xff,0xc0,0x03,0x1e,0x00,0x00,0x00,0xf0,0xe0,0x3f,0xe0,0xff,
0x01,0xfc,0x1f,0xf0,0x7f,0xc0,0x03,0x1e,0x00,0x00,0x00,0xf0,
0xf0,0x3f,0xf0,0xfd,0x00,0xfe,0x1f,0xf8,0x7f,0xe0,0x01,0x0f,
0x00,0x00,0x00,0xf0,0xf0,0x1f,0xf0,0x3c,0x00,0x1e,0x00,0x78,
0x00,0xe0,0x01,0x0f,0x00,0x00,0x00,0xf0,0xf8,0x00,0x78,0x78,
0x00,0x0f,0x00,0x3c,0x00,0xf0,0x80,0x07,0x00,0x00,0x00,0xf0,
0x78,0x00,0x78,0x78,0x00,0x0f,0x00,0x3c,0x00,0xf0,0x80,0x07,
0x00,0x00,0x00,0xf0,0x7c,0x00,0x3c,0xf0,0x80,0xff,0x0f,0xfe,
0x3f,0x78,0xc0,0x03,0x00,0x00,0x00,0xf0,0x3c,0x00,0x3c,0xf0,
0x80,0xff,0x0f,0xfe,0x3f,0xf8,0xff,0x01,0x00,0x00,0x00,0xf0,
0x3e,0x00,0x1e,0xe0,0xc1,0xff,0x07,0xff,0x1f,0xfc,0xff,0x00,
0x00,0x00,0x00,0xf0,0x1e,0x00,0x1e,0xe0,0xc1,0xff,0x07,0xff,
0x1f,0xfc,0x7f,0x00,0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf0,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xf0};
#define WT_Logo_width 24
#define WT_Logo_height 24
const unsigned char WT_logo [] = {
0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x18,0x00,0x00,0x18,0x00,
0x00,0xf8,0x01,0x00,0x18,0x00,0x00,0x18,0x00,0x00,0x18,0x00,
0x00,0xf8,0x01,0x00,0x18,0x00,0x00,0x18,0x00,0x00,0x18,0x00,
0x00,0xf8,0x01,0x00,0x18,0x00,0x00,0x18,0x00,0x00,0x24,0x00,
0x32,0x42,0x4c,0xcc,0x42,0x33,0x00,0x24,0x00,0x00,0x18,0x00,
0x98,0xc3,0x19,0x60,0x3c,0x06,0x00,0x00,0x00,0x00,0x00,0x00};
void setup() {
int i;
display.init();
display.setContrast(255);
Serial.begin(115200);
Serial.println("iotsharing.com CAN demo");
CAN_cfg.speed=CAN_SPEED_500KBPS;
CAN_cfg.tx_pin_id = GPIO_NUM_15;
CAN_cfg.rx_pin_id = GPIO_NUM_14;
CAN_cfg.rx_queue = xQueueCreate(10,sizeof(CAN_frame_t));
//start CAN Module
ESP32Can.CANInit();
Serial.print("APB_CLK_FREQ");
Serial.println(APB_CLK_FREQ);
prev_time = millis();
display.flipScreenVertically();
display.setLogBuffer(5, 30);
display.clear();
display.drawXbm(18, 0, Freed_width, Freed_height, Image2);
display.drawLogBuffer(0, 0);
display.display();
delay(2000);
DisplayOut(0,0);
}
void loop() {
static int current_rpm;
static int current_temp;
CAN_frame_t rx_frame;
//receive next CAN frame from queue
if(xQueueReceive(CAN_cfg.rx_queue,&rx_frame, 3*portTICK_PERIOD_MS)==pdTRUE){
if ((rx_frame.MsgID & 0xFFFFFF00) == 0x18daf100) {
if ((rx_frame.data.u8[2])== 0x05) {
current_temp = (int)((rx_frame.data.u8[3]) - 40);
DisplayOut(current_temp,current_rpm);
}else if ((rx_frame.data.u8[2])== 0x0c) {
current_rpm=(int)(((int)rx_frame.data.u8[3])*256 + (int)rx_frame.data.u8[4])/4;
DisplayOut(current_temp,current_rpm);
}
}
}
if ((prev_time-millis())> 50) {
prev_time=millis();
if (loop_counter == ((loop_counter/4)*4)) { // if counter value is even
rx_frame.FIR.B.FF = CAN_frame_ext;
rx_frame.MsgID = 0x18db33f1 ;
rx_frame.FIR.B.DLC = 3;
rx_frame.data.u8[0] = 0x02;
rx_frame.data.u8[1] = 0x01;
rx_frame.data.u8[2] = 0x0C; // Request engine speed
ESP32Can.CANWriteFrame(&rx_frame);
} else if (loop_counter == 19) { // 1sec expired
rx_frame.FIR.B.FF = CAN_frame_ext;
rx_frame.MsgID = 0x18db33f1 ;
rx_frame.FIR.B.DLC = 3;
rx_frame.data.u8[0] = 0x02;
rx_frame.data.u8[1] = 0x01;
rx_frame.data.u8[2] = 0x05; // Request coolant temperature
ESP32Can.CANWriteFrame(&rx_frame);
}
if (loop_counter >= 19) {
loop_counter = 0;
} else {
loop_counter++;
}
}
}
void DisplayOut(int temp,int rpm) {
char Str[3];
display.setLogBuffer(5, 30);
display.clear();
display.drawXbm(0, 3, WT_Logo_width, WT_Logo_height, WT_logo);
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_24);
sprintf(Str, "%d", temp);
display.drawString(45, 1, Str);
display.setFont(ArialMT_Plain_10);
display.drawString(0, 28, "Engine rpm (x1000)");
display.drawString(0, 39, "0");
display.drawString(18, 39, "1");
display.drawString(38, 39, "2");
display.drawString(58, 39, "3");
display.drawString(78, 39, "4");
display.drawString(98, 39, "5");
display.drawString(118, 39, "6");
display.setPixel(10,49);
display.setPixel(30,49);
display.setPixel(50,49);
display.setPixel(70,49);
display.setPixel(90,49);
display.setPixel(110,49);
display.fillRect(0,50,rpm/50,14);
// Draw it to the internal screen buffer
display.drawLogBuffer(0, 0);
// Display it on the screen
display.display();
}
Posted at 2021/01/09 15:18:03 | |
トラックバック(0) | 日記