MongooseESPWemosOled64x48/src/main.cpp

195 lines
6.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (c) 2014-2017 Cesanta Software Limited
* All rights reserved
*/
#define USE_I2C true
#define USE_SPI false
#define SSD1306_64_48
#include <stdio.h>
#include "common/platform.h"
#include "common/cs_file.h"
#include "mgos_app.h"
#include "mgos_gpio.h"
#include "mgos_sys_config.h"
#include "mgos_timers.h"
#include "mgos_hal.h"
#include "mgos_dlsym.h"
#include "mjs.h"
#include <Arduino.h>
#include "Adafruit_SSD1306dj.h"
#include "mgos_rpc.h"
#include "common/cs_dbg.h"
#include "common/json_utils.h"
#include "frozen/frozen.h"
#include "mgos_net.h"
Adafruit_SSD1306 *d1 = nullptr, *d2 = nullptr;
static void timer_cb(void *arg);
#define FREMO_LOGO_WIDTH 64
#define FREMO_LOGO_HEIGHT 48
const PROGMEM uint8_t fremo_logo[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x5A, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00,
0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x06, 0x68, 0x99, 0x20, 0x00, 0x00,
0x00, 0x80, 0x40, 0x4D, 0x5B, 0x86, 0x01, 0x00, 0x00, 0x20, 0x5A, 0x09,
0x88, 0x36, 0x04, 0x00, 0x00, 0x00, 0x35, 0x50, 0x03, 0x94, 0x10, 0x00,
0x00, 0x44, 0x82, 0x02, 0x40, 0x40, 0x21, 0x00, 0x00, 0xC1, 0x10, 0x00,
0x00, 0x0C, 0x83, 0x00, 0x80, 0x00, 0x04, 0x00, 0x00, 0x20, 0x00, 0x01,
0x40, 0x00, 0x01, 0xF8, 0x3F, 0x80, 0x00, 0x02, 0x20, 0x40, 0x00, 0xFC,
0x3F, 0x00, 0x02, 0x04, 0x00, 0x20, 0x00, 0xFC, 0x3F, 0x00, 0x04, 0x00,
0x00, 0x10, 0x00, 0xFC, 0x3F, 0x00, 0x08, 0x00, 0x08, 0x08, 0x00, 0xFC,
0x3F, 0x00, 0x10, 0x10, 0x04, 0x00, 0x00, 0xFE, 0x7F, 0x00, 0x00, 0x20,
0x00, 0x02, 0x00, 0xFE, 0x7F, 0x00, 0x40, 0x00, 0x02, 0x01, 0x00, 0xFC,
0x3F, 0x00, 0x80, 0x40, 0x00, 0x00, 0x00, 0xF0, 0x07, 0x00, 0x00, 0x00,
0x80, 0x00, 0x00, 0xC0, 0x03, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x80, 0x01, 0xF8, 0xFD, 0xB8, 0x33, 0x0C, 0x0F, 0x80,
0x40, 0x10, 0x11, 0x11, 0x62, 0xC4, 0x18, 0x00, 0x01, 0x50, 0x11, 0x91,
0x50, 0x4E, 0x10, 0x02, 0x40, 0x70, 0xF0, 0xF0, 0x60, 0x44, 0x10, 0x82,
0x01, 0x50, 0x90, 0x91, 0x82, 0x45, 0x10, 0x00, 0x40, 0x10, 0x10, 0x11,
0x92, 0xC5, 0x18, 0x82, 0x01, 0x38, 0x38, 0xF9, 0xB1, 0x0E, 0x07, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x80,
0x03, 0x00, 0x00, 0x01, 0x02, 0x00, 0x00, 0xC0, 0x01, 0x00, 0x00, 0x40,
0x22, 0x01, 0x00, 0x80, 0x03, 0x00, 0x80, 0x04, 0x30, 0x02, 0x00, 0xC0,
0x03, 0x00, 0x40, 0x26, 0x54, 0x00, 0x00, 0xE0, 0x0F, 0x00, 0x00, 0x24,
0x08, 0x09, 0x00, 0xFC, 0x3F, 0x00, 0x90, 0x13, 0xC0, 0x01, 0xC0, 0xFF,
0xFF, 0x07, 0x80, 0x02, 0x10, 0x23, 0xF0, 0xFF, 0xFF, 0x0F, 0xC4, 0x09,
0x20, 0x4E, 0xB0, 0xBF, 0xFF, 0x07, 0x62, 0x04, 0x40, 0x10, 0x01, 0x00,
0x00, 0x80, 0x10, 0x02, 0x80, 0x14, 0x04, 0x00, 0x00, 0x20, 0x14, 0x01,
0x00, 0xB1, 0x20, 0x00, 0x00, 0x88, 0x8D, 0x00, 0x00, 0xC4, 0x82, 0x12,
0xA0, 0xA0, 0x23, 0x00, 0x00, 0x08, 0x27, 0x40, 0x02, 0x08, 0x11, 0x00,
0x00, 0x20, 0x9C, 0x06, 0xD0, 0x5A, 0x04, 0x00, 0x00, 0x80, 0xA0, 0x75,
0x2F, 0x87, 0x01, 0x00, 0x00, 0x00, 0x04, 0x16, 0xF7, 0x20, 0x00, 0x00,
0x00, 0x00, 0x70, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x99,
0x2A, 0x00, 0x00, 0x00, };
static void printTextPattern(Adafruit_SSD1306 *d) {
d->setTextSize(1);
d->setTextColor(WHITE);
d->setCursor(0, 18);
d->printf("%s", "abcdefghijk");
d->setCursor(0, 27);
d->printf("%s", "@+#$%[]|{}");
d->setCursor(0, 36);
d->printf("%s", "ABCDEFGHIJK");
// d->setCursor(0, 48);
// d->printf("%s", "01234567890");
// d->display();
}
static void show_num(Adafruit_SSD1306 *d, const char *s, int i) {
d->clearDisplay();
d->setTextSize(1);
d->setTextColor(WHITE);
// d->setCursor(0, 0);
// d->printf("%s", " ");
d->setCursor(0, 0);
d->printf("%s%d", s, i);
printTextPattern(d1);
// draw frame
d->writeFastVLine(d->width()-1, 0, d->height(), WHITE);
d->writeFastHLine(0, d->height()-1, d->width(), WHITE);
d->display();
}
static void show_msg(Adafruit_SSD1306 *d, const char *s) {
d->setTextSize(1);
d->setTextColor(WHITE);
// d->setCursor(0, 9);
// d->printf("%s", " ");
d->setCursor(0, 9);
d->printf("%s", s);
d->display();
}
static void print_message_handler(struct mg_rpc_request_info *ri, void *cb_arg,
struct mg_rpc_frame_info *fi, struct mg_str args) {
struct mbuf fb;
struct json_out out = JSON_OUT_MBUF(&fb);
mbuf_init(&fb, 20);
const char *msg = "";
if (json_scanf(args.p, args.len, ri->args_fmt, &msg) == 1) {
json_printf(&out, "{result: %Q, msg: %Q}", "OK", msg);
} else {
json_printf(&out, "{error: %Q}", "num is required");
}
// mgos_gpio_toggle(LED_GPIO);
if (d1 != nullptr) show_msg(d1, msg);
printf("msg = %s\n", msg);
mg_rpc_send_responsef(ri, "%.*s", fb.len, fb.buf);
ri = NULL;
mbuf_free(&fb);
(void) cb_arg;
(void) fi;
}
void setup(void) {
// I2C
d1 = new Adafruit_SSD1306(4 /* RST GPIO */, Adafruit_SSD1306::RES_64_48);
if (d1 != nullptr) {
d1->begin(SSD1306_SWITCHCAPVCC, 0x3c, true /* reset */);
d1->display();
}
// set codepage correctly / apply bug fix
d1->cp437(true);
d1->setTextWrap(false);
struct mg_rpc *c = mgos_rpc_get_global();
mg_rpc_add_handler(c, "Display.Message", "{msg: %s}", print_message_handler, NULL);
mgos_set_timer(1000 /* ms */, true /* repeat */, timer_cb, NULL);
}
static void timer_cb(void *arg) {
static int i = 0, j = 0;
static int startupStep = 0;
switch (startupStep) {
case 0: startupStep++; break;
case 1:
d1->clearDisplay();
d1->drawBitmap(0, 0, fremo_logo, FREMO_LOGO_WIDTH, FREMO_LOGO_HEIGHT, BLACK, WHITE);
d1->display();
startupStep++;
break;
case 2: startupStep++; break;
case 3: startupStep ++; break;
case 4:
d1->clearDisplay();
show_msg(d1, "*Init´ed*");
startupStep++;
break;
default:
show_num(d1, "i = ", i);
LOG(LL_INFO, ("i = %d, j = %d", i, j));
if (i % 10 == 0) {
switch (i/10) {
case 1: show_msg(d1, "-> 1st dec"); break;
case 2: show_msg(d1, "-> 2nd dec"); break;
case 3: show_msg(d1, "-> 3rd dec"); break;
case 4: show_msg(d1, "-> 4th dec"); break;
default: show_msg(d1, "-> more ..");
}
}
i++;
j++;
}
(void) arg;
}