personal_state.c 2.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include "epicardium.h"
#include "leds.h"
#include "modules.h"

#include <math.h>

uint8_t _personal_state_enabled   = 0;
uint8_t personal_state            = STATE_NONE;
uint8_t personal_state_persistent = 0;

int led_animation_ticks = 0;
int led_animation_state = 0;

int personal_state_enabled()
{
	return _personal_state_enabled;
}

int epic_personal_state_set(uint8_t state, bool persistent)
{
21
	if (state > STATE_CAMP)
22 23 24 25 26 27 28 29 30 31 32 33 34
		return -EINVAL;

	led_animation_state = 0;
	led_animation_ticks = 0;
	personal_state      = state;

	uint8_t was_enabled = _personal_state_enabled;

	_personal_state_enabled   = (state != STATE_NONE);
	personal_state_persistent = persistent;

	if (was_enabled && !_personal_state_enabled) {
		leds_prep(PERSONAL_STATE_LED, 0, 0, 0);
35
		epic_leds_update();
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
	}

	return 0;
}

int epic_personal_state_get()
{
	return personal_state;
}

int epic_personal_state_is_persistent()
{
	return personal_state_persistent;
}

void vLedTask(void *pvParameters)
{
	const int led_animation_rate = 1000 / 25; /* 25Hz -> 40ms*/
	while (1) {
		if (_personal_state_enabled) {
			led_animation_ticks++;
			if (personal_state == STATE_NO_CONTACT) {
				leds_prep(PERSONAL_STATE_LED, 255, 0, 0);
			} else if (personal_state == STATE_CHAOS) {
				if (led_animation_state == 0) {
					leds_prep(
						PERSONAL_STATE_LED, 0, 0, 255
					);
					if (led_animation_ticks >
					    (200 / led_animation_rate)) {
						led_animation_ticks = 0;
						led_animation_state = 1;
					}
				} else if (led_animation_state == 1) {
					leds_prep(PERSONAL_STATE_LED, 0, 0, 0);
					if (led_animation_ticks >
					    (300 / led_animation_rate)) {
						led_animation_ticks = 0;
						led_animation_state = 2;
					}
				} else if (led_animation_state == 2) {
					leds_prep(
						PERSONAL_STATE_LED, 0, 0, 255
					);
					if (led_animation_ticks >
					    (1000 / led_animation_rate)) {
						led_animation_ticks = 0;
						led_animation_state = 3;
					}
				} else if (led_animation_state == 3) {
					leds_prep(PERSONAL_STATE_LED, 0, 0, 0);
					if (led_animation_ticks >
					    (300 / led_animation_rate)) {
						led_animation_ticks = 0;
						led_animation_state = 0;
					}
				}
			} else if (personal_state == STATE_COMMUNICATION) {
				if (led_animation_state == 0) {
					leds_prep(
						PERSONAL_STATE_LED, 255, 255, 0
					);
					if (led_animation_ticks >
					    (1000 / led_animation_rate)) {
						led_animation_ticks = 0;
						led_animation_state = 1;
					}
				} else if (led_animation_state == 1) {
					leds_prep(PERSONAL_STATE_LED, 0, 0, 0);
					if (led_animation_ticks >
					    (300 / led_animation_rate)) {
						led_animation_ticks = 0;
						led_animation_state = 0;
					}
				}
			} else if (personal_state == STATE_CAMP) {
				leds_prep_hsv(
					PERSONAL_STATE_LED,
					120.0f,
					1.0f,
					fabs(sin(
						led_animation_ticks /
						(float)(1000 /
							led_animation_rate))));
			}
121

122
			epic_leds_update();
123 124 125 126 127
		}

		vTaskDelay(led_animation_rate / portTICK_PERIOD_MS);
	}
}