added file headers and cleaned up files

This commit is contained in:
Steffen Vogel 2013-02-09 16:33:04 +01:00
parent f5c1da50aa
commit b74238854c
6 changed files with 251 additions and 65 deletions

View file

@ -1,4 +1,32 @@
#include <stdio.h> /* Standard input/output definitions */
/**
* fnordlicht CLI control frontend for libfn
*
* implements the fnordlicht bus protocol
* @see https://raw.github.com/fd0/fnordlicht/master/doc/PROTOCOL
*
* @copyright 2013 Steffen Vogel
* @license http://www.gnu.org/licenses/gpl.txt GNU Public License
* @author Steffen Vogel <post@steffenvogel.de>
* @link http://www.steffenvogel.de
*/
/*
* This file is part of libfn
*
* libfn 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
* any later version.
*
* libfn 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 libfn. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <termios.h>
#include <fcntl.h>
#include <unistd.h>
@ -15,8 +43,8 @@
#define DEFAULT_PORT "7909"
struct command_t {
char * name;
char * description;
char *name;
char *description;
uint8_t cmd;
};
@ -34,6 +62,7 @@ static struct command_t commands[] = {
{"powerdown", "power down the device", REMOTE_CMD_POWERDOWN},
{"config", "configure startup & offsets", REMOTE_CMD_CONFIG_OFFSETS},
{"reset", "reset fnordlichter", REMOTE_CMD_BOOTLOADER},
{"pullint", "pull down INT line", REMOTE_CMD_PULL_INT},
{"eeprom", "put sequence to EEPROM", LOCAL_CMD_EEPROM},
{"count", "count modules on the bus", LOCAL_CMD_COUNT},
{} /* stop condition for iterator */
@ -50,7 +79,7 @@ static struct option long_options[] = {
{"start", required_argument, 0, 'f'},
{"end", required_argument, 0, 't'},
{"repeat", required_argument, 0, 'r'},
{"help", required_argument, 0, 'h'},
{"help", no_argument, 0, 'h'},
{"port", required_argument, 0, 'P'},
{"host", required_argument, 0, 'H'},
{"filename", required_argument, 0, 'F'},
@ -58,7 +87,7 @@ static struct option long_options[] = {
{} /* stop condition for iterator */
};
static char * long_options_descs[] = {
static char *long_options_descs[] = {
"delay between steps when fading (in 10ms)",
"increment step for fading (1-255)",
"address of fnordlicht (0-254, 255 for broadcast)",
@ -98,23 +127,23 @@ void print_cmd(struct remote_msg_t * msg) {
printf("\n");
}
void usage(char ** argv) {
printf("usage: fnctl command [options]\n\n");
printf(" following commands are available:\n");
void usage(char **argv) {
printf("Usage: fnctl command [options]\n\n");
printf("Commands:\n");
struct command_t * cp = commands;
struct command_t *cp = commands;
while (cp->name) {
printf("\t%s%s%s\n", cp->name, (strlen(cp->name) < 7) ? "\t\t" : "\t", cp->description);
printf(" %s%s%s\n", cp->name, (strlen(cp->name) <= 7) ? "\t\t" : "\t", cp->description);
cp++;
}
printf("\n");
printf(" following options are available\n");
printf("Options:\n");
struct option * op = long_options;
char ** desc = long_options_descs;
struct option *op = long_options;
char **desc = long_options_descs;
while (op->name && desc) {
printf("\t-%c, --%s\t%s\n", op->val, op->name, *desc);
printf(" -%c, --%s\t%s\n", op->val, op->name, *desc);
op++;
desc++;
}
@ -154,7 +183,7 @@ int main(int argc, char ** argv) {
exit(EXIT_FAILURE);
}
struct command_t * cp = commands;
struct command_t *cp = commands;
while (cp->name && strcmp(cp->name, argv[1]) != 0) {
cp++;
}
@ -166,9 +195,8 @@ int main(int argc, char ** argv) {
int c = getopt_long(argc, argv, "hva:m:f:d:t:s:f:w:r:d:p:c:P:H:F:", long_options, &option_index);
/* Detect the end of the options. */
if (c == -1)
break;
/* detect the end of the options. */
if (c == -1) break;
switch (c) {
case 'a':
@ -224,7 +252,7 @@ int main(int argc, char ** argv) {
break;
case 'H': {
char * ps = strrchr(optarg, ':');
char *ps = strrchr(optarg, ':');
if (ps) { /* with port: "localhost:1234" */
strcpy(port, optarg + 1);
strncpy(host, optarg, ps - optarg);
@ -323,6 +351,10 @@ int main(int argc, char ** argv) {
msg.start_program.params = params;
break;
case REMOTE_CMD_PULL_INT:
msg.pull_int.delay = 5;
break;
/* no special parameters */
case REMOTE_CMD_STOP:
case REMOTE_CMD_POWERDOWN:

View file

@ -1,3 +1,30 @@
/**
* fnordlicht power monitor for volkzaehler.org smartmetering platform
*
* @see http://wiki.volkszaehler.org/software/frontends/fnordlicht
*
* @copyright 2013 Steffen Vogel
* @license http://www.gnu.org/licenses/gpl.txt GNU Public License
* @author Steffen Vogel <post@steffenvogel.de>
* @link http://www.steffenvogel.de
*/
/*
* This file is part of libfn
*
* libfn 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
* any later version.
*
* libfn 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 libfn. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

View file

@ -1,4 +1,31 @@
/* stdlibs */
/**
* fnordlicht vu meter
*
* vizualize pulsaudio
* @see http://www.steffenvogel.de/2010/11/12/fnordlicht-vu-meter/
*
* @copyright 2013 Steffen Vogel
* @license http://www.gnu.org/licenses/gpl.txt GNU Public License
* @author Steffen Vogel <post@steffenvogel.de>
* @link http://www.steffenvogel.de
*/
/*
* This file is part of libfn
*
* libfn 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
* any later version.
*
* libfn 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 libfn. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
@ -18,7 +45,7 @@
#include "libfn.h"
#define N 512
#define N 2048
#define SAMPLING_RATE 44100
#define MIN_FREQ 70
@ -27,7 +54,7 @@
#define MIN_K (MIN_FREQ*N/SAMPLING_RATE)
#define MAX_K (MAX_FREQ*N /SAMPLING_RATE)
#define LINE_WIDTH 2
#define LINE_WIDTH 1
#define SCREEN_WIDTH LINE_WIDTH*(MAX_K - MIN_K)
#define SCREEN_HEIGHT SCREEN_WIDTH/2
#define VUM_HEIGHT SCREEN_HEIGHT/6
@ -44,7 +71,7 @@ struct rgb_color_t hsv2rgb(struct hsv_color_t hsv) {
uint16_t h = hsv.hue % 360;
uint8_t s = hsv.saturation;
uint8_t v = hsv.value;
uint16_t f = ((h % 60) * 255 + 30)/60;
uint16_t p = (v * (255-s)+128)/255;
uint16_t q = ((v * (255 - (s*f+128)/255))+128)/255;
@ -59,27 +86,27 @@ struct rgb_color_t hsv2rgb(struct hsv_color_t hsv) {
case 4: rgb.red = t; rgb.green = p; rgb.blue = v; break;
case 5: rgb.red = v; rgb.green = p; rgb.blue = q; break;
}
return rgb;
}
struct rgb_color_t phasor2color(complex phasor) {
struct hsv_color_t hsv;
hsv.hue = 180*(carg(phasor)+M_PI)/M_PI;
hsv.saturation = 255;
hsv.value = 255;
return hsv2rgb(hsv);
}
struct rgb_color_t level2color(double level) {
struct hsv_color_t hsv;
hsv.hue = level * 360;
hsv.saturation = 255;
hsv.value = 255;
return hsv2rgb(hsv);
}
@ -89,16 +116,16 @@ void show_spectrum(SDL_Surface * dst, complex * fft_data) {
uint32_t foreground;
SDL_FillRect(dst, &dst->clip_rect, background);
SDL_Rect rect;
SDL_Rect rect;
rect.w = LINE_WIDTH;
int k;
for (k = MIN_K; k <= MAX_K; k++) {
double ampl = cabs(fft_data[k]) / 500000;
rect.x = (k - MIN_K) * LINE_WIDTH;
rect.h = ampl * (SCREEN_HEIGHT - VUM_HEIGHT);
rect.y = (SCREEN_HEIGHT - VUM_HEIGHT) - rect.h;
rgb = phasor2color(fft_data[k]);
foreground = SDL_MapRGB(dst->format, rgb.red, rgb.green, rgb.blue);
@ -122,11 +149,11 @@ void fade_spectrum(int fd, complex * fft_data, int fn_num) {
ampl += cabs(fft_data[i]);
}
ampl /= 1000000*fn_num;
fn_cmd.fade_rgb.color.red = 255 * ampl;
fn_cmd.fade_rgb.color.green = 255 * ampl;
fn_cmd.fade_rgb.color.blue = 255 * ampl;
fn_cmd.address = k;
fn_send(fd, &fn_cmd);
}
@ -134,7 +161,7 @@ void fade_spectrum(int fd, complex * fft_data, int fn_num) {
void show_level(SDL_Surface *dst, float level) {
SDL_Rect rect = {0, SCREEN_HEIGHT - VUM_HEIGHT, SCREEN_WIDTH, VUM_HEIGHT};
struct rgb_color_t rgb = level2color(level);
uint32_t background = SDL_MapRGB(dst->format, 0, 0, 0);
uint32_t foreground = SDL_MapRGB(dst->format, rgb.red, rgb.green, rgb.blue);
@ -146,15 +173,16 @@ void show_level(SDL_Surface *dst, float level) {
void fade_level(int fd, double level) {
struct remote_msg_t fn_cmd;
struct rgb_color_t rgb = {{{255, 255, 255}}}; // level2color(level);
// struct rgb_color_t rgb = {{{255, 255, 255}}};
struct rgb_color_t rgb = level2color(level);
memset(&fn_cmd, 0, sizeof(struct remote_msg_t));
fn_cmd.cmd = REMOTE_CMD_FADE_RGB;
fn_cmd.address = 255;
fn_cmd.fade_rgb.step = 200;
fn_cmd.fade_rgb.delay = 0;
fn_cmd.fade_rgb.step = 60;
fn_cmd.fade_rgb.delay = 2;
fn_cmd.fade_rgb.color.red = rgb.red * level;
fn_cmd.fade_rgb.color.green = rgb.green * level;
fn_cmd.fade_rgb.color.blue = rgb.blue * level;
@ -169,7 +197,7 @@ int main(int argc, char *argv[]) {
.rate = SAMPLING_RATE,
.channels = 1
};
pa_simple *s = NULL;
SDL_Surface *screen = NULL;
SDL_Event event;
@ -189,9 +217,16 @@ int main(int argc, char *argv[]) {
fn_init(fd);
fn_sync(fd);
fn_num = fn_count_devices(fd);
printf("found %d fnordlichts\n", fn_num);
usleep(25000);
if (argc > 2) {
fn_num = atoi(argv[2]);
printf("set to %d fnordlichts\n", fn_num);
}
else {
fn_num = fn_count_devices(fd);
printf("found %d fnordlichts\n", fn_num);
usleep(25000);
}
}
/* init screen & window */
@ -253,7 +288,9 @@ int main(int argc, char *argv[]) {
level = (float) sum / (N * pow(2, 15)) * 2;
//if (counter % 2 == 0) fade_spectrum(fd, fft_data, fn_num);
if (level > 0.05) fade_level(fd, level);
if (level > 0.67) fade_level(fd, 1);
else fade_level(fd, 0);
//if (level > 0.05) fade_level(fd, level);
show_spectrum(screen, fft_data);
show_level(screen, level);

View file

@ -1,3 +1,30 @@
/**
* fnordlicht web control frontend
*
* simple ajax colorwheel frontend to control fnordlicht's
*
* @copyright 2013 Steffen Vogel
* @license http://www.gnu.org/licenses/gpl.txt GNU Public License
* @author Steffen Vogel <post@steffenvogel.de>
* @link http://www.steffenvogel.de
*/
/*
* This file is part of libfn
*
* libfn 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
* any later version.
*
* libfn 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 libfn. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
@ -11,7 +38,7 @@
#include "libfn.h"
volatile bool shutdown = false; /* will be set to TRUE in our signal handler */
volatile bool terminate = false;/* will be set to TRUE in our signal handler */
char *httpd_root; /* where static HTML content is located */
int httpd_port; /* TCP port the webserver should listen to */
int fnordlicht_fd; /* file descriptor for serial port */
@ -34,7 +61,7 @@ struct rgb_color_t parse_color(const char *identifier) {
}
void quit(int sig) {
shutdown = true;
terminate = true;
}
const char * get_filename_ext(char *filename) {
@ -175,8 +202,21 @@ int handle_request(void *cls, struct MHD_Connection *connection, const char *url
msg.fade_rgb.delay = (delay) ? atoi(delay) : 0;
msg.fade_rgb.color = current;
}
else if (strcmp(url+1, "start") == 0) { // TODO not working: padding bytes in program_params_t?
else if (strcmp(url+1, "start") == 0) {
/* parameters */
const char *script = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "script");
const char *step = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "step");
const char *delay = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "delay");
const char *sleep = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "sleep");
const char *value = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "value");
const char *saturation = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "saturation");
const char *use_address = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "use_address");
const char *wait_for_fade = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "wait_for_fade");
const char *min_distance = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "min_distance");
msg.cmd = REMOTE_CMD_START_PROGRAM;
msg.start_program.script = (uint8_t) atoi(script);
@ -185,28 +225,28 @@ int handle_request(void *cls, struct MHD_Connection *connection, const char *url
printf("Start program: colorwheel\n");
msg.start_program.params.colorwheel.hue_start = 0;
msg.start_program.params.colorwheel.hue_step = 60;
msg.start_program.params.colorwheel.add_addr = 0;
msg.start_program.params.colorwheel.saturation = 255;
msg.start_program.params.colorwheel.value = 255;
msg.start_program.params.colorwheel.add_addr = (use_address) ? atoi(use_address) : 1;
msg.start_program.params.colorwheel.saturation = (saturation) ? atoi(saturation) : 255;
msg.start_program.params.colorwheel.value = (value) ? atoi(value) : 255;
msg.start_program.params.colorwheel.fade_step = 1;
msg.start_program.params.colorwheel.fade_delay = 2;
msg.start_program.params.colorwheel.fade_sleep = 0;
msg.start_program.params.colorwheel.fade_step = (step) ? atoi(step) : 1;
msg.start_program.params.colorwheel.fade_delay = (delay) ? atoi(delay) : 2;
msg.start_program.params.colorwheel.fade_sleep = (sleep) ? atoi(sleep) : 0;
break;
case 1:
printf("Start program: random\n");
msg.start_program.params.random.seed = (uint16_t) (rand() % 0xffff);
msg.start_program.params.random.use_address = 0;
msg.start_program.params.random.wait_for_fade = 1;
msg.start_program.params.random.min_distance = 60;
msg.start_program.params.random.use_address = (use_address) ? atoi(use_address) : 1;
msg.start_program.params.random.wait_for_fade = (wait_for_fade) ? atoi(wait_for_fade) : 1;
msg.start_program.params.random.min_distance = (min_distance) ? atoi(min_distance) : 60;
msg.start_program.params.random.saturation = 255;
msg.start_program.params.random.value = 255;
msg.start_program.params.random.saturation = (saturation) ? atoi(saturation) : 255;
msg.start_program.params.random.value = (value) ? atoi(value) : 255;
msg.start_program.params.random.fade_step = 1;
msg.start_program.params.random.fade_delay = 2;
msg.start_program.params.random.fade_sleep = 3;
msg.start_program.params.random.fade_step = (step) ? atoi(step) : 25;
msg.start_program.params.random.fade_delay = (delay) ? atoi(delay) : 3;
msg.start_program.params.random.fade_sleep = (sleep) ? atoi(sleep) : 10;
break;
default:
@ -277,7 +317,7 @@ int main(int argc, char *argv[]) {
srand(time(0));
/* start embedded HTTPd */
httpd_port = (argc == 4) ? atoi(argv[3]) : 80; /* default port */
httpd_port = (argc == 4) ? atoi(argv[3]) : 80; /* default port */
httpd_root = argv[2];
printf("Starting HTTPd on port: %i with root dir: %s\n", httpd_port, httpd_root);
@ -296,7 +336,7 @@ int main(int argc, char *argv[]) {
}
/* busy loop */
while (!shutdown) { }
while (!terminate) { }
/* stop embedded HTTPd */
MHD_stop_daemon(httpd);

View file

@ -1,3 +1,28 @@
/**
* fnordlicht C library
*
* @copyright 2013 Steffen Vogel
* @license http://www.gnu.org/licenses/gpl.txt GNU Public License
* @author Steffen Vogel <post@steffenvogel.de>
* @link http://www.steffenvogel.de
*/
/*
* This file is part of libfn
*
* libfn 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
* any later version.
*
* libfn 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 libfn. If not, see <http://www.gnu.org/licenses/>.
*/
#include <unistd.h>
#include <sys/ioctl.h>
#include <stdio.h>
@ -66,7 +91,7 @@ uint8_t fn_count_devices(int fd) {
msg.address = 0;
msg.cmd = REMOTE_CMD_PULL_INT;
msg.pull_int.delay = 1;
msg.pull_int.delay = 1; /* 50ms */
while (1) {
fn_send(fd, (struct remote_msg_t *) &msg);

View file

@ -1,3 +1,28 @@
/**
* fnordlicht C library header
*
* @copyright 2013 Steffen Vogel
* @license http://www.gnu.org/licenses/gpl.txt GNU Public License
* @author Steffen Vogel <post@steffenvogel.de>
* @link http://www.steffenvogel.de
*/
/*
* This file is part of libfn
*
* libfn 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
* any later version.
*
* libfn 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 libfn. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LIBFN_H
#define LIBFN_H