Skip to content

Instantly share code, notes, and snippets.

@ya-ma-cho
Created February 13, 2019 04:52
Show Gist options
  • Save ya-ma-cho/05d35efc7e3d97e87983a5bb3fdfce98 to your computer and use it in GitHub Desktop.
Save ya-ma-cho/05d35efc7e3d97e87983a5bb3fdfce98 to your computer and use it in GitHub Desktop.
#include <M5Stack.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
WiFiClientSecure client;
//WiFi設定
const char* ssid = "******";
const char* password = "*****";
//Slack設定
const String slackToken = "*****-*****-*****-*****-**********";//事前に取得した 「OAuth Access Token」 を指定
//Myチャンネル設定
const String myChannel = "*****";//メッセージを投稿するチャンネルを指定
const String myMemberId = "*****";//事前に取得した自分のメンバーIDを指定
//ポモドーロ時間設定(集中)
const int activeMm =25;
const int activeSs = 0;
//ポモドーロ時間設定(休憩)
const int breakMm = 5;
const int breakSs = 0;
int targetTime = 0;
int omm = 99;
int oss = 99;
int mm = activeMm;
int ss = activeSs;
bool hasStop = true;
bool hasEnd = false;
bool active = true;
void setup() {
Serial.begin(115200);
M5.begin();
M5.Lcd.fillScreen(TFT_BLACK);
M5.Lcd.setCursor(10, 10);
M5.Lcd.setTextColor(TFT_WHITE, TFT_BLACK);
M5.Lcd.setTextSize(3);
// Start WiFi
Serial.println("Connecting to ");
Serial.print(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// WiFi Connected
Serial.println("\nWiFi Connected.");
M5.Lcd.setCursor(10, 40);
M5.Lcd.setTextColor(WHITE);
M5.Lcd.setTextSize(3);
M5.Lcd.printf("WiFi Connected.");
delay(3000);
M5.Lcd.fillScreen(TFT_BLACK);
M5.Lcd.setTextFont(7);
M5.Lcd.setTextSize(2);
targetTime = millis() + 1000;
resetActive();
Serial.println("START");
}
void loop() {
if (M5.BtnA.wasPressed()) {
resetActive();
hasStop = true;
}
if (M5.BtnB.wasPressed()) {
resetBreak();
hasStop = true;
}
if (M5.BtnC.wasPressed() && !hasStop) {
hasStop = true;
} else if (M5.BtnC.wasPressed() && hasStop) {
hasStop = false;
}
if (targetTime < millis()) {
targetTime = millis() + 1000;
// 時間停止してない場合
if (!hasStop) {
ss--;
if (ss == -1) {
// 分秒ともに0なら時間停止&タイマー終了
if (mm == 0) {
ss = 0;
hasStop = true;
hasEnd = true;
} else {
ss = 59;
omm = mm;
mm--;
}
}
}
//タイマー終了した場合
if (hasEnd) {
hasEnd = false;
hasStop = false;
if (active) {
resetBreak();
} else {
resetActive();
}
}
// 分の更新
if (omm != mm) {
omm = mm;
drawMinutes(mm);
}
// 秒の更新
if (oss != ss) {
oss = ss;
drawIndivator(ss, hasStop);
drawSeconds(ss);
}
M5.update();
}
}
// 分の表示
void drawMinutes(int mm) {
M5.Lcd.setTextColor(0x1903);
M5.Lcd.setCursor( 15, 80);
M5.Lcd.print("88");
M5.Lcd.setTextColor(TFT_ORANGE);
if (mm < 10) {
M5.Lcd.setCursor( 15, 80);
M5.Lcd.print("0");
M5.Lcd.print(mm);
} else {
M5.Lcd.setCursor( 15, 80);
M5.Lcd.print(mm);
}
}
// 秒の表示
void drawSeconds(int ss) {
M5.Lcd.setTextColor(0x1903);
M5.Lcd.setCursor( 180, 80);
M5.Lcd.print("88");
M5.Lcd.setTextColor(TFT_ORANGE);
if (ss < 10) {
M5.Lcd.setCursor( 180, 80);
M5.Lcd.print("0");
M5.Lcd.print(ss);
} else {
M5.Lcd.setCursor( 180, 80);
M5.Lcd.print(ss);
}
}
// インジケーターの表示
void drawIndivator(int ss, bool hasStop) {
if (ss % 2 && !hasStop) {
M5.Lcd.setCursor( 150, 80);
M5.Lcd.setTextColor(0x1903);
M5.Lcd.print(":");
M5.Lcd.setTextColor(TFT_ORANGE);
} else {
M5.Lcd.setCursor( 150, 80);
M5.Lcd.print(":");
}
}
//集中に切り替え
void resetActive() {
M5.Lcd.clear();
M5.Lcd.drawJpgFile(SD, "/active_wait.jpg");
sendMessage("<@" + myMemberId + "> は集中しています");
setStatus("集中しています", "u7981");
setPresence("away");
setSnooze(30);
M5.Lcd.drawJpgFile(SD, "/active.jpg");
active = true;
mm = activeMm;
ss = activeSs;
drawSeconds(ss);
drawMinutes(mm);
M5.update();
}
//休憩に切り替え
void resetBreak() {
M5.Lcd.clear();
M5.Lcd.drawJpgFile(SD, "/break_wait.jpg");
setStatus("休憩しています", "u7a7a");
setPresence("auto");
endSnooze();
sendMessage("<@" + myMemberId + "> は休憩しています");
M5.Lcd.drawJpgFile(SD, "/break.jpg");
active = false;
mm = breakMm;
ss = breakSs;
drawSeconds(ss);
drawMinutes(mm);
M5.update();
}
//Slackの通知状態を通知に設定
void endSnooze() {
String slackApi = "dnd.endSnooze";
slackSend(slackApi, "");
}
//Slackの通知状態を非通知に設定
void setSnooze(int num_minutes) {
String slackApi = "dnd.setSnooze";
String slackParam = "num_minutes=" + String(num_minutes);
slackSend(slackApi, slackParam);
}
//Slackのアクティブ・離席の状態を設定
void setPresence(String presence) {
String slackApi = "users.setPresence";
String slackParam = "presence=" + presence;
slackSend(slackApi, slackParam);
}
//Slackのステータスアイコンとステータスメッセージを設定
void setStatus(String status_text, String status_emoji) {
String slackApi = "users.profile.set";
String slackParam = "profile={\"status_text\":\"" + status_text + "\",\"status_emoji\":\":" + status_emoji + ":\"}";
slackSend(slackApi, slackParam);
}
//Slackのメッセージ送信
void sendMessage(String text) {
String slackApi = "chat.postMessage";
String slackParam = "channel=" + myChannel + "&text=" + text + "&icon_emoji=:spaghetti:";
slackSend(slackApi, slackParam);
}
void slackSend(String slackApi, String slackParam) {
if (client.connect("slack.com", 443)) {
client.println("POST /api/" + slackApi + " HTTP/1.1");
client.println("Host: slack.com");
client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Authorization: Bearer " + slackToken);
client.println("Connection: close");
client.print("Content-Length: ");
client.println(slackParam.length());
client.println();
client.println(slackParam);
delay(1000);
client.stop();
Serial.println("OK");
} else {
Serial.println("Err");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment