#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>
#if CONFIG_FREERTOS_UNICORE
#define ARDUINO_RUNNING_CORE 0
#else
#define ARDUINO_RUNNING_CORE 1
#endif
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define BTN_PIN 5
#define LED_RED 1
#define LED_BLUE 4
#define LED_GREEN 0
// 'icons8-bitcoin-24', 24x24px
const unsigned char bitcoinIcon [] PROGMEM = {
0x00, 0x7e, 0x00, 0x03, 0xff, 0xc0, 0x07, 0x81, 0xe0, 0x0e, 0x00, 0x70, 0x18, 0x28, 0x18, 0x30,
0x28, 0x0c, 0x70, 0xfc, 0x0e, 0x60, 0xfe, 0x06, 0x60, 0xc7, 0x06, 0xc0, 0xc3, 0x03, 0xc0, 0xc7,
0x03, 0xc0, 0xfe, 0x03, 0xc0, 0xff, 0x03, 0xc0, 0xc3, 0x83, 0xc0, 0xc1, 0x83, 0x60, 0xc3, 0x86,
0x60, 0xff, 0x06, 0x70, 0xfe, 0x0e, 0x30, 0x28, 0x0c, 0x18, 0x28, 0x18, 0x0e, 0x00, 0x70, 0x07,
0x81, 0xe0, 0x03, 0xff, 0xc0, 0x00, 0x7e, 0x00
};
//创建任务函数
void Task1(void *pvParameters);
void Task2(void *pvParameters);
void setup() {
Serial.begin(115200);
pinMode(BTN_PIN, INPUT_PULLUP);
pinMode(LED_RED, OUTPUT);
pinMode(LED_BLUE, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.drawBitmap((128/2) - (24/2), 0, bitcoinIcon, 24, 24, WHITE);
display.display();
Serial.print("START...... ");
//任务名称,任务栈大小,任务参数指针,任务优先级大小[值越大优先级越大],任务句柄指针,处理器核心编号
xTaskCreatePinnedToCore(Task1, "Task1",1024,NULL,2,NULL,ARDUINO_RUNNING_CORE);
xTaskCreatePinnedToCore(Task2, "Task2",1024,NULL,1,NULL,ARDUINO_RUNNING_CORE);
}
void loop() {
Serial.println("Getting current data...");
printCenter("$ 389887.88", 0, 32);
display.display();
if (digitalRead(BTN_PIN) == LOW) {
Serial.println("GPIO_BTN is LOW");
}
delay(250);
}
void Task1(void *pvParameters) { // 任务1
for (;;) {
//
Serial.println("task1");
digitalWrite(LED_RED, HIGH);
vTaskDelay(pdMS_TO_TICKS(1000));
digitalWrite(LED_RED, LOW);
vTaskDelay(pdMS_TO_TICKS(1000));
/*
if (digitalRead(BTN_PIN) == LOW) {
vTaskDelete(NULL); // 删除当前任务
}
*/
}
}
void Task2(void *pvParameters) { // 任务2
for (;;) {
Serial.println("task2");
digitalWrite(LED_BLUE, HIGH);
vTaskDelay(pdMS_TO_TICKS(2000));
digitalWrite(LED_BLUE, LOW);
vTaskDelay(pdMS_TO_TICKS(2000));
}
}
void printCenter(const String buf, int x, int y)
{
int16_t x1, y1;
uint16_t w, h;
display.getTextBounds(buf, x, y, &x1, &y1, &w, &h); //calc width of new string
display.setCursor((x - w / 2) + (128 / 2), y);
display.print(buf);
}