Skip to content

Instantly share code, notes, and snippets.

@namikiri
Created March 10, 2017 19:46
Show Gist options
  • Save namikiri/1f4029e02d07afd45de2060b4621ba24 to your computer and use it in GitHub Desktop.
Save namikiri/1f4029e02d07afd45de2060b4621ba24 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "bass.h"
int write_int(char const* path, int value) {
int fd, amt, bytes;
char buffer[20];
/* Int output to path */
fd = open(path, O_RDWR);
if (fd >= 0) {
bytes = snprintf(buffer, sizeof(buffer), "%d\n", value);
amt = write(fd, buffer, bytes);
close(fd);
return (amt == -1 ? -errno : 0);
}
else {
printf("write_int failed to open %s\n", path);
return -errno;
}
}
void led_brightness(uint8_t r, uint8_t g, uint8_t b)
{
write_int("/sys/class/leds/LED1_R/brightness", r);
write_int("/sys/class/leds/LED2_R/brightness", r);
write_int("/sys/class/leds/LED3_R/brightness", r);
write_int("/sys/class/leds/LED1_G/brightness", g);
write_int("/sys/class/leds/LED2_G/brightness", g);
write_int("/sys/class/leds/LED3_G/brightness", g);
write_int("/sys/class/leds/LED1_B/brightness", b);
write_int("/sys/class/leds/LED2_B/brightness", b);
write_int("/sys/class/leds/LED3_B/brightness", b);
}
// Board: set led colors
void led_color(uint8_t r, uint8_t g, uint8_t b)
{
write_int("/sys/class/leds/LED1_R/led_current", r);
write_int("/sys/class/leds/LED2_R/led_current", r);
write_int("/sys/class/leds/LED3_R/led_current", r);
write_int("/sys/class/leds/LED1_G/led_current", g);
write_int("/sys/class/leds/LED2_G/led_current", g);
write_int("/sys/class/leds/LED3_G/led_current", g);
write_int("/sys/class/leds/LED1_B/led_current", b);
write_int("/sys/class/leds/LED2_B/led_current", b);
write_int("/sys/class/leds/LED3_B/led_current", b);
}
void led_brightness_n (int n, uint8_t r, uint8_t g, uint8_t b)
{
switch (n)
{
case 1 :
write_int("/sys/class/leds/LED1_R/brightness", r);
write_int("/sys/class/leds/LED1_G/brightness", g);
write_int("/sys/class/leds/LED1_B/brightness", b);
break;
case 2 :
write_int("/sys/class/leds/LED2_R/brightness", r);
write_int("/sys/class/leds/LED2_G/brightness", g);
write_int("/sys/class/leds/LED2_B/brightness", b);
break;
case 3 :
write_int("/sys/class/leds/LED3_R/brightness", r);
write_int("/sys/class/leds/LED3_G/brightness", g);
write_int("/sys/class/leds/LED3_B/brightness", b);
break;
default:
printf ("R u mad bro? WTF?\n");
break;
}
}
void led_color_n (int n, uint8_t r, uint8_t g, uint8_t b)
{
switch (n)
{
case 1 :
write_int("/sys/class/leds/LED1_R/led_current", r);
write_int("/sys/class/leds/LED1_G/led_current", g);
write_int("/sys/class/leds/LED1_B/led_current", b);
break;
case 2 :
write_int("/sys/class/leds/LED2_R/led_current", r);
write_int("/sys/class/leds/LED2_G/led_current", g);
write_int("/sys/class/leds/LED2_B/led_current", b);
break;
case 3 :
write_int("/sys/class/leds/LED3_R/led_current", r);
write_int("/sys/class/leds/LED3_G/led_current", g);
write_int("/sys/class/leds/LED3_B/led_current", b);
break;
default:
printf ("R u mad bro? WTF?\n");
break;
}
}
int main(int argc, char* argv[])
{
printf("I love Sony LEDs! They're cool!\n");
printf("Starting up the BASS library...\n");
if (!BASS_Init(-1, 44100, 0, 0, 0))
{
printf ("BASS could not start up, the code is %d\n", BASS_ErrorGetCode());
exit(-1);
}
printf ("Creating BASS Stream...\n");
int stream = BASS_StreamCreateFile(0, "/sdcard/1.mp3", 0, 0, 0);
if (BASS_ErrorGetCode() == 0)
printf ("It seems that we can play a BASS stream! Yay!\n");
BASS_ChannelPlay(stream, 0);
if (BASS_ErrorGetCode() == 0)
printf ("Does it play? Say yes, please! *.*\n");
printf("Resetting the LEDs. I will wait for 3s, stop me if reset is all what you need!\n");
led_brightness(255, 255, 255);
led_color(0, 0, 0);
// usleep(3000000);
printf ("OK, let it go!\n");
uint8_t levels[9];
float fft[128]; // fft data buffer
float tmp;
while (BASS_ChannelIsActive(stream) == BASS_ACTIVE_PLAYING)
{
BASS_ChannelGetData(stream, fft, BASS_DATA_FFT256);
for (int i = 0; i < 64; i += 7)
{
tmp = 0;
for (int j = i; j < i+7; j++)
tmp += fft[j];
tmp = tmp / 7;
levels[(int)(i / 7)] = (uint8_t)(tmp*255);
}
led_color_n(1, levels[0], levels[1], levels[2]);
led_color_n(2, levels[3], levels[4], levels[5]);
led_color_n(3, levels[6], levels[7], levels[8]);
usleep(5);
}
led_brightness(0, 0, 0);
led_color(0, 0, 0);
BASS_Free();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment