-
Notifications
You must be signed in to change notification settings - Fork 2
/
piglow.c
95 lines (84 loc) · 2.31 KB
/
piglow.c
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
// Pithesiser - a software synthesiser for Raspberry Pi
// Copyright (C) 2015 Nicholas Tuckett
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
/*
* piglow.c
*
* Created on: 23 Dec 2013
* Author: ntuckett
*/
#include <wiringPi.h>
#include <piGlow.h>
#include <memory.h>
#include <libconfig.h>
#include "voice.h"
#define PIGLOW_LED_COUNT 6
#define PIGLOW_LED_MAX 255
static char last_led_brights[PIGLOW_LED_COUNT];
static int piglow_enabled = 0;
static int piglow_brightness = PIGLOW_LED_MAX;
void piglow_initialise(config_setting_t* config)
{
if (config != NULL)
{
config_setting_lookup_bool(config, "enabled", &piglow_enabled);
if (piglow_enabled)
{
double brightness = 1.0;
config_setting_lookup_float(config, "brightness", &brightness);
piglow_brightness = PIGLOW_LED_MAX * brightness;
piGlowSetup(0);
memset(last_led_brights, 0, sizeof(last_led_brights));
}
}
}
void piglow_update(voice_t* voice, int voice_count)
{
if (piglow_enabled)
{
char led_brights[PIGLOW_LED_COUNT];
memset(led_brights, 0, sizeof(led_brights));
for (int i = 0; i < voice_count; i++)
{
if (voice[i].current_state != NOTE_NOT_PLAYING)
{
int led = (voice[i].note % 12) / 2;
int bright = (voice[i].oscillator.level * PIGLOW_LED_MAX) / LEVEL_MAX;
bright = bright * piglow_brightness / PIGLOW_LED_MAX;
if (bright > led_brights[led])
{
led_brights[led] = bright;
}
}
}
for (int i = 0; i < PIGLOW_LED_COUNT; i++)
{
if (led_brights[i] != last_led_brights[i])
{
piGlowRing(i, led_brights[i]);
}
}
memcpy(last_led_brights, led_brights, sizeof(last_led_brights));
}
}
void piglow_deinitialise()
{
if (piglow_enabled)
{
piGlowLeg(0,0);
piGlowLeg(1,0);
piGlowLeg(2,0);
}
}