From fda93e381f7f9f8857a93f646a5a26643b99e548 Mon Sep 17 00:00:00 2001 From: bsx Date: Thu, 22 Dec 2011 14:33:48 +0100 Subject: [PATCH] moved flame code into core - flames now hav an API - default flame code handles both types simultaneously - l0dables may claim a flame to manipulate as they wish --- firmware/Makefile | 2 + firmware/applications/final/flame.c | 193 +++++++++-------------- firmware/applications/final/flame_rgb.c | 196 ------------------------ firmware/flame/Makefile | 23 +++ firmware/flame/flame.c | 92 +++++++++++ firmware/flame/flame.h | 95 ++++++++++++ firmware/l0dable/EXPORTS | 7 + 7 files changed, 293 insertions(+), 315 deletions(-) delete mode 100644 firmware/applications/final/flame_rgb.c create mode 100644 firmware/flame/Makefile create mode 100644 firmware/flame/flame.c create mode 100644 firmware/flame/flame.h diff --git a/firmware/Makefile b/firmware/Makefile index 359e39b..bba9bfb 100644 --- a/firmware/Makefile +++ b/firmware/Makefile @@ -23,6 +23,7 @@ LIBS += filesystem/libfat.a LIBS += usb/libusb.a LIBS += funk/libfunk.a LIBS += usbcdc/libusbcdc.a +LIBS += flame/libflame.a ########################################################################## # GNU GCC compiler flags @@ -49,6 +50,7 @@ LDLIBS += -Lcore -lcore LDLIBS += -Lusb -lusb LDLIBS += -lbasic LDLIBS += -lfunk +LDLIBS += -Lflame -lflame OCFLAGS = --strip-unneeded SUBDIRS?= $(foreach lib,$(LIBS),$(dir $(lib))) diff --git a/firmware/applications/final/flame.c b/firmware/applications/final/flame.c index 6359137..0776f82 100644 --- a/firmware/applications/final/flame.c +++ b/firmware/applications/final/flame.c @@ -1,176 +1,131 @@ /* - flame m0dul - https://github.com/kiu/flame + original flame m0dul - https://github.com/kiu/flame + flame m0dul with RGB (rev c) - https://github.com/schneider42/flame */ #include "basic/basic.h" -#include "core/i2c/i2c.h" #include "basic/config.h" +#include "basic/xxtea.h" +#include "flame/flame.h" -#define FLAME_I2C_WRITE 0xC4 -#define FLAME_I2C_READ 0xC5 +#include "filesystem/util.h" -#define FLAME_I2C_CR_INPUT 0x00 -#define FLAME_I2C_CR_PSC0 0x01 -#define FLAME_I2C_CR_PWM0 0x02 -#define FLAME_I2C_CR_PSC1 0x03 -#define FLAME_I2C_CR_PWM1 0x04 -#define FLAME_I2C_CR_LS0 0x05 +#define nick GLOBAL(nickname) -#define FLAME_I2C_LS0_OFF 0x00 -#define FLAME_I2C_LS0_ON 0x01 -#define FLAME_I2C_LS0_PWM0 0x02 -#define FLAME_I2C_LS0_PWM1 0x03 +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; -#define FLAME_I2C_LS0_LED0 0x00 -#define FLAME_I2C_LS0_LED1 0x02 -#define FLAME_I2C_LS0_LED2 0x04 -#define FLAME_I2C_LS0_LED3 0x06 +static uint8_t rgbData[24]; +static uint8_t rgbDataSize = 0; +static uint8_t rgbDataOffset = 0; -#define FLAME_OFF 0x00 -#define FLAME_UP 0x01 -#define FLAME_UP_WAIT 0x02 -#define FLAME_DOWN 0x03 -#define FLAME_DOWN_WAIT 0x04 +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); -/**************************************************************************/ - -#define flameBrightnessMax GLOBAL(flamemax) -#define flameBrightnessMin GLOBAL(flamemin) -#define flameSpeedUp GLOBAL(flamespeed) -#define flameSpeedDown GLOBAL(flamespeed) -#define flameWaitUp GLOBAL(flamemaxw) -#define flameWaitDown GLOBAL(flameminw) - -uint8_t flameEnabled = 0; -uint8_t flameMode = FLAME_OFF; -uint8_t flameI2Cpwm = 0; -uint8_t flameTicks = 0; - -uint32_t flameSetI2C(uint8_t cr, uint8_t value) { - I2CMasterBuffer[0] = FLAME_I2C_WRITE; - I2CMasterBuffer[1] = cr; - I2CMasterBuffer[2] = value; - I2CWriteLength = 3; - I2CReadLength = 0; - return i2cEngine(); -} - -void setFlamePWM() { - flameSetI2C(FLAME_I2C_CR_PWM0, flameI2Cpwm); // set pwm + flameSetColor(flamesOwned, hash[0] & 0xFF, hash[1] & 0xFF, hash[2] & 0xFF); + } } void tick_flame(void) { // every 10ms static char night=0; + flamesOwned = flamesEnabled & ~flamesClaimed; - if (!flameEnabled) { + if (!flamesOwned) { return; } if (night != isNight()) { night = isNight(); if (!night) { - flameMode = FLAME_OFF; - flameI2Cpwm = 0; - push_queue(&setFlamePWM); + flameMode = FLAME_STATE_OFF; + flameBrightness = 0; + flameSetBrightness(flamesOwned, flameBrightness); }; }; flameTicks++; - if (flameI2Cpwm > flameBrightnessMax) { - flameI2Cpwm = flameBrightnessMax; + if (flameBrightness > flameBrightnessMax) { + flameBrightness = flameBrightnessMax; } - if (flameI2Cpwm < flameBrightnessMin) { - flameI2Cpwm = flameBrightnessMin; + if (flameBrightness < flameBrightnessMin) { + flameBrightness = flameBrightnessMin; } - if (flameMode == FLAME_OFF) { + if (flameMode == FLAME_STATE_OFF) { if (isNight()) { flameTicks = 0; - flameMode = FLAME_UP; + flameMode = FLAME_STATE_UP; + changeColor(flamesOwned, flameBrightness); } } - if (flameMode == FLAME_UP) { - if (0xFF - flameI2Cpwm >= flameSpeedUp ) { - flameI2Cpwm += flameSpeedUp; - } else { - flameI2Cpwm = 0xFF; - } - push_queue(&setFlamePWM); - if (flameI2Cpwm >= flameBrightnessMax) { - flameMode = FLAME_UP_WAIT; - flameTicks = 0; - } - } - - if (flameMode == FLAME_UP_WAIT) { - if (flameTicks >= flameWaitUp) { - flameMode = FLAME_DOWN; - } - } - - if (flameMode == FLAME_DOWN) { - if (flameSpeedDown <= flameI2Cpwm) { - flameI2Cpwm -= flameSpeedDown; + if (flameMode == FLAME_STATE_UP) { + if (0xFF - flameBrightness >= flameSpeedUp ) { + flameBrightness += flameSpeedUp; } else { - flameI2Cpwm = 0x00; + flameBrightness = 0xFF; } - push_queue(&setFlamePWM); - if (flameI2Cpwm <= flameBrightnessMin) { - flameMode = FLAME_DOWN_WAIT; + flameSetBrightness(flamesOwned, flameBrightness); + if (flameBrightness >= flameBrightnessMax) { + flameMode = FLAME_STATE_UP_WAIT; flameTicks = 0; } } - if (flameMode == FLAME_DOWN_WAIT) { + if (flameMode == FLAME_STATE_UP_WAIT) { + if (flameTicks >= flameWaitUp) { + flameMode = FLAME_STATE_DOWN; + } + } + + if (flameMode == FLAME_STATE_DOWN) { + if (flameSpeedDown <= flameBrightness) { + flameBrightness -= flameSpeedDown; + } else { + flameBrightness = 0x00; + } + flameSetBrightness(flamesOwned, flameBrightness); + if (flameBrightness <= flameBrightnessMin) { + flameMode = FLAME_STATE_DOWN_WAIT; + flameTicks = 0; + } + } + + if (flameMode == FLAME_STATE_DOWN_WAIT) { if (flameTicks >= flameWaitDown) { - flameMode = FLAME_OFF; + flameMode = FLAME_STATE_OFF; } } } void init_flame(void) { - i2cInit(I2CMASTER); // Init I2C + flamesEnabled = flameDetect(); - flameEnabled = (flameSetI2C(FLAME_I2C_CR_LS0, FLAME_I2C_LS0_OFF << FLAME_I2C_LS0_LED0) == I2CSTATE_ACK); // probe i2c - - if (!flameEnabled) + if (!flamesEnabled) return; - flameSetI2C(FLAME_I2C_CR_LS0, FLAME_I2C_LS0_OFF << FLAME_I2C_LS0_LED0); // set led0 off - flameSetI2C(FLAME_I2C_CR_LS0, FLAME_I2C_LS0_OFF << FLAME_I2C_LS0_LED1); // set led1 off - flameSetI2C(FLAME_I2C_CR_LS0, FLAME_I2C_LS0_OFF << FLAME_I2C_LS0_LED2); // set led2 off - flameSetI2C(FLAME_I2C_CR_LS0, FLAME_I2C_LS0_OFF << FLAME_I2C_LS0_LED3); // set led3 off + flameInit(flamesEnabled); - flameSetI2C(FLAME_I2C_CR_PSC0, 0x00); // set prescaler - flameSetI2C(FLAME_I2C_CR_PWM0, 0x00); // set pwm - flameSetI2C(FLAME_I2C_CR_LS0, FLAME_I2C_LS0_PWM0 << FLAME_I2C_LS0_LED0); // set led0 to pwm + rgbDataSize = readTextFile("FLAME.RGB", rgbData, 24); enableConfig(CFG_TYPE_FLAME,1); } -#include "lcd/print.h" - -// //# MENU flame -void ChkFlame(void) { - do{ - lcdClear(); - lcdPrint("Enabled:"); - lcdPrintln(IntToStr(flameEnabled,1,0)); - - lcdPrint("State:"); - lcdPrintln(IntToStr(flameMode,1,0)); - - lcdPrint("PWMtarg:"); - lcdPrintln(IntToStr(flameI2Cpwm,3,0)); - - lcdPrint("FTicks:"); - lcdPrintln(IntToStr(flameTicks,3,0)); - - lcdRefresh(); - delayms_queue(10); - } while ((getInputRaw())==BTN_NONE); -} diff --git a/firmware/applications/final/flame_rgb.c b/firmware/applications/final/flame_rgb.c deleted file mode 100644 index 3ea22e1..0000000 --- a/firmware/applications/final/flame_rgb.c +++ /dev/null @@ -1,196 +0,0 @@ -/* - - flame m0dul with RGB (rev c) - https://github.com/schneider42/flame - LED driver is a PCA9633 -*/ - -#include "basic/basic.h" -#include "core/i2c/i2c.h" -#include "basic/config.h" -#include "basic/xxtea.h" - -#include "filesystem/util.h" - -#define FLAME_I2C_WRITE 0xC6 -#define FLAME_I2C_READ 0xC7 - -#define FLAME_I2C_CR_MODE1 0x00 -#define FLAME_I2C_CR_MODE2 0x01 -#define FLAME_I2C_CR_PWM0 0x02 -#define FLAME_I2C_CR_PWM1 0x03 -#define FLAME_I2C_CR_PWM2 0x04 -#define FLAME_I2C_CR_PWM3 0x05 -#define FLAME_I2C_CR_GRPPWM 0x06 -#define FLAME_I2C_CR_GRPFREQ 0x07 -#define FLAME_I2C_CR_LEDOUT 0x08 - -#define FLAME_I2C_LED0_OFF 0x00 -#define FLAME_I2C_LED0_FULL 0x01 -#define FLAME_I2C_LED0_INDY 0x02 -#define FLAME_I2C_LED0_GRP 0x03 -#define FLAME_I2C_LED1_OFF 0x00 -#define FLAME_I2C_LED1_FULL 0x04 -#define FLAME_I2C_LED1_INDY 0x08 -#define FLAME_I2C_LED1_GRP 0x0c -#define FLAME_I2C_LED2_OFF 0x00 -#define FLAME_I2C_LED2_FULL 0x10 -#define FLAME_I2C_LED2_INDY 0x20 -#define FLAME_I2C_LED2_GRP 0x30 -#define FLAME_I2C_LED3_OFF 0x00 -#define FLAME_I2C_LED3_FULL 0x40 -#define FLAME_I2C_LED3_INDY 0x80 -#define FLAME_I2C_LED3_GRP 0xC0 - -#define FLAME_OFF 0x00 -#define FLAME_UP 0x01 -#define FLAME_UP_WAIT 0x02 -#define FLAME_DOWN 0x03 -#define FLAME_DOWN_WAIT 0x04 - -/**************************************************************************/ - -#define flameBrightnessMax GLOBAL(flamemax) -#define flameBrightnessMin GLOBAL(flamemin) -#define flameSpeedUp GLOBAL(flamespeed) -#define flameSpeedDown GLOBAL(flamespeed) -#define flameWaitUp GLOBAL(flamemaxw) -#define flameWaitDown GLOBAL(flameminw) - -#define nick GLOBAL(nickname) - -static uint32_t flameRGBSetI2C(uint8_t cr, uint8_t value) { - I2CMasterBuffer[0] = FLAME_I2C_WRITE; - I2CMasterBuffer[1] = cr; - I2CMasterBuffer[2] = value; - I2CWriteLength = 3; - I2CReadLength = 0; - return i2cEngine(); -} - -uint8_t flameRGBEnabled = 0; -uint8_t flameRGBMode = FLAME_OFF; -uint8_t flameRGBI2Cpwm = 0; -uint8_t flameRGBTicks = 0; - -uint8_t rgbData[24]; -uint8_t rgbDataSize = 0; -uint8_t rgbDataOffset = 0; - -void setFlameRGBPWM() { - flameRGBSetI2C(FLAME_I2C_CR_GRPPWM, flameRGBI2Cpwm); // set pwm -} - -void setFlameRGBColor() { - if (rgbDataSize > 2) { - flameRGBSetI2C(FLAME_I2C_CR_PWM0, rgbData[rgbDataOffset]); - flameRGBSetI2C(FLAME_I2C_CR_PWM3, rgbData[rgbDataOffset + 1]); - flameRGBSetI2C(FLAME_I2C_CR_PWM2, 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); - // set color, LED0 = red, LED1 = unused, LED2 = blue, LED3 = green - int i; - uint8_t regs[] = {FLAME_I2C_CR_PWM0, FLAME_I2C_CR_PWM1, - FLAME_I2C_CR_PWM2, FLAME_I2C_CR_PWM3 }; - for(i=0; i<4; i++) - flameRGBSetI2C(regs[i], hash[i] & 0xFF); - } -} - -void tick_flame_rgb(void) { // every 10ms - static char night=0; - - if (!flameRGBEnabled) { - return; - } - - if (night != isNight()) { - night = isNight(); - if (!night) { - flameRGBMode = FLAME_OFF; - flameRGBI2Cpwm = 0; - push_queue(&setFlameRGBPWM); - }; - }; - - flameRGBTicks++; - - if (flameRGBI2Cpwm > flameBrightnessMax) { - flameRGBI2Cpwm = flameBrightnessMax; - } - if (flameRGBI2Cpwm < flameBrightnessMin) { - flameRGBI2Cpwm = flameBrightnessMin; - } - - if (flameRGBMode == FLAME_OFF) { - if (isNight()) { - flameRGBTicks = 0; - flameRGBMode = FLAME_UP; - push_queue(&setFlameRGBColor); - } - } - - if (flameRGBMode == FLAME_UP) { - if (0xFF - flameRGBI2Cpwm >= flameSpeedUp ) { - flameRGBI2Cpwm += flameSpeedUp; - } else { - flameRGBI2Cpwm = 0xFF; - } - push_queue(&setFlameRGBPWM); - if (flameRGBI2Cpwm >= flameBrightnessMax) { - flameRGBMode = FLAME_UP_WAIT; - flameRGBTicks = 0; - } - } - - if (flameRGBMode == FLAME_UP_WAIT) { - if (flameRGBTicks >= flameWaitUp) { - flameRGBMode = FLAME_DOWN; - } - } - - if (flameRGBMode == FLAME_DOWN) { - if (flameSpeedDown <= flameRGBI2Cpwm) { - flameRGBI2Cpwm -= flameSpeedDown; - } else { - flameRGBI2Cpwm = 0x00; - } - push_queue(&setFlameRGBPWM); - if (flameRGBI2Cpwm <= flameBrightnessMin) { - flameRGBMode = FLAME_DOWN_WAIT; - flameRGBTicks = 0; - } - } - - if (flameRGBMode == FLAME_DOWN_WAIT) { - if (flameRGBTicks >= flameWaitDown) { - flameRGBMode = FLAME_OFF; - } - } -} - -void init_flame_rgb(void) { - i2cInit(I2CMASTER); // Init I2C - - flameRGBEnabled = (flameRGBSetI2C(FLAME_I2C_CR_MODE1, 0x00) == I2CSTATE_ACK); // probe i2c and enable flame if present - - if (!flameRGBEnabled) - return; - - // enable individual PWN and group PWN for LED0, LED3 and LED2 and disable LED1 - flameRGBSetI2C(FLAME_I2C_CR_LEDOUT, FLAME_I2C_LED0_GRP | FLAME_I2C_LED1_OFF | FLAME_I2C_LED2_GRP | FLAME_I2C_LED3_GRP); - - flameRGBSetI2C(FLAME_I2C_CR_GRPPWM, 0x00); // overall dimming - - rgbDataSize = readTextFile("FLAME.RGB", rgbData, 24); - - enableConfig(CFG_TYPE_FLAME,1); -} - diff --git a/firmware/flame/Makefile b/firmware/flame/Makefile new file mode 100644 index 0000000..59143bd --- /dev/null +++ b/firmware/flame/Makefile @@ -0,0 +1,23 @@ +########################################################################## +# User configuration and firmware specific object files +########################################################################## + +OBJS = + +OBJS += flame.o + +LIBNAME=flame + +########################################################################## +# GNU GCC compiler flags +########################################################################## +ROOT_PATH?= .. +INCLUDE_PATHS = -I$(ROOT_PATH) -I../core -I. + +include $(ROOT_PATH)/Makefile.inc + +########################################################################## +# Actual work +########################################################################## + +include $(ROOT_PATH)/Makefile.util diff --git a/firmware/flame/flame.c b/firmware/flame/flame.c new file mode 100644 index 0000000..975dade --- /dev/null +++ b/firmware/flame/flame.c @@ -0,0 +1,92 @@ +#include "flame/flame.h" + +#include "basic/basic.h" +#include "core/i2c/i2c.h" + +uint8_t flamesClaimed = FLAME_TYPE_NONE; + +uint8_t flameBrigthnessRGB = 0; +uint8_t flameBrigthnessM0n0 = 0; + +uint8_t rgb[3]; + +uint32_t flameSetI2C(uint8_t addr, uint8_t cr, uint8_t value) { + I2CMasterBuffer[0] = addr; + I2CMasterBuffer[1] = cr; + I2CMasterBuffer[2] = value; + I2CWriteLength = 3; + I2CReadLength = 0; + return i2cEngine(); +} + +uint8_t flameDetect() { + i2cInit(I2CMASTER); // Init I2C + uint8_t found = FLAME_TYPE_NONE; + if (flameSetI2C(FLAME_RGB_I2C_WRITE, FLAME_RGB_I2C_CR_MODE1, 0x00) == I2CSTATE_ACK) { + found |= FLAME_TYPE_RGB; + } + if (flameSetI2C(FLAME_M0N0_I2C_WRITE, FLAME_M0N0_I2C_CR_LS0, FLAME_M0N0_I2C_LS0_OFF << FLAME_M0N0_I2C_LS0_LED0) == I2CSTATE_ACK) { + found |= FLAME_TYPE_M0N0; + } + return found; +} + +void flameInit(uint8_t type) { + if (type & FLAME_TYPE_M0N0) { + flameSetI2Cm0n0(FLAME_M0N0_I2C_CR_LS0, FLAME_M0N0_I2C_LS0_OFF << FLAME_M0N0_I2C_LS0_LED0); // set led0 off + flameSetI2Cm0n0(FLAME_M0N0_I2C_CR_LS0, FLAME_M0N0_I2C_LS0_OFF << FLAME_M0N0_I2C_LS0_LED1); // set led1 off + flameSetI2Cm0n0(FLAME_M0N0_I2C_CR_LS0, FLAME_M0N0_I2C_LS0_OFF << FLAME_M0N0_I2C_LS0_LED2); // set led2 off + flameSetI2Cm0n0(FLAME_M0N0_I2C_CR_LS0, FLAME_M0N0_I2C_LS0_OFF << FLAME_M0N0_I2C_LS0_LED3); // set led3 off + + flameSetI2Cm0n0(FLAME_M0N0_I2C_CR_PSC0, 0x00); // set prescaler + flameSetI2Cm0n0(FLAME_M0N0_I2C_CR_PWM0, 0x00); // set pwm + flameSetI2Cm0n0(FLAME_M0N0_I2C_CR_LS0, FLAME_M0N0_I2C_LS0_PWM0 << FLAME_M0N0_I2C_LS0_LED0); // set led0 to pwm + } + if (type & FLAME_TYPE_RGB) { + // enable individual PWN and group PWN for LED0, LED3 and LED2 and disable LED1 + flameSetI2Crgb(FLAME_RGB_I2C_CR_LEDOUT, FLAME_RGB_I2C_LED0_GRP | FLAME_RGB_I2C_LED1_OFF | FLAME_RGB_I2C_LED2_GRP | FLAME_RGB_I2C_LED3_GRP); + flameSetI2Crgb(FLAME_RGB_I2C_CR_GRPPWM, 0x00); // overall dimming + } +} + +void _setFlamePWMrgb() { + flameSetI2Crgb(FLAME_RGB_I2C_CR_GRPPWM, flameBrigthnessRGB); +} +void _setFlamePWMm0n0() { + flameSetI2Cm0n0(FLAME_M0N0_I2C_CR_PWM0, flameBrigthnessM0n0); +} + +void flameSetBrightness(uint8_t type, uint8_t bright) { + if (type & FLAME_TYPE_M0N0) { + flameBrigthnessM0n0 = bright; + push_queue(&_setFlamePWMm0n0); + } + if (type & FLAME_TYPE_RGB) { + flameBrigthnessRGB = bright; + push_queue(&_setFlamePWMrgb); + } +} + +void _setFlameColor() { + flameSetI2Crgb(FLAME_RGB_I2C_CR_PWM0, rgb[0]); + flameSetI2Crgb(FLAME_RGB_I2C_CR_PWM3, rgb[1]); + flameSetI2Crgb(FLAME_RGB_I2C_CR_PWM2, rgb[2]); +} + +void flameSetColor(uint8_t type, uint8_t red, uint8_t green, uint8_t blue) { + if (type & FLAME_TYPE_RGB) { + rgb[0] = red; + rgb[1] = green; + rgb[2] = blue; + push_queue(&_setFlameColor); + } +} + +void flameClaim(uint8_t type) { + flamesClaimed |= type; +} + +void flameFree(uint8_t type) { + flamesClaimed &= ~type; +} + diff --git a/firmware/flame/flame.h b/firmware/flame/flame.h new file mode 100644 index 0000000..f3ba9d9 --- /dev/null +++ b/firmware/flame/flame.h @@ -0,0 +1,95 @@ +#ifndef _FLAME_H +#define _FLAME_H 1 + +#include +#include "basic/config.h" + +/* available types of flames */ +#define FLAME_TYPE_NONE 0 +#define FLAME_TYPE_M0N0 1 +#define FLAME_TYPE_RGB 2 + +/* I2C protocol for m0n0 flame (PCA9533) */ +#define FLAME_M0N0_I2C_WRITE 0xC4 +#define FLAME_M0N0_I2C_READ 0xC5 + +#define FLAME_M0N0_I2C_CR_INPUT 0x00 +#define FLAME_M0N0_I2C_CR_PSC0 0x01 +#define FLAME_M0N0_I2C_CR_PWM0 0x02 +#define FLAME_M0N0_I2C_CR_PSC1 0x03 +#define FLAME_M0N0_I2C_CR_PWM1 0x04 +#define FLAME_M0N0_I2C_CR_LS0 0x05 + +#define FLAME_M0N0_I2C_LS0_OFF 0x00 +#define FLAME_M0N0_I2C_LS0_ON 0x01 +#define FLAME_M0N0_I2C_LS0_PWM0 0x02 +#define FLAME_M0N0_I2C_LS0_PWM1 0x03 + +#define FLAME_M0N0_I2C_LS0_LED0 0x00 +#define FLAME_M0N0_I2C_LS0_LED1 0x02 +#define FLAME_M0N0_I2C_LS0_LED2 0x04 +#define FLAME_M0N0_I2C_LS0_LED3 0x06 + +/* I2C protocol for rgb flame (PC9633) */ +#define FLAME_RGB_I2C_WRITE 0xC6 +#define FLAME_RGB_I2C_READ 0xC7 + +#define FLAME_RGB_I2C_CR_MODE1 0x00 +#define FLAME_RGB_I2C_CR_MODE2 0x01 +#define FLAME_RGB_I2C_CR_PWM0 0x02 +#define FLAME_RGB_I2C_CR_PWM1 0x03 +#define FLAME_RGB_I2C_CR_PWM2 0x04 +#define FLAME_RGB_I2C_CR_PWM3 0x05 +#define FLAME_RGB_I2C_CR_GRPPWM 0x06 +#define FLAME_RGB_I2C_CR_GRPFREQ 0x07 +#define FLAME_RGB_I2C_CR_LEDOUT 0x08 + +#define FLAME_RGB_I2C_LED0_OFF 0x00 +#define FLAME_RGB_I2C_LED0_FULL 0x01 +#define FLAME_RGB_I2C_LED0_INDY 0x02 +#define FLAME_RGB_I2C_LED0_GRP 0x03 +#define FLAME_RGB_I2C_LED1_OFF 0x00 +#define FLAME_RGB_I2C_LED1_FULL 0x04 +#define FLAME_RGB_I2C_LED1_INDY 0x08 +#define FLAME_RGB_I2C_LED1_GRP 0x0c +#define FLAME_RGB_I2C_LED2_OFF 0x00 +#define FLAME_RGB_I2C_LED2_FULL 0x10 +#define FLAME_RGB_I2C_LED2_INDY 0x20 +#define FLAME_RGB_I2C_LED2_GRP 0x30 +#define FLAME_RGB_I2C_LED3_OFF 0x00 +#define FLAME_RGB_I2C_LED3_FULL 0x40 +#define FLAME_RGB_I2C_LED3_INDY 0x80 +#define FLAME_RGB_I2C_LED3_GRP 0xC0 + +/* state machine constants for default blinking */ + +#define FLAME_STATE_OFF 0x00 +#define FLAME_STATE_UP 0x01 +#define FLAME_STATE_UP_WAIT 0x02 +#define FLAME_STATE_DOWN 0x03 +#define FLAME_STATE_DOWN_WAIT 0x04 + +/* config options */ + +#define flameBrightnessMax GLOBAL(flamemax) +#define flameBrightnessMin GLOBAL(flamemin) +#define flameSpeedUp GLOBAL(flamespeed) +#define flameSpeedDown GLOBAL(flamespeed) +#define flameWaitUp GLOBAL(flamemaxw) +#define flameWaitDown GLOBAL(flameminw) + +/* flame "API" */ +uint32_t flameSetI2C(uint8_t addr, uint8_t cr, uint8_t value); +uint8_t flameDetect(); +void flameInit(uint8_t type); +void flameSetBrightness(uint8_t type, uint8_t bright); +void flameSetColor(uint8_t type, uint8_t red, uint8_t green, uint8_t blue); +void flameClaim(uint8_t type); +void flameFree(uint8_t type); + +extern uint8_t flamesClaimed; + +#define flameSetI2Cm0n0(a, b) flameSetI2C(FLAME_M0N0_I2C_WRITE, a, b) +#define flameSetI2Crgb(a, b) flameSetI2C(FLAME_RGB_I2C_WRITE, a, b) + +#endif /* _FLAME_H */ diff --git a/firmware/l0dable/EXPORTS b/firmware/l0dable/EXPORTS index 2b0fd3f..df47bee 100644 --- a/firmware/l0dable/EXPORTS +++ b/firmware/l0dable/EXPORTS @@ -130,3 +130,10 @@ lcdSetCrsr lcdSetCrsrX getInputWaitRepeat applyConfig +# flame API +flameDetect +flameSetBrightness +flameSetColor +flameClaim +flameFree +