2011-08-02 00:10:15 +00:00
|
|
|
/*
|
|
|
|
|
2011-12-22 13:33:48 +00:00
|
|
|
original flame m0dul - https://github.com/kiu/flame
|
2011-08-02 00:10:15 +00:00
|
|
|
|
2011-12-22 13:33:48 +00:00
|
|
|
flame m0dul with RGB (rev c) - https://github.com/schneider42/flame
|
2011-08-02 00:10:15 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "basic/basic.h"
|
|
|
|
#include "basic/config.h"
|
2011-12-22 13:33:48 +00:00
|
|
|
#include "basic/xxtea.h"
|
|
|
|
#include "flame/flame.h"
|
2011-08-02 00:10:15 +00:00
|
|
|
|
2011-12-22 13:33:48 +00:00
|
|
|
#include "filesystem/util.h"
|
|
|
|
|
|
|
|
#define nick GLOBAL(nickname)
|
|
|
|
|
|
|
|
static uint8_t flamesEnabled = 0;
|
|
|
|
static uint8_t flameMode = FLAME_STATE_OFF;
|
|
|
|
static uint8_t flameBrightness = 0;
|
|
|
|
static uint8_t flameTicks = 0;
|
|
|
|
static uint8_t flamesOwned = 0;
|
|
|
|
|
|
|
|
static uint8_t rgbData[24];
|
|
|
|
static uint8_t rgbDataSize = 0;
|
|
|
|
static uint8_t rgbDataOffset = 0;
|
2011-08-02 00:10:15 +00:00
|
|
|
|
2011-12-22 13:33:48 +00:00
|
|
|
static void changeColor() {
|
|
|
|
if (rgbDataSize > 2) {
|
|
|
|
flameSetColor(flamesOwned, rgbData[rgbDataOffset],
|
|
|
|
rgbData[rgbDataOffset + 1], rgbData[rgbDataOffset + 2]);
|
|
|
|
if (rgbDataSize >= rgbDataOffset + 5) {
|
|
|
|
rgbDataOffset += 3;
|
|
|
|
} else {
|
|
|
|
rgbDataOffset = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// generate a hash from the nickname
|
|
|
|
uint32_t hash[4];
|
|
|
|
uint32_t const key[4] = {0, 0, 0, 0};
|
|
|
|
xxtea_cbcmac(hash, (uint32_t *)nick, 4, key);
|
|
|
|
|
|
|
|
flameSetColor(flamesOwned, hash[0] & 0xFF, hash[1] & 0xFF, hash[2] & 0xFF);
|
|
|
|
}
|
2011-08-02 00:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tick_flame(void) { // every 10ms
|
2011-08-02 08:51:34 +00:00
|
|
|
static char night=0;
|
2011-12-22 13:33:48 +00:00
|
|
|
flamesOwned = flamesEnabled & ~flamesClaimed;
|
2011-08-02 08:51:34 +00:00
|
|
|
|
2011-12-22 13:33:48 +00:00
|
|
|
if (!flamesOwned) {
|
2011-08-02 08:51:34 +00:00
|
|
|
return;
|
2011-08-02 22:35:02 +00:00
|
|
|
}
|
2011-08-02 08:51:34 +00:00
|
|
|
|
2011-08-02 22:35:02 +00:00
|
|
|
if (night != isNight()) {
|
|
|
|
night = isNight();
|
|
|
|
if (!night) {
|
2011-12-22 13:33:48 +00:00
|
|
|
flameMode = FLAME_STATE_OFF;
|
|
|
|
flameBrightness = 0;
|
|
|
|
flameSetBrightness(flamesOwned, flameBrightness);
|
2011-08-02 08:51:34 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2011-08-02 00:10:15 +00:00
|
|
|
flameTicks++;
|
|
|
|
|
2011-12-22 13:33:48 +00:00
|
|
|
if (flameBrightness > flameBrightnessMax) {
|
|
|
|
flameBrightness = flameBrightnessMax;
|
2011-08-02 00:10:15 +00:00
|
|
|
}
|
2011-12-22 13:33:48 +00:00
|
|
|
if (flameBrightness < flameBrightnessMin) {
|
|
|
|
flameBrightness = flameBrightnessMin;
|
2011-08-02 00:10:15 +00:00
|
|
|
}
|
|
|
|
|
2011-12-22 13:33:48 +00:00
|
|
|
if (flameMode == FLAME_STATE_OFF) {
|
2011-08-02 22:35:02 +00:00
|
|
|
if (isNight()) {
|
2011-08-02 00:10:15 +00:00
|
|
|
flameTicks = 0;
|
2011-12-22 13:33:48 +00:00
|
|
|
flameMode = FLAME_STATE_UP;
|
|
|
|
changeColor(flamesOwned, flameBrightness);
|
2011-08-02 00:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-22 13:33:48 +00:00
|
|
|
if (flameMode == FLAME_STATE_UP) {
|
|
|
|
if (0xFF - flameBrightness >= flameSpeedUp ) {
|
|
|
|
flameBrightness += flameSpeedUp;
|
|
|
|
} else {
|
|
|
|
flameBrightness = 0xFF;
|
2011-08-02 00:10:15 +00:00
|
|
|
}
|
2011-12-22 13:33:48 +00:00
|
|
|
flameSetBrightness(flamesOwned, flameBrightness);
|
|
|
|
if (flameBrightness >= flameBrightnessMax) {
|
|
|
|
flameMode = FLAME_STATE_UP_WAIT;
|
2011-08-02 00:10:15 +00:00
|
|
|
flameTicks = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-22 13:33:48 +00:00
|
|
|
if (flameMode == FLAME_STATE_UP_WAIT) {
|
2011-08-02 00:10:15 +00:00
|
|
|
if (flameTicks >= flameWaitUp) {
|
2011-12-22 13:33:48 +00:00
|
|
|
flameMode = FLAME_STATE_DOWN;
|
2011-08-02 00:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-22 13:33:48 +00:00
|
|
|
if (flameMode == FLAME_STATE_DOWN) {
|
|
|
|
if (flameSpeedDown <= flameBrightness) {
|
|
|
|
flameBrightness -= flameSpeedDown;
|
2011-08-02 00:10:15 +00:00
|
|
|
} else {
|
2011-12-22 13:33:48 +00:00
|
|
|
flameBrightness = 0x00;
|
2011-08-02 00:10:15 +00:00
|
|
|
}
|
2011-12-22 13:33:48 +00:00
|
|
|
flameSetBrightness(flamesOwned, flameBrightness);
|
|
|
|
if (flameBrightness <= flameBrightnessMin) {
|
|
|
|
flameMode = FLAME_STATE_DOWN_WAIT;
|
2011-08-02 00:10:15 +00:00
|
|
|
flameTicks = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-22 13:33:48 +00:00
|
|
|
if (flameMode == FLAME_STATE_DOWN_WAIT) {
|
2011-08-02 00:10:15 +00:00
|
|
|
if (flameTicks >= flameWaitDown) {
|
2011-12-22 13:33:48 +00:00
|
|
|
flameMode = FLAME_STATE_OFF;
|
2011-08-02 00:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-02 22:42:44 +00:00
|
|
|
void init_flame(void) {
|
2011-12-22 13:33:48 +00:00
|
|
|
flamesEnabled = flameDetect();
|
2011-08-02 00:10:15 +00:00
|
|
|
|
2011-12-22 13:33:48 +00:00
|
|
|
if (!flamesEnabled)
|
2011-08-04 12:33:39 +00:00
|
|
|
return;
|
|
|
|
|
2011-12-22 13:33:48 +00:00
|
|
|
flameInit(flamesEnabled);
|
2011-12-25 13:12:30 +00:00
|
|
|
|
|
|
|
int s = readFile("FLAME.RGB", (char *)rgbData, 24);
|
|
|
|
if( s > 0 )
|
|
|
|
rgbDataSize = s;
|
|
|
|
|
2011-08-04 12:33:39 +00:00
|
|
|
enableConfig(CFG_TYPE_FLAME,1);
|
2011-08-02 00:10:15 +00:00
|
|
|
}
|
2011-08-02 08:51:34 +00:00
|
|
|
|