Skip to content

Instantly share code, notes, and snippets.

@shimarin
Created October 28, 2018 11:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shimarin/62453452d3a611da97e8f916fc41d780 to your computer and use it in GitHub Desktop.
Save shimarin/62453452d3a611da97e8f916fc41d780 to your computer and use it in GitHub Desktop.
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <Adafruit_GFX.h> // Core graphics library by Adafruit
#include <Arduino_ST7789.h> //https://github.com/ananevilya/Arduino-ST7789-Library
#include "edogawa_unit.h"
#include "background.h" // 背景イメージ
/*
const uint16_t background0[] PROGMEM = { RGB565 240x60 pixels };
const uint16_t background1[] PROGMEM = { RGB565 240x60 pixels };
*/
// ST7789 TFTディスプレイ
#define TFT_DC 49
#define TFT_RST 48
Arduino_ST7789 tft = Arduino_ST7789(TFT_DC, TFT_RST); // 51 = MOSI = SDA, 52 =SCL = SCK, 53 = CS = SS
// BME280 環境センサ
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme; // I2C
const uint8_t NUM_EDOGAWA_UNIT = 5;
EdogawaUnit edg[NUM_EDOGAWA_UNIT];
uint8_t swPin[NUM_EDOGAWA_UNIT] = { 2, 4, 6, 8, 10 };
uint8_t ledPin[NUM_EDOGAWA_UNIT] = { 3, 5, 7, 9, 11 };
unsigned long lastEdogawaUnitOperation = 0;
const uint16_t EDOGAWA_UNIT_OPERATION_INTERVAL_SECS = 300;
void setup(void) {
Serial.begin(115200);
tft.init(240, 240); // initialize a ST7789 chip, 240x240 pixels
Serial.println("TFT Initialized");
if (!bme.begin()) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
// else
Serial.println("Temperature sensor Initialized");
// 背景描画
tft.fillScreen(WHITE); // 全体を白でフィル
tft.drawRGBBitmap(0, 120, background0, 240, 60); // 下半分に 240x120のビットマップを描画
tft.drawRGBBitmap(0, 180, background1, 240, 60);
tft.setTextWrap(false);
for (int i = 0; i < NUM_EDOGAWA_UNIT; i++) {
edg[i].begin(swPin[i], ledPin[i]);
}
lastEdogawaUnitOperation = millis();
}
void loop() {
// 温度表示
tft.setCursor(10, 20);
tft.setTextColor(RED, WHITE);
tft.setTextSize(7);
char buf[6];
float temperature = bme.readTemperature();
dtostrf(temperature, 4, 1, buf);
if (strlen(buf) < 4) tft.print(" ");
tft.print(buf);
// 上付き丸印がフォントにないので図形描画で表現
int16_t x = tft.getCursorX();
int16_t y = tft.getCursorY();
for (int i = 4; i <= 6; i++) {
tft.drawCircle(x + 8, y + 5, i, RED);
}
tft.setCursor(x + 20, y);
tft.println("C");
// 湿度表示
tft.setCursor(64, 85);
tft.setTextSize(4);
tft.setTextColor(BLUE, WHITE);
dtostrf(bme.readHumidity(), 4, 1, buf);
if (strlen(buf) < 4) tft.print(" ");
tft.print(buf);
tft.println("%");
// 気圧表示
tft.setCursor(20, 140);
tft.setTextSize(3);
tft.setTextColor(GREEN, WHITE);
sprintf(buf, "%d", (int)(bme.readPressure() / 100.0F));
if (strlen(buf) < 4) tft.print(" ");
tft.println(buf);
tft.println(" hPa");
// 高度は画面に出さなくても良い
Serial.print("Approx. Altitude = ");
Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
Serial.println(" m");
// 江戸川装置
for (int i = 0; i < NUM_EDOGAWA_UNIT; i++) {
tft.drawRect(10 * i, 0, 9, 9, RED);
tft.fillRect(10 * i + 1, 1, 7, 7, edg[i].is_power_on()? RED : WHITE);
}
unsigned long currentTime = millis();
if (lastEdogawaUnitOperation + EDOGAWA_UNIT_OPERATION_INTERVAL_SECS * 1000L < currentTime) {
if (temperature < 22.0) {
for (int i = 0; i < NUM_EDOGAWA_UNIT; i++) {
if (edg[i].is_power_on()) continue;
// else
Serial.print("Power on through edogawa unit #");
Serial.println(i);
if (edg[i].power_on()) {
lastEdogawaUnitOperation = currentTime;
break;
}
}
lastEdogawaUnitOperation = millis();
} else if (temperature > 25.0) {
for (int i = NUM_EDOGAWA_UNIT - 1; i >= 0; i--) {
if (edg[i].is_power_on()) {
Serial.print("Power off through edogawa unit #");
Serial.println(i);
if (edg[i].power_off()) lastEdogawaUnitOperation = currentTime;
break;
}
}
}
}
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment