Voss Connection Detection  1
Detect a connector click using realtime code on Bela hardware
leds.cpp
Go to the documentation of this file.
1 #include "leds.h"
2 #include <Bela.h>
3 #include <iostream>
4 using namespace std;
5 
6 Leds::Leds(BelaContext* ctx) {
7  dT = ctx->digitalFrames/ctx->digitalSampleRate;
8  pinMode(ctx, 0, LED_PIN_WRIST, OUTPUT);
9  pinMode(ctx, 0, LED_PIN_THUMB, OUTPUT);
10  pinMode(ctx, 0, VIBRATOR_PIN, OUTPUT);
11  pinMode(ctx, 0, LED_PIN_CONTROLBOX, OUTPUT);
12 }
13 
14 bool Leds::processLed(Led _led) {
15  int ledno = (int) _led;
16  LedState ledstate = _ledstate[ledno];
17  switch(ledstate) {
18  case off:
19  return false;
20  break;
21  case on:
22  return true;
23  break;
24  case blink_permanent:
25  {
26  bool state = false;
27  float bp = _blinkPeriod[ledno];
28  float *time = &(_timer[ledno]);
29  float dc = _blinkDutyCycle[ledno];
30  if(*time > bp) {(*time) -= bp;}
31  state = (*time < dc*bp) ? true : false;
32 
33  *time += dT;
34  return state;
35  }
36  case blink_once:
37  {
38  bool state = false;
39  float bp = _blinkPeriod[ledno];
40  float *time = &(_timer[ledno]);
41  state = (*time < bp) ? true : false;
42  if(*time > bp) {
43  (*time) = 0;
44  _ledstate[ledno] = off;
45  }
46 
47  *time += dT;
48  return state;
49 
50  }
51 
52  }
53  return false;
54 
55 }
56 
57 void Leds::processBuf(BelaContext* ctx) {
58 
59  _on[(int) wrist] = processLed(wrist);
60  _on[(int) thumb] = processLed(thumb);
61  _on[(int) vibrator] = processLed(vibrator);
62  _on[(int) controlbox] = processLed(controlbox);
63 
64  /* pinMode(ctx, 0, LED_PIN_WRIST, OUTPUT); */
65  /* pinMode(ctx, 0, LED_PIN_THUMB, OUTPUT); */
66  /* pinMode(ctx, 0, VIBRATOR_PIN, OUTPUT); */
67  /* pinMode(ctx, 0, LED_PIN_CONTROLBOX, OUTPUT); */
68 
69 }
70 
71 void Leds::processFrame(BelaContext* ctx, const us frameno) {
72  digitalWrite(ctx, frameno, LED_PIN_WRIST, _on[(int) wrist]);
73  digitalWrite(ctx, frameno, LED_PIN_THUMB, _on[(int) thumb]);
74  digitalWrite(ctx, frameno, VIBRATOR_PIN, _on[(int) vibrator]);
75  digitalWrite(ctx, frameno, LED_PIN_CONTROLBOX, _on[(int) controlbox]);
76 }
77 
78 
79 void Leds::setState(Led led,LedState ledstate, float period, float dutycycle) {
80 
81  _ledstate[(int) led] = ledstate;
82  _timer[(int) led] = 0.0f;
83  _blinkPeriod[(int) led] = period;
84  _blinkDutyCycle[(int) led] = dutycycle;
85 
86 }
87 
Leds::processBuf
void processBuf(BelaContext *ctx)
Run this function in render.
Definition: leds.cpp:57
Leds::Leds
Leds(BelaContext *ctx)
Initialize LED controller and state machine.
Definition: leds.cpp:6
Leds::setState
void setState(Led led, LedState ledstate, float period=LED_HOLD_TIME, float dutycycle=0.5)
Set a certain LED in a certain state. The state blink_once will - after it has blinked - automaticall...
Definition: leds.cpp:79
LED_PIN_WRIST
const int LED_PIN_WRIST
The digital pin number for the wrist led.
Definition: config.h:59
LED_PIN_THUMB
const int LED_PIN_THUMB
The digital pin number for the thumb led.
Definition: config.h:64
leds.h
Leds::processFrame
void processFrame(BelaContext *ctx, us frame)
Definition: leds.cpp:71
Leds::Led
Led
Led enumeration.
Definition: leds.h:22
us
unsigned int us
Used to much to not abbreviate.
Definition: config.h:38
VIBRATOR_PIN
const int VIBRATOR_PIN
The digital pin number for the vibrometer.
Definition: config.h:69
LED_PIN_CONTROLBOX
const int LED_PIN_CONTROLBOX
The digital pin number for the control box led.
Definition: config.h:54
Leds::LedState
LedState
A state in which a certain LED can be set.
Definition: leds.h:44