Version 1.1 (more effects)

This commit is contained in:
Derek Jamison 2023-08-10 17:08:58 -07:00
parent 593b7dd948
commit 961294756e
2 changed files with 113 additions and 21 deletions

View File

@ -6,5 +6,5 @@ App(
requires=["gui", "subghz"],
stack_size=2 * 1024,
fap_icon="gpio_badge.png",
fap_category="Utilities",
fap_category="Misc",
)

View File

@ -17,7 +17,20 @@ const GpioPin* const pin_leds[] = {
&gpio_ibutton,
};
uint32_t speed = 80;
uint32_t speed = 50;
typedef enum {
EffectIdLRL,
EffectIdBothLRL,
EffectIdLR,
EffectIdCount // Make sure this is last
} EffectId;
EffectId effect = EffectIdLR;
bool animating = true;
int lri = 0;
#define MAX_EFFECT 1
const GpioPin* const pin_back = &gpio_button_back;
@ -25,6 +38,8 @@ static void my_draw_callback(Canvas* canvas, void* context) {
UNUSED(context);
canvas_set_font(canvas, FontPrimary);
canvas_draw_str(canvas, 5, 8, "GPIO BADGE");
canvas_draw_str(canvas, 5, 28, "Left/Right : Speed");
canvas_draw_str(canvas, 5, 38, "Up/Down : Effect");
}
static void my_input_callback(InputEvent* event, void* context) {
@ -32,15 +47,108 @@ static void my_input_callback(InputEvent* event, void* context) {
if(event->type == InputTypePress) {
uint32_t last_speed = speed;
if(event->key == InputKeyLeft) {
speed *= 0.8;
speed *= 0.9;
if(speed < 1) {
speed = 1;
}
} else if(event->key == InputKeyRight) {
speed *= 1.2;
speed *= 1.1;
if(speed < last_speed + 5) {
speed += 5;
}
} else if(event->key == InputKeyUp) {
if(effect-- == 0) {
animating = true;
effect = EffectIdCount - 1;
}
} else if(event->key == InputKeyDown) {
animating = true;
if(++effect == EffectIdCount) {
effect = 0;
}
} else if(event->key == InputKeyOk) {
animating = !animating;
}
}
}
void do_effect_lrl() {
int i = 0;
int d = 1;
for(size_t c = 0; c < COUNT_OF(pin_leds) * 2; c++) {
furi_hal_gpio_write(pin_leds[i], true);
furi_delay_ms(speed);
furi_hal_gpio_write(pin_leds[i], false);
i += d;
if(i == COUNT_OF(pin_leds) || i < 0) {
if(i < 1)
i++;
else
i--;
d = d * -1;
}
if(furi_hal_gpio_read(pin_back) == false || effect != EffectIdLRL) {
break;
}
}
}
void do_effect_both_lrl() {
int i = 0;
int d = 1;
for(size_t c = 0; c < COUNT_OF(pin_leds) * 2; c++) {
furi_hal_gpio_write(pin_leds[i], true);
furi_hal_gpio_write(pin_leds[COUNT_OF(pin_leds) - (i + 1)], true);
furi_delay_ms(speed);
furi_hal_gpio_write(pin_leds[i], false);
furi_hal_gpio_write(pin_leds[COUNT_OF(pin_leds) - (i + 1)], false);
i += d;
if(i == COUNT_OF(pin_leds) || i < 0) {
if(i < 1)
i++;
else
i--;
d = d * -1;
}
if(furi_hal_gpio_read(pin_back) == false || effect != EffectIdBothLRL) {
break;
}
}
}
void do_effect_lr() {
for(size_t c = 0; c < COUNT_OF(pin_leds) * 2; c++) {
furi_hal_gpio_write(pin_leds[lri], true);
furi_delay_ms(speed);
furi_hal_gpio_write(pin_leds[lri], false);
if(animating) {
lri++;
}
if(lri == COUNT_OF(pin_leds)) {
lri = 0;
}
if(furi_hal_gpio_read(pin_back) == false || effect != EffectIdLR) {
break;
}
}
}
void do_effects() {
while(furi_hal_gpio_read(pin_back)) {
switch(effect) {
case EffectIdLRL:
do_effect_lrl();
break;
case EffectIdBothLRL:
do_effect_both_lrl();
break;
default:
do_effect_lr();
break;
}
}
}
@ -58,23 +166,7 @@ int gpio_diag_lights_app(void* p) {
furi_hal_gpio_init_simple(pin_leds[i], GpioModeOutputPushPull);
}
int i = 0;
int d = 1;
do {
furi_hal_gpio_write(pin_leds[i], true);
furi_delay_ms(speed);
furi_hal_gpio_write(pin_leds[i], false);
i += d;
if(i == COUNT_OF(pin_leds) || i < 0) {
if(i < 1)
i++;
else
i--;
d = d * -1;
}
// Hold the back button to exit (since we only scan it when restarting loop).
} while(furi_hal_gpio_read(pin_back));
do_effects();
// Typically when a pin is no longer in use, it is set to analog mode.
// but we need to use open drain for the 1Wire pin.