summaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 75c4d8bc5116a69eec65813fc6ee7ae8339c4983 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <Arduino.h>

#include <NimBLEDevice.h>

#include "badge/config.h"
#include "badge/display.h"
#include "badge/gpio.h"

#if defined(BADGE_SSID_NAME) && defined(BADGE_SSID_PASS)
#include <WiFi.h>
#define BADGE_USE_WIFI 1
#else
#define BADGE_USE_WIFI 0
#endif

NimBLEServer *server;

#define PRESS_TIME_DRAW 1000
#define PRESS_TIME_BLE_ADV 3000
#define SLEEP_TIME 10000
NimBLEAdvertising *pAdvertising;

void setup()
{
    Serial.begin(115200);
    Serial.println("Yes, it works!");

    NimBLEDevice::init("Espadge");
    server = NimBLEDevice::createServer();
    NimBLEService *service = new NimBLEService("ca260000-b4bb-46b2-bd06-b7b7a61ea990");
    auto c = service->createCharacteristic("ca260001-b4bb-46b2-bd06-b7b7a61ea990");
    service->start();
    c->setValue("1");

    server->addService(service);

    pAdvertising = NimBLEDevice::getAdvertising();
    pAdvertising->setName("NimBLE");
    de::uvok::badge::display_init();

#if BADGE_USE_WIFI
    WiFi.begin(BADGE_SSID_NAME, BADGE_SSID_PASS);
    while (WiFi.status() != WL_CONNECTED)
        delay(500);
    Serial.printf("> My IP is %s\n", WiFi.localIP().toString());
#endif

    // doesn't work as expected?
    // gpio_wakeup_enable(GPIO_NUM_0, GPIO_INTR_LOW_LEVEL);
    // esp_sleep_enable_gpio_wakeup();
    // esp_light_sleep_start();

    de::uvok::badge::gpio_init();
}

void loop()
{
    uint16_t pressDuration = de::uvok::badge::gpio_poll();
    if (pressDuration < 64)
    {
        if (pressDuration == NEXT_KEY) de::uvok::badge::display_next();
        if (pressDuration == PRV_KEY) de::uvok::badge::display_prev();
        if (pressDuration == OK_KEY) de::uvok::badge::display_refresh();
    }
    else if (pressDuration > PRESS_TIME_BLE_ADV)
    {
        if (!pAdvertising->isAdvertising())
        {
            Serial.println("Long press detected. Starting advertising...");
            pAdvertising->start(10000);
        }
    }
    else if (pressDuration > PRESS_TIME_DRAW)
    {
        Serial.println("Drawing...");
        de::uvok::badge::display_demo();
    }

    // if (millis() - releasedTime > SLEEP_TIME && !pAdvertising->isAdvertising())
    // {
    //   Serial.println("Go to sleep...");
    //   esp_light_sleep_start();
    // }
}