minor improvement and fixes
This commit is contained in:
parent
031c989ec6
commit
11995c1585
3 changed files with 34 additions and 31 deletions
54
src/fnpom.c
54
src/fnpom.c
|
@ -18,20 +18,20 @@ char * http_get_body(char * response) {
|
|||
if (body[0] == '\r' && body[1] == '\n' && body[2] == '\r' && body[3] == '\n') {
|
||||
return body + 4;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct rgb_color_t calc_gradient(double quota, struct rgb_color_t start, struct rgb_color_t end) {
|
||||
struct rgb_color_t gradient = start;
|
||||
|
||||
|
||||
gradient.red = (end.red - start.red) * quota;
|
||||
gradient.green = (end.green - start.green) * quota;
|
||||
gradient.blue = (end.blue - start.blue) * quota;
|
||||
|
||||
|
||||
return gradient;
|
||||
}
|
||||
|
||||
|
@ -54,7 +54,7 @@ char * http_get(char * response, size_t bytes, char * host, char * port, char *
|
|||
perror(host);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
|
||||
/* send request */
|
||||
sprintf(request, "GET %s HTTP/1.0\r\n\r\n", path);
|
||||
send(sd, request, strlen(request), 0);
|
||||
|
@ -67,18 +67,18 @@ char * http_get(char * response, size_t bytes, char * host, char * port, char *
|
|||
}
|
||||
/* close socket */
|
||||
close(sd);
|
||||
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
char host[] = "volkszaehler.org";
|
||||
char port[] = "80";
|
||||
char path[] = "/demo/backend.php/data";
|
||||
char path[] = "/demo/middleware.php/data";
|
||||
char uuid[] = "12345678-1234-1234-1234-123456789012";
|
||||
char device[] = "/dev/ttyUSB0";
|
||||
char response[8192], url[1024], * json;
|
||||
|
||||
|
||||
int debug = 1, fd;
|
||||
|
||||
struct json_tokener * json_tok;
|
||||
|
@ -87,57 +87,59 @@ int main(int argc, char * argv[]) {
|
|||
struct rgb_color_t start = {0, 0xff, 0};
|
||||
struct rgb_color_t end = {0xff, 0, 0};
|
||||
|
||||
/* build url path */
|
||||
/* build url path */
|
||||
sprintf(url, "%s/%s.json?from=last%%20year&tuples=1", path, uuid); /* store request uri in buffer */
|
||||
|
||||
|
||||
if (debug) printf("url: http://%s:%s%s\n", host, port, url);
|
||||
|
||||
|
||||
/* get json */
|
||||
http_get(response, sizeof(response), host, port, url); /* fetch json data to buffer */
|
||||
json = http_get_body(response); /* get pointer to http body */
|
||||
|
||||
/* parse json */
|
||||
/* parse json */
|
||||
json_tok = json_tokener_new();
|
||||
json_obj = json_tokener_parse_ex(json_tok, json, strlen(json));
|
||||
|
||||
|
||||
if (json_tok->err != json_tokener_success) {
|
||||
fprintf(stderr, "failed to parse json: %s\n", json_tokener_errors[json_tok->err]);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
|
||||
if (debug) printf("%s\n", json_object_to_json_string(json_obj));
|
||||
|
||||
|
||||
json_obj = json_object_object_get(json_obj, "data");
|
||||
|
||||
|
||||
double last = json_object_get_double(json_object_object_get(json_obj, "last"));
|
||||
double max = json_object_get_double(json_object_object_get(json_object_object_get(json_obj, "max"), "value"));
|
||||
double min = json_object_get_double(json_object_object_get(json_object_object_get(json_obj, "min"), "value"));
|
||||
|
||||
|
||||
/* calc quota and color for lighting */
|
||||
double quota = (last - min) / (max - min);
|
||||
|
||||
|
||||
if (debug) {
|
||||
printf("Last value: %.2f\n", last);
|
||||
printf("Min value: %.2f\n", min);
|
||||
printf("Max value: %.2f\n", max);
|
||||
printf("Quota: %d%%\n", (int) (quota*100));
|
||||
}
|
||||
|
||||
|
||||
struct rgb_color_t gradient = calc_gradient(quota, start, end);
|
||||
|
||||
|
||||
if (debug) printf("resulting color: #%0X%0X%0X\n", gradient.red, gradient.green, gradient.blue);
|
||||
|
||||
|
||||
/* fade fnordlichts */
|
||||
if (debug) printf("connect via rs232: %s\n", device);
|
||||
|
||||
|
||||
fd = open(device, O_RDWR | O_NOCTTY);
|
||||
if (fd < 0) {
|
||||
perror(device);
|
||||
exit(-1);
|
||||
}
|
||||
oldtio = fn_init(fd);
|
||||
usleep(25000);
|
||||
fn_sync(fd);
|
||||
|
||||
usleep(25000);
|
||||
|
||||
struct remote_msg_fade_rgb_t fn_cmd = {
|
||||
255, /* address */
|
||||
REMOTE_CMD_FADE_RGB, /* command */
|
||||
|
@ -145,15 +147,15 @@ int main(int argc, char * argv[]) {
|
|||
0, /* delay */
|
||||
gradient /* color */
|
||||
};
|
||||
|
||||
|
||||
if (fn_send(fd, (struct remote_msg_t *) &fn_cmd) < 0) {
|
||||
perror(device);
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
|
||||
/* housekeeping */
|
||||
json_tokener_free(json_tok); /* free json objects */
|
||||
tcsetattr(fd, TCSANOW, &oldtio); /* reset serial port */
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -57,7 +57,7 @@ void show_spectrum(SDL_Surface * dst, complex_t * dft_data) {
|
|||
SDL_Flip(dst);
|
||||
}
|
||||
|
||||
void fade_level(int fd, y level) {
|
||||
void fade_level(int fd, double level) {
|
||||
struct remote_msg_fade_rgb_t fncmd;
|
||||
memset(&fncmd, 0, sizeof(struct remote_msg_t));
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
#include <pulse/gccmacro.h>
|
||||
#include <fftw3.h>
|
||||
|
||||
#define N 2048
|
||||
#define N 1024
|
||||
#define SAMPLING_RATE 44100
|
||||
|
||||
#define MIN_FREQ 70
|
||||
|
@ -27,7 +27,7 @@
|
|||
#define MIN_K (MIN_FREQ*N/SAMPLING_RATE)
|
||||
#define MAX_K (MAX_FREQ*N /SAMPLING_RATE)
|
||||
|
||||
#define LINE_WIDTH 1
|
||||
#define LINE_WIDTH 2
|
||||
#define SCREEN_WIDTH LINE_WIDTH*(MAX_K - MIN_K)
|
||||
#define SCREEN_HEIGHT SCREEN_WIDTH/2
|
||||
#define VUM_HEIGHT SCREEN_HEIGHT/6
|
||||
|
@ -146,7 +146,8 @@ void show_level(SDL_Surface *dst, float level) {
|
|||
|
||||
void fade_level(int fd, double level) {
|
||||
struct remote_msg_fade_rgb_t fn_cmd;
|
||||
struct rgb_color_t rgb = level2color(level);
|
||||
//struct rgb_color_t rgb = level2color(level);
|
||||
struct rgb_color_t rgb = {255, 255, 255};
|
||||
|
||||
memset(&fn_cmd, 0, sizeof (struct remote_msg_t));
|
||||
|
||||
|
@ -253,7 +254,7 @@ 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);
|
||||
fade_level(fd, level);
|
||||
if (level > 0.05) fade_level(fd, level);
|
||||
|
||||
show_spectrum(screen, fft_data);
|
||||
show_level(screen, level);
|
||||
|
|
Loading…
Add table
Reference in a new issue