forgot to commit everything...

This commit is contained in:
2018-02-03 16:55:37 +01:00
parent 20d46af1fb
commit 3da4722748
15 changed files with 991 additions and 0 deletions

21
user/ADC.h Executable file
View File

@@ -0,0 +1,21 @@
#ifndef ADC_H
#define ADC_H
class ADC {
public:
static uint16_t getA0() {
return 0xffff & system_adc_read();
}
static uint16_t getVcc() {
static constexpr float SCALER = 1.93; // divider of 10k / 10k -> * 2
static constexpr int V_REF = 330; // 3.3 volt
static constexpr int MUL = SCALER * V_REF;
return getA0() * MUL / 1024;
}
};
#endif // ADC_H

320
user/Buzzer.h Executable file
View File

@@ -0,0 +1,320 @@
#ifndef BUZZER_H
#define BUZZER_H
#include "ESP8266lib/net/UDP.h"
#include "ESP8266lib/net/IP.h"
#include "ESP8266lib/net/MAC.h"
#include "ESP8266lib/net/WiFiRaw.h"
#include "ESP8266lib/io/IO.h"
#include "Fader.h"
#include "FadeOnce.h"
#include "Rainbow.h"
#include "RainbowBeat.h"
#define FIRMWARE_NR 5
#define CODE(a, b, c, d) (a << 24 | b << 16 | c << 8 | d)
enum LEDMode {
OFF,
FIXED_COLOR,
RAINBOW_COLOR,
FADE_BETWEEN_COLOR,
FADE_ONCE,
STROBO_COLOR,
RAINBOW_BEAT,
};
class Buzzer;
extern Buzzer buzzer;
class Buzzer {
private:
static constexpr const char* NAME = "Buzzer";
UDP udp;
IP server = IP();
WS2812B<1> leds;
LEDMode ledMode;
WiFiRaw::MACAddress myMac;
FadeBetween fadeBetween;
FadeOnce fadeOnce;
Rainbow rainbow;
RainbowBeat rainbowBeat;
public:
/** ctor */
Buzzer() {
debugMod(NAME, "ctor()");
udp.bind(localPort);
udp.setRecvCallback(&Buzzer::onUDP);
ledMode = LEDMode::FIXED_COLOR;
myMac = WiFiRaw::getMyMAC();
// buzzer interrupt (Pin D2)
GPIO4_INPUT_SET;
GPIO4_INPUT_PULLUP_SET;
// interrupt
//ETS_GPIO_INTR_DISABLE();
//ETS_GPIO_INTR_ATTACH(isrRoutine, this);
//gpio_pin_intr_state_set(GPIO_ID_PIN(4), GPIO_PIN_INTR_ANYEDGE);
//ETS_GPIO_INTR_ENABLE();
}
/** send a heartbeat packet */
void sendHeartbeat() {
const uint16_t vcc = ADC::getVcc();
os_printf("send: heartbeat, Vcc: %d\n", vcc);
char data[5+12+2+3];
os_memcpy(&data[0], "*PING", 5);
os_memcpy(&data[5], myMac.asPtr(), 12);
data[17] = '_';
data[18] = FIRMWARE_NR;
data[19] = '_';
data[20] = (vcc >> 8) & 0xFF;
data[21] = (vcc >> 0) & 0xFF;
udp.send(remoteIP, remotePort, data, sizeof(data));
}
/** send a buzzer packet */
void sendBuzzer() {
char data[5+12];
os_memcpy(&data[0], "*BUZZ", 5);
os_memcpy(&data[5], myMac.asPtr(), 12);
udp.send(remoteIP, remotePort, data, 5+12);
os_printf("send: buzzer\n");
}
/** incoming UDP data */
void onUDP(const char* data, uint16_t len) {
if (len < 2) {return;}
if (data[0] == '*') {
debugMod(NAME, "got command");
checkCode(&data[1]);
}
}
void update() {
static int cnt = 0; ++cnt;
// every 9 seconds
if (cnt % 9000 == 0) {
buzzer.sendHeartbeat();
}
// every 20 milliseconds
if (cnt % 20 == 0) {
updateLED();
}
// block the buzzer for some time after buzzering
static int block = 0;
if (block > 0) {
--block;
if (block == 0) {onUnBuzzer();}
}
// check buzzer-state
if (cnt % 20 == 0) {
// buzzer currently blocked
if (block > 0) {return;}
static uint8_t lastVal = 1;
const uint8_t curVal = GPIO4_IN;
if (curVal < lastVal) {
block = 1000;
onBuzzer();
}
lastVal = curVal;
}
}
// buzzer button pressed
void onBuzzer() {
sendBuzzer();
internalLED(true);
//setOff();
}
void onUnBuzzer() {
internalLED(false);
//os_printf("free\n");
//setRainbow();
//setFade(255,0,0, 0,0,255);
//setFade(180,0,16, 16,190,16);
}
/** wemos D1 mini only */
void internalLED(bool on) {
GPIO2_OUTPUT_SET;
if (on) { // LED is inverted
GPIO2_L;
} else {
GPIO2_H;
}
}
private:
/** check the command-code behind the given char-string */
void checkCode(const char* buf) {
// convert the buffer's first 4 bytes to a uint32_t for faster comparison
const uint32_t code = CODE(buf[0], buf[1], buf[2], buf[3]);
switch(code) {
case CODE('S', 'T', 'R', 'O'): setStrobo(buf[4], buf[5], buf[6]); break;
case CODE('O', 'F', 'F', ' '): setOff(); break;
case CODE('S', 'R', 'G', 'B'): setRGB(buf[4], buf[5], buf[6]); break;
case CODE('R', 'A', 'I', 'N'): setRainbow(); break;
case CODE('F', 'A', 'D', 'E'): setFade(buf[4], buf[5], buf[6], buf[7], buf[8], buf[9]); break;
case CODE('F', 'L', 'S', 'H'): setFlash(buf[4], buf[5], buf[6], buf[7], buf[8], buf[9], buf[10]); break;
case CODE('R', 'A', 'I', 'R'): setRainbowBeatRestart(); break;
case CODE('R', 'A', 'I', 'B'): setRainbowBeatBeat(buf[4]); break;
case CODE('R', 'A', 'I', 'S'): setRainbowBeatShift(buf[4]); break;
default: break;
}
}
/** upadte the current LED output (for animations) */
void updateLED() {
// fading LED color?
if (ledMode == LEDMode::RAINBOW_COLOR) {
rainbow.update();
leds.setColor(0, rainbow.getCurrent());
leds.flush();
} else if (ledMode == LEDMode::STROBO_COLOR) {
leds.setEnabled(0, !leds.isEnabled(0));
leds.flush();
} else if (ledMode == LEDMode::FADE_BETWEEN_COLOR) {
fadeBetween.update();
leds.setColor(0, fadeBetween.getCurrent());
leds.flush();
} else if (ledMode == LEDMode::FADE_ONCE) {
fadeOnce.update();
leds.setColor(0, fadeOnce.getCurrent());
leds.flush();
} else if (ledMode == LEDMode::RAINBOW_BEAT) {
rainbowBeat.update();
leds.setColor(0, rainbowBeat.getCurrent());
leds.flush();
}
}
public:
/** disable LED */
void setOff() {
ledMode = LEDMode::OFF;
leds.getColor(0).setRGB(0,0,255); // for testing
leds.setEnabled(0, false);
leds.flush();
}
/** set a fixed RGB color */
void setRGB(const uint8_t r, const uint8_t g, const uint8_t b) {
debugMod(NAME, "setting LEDS to fixed RGB color");
ledMode = LEDMode::FIXED_COLOR;
leds.getColor(0).setRGB(r,g,b);
leds.setEnabled(0, true);
leds.flush();
}
/** set strobo mode */
void setStrobo(const uint8_t r, const uint8_t g, const uint8_t b) {
ledMode = LEDMode::STROBO_COLOR;
leds.getColor(0).setRGB(r,g,b);
leds.setEnabled(0, true);
leds.flush();
}
/** set LED rainbow fading */
void setRainbow() {
rainbow.restart();
ledMode = LEDMode::RAINBOW_COLOR;
leds.setEnabled(0, true);
leds.flush();
}
/** set LED fading between two colors */
void setFade(const uint8_t r1, const uint8_t g1, const uint8_t b1, const uint8_t r2, const uint8_t g2, const uint8_t b2) {
fadeBetween.setColor1(Color::fromRGB(r1, g1, b1));
fadeBetween.setColor2(Color::fromRGB(r2, g2, b2));
fadeBetween.restart();
ledMode = LEDMode::FADE_BETWEEN_COLOR;
leds.setEnabled(0, true);
}
/** shortly flash the led and fade back to black */
void setFlash(const uint8_t r1, const uint8_t g1, const uint8_t b1, const uint8_t r2, const uint8_t g2, const uint8_t b2, const int ms) {
fadeOnce.setColor(Color::fromRGB(r1,g1,b1), Color::fromRGB(r2,g2,b2));
fadeOnce.setFadeDuration(ms);
ledMode = LEDMode::FADE_ONCE;
leds.setEnabled(0, true);
}
/** reset the rainbow-beats HUE to 0 */
void setRainbowBeatRestart() {
rainbowBeat.restart();
ledMode = LEDMode::RAINBOW_BEAT;
leds.setEnabled(0, true);
}
/** show a beat */
void setRainbowBeatBeat(const int ms) {
rainbowBeat.flash(ms);
ledMode = LEDMode::RAINBOW_BEAT;
leds.setEnabled(0, true);
}
/** show a beat */
void setRainbowBeatShift(const int increment) {
rainbowBeat.shift(increment);
ledMode = LEDMode::RAINBOW_BEAT;
leds.setEnabled(0, true);
}
public:
/** udp callback */
static void onUDP(void*, char* data, unsigned short len) {
debugMod(NAME, "got udp data");
buzzer.onUDP(data, len);
}
};
#endif // BUZZER_H

16
user/ESP.h Executable file
View File

@@ -0,0 +1,16 @@
#ifndef ESP_H
#define ESP_H
class ESP {
public:
static inline uint32_t getCycleCount() {
uint32_t ccount;
__asm__ __volatile__("esync; rsr %0,ccount":"=a" (ccount));
return ccount;
}
};
#endif // ESP_H

44
user/FadeOnce.h Executable file
View File

@@ -0,0 +1,44 @@
#ifndef FLASHONCE_H
#define FLASHONCE_H
#include "ESP8266lib/ext/led/WS2812B.h"
/** fade between two colors */
class FadeOnce {
Color c1;
Color c2;
int fadeOut_ticks = 0;
int ticksLeft = 0;
public:
/** ctor */
FadeOnce() {
;
}
void setColor(Color c1, Color c2) {
this->c1 = c1;
this->c2 = c2;
}
void setFadeDuration(int duration_ticks) {
this->fadeOut_ticks = duration_ticks;
this->ticksLeft = duration_ticks;
}
void update() {
if (ticksLeft > 0) {--ticksLeft;} // fade-out
}
Color getCurrent() const {
const int percent = (ticksLeft * 100 / fadeOut_ticks);
return Color::mix(c1, c2, percent);
}
};
#endif // FLASHONCE_H

68
user/Fader.h Executable file
View File

@@ -0,0 +1,68 @@
#ifndef FADER_H
#define FADER_H
#include "ESP8266lib/ext/led/WS2812B.h"
/** fade between two colors */
class FadeBetween {
Color c1;
Color c2;
int pos;
int speed;
int dir;
public:
/** ctor */
FadeBetween() {
restart();
}
void setColor1(Color c1) {
this->c1 = c1;
}
void setColor2(Color c2) {
this->c2 = c2;
}
void restart() {
pos = 0;
speed = 128;
dir = 0;
}
void update() {
if (dir == 0) {
++pos;
if (pos == speed) {dir = 1;}
} else {
--pos;
if (pos == 0) {dir = 0;}
}
}
Color getCurrent() const {
const int fac0 = pos;
const int fac1 = speed - fac0;
Color c;
c.r = clamp( (c1.r * fac0 + c2.r * fac1) / speed );
c.g = clamp( (c1.g * fac0 + c2.g * fac1) / speed );
c.b = clamp( (c1.b * fac0 + c2.b * fac1) / speed );
return c;
}
private:
static inline uint8_t clamp(int val) {
if (val > 255) {return 255;}
if (val < 0) {return 0;}
return val;
}
};
#endif // FADER_H

45
user/Rainbow.h Normal file
View File

@@ -0,0 +1,45 @@
#ifndef RAINBOW_H
#define RAINBOW_H
#include "ESP8266lib/ext/led/WS2812B.h"
/**
* show rainbow colors using HUE
*/
class Rainbow {
uint8_t hue = 0;
public:
/** ctor */
Rainbow() {
;
}
/** show a beat by shifting the hue color */
void shift(const int val) {
hue += val;
}
/** set hue back to 0 */
void restart() {
hue = 0;
}
/** called from the main */
void update() {
++hue;
}
/** called from the main */
Color getCurrent() const {
Color c; c.setHSV(hue, 255, 255);
return c;
}
};
#endif // RAINBOW_H

72
user/RainbowBeat.h Executable file
View File

@@ -0,0 +1,72 @@
#ifndef RAINBOWBEAT_H
#define RAINBOWBEAT_H
#include "ESP8266lib/ext/led/WS2812B.h"
/**
* show rainbow colors using HUE
* use only 25% percent brightness
* when there is a beat, switch to 100% brightness
* and fade back to 25% again
*/
class RainbowBeat {
int p1 = 255;
int p2 = 63;
int fadeOut_ticks = 0;
int ticksLeft = 0;
uint8_t hue = 0;
public:
/** ctor */
RainbowBeat() {
;
}
/** configure the amounts for idle and beat */
void setAmounts(int p1, int p2) {
this->p1 = p1;
this->p2 = p2;
}
/** show a bight beat for x ticks */
void flash(const int duration_ticks) {
this->fadeOut_ticks = duration_ticks;
this->ticksLeft = duration_ticks;
}
/** show a beat by shifting the hue color */
void shift(const int val) {
hue += val;
}
/** shift to the given absolute hue */
void shiftTo(const int valAbs) {
hue = valAbs;
}
/** set hue back to 0 */
void restart() {
this->hue = 0;
}
/** called from the main */
void update() {
if (ticksLeft > 0) {--ticksLeft;} // fade-out
hue += 1;
}
/** called from the main */
Color getCurrent() const {
const int percent = (ticksLeft * 100 / fadeOut_ticks);
const int val = ((p1 * percent) + (p2 * (100-percent))) / 100;
Color c; c.setHSV(hue, 255, val);
return c;
}
};
#endif // RAINBOWBEAT_H

132
user/run_Buzzer.h Executable file
View File

@@ -0,0 +1,132 @@
#include "ESP8266lib/io/IO.h"
#include "ESP8266lib/ext/led/WS2812B.h"
const uint16_t localPort = 1337;
//const char* remoteIP = "192.168.22.255"; // OGWLAN
//const char* remoteIP = "192.168.24.255"; // UGWLAN
const char* remoteIP = "255.255.255.255"; // BUZZER
const uint16_t remotePort = 7331;
//char ssid[32] = "OGWLAN";
//char password[64] = "LeckereKekse!";
//char ssid[32] = "UGWLAN";
//char password[64] = "LeckereKekse!";
char ssid[32] = "Buzzer";
char password[64] = "LeckereKekse!";
#include "ADC.h"
#include "Buzzer.h"
Buzzer buzzer;
bool connected;
void onWifiEvent(System_Event_t* evt) {
os_printf("event %x\n", evt->event);
switch (evt->event) {
case EVENT_STAMODE_CONNECTED:
os_printf("connect to ssid %s, channel %d\n", evt->event_info.connected.ssid, evt->event_info.connected.channel);
buzzer.setRGB(0,0,255); // blue
break;
case EVENT_STAMODE_GOT_IP:
os_printf("got IP\n");
buzzer.setOff();
connected = true;
buzzer.sendHeartbeat();
break;
case EVENT_STAMODE_DISCONNECTED:
os_printf("disconnect from ssid %s, reason %d\n", evt->event_info.disconnected.ssid, evt->event_info.disconnected.reason);
buzzer.setRGB(255,0,0);
break;
}
}
void my_init() {
connected = false;
wifi_station_disconnect();
os_delay_us(1000*250);
os_printf("my_init\r\n");
// stty -F /dev/ttyUSB0 115200 raw -echo && cat /dev/ttyUSB0
// yellow
buzzer.setRGB(255,255,0);
// register the event handler
wifi_set_event_handler_cb(onWifiEvent);
// i am a client
wifi_set_opmode(STATION_MODE);
wifi_station_disconnect();
os_delay_us(1000*500);
// connect
struct station_config stationConf;
os_memset(&stationConf, 0, sizeof(stationConf));
os_memcpy(&stationConf.ssid, ssid, 32);
os_memcpy(&stationConf.password, password, 32);
wifi_station_set_config(&stationConf);
wifi_station_connect();
// power safe
//NONE_SLEEP_T
//LIGHT_SLEEP_T
//MODEM_SLEEP_T
//wifi_set_opmode_current(NULL_MODE);
//wifi_set_sleep_type(LIGHT_SLEEP_T);
//wifi_set_sleep_type(MODEM_SLEEP_T);
// green
buzzer.setRGB(0,255,0);
os_delay_us(1000*150);
}
void my_once() {
//buzzer.setFade();
//buzzer.setStrobo(255,255,255);
}
void my_loop() {
// 1 ms delay
os_delay_us(1000);
if (connected) {
buzzer.update();
}
}

1
user/user_config.h Executable file
View File

@@ -0,0 +1 @@
#

83
user/user_main.cpp Executable file
View File

@@ -0,0 +1,83 @@
extern "C" {
#include "ets_sys.h"
#include "c_types.h"
#include "osapi.h"
//#include "gpio.h"
//#include "os_type.h"
//#include "user_config.h"
#include "user_interface.h"
//#include "wpa2_enterprise.h"
//#include "inttypes.h"
#include "mem.h"
#include "espconn.h"
#include "ESP8266lib/c++.h"
//#include "driver/uart.h"
}
#define user_procTaskQueueLen 1
#define WEMOS_D1_MINI 1
#define NODE_MCU 2
#define PLATFORM WEMOS_D1_MINI
#define LED_PIN GPIO5
//#define PLATFORM NODE_MCU
#include "run_Buzzer.h"
static os_event_t user_procTaskQueue[user_procTaskQueueLen];
extern "C" void ICACHE_FLASH_ATTR user_init();
bool once = true;
//Main code function
void ICACHE_FLASH_ATTR user_loop(os_event_t*) {
// custome once-code
if (once) {
my_once();
once = false;
}
// custome loop code
my_loop();
// run again
system_os_post(USER_TASK_PRIO_0, 0, 0 );
}
void ICACHE_FLASH_ATTR user_init() {
// basic hardware setup
os_delay_us(1000*50);
gpio_init();
uart_div_modify(0, UART_CLK_FREQ / 115200);
os_delay_us(1000*50);
os_printf("init\r\n");
// ensure global constructors are called
do_global_ctors();
// custom one-time init code
my_init();
// start the loop-task
system_os_task(user_loop, USER_TASK_PRIO_0, user_procTaskQueue, user_procTaskQueueLen);
system_os_post(USER_TASK_PRIO_0, 0, 0 );
}