aboutsummaryrefslogtreecommitdiffstats
path: root/usr/space_light/src/space_light.c
blob: f1a03736d7f6b2d614bcf026708f24f9f36cdc15 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
#include "comm.h"
#include "comm_task.h"
#include <math.h>
#include <space_light.h>
#include <stdint.h>

#include "BkDriverPwm.h"

#define WARM BK_PWM_0
#define COLD BK_PWM_2
#define RED BK_PWM_3
#define GREEN BK_PWM_4
#define BLUE BK_PWM_5

static const float max_freq = 1000.0f;

// FIXME: consider creating a lookup table
static inline uint16_t get_led_frequency(const uint8_t value,
                                         const float gamma) {
  return (uint16_t)pow((float)value / 255.0f, gamma) * max_freq + 0.5;
}

void setRGBCW(struct RGBCW *rgbcw) {
  if (rgbcw->r) {
    bk_pwm_initialize(RED, get_led_frequency(rgbcw->r, 2.8), 10);
    bk_pwm_start(RED);
  } else {
    // FIXME: same as in the init ^^ should be stop
    bk_pwm_initialize(RED, 100, 10);
    bk_pwm_start(RED);
  }

  if (rgbcw->g) {
    bk_pwm_initialize(GREEN, get_led_frequency(rgbcw->g, 2.8), 10);
    bk_pwm_start(GREEN);
  } else {
    // FIXME: same as in the init ^^ should be stop
    bk_pwm_initialize(GREEN, 100, 10);
    bk_pwm_start(GREEN);
  }

  if (rgbcw->b) {
    bk_pwm_initialize(BLUE, get_led_frequency(rgbcw->b, 2.8), 10);
    bk_pwm_start(BLUE);
  } else {
    // FIXME: same as in the init ^^ should be stop
    bk_pwm_initialize(BLUE, 100, 10);
    bk_pwm_start(BLUE);
  }

  if (rgbcw->c) {
    uint16_t value = rgbcw->c;
    if (value > 100) {
      value = 100;
    }
    value = value * 10;
    bk_pwm_initialize(COLD, value, 50);
    bk_pwm_start(COLD);
  } else {
    bk_pwm_initialize(COLD, 0, 50);
    bk_pwm_stop(COLD);
  }

  if (rgbcw->w) {
    uint16_t value = rgbcw->w;
    if (value > 100) {
      value = 100;
    }
    value = value * 10;
    value = 1000 - value; // the motor is inverted 1000 is the slowest value
    bk_pwm_initialize(WARM, value, 10);
    bk_pwm_start(WARM);
  } else {
    bk_pwm_initialize(WARM, 0, 10);
    bk_pwm_stop(WARM);
  }
}

void init_led_thread(void *arg) {
  // FIXME: this is as it is stupid and bad but it works this way..
  // TODO: figure out how to just disable pwm without setting defaults first ^^
  // TODO: update pwm also is not working like it should
  bk_pwm_stop(RED);
  bk_pwm_stop(GREEN);
  bk_pwm_stop(BLUE);
  bk_pwm_stop(COLD);
  bk_pwm_stop(WARM);

  bk_pwm_initialize(RED, 100, 50);
  bk_pwm_start(RED);
  bk_pwm_initialize(GREEN, 100, 50);
  bk_pwm_start(GREEN);
  bk_pwm_initialize(BLUE, 100, 50);
  bk_pwm_start(BLUE);
  // FIXME: laser is still a puzzle to me
  bk_pwm_initialize(COLD, 100, 50);
  bk_pwm_start(COLD);
  bk_pwm_initialize(WARM, 1000, 50);
  bk_pwm_start(WARM);

  // FIXME: doing this will start all leds to full color ^^
  // TODO: figgure out how to reverse the level in the configuration of each PWM
  // bk_pwm_stop(RED);
  // bk_pwm_stop(GREEN);
  // bk_pwm_stop(BLUE);
  // bk_pwm_stop(COLD);
  // bk_pwm_stop(WARM);

  rtos_delete_thread(NULL);
}