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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
|
/*
* Parses the command from the pipe.
*/
#include "ledd.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
/* Helper functions */
static gchar *parse_getnumber(gchar *str,gint *n);
static gchar *parse_getfloat(gchar *str,gfloat *n);
static GSList *parse_function_opts(gchar *string,gchar **end,gchar **cmd);
/* Functions for actual doing of stuff. */
static gboolean parse_set_constant(options *opts, gint type,
GSList *arglist,gchar *string);
static gboolean parse_set_blink(options *opts, gint type,
GSList *arglist,gchar *string);
static gboolean parse_set_frequency(options *opts, gint type,
GSList *arglist,gchar *string);
static gboolean parse_set_dutycycle(options *opts, gint type,
GSList *arglist,gchar *string);
static gboolean parse_anim(options *opts, gint type,
GSList *arglist, gchar *string);
static gboolean parse_nop(options *opts, gint type,
GSList *arglist, gchar *string);
/* Helper functions for those: */
static GSList *parse_ledlist(options *opts, gchar *str);
static GSList *parse_blinkopts(gchar *str);
static GSList *parse_make_dutycycle(gint total, gfloat min, gfloat max,
gfloat set);
static GSList *parse_make_frequency(gfloat min, gint freq1,
gfloat max, gint freq2, gfloat value);
/* Function list typedefinition */
/* MAXCMDPIECES is the maximum amount of elements allowed in cmd.
* If you need more, feel free to enlarge it. It is used only below
* in the definition. */
#define MAXCMDPIECES 16
typedef struct {
gchar *cmd[MAXCMDPIECES];
gint type;
gint (*function)(options *, gint, GSList *, gchar *);
} function_list;
/*
* List of the functions.
* These are gone through in order (from the top down). When a match is
* found, the function given is called and no further checks are done.
*
* First argument is a list of strings corresponding to the commands to
* be found. Everything is taken literally (but case insensitively),
* except lines beginning with '%':
* %s String, ended by a space (data in GSList is gchar *).
* %d Integer, both positive and negative accepted (gint *).
* %f Float, positive and negative accepted (gfloat *).
* %. End of line. If any options follow, line is rejected.
* The last item MUST be NULL (also if you have a "%." as the last opt).
*
* The second value is passed to the function in the second argument.
* It is so that you can use one function for several purposes.
*
* The third value is the function to be called. This function will be
* given as arguments the options, the second argument of the list (type),
* the list of arguments gathered from the %x's and the remaining list
* of arguments (in that order). The function should not free anything
* in the list and should return FALSE in case of bad arguments, otherwise
* TRUE.
*
* The whole list is ended in a line full of NULL's or 0's.
*/
static function_list parse_function_list[]={
{{"set","%s","on","%.",NULL},
LEDTYPE_ON,parse_set_constant},
{{"set","%s","off","%.",NULL},
LEDTYPE_OFF,parse_set_constant},
{{"set","%s","normal","%.",NULL},
LEDTYPE_NORMAL,parse_set_constant},
{{"set","%s","blink",NULL},
0,parse_set_blink},
/* Accept a few different spellings... */
{{"set","%s","dutycycle","%d","%f","%f","%f","%.",NULL},
0,parse_set_dutycycle},
{{"set","%s","duty_cycle","%d","%f","%f","%f","%.",NULL},
0,parse_set_dutycycle},
{{"set","%s","duty-cycle","%d","%f","%f","%f","%.",NULL},
0,parse_set_dutycycle},
{{"set","%s","duty","cycle","%d","%f","%f","%f","%.",NULL},
0,parse_set_dutycycle},
{{"set","%s","frequency","%f","%d","%f","%d","%f","%.",NULL},
0,parse_set_frequency},
{{"anim",NULL},
0,parse_anim},
{{"nop",NULL},
0,parse_nop},
{{NULL},0,NULL}
};
/*
* Parses a command that is coming in from the pipe.
*/
void parse_pipe_command(options *opts,gchar *string,File *src) {
gchar *str;
gint n;
GSList *list;
gchar *type;
/* For error messages: */
if (src->type==TYPE_SCRIPT)
type="script";
else if (src->type==TYPE_PIPE)
type="pipe";
else
type="boogie-woogie";
str=parse_jumpspace(string);
/* Accept empty strings without a hassle. */
if (str[0]==0)
return;
/* Search for a match. */
for (n=0, list=NULL; parse_function_list[n].cmd[0]; n++) {
list=parse_function_opts(string,&str,
parse_function_list[n].cmd);
if (list!=NULL)
break;
}
if (parse_function_list[n].cmd[0]==NULL) {
/* No match was found. */
g_warning("error parsing command \"%s\" from %s %s",
string,type,src->name);
return;
}
/* We have a match, number n. Call it. */
if (parse_function_list[n].function(opts,parse_function_list[n].type,
list,str)==FALSE) {
/* Function failed. */
g_warning("error parsing command \"%s\" from %s %s",
string,type,src->name);
gslist_free_all(list);
return;
}
/* We're done. */
gslist_free_all(list);
return;
}
/*
* Parses a command list and makes a list of the appropriate pieces of
* string. Returns NULL on error (string doesn't match cmd).
*/
static GSList *parse_function_opts(gchar *string,gchar **end,gchar **cmd) {
gint i;
gchar buf[MAXPIECELENGTH];
gchar *str;
gint *d;
gfloat *f;
GSList *list=NULL; /* This way the first one is always filled too. */
str=string;
for (i=0; cmd[i]!=NULL; i++) {
if (cmd[i][0]=='%') {
switch (cmd[i][1]) {
case 's':
str=parse_getpiece(str,buf);
if (str==NULL) {
gslist_free_all(list);
return NULL;
}
list=g_slist_append(list,g_strdup(buf));
break;
case 'd':
d=g_new(gint,1);
str=parse_getnumber(str,d);
if (str==NULL) {
gslist_free_all(list);
g_free(d);
return NULL;
}
list=g_slist_append(list,d);
break;
case 'f':
f=g_new(gfloat,1);
str=parse_getfloat(str,f);
if (str==NULL) {
gslist_free_all(list);
g_free(f);
return NULL;
}
list=g_slist_append(list,f);
break;
case '.':
str=parse_jumpspace(str);
if (str[0]) {
gslist_free_all(list);
return NULL;
}
break;
default:
G_ERROR("Internal bug!!! Testing in "
"parse_function_opts for \"%s\"!\n",
cmd[i]);
break;
}
} else {
/* Check text matching. */
str=parse_getpiece(str,buf);
if (str==NULL ||
strcasecmp(buf,cmd[i])!=0) {
gslist_free_all(list);
return NULL;
}
}
}
/* We're successful. */
*end=str; /* Mark the ending point. */
/* Check for no argument requested. */
if (list==NULL)
return g_slist_alloc();
return list;
}
/*
* The functions that actually DO something...
*/
/*
* Set a constant setting (in type, value is one of LEDTYPE_xxx) to the
* list of LEDs in the first (and only) argument (list->data).
*/
static gboolean parse_set_constant(options *opts, gint type, GSList *arglist,
gchar *string) {
GSList *ledlist;
GSList *led;
LedControl *cnt;
ledlist=parse_ledlist(opts,(gchar *)arglist->data);
if (ledlist==NULL)
return FALSE;
for (led=ledlist; led; led=g_slist_next(led)) {
if (led->data==NULL)
continue;
cnt=(LedControl *)led->data;
led_free(cnt);
cnt->type=type; /* LED setting is in type. */
}
g_slist_free(ledlist);
return TRUE;
}
/*
* Set a "blink" setting. Blink sequence is totally parsed from string.
* Only argument already parsed is list of LEDs to set.
*/
static gboolean parse_set_blink(options *opts, gint type, GSList *arglist,
gchar *string) {
GSList *list;
GSList *ledlist;
GSList *led;
LedControl *cnt;
ledlist=parse_ledlist(opts,(gchar *)arglist->data);
if (ledlist==NULL)
return FALSE;
list=parse_blinkopts(string);
if (list==NULL) {
g_slist_free(ledlist);
return FALSE;
}
for (led=ledlist; led; led=g_slist_next(led)) {
if (led->data==NULL)
continue;
cnt=(LedControl *)led->data;
led_free(cnt);
/* Set the type */
cnt->type=LEDTYPE_BLINK;
/* Start the timer... */
cnt->timer=g_timer_new();
g_timer_start(cnt->timer);
cnt->mode=TRUE;
/* Copy the list */
cnt->blink=g_slist_copy(list);
}
g_slist_free(list);
return TRUE;
}
/*
* Makes a dutycycle setting. Read the MANUAL on how it works.
* Arguments are already in arglist (LEDs total-time min max set)
*/
static gboolean parse_set_dutycycle(options *opts, gint type,
GSList *arglist,gchar *string) {
GSList *ledlist;
GSList *led;
LedControl *cnt;
GSList *list;
gint total;
gfloat min,max,set;
LedType ledtype;
/* Parse leds */
ledlist=parse_ledlist(opts,(gchar *)arglist->data);
if (ledlist==NULL)
return FALSE;
/* No segfault _should_ be able to happen here...
* All the arguments _should_ be there... */
total=*(gint *)g_slist_nth_data(arglist,1);
min=*(gfloat *)g_slist_nth_data(arglist,2);
max=*(gfloat *)g_slist_nth_data(arglist,3);
set=*(gfloat *)g_slist_nth_data(arglist,4);
list=parse_make_dutycycle(total,min,max,set);
/* Check return */
if (list==NULL) {
g_slist_free(ledlist);
return FALSE;
}
if (((gint)list)<0) {
ledtype=-(gint)list;
list=NULL;
} else {
ledtype=LEDTYPE_BLINK;
}
for (led=ledlist; led; led=g_slist_next(led)) {
if (led->data==NULL)
continue;
cnt=(LedControl *)led->data;
led_free(cnt);
/* Set the type */
cnt->type=ledtype;
/* First on */
cnt->mode=TRUE;
if (ledtype==LEDTYPE_BLINK) {
/* Start the timer... */
cnt->timer=g_timer_new();
g_timer_start(cnt->timer);
/* Copy the list */
cnt->blink=g_slist_copy(list);
}
}
g_slist_free(list); /* Accepts NULL gracefully */
return TRUE;
}
/*
* Makes a frequency setting. Read the MANUAL on how it works.
* Arguments are already in arglist (LEDs min freq1 max freq2 value)
*/
static gboolean parse_set_frequency(options *opts, gint type,
GSList *arglist,gchar *string) {
GSList *ledlist;
GSList *led;
LedControl *cnt;
GSList *list;
gint freq1, freq2;
gfloat min,max,set;
LedType ledtype;
/* Parse leds */
ledlist=parse_ledlist(opts,(gchar *)arglist->data);
if (ledlist==NULL)
return FALSE;
/* No segfault _should_ be able to happen here...
* All the arguments _should_ be there... */
min=*(gfloat *)g_slist_nth_data(arglist,1);
freq1=*(gint *)g_slist_nth_data(arglist,2);
max=*(gfloat *)g_slist_nth_data(arglist,3);
freq2=*(gint *)g_slist_nth_data(arglist,4);
set=*(gfloat *)g_slist_nth_data(arglist,5);
list=parse_make_frequency(min,freq1,max,freq2,set);
/* Check return */
if (list==NULL) {
g_slist_free(ledlist);
return FALSE;
}
if (((gint)list)<0) {
ledtype=-(gint)list;
list=NULL;
} else {
ledtype=LEDTYPE_BLINK;
}
for (led=ledlist; led; led=g_slist_next(led)) {
if (led->data==NULL)
continue;
cnt=(LedControl *)led->data;
led_free(cnt);
/* Set the type */
cnt->type=ledtype;
/* First on */
cnt->mode=TRUE;
if (ledtype==LEDTYPE_BLINK) {
/* Start the timer... */
cnt->timer=g_timer_new();
g_timer_start(cnt->timer);
/* Copy the list */
cnt->blink=g_slist_copy(list);
}
}
g_slist_free(list); /* Accepts NULL gracefully */
return TRUE;
}
/*
* Parses a space-separated list of ncsNCSx and numbers into
* a list of AnimActions types.
*/
static gboolean parse_anim(options *opts, gint type,
GSList *arglist, gchar *string) {
gchar realbuf[MAXPIECELENGTH+1]={0};
gchar *buf=realbuf + 1; /* So that buf[0-1] is OK... */
int i;
GSList *list;
AnimAction *anim;
gchar *str;
gint n;
gboolean met_loop=FALSE;
list=g_slist_alloc();
/* Make the first one a useless timeout (for led_do_your_thing). */
anim=g_new0(AnimAction,1);
anim->type=ANIM_WAIT;
anim->data=-1;
list->data=anim;
str=string;
while ((str=parse_getpiece(str,buf))!=NULL) {
anim=g_new0(AnimAction,1);
if (strcasecmp(buf,"loop")==0) {
/* LOOP CONDITION */
/* Don't allow two LOOPs - assumed later on. */
if (met_loop) {
gslist_free_all(list);
g_free(anim);
return FALSE;
}
met_loop=TRUE;
anim->type=ANIM_LOOP;
} else if (parse_getnumber(buf,&n)!=NULL) {
/* DELAY CONDITION */
anim->type=ANIM_WAIT;
anim->data=n;
} else {
/* SET ON/OFF */
for (i=0; buf[i]; i++) {
if (buf[i]=='x' || buf[i]=='X')
i++;
switch (buf[i]) {
case 'n':
case 'N':
anim->data=LED_N;
break;
case 's':
case 'S':
anim->data=LED_S;
break;
case 'c':
case 'C':
anim->data=LED_C;
break;
default:
gslist_free_all(list);
g_free(anim);
return FALSE;
}
if (isupper(buf[i]))
anim->type=ANIM_ON;
else
anim->type=ANIM_OFF;
/* buf[-1] is OK... */
if (buf[i-1]=='x' || buf[i-1]=='X')
anim->type=ANIM_NORMAL;
if (buf[i+1]) {
g_slist_append(list,anim);
anim=g_new0(AnimAction,1);
}
}
}
g_slist_append(list,anim);
}
/* I guess we're done. Free possible old anim, set defaults
and set this one. */
gslist_free_all(opts->anim);
led_flag_set(&opts->anim_and,&opts->anim_or,LED_S,LEDTYPE_NORMAL);
led_flag_set(&opts->anim_and,&opts->anim_or,LED_C,LEDTYPE_NORMAL);
led_flag_set(&opts->anim_and,&opts->anim_or,LED_N,LEDTYPE_NORMAL);
opts->anim_loop=FALSE;
opts->anim=list;
return TRUE;
}
/*
* Very useful...
*/
static gboolean parse_nop(options *opts, gint type,
GSList *arglist, gchar *str) {
return TRUE;
}
/*
* A few external helper functions.
*/
/*
* Returns pointer to next character in str that is not a space.
*/
gchar *parse_jumpspace(gchar *str) {
gint i;
for (i=0; str[i] && isspace(str[i]); i++)
;
return str+i;
}
/*
* Gets a space-separated piece from str and stores it into buf.
* Returns pointer to str directly after the piece.
* If piece is longer that MAXPIECELENGTH-1, returns NULL.
* If no piece can be found, returns NULL.
*/
gchar *parse_getpiece(gchar *str,gchar *buf) {
gint i;
str=parse_jumpspace(str);
if (str[0]==0)
return NULL;
for (i=0; i<(MAXPIECELENGTH-1) && str[i] && !isspace(str[i]); i++)
buf[i]=str[i];
if (i>=(MAXPIECELENGTH-1))
return NULL;
buf[i]=0;
return str+i;
}
/*
* Returns TRUE if str contains only whitespace etc. If str==NULL, returns
* TRUE.
*/
gboolean parse_eol(gchar *str) {
gint i;
if (str==NULL)
return TRUE;
for (i=0; str[i]; i++)
if (!isspace(str[i]))
return FALSE;
return TRUE;
}
/*
* A few internal helper functions:
*/
/*
* Gets a space-separated non-negative number from str and stores it in *n.
* Returns pointer to str directly after the piece.
* If string is not a number returns NULL.
*/
static gchar *parse_getnumber(gchar *str,gint *n) {
gint mul=1;
*n=0;
str=parse_jumpspace(str);
if (str[0]=='+')
str++;
else if (str[0]=='-') {
mul=-1;
str++;
}
if (!str[0])
return NULL;
for (; str[0] && !isspace(str[0]); str++) {
if (!isdigit(str[0]))
return NULL;
*n=*n*10+mul*(str[0]-'0');
}
return str;
}
/*
* Gets a space-separated floating point number from str and stores it
* in *n. If string is not a number returns NULL.
*/
static gchar *parse_getfloat(gchar *str,gfloat *n) {
gfloat d;
gint sign=1;
*n=0;
/* Check for negative number */
str=parse_jumpspace(str);
if (str[0]==0)
return NULL;
if (str[0]=='-') {
sign=-1;
str++;
}
for (; str[0] && str[0]!='.' && str[0]!=',' &&
!isspace(str[0]); str++) {
if (!isdigit(str[0]))
return NULL;
*n=*n*10+str[0]-'0';
}
if (isspace(str[0]) || str[0]==0) {
if (sign<0)
*n=-*n;
return str;
}
/* Contains '.' or ',' */
str++;
d=10;
for (; str[0] && !isspace(str[0]); str++) {
if (!isdigit(str[0]))
return NULL;
*n=*n+((gfloat)(str[0]-'0')/d);
d*=10;
}
if (sign<0)
*n=-*n;
return str;
}
/*
* Parses the first option field for a "set" command (affected led list).
* Returns a GSList containing pointers to the affected structs.
* Returns NULL on parse error.
*/
static GSList *parse_ledlist(options *opts,gchar *str) {
gint i;
LedName led;
gint priority;
GSList *list;
list=g_slist_alloc();
for (i=0; str[i]; i++) {
priority=0;
switch (str[i]) {
case 's':
case 'S':
led=LED_S;
break;
case 'n':
case 'N':
led=LED_N;
break;
case 'c':
case 'C':
led=LED_C;
break;
default:
g_slist_free(list);
return NULL;
}
if (isdigit(str[i+1]) && (str[i+1]-'0'<=PRIORITY_MAX)) {
i++;
priority=str[i]-'0';
}
g_slist_append(list,opts->leds[led][priority]);
}
return list;
}
/*
* Parses a list of times for light-flashing. Returns a new GSList.
*/
static GSList *parse_blinkopts(gchar *str) {
GSList *list=NULL;
gint n;
while (1) {
str=parse_jumpspace(str);
if (str[0]==0) { /* End of line */
return list;
}
str=parse_getnumber(str,&n);
if (str==NULL || n<0) {
g_slist_free(list);
return NULL;
}
list=g_slist_append(list,GINT_TO_POINTER(n));
} /* Never gets beyond this. */
}
/*
* Makes a GSList as a dutycycle for the given settings.
*
* If set < min, then LED is totally off.
* If set > max, then LED is totally on.
* Otherwise, LED is on total-time * (set - min) / (max - min), off
* the rest of total-time.
*
* If min > max, then everything is swapped around (the larger set gets,
* the less time LED is on).
*
* Returns:
* NULL - Error occurred
* -n - Set type to n.
* n - Pointer to newly allocated GSList.
*/
static GSList *parse_make_dutycycle(gint total, gfloat min, gfloat max,
gfloat set) {
gfloat a,b;
gfloat tmp;
GSList *list;
/* Sanity check */
if (total<=0)
return NULL;
if (((max-min)==0) || (max==min)) /* Both just to be sure (floats) */
return NULL;
/* All values sane. Just do it. */
if (max<min) {
/* Flip everything around (max+min)/2 = d.
* Then set will be
* -(x-d)+d = 2*d-x = max+min-x
*/
tmp=max;
max=min;
min=tmp;
set=max+min-set;
}
/* Calc the times */
a=(gfloat)total*((gfloat)(set-min)/(max-min));
b=(gfloat)total*((gfloat)(max-set)/(max-min));
/* Check for ON and OFF */
if (a<1) /* ON time low */
return (GSList *)GINT_TO_POINTER(-LEDTYPE_OFF);
if (b<1) /* OFF time low */
return (GSList *)GINT_TO_POINTER(-LEDTYPE_ON);
/* Were going to BLINK */
list=NULL;
list=g_slist_append(list,GINT_TO_POINTER((gint)a));
list=g_slist_append(list,GINT_TO_POINTER((gint)b));
return list;
}
/*
* Makes a GSList as a frequency for the given settings.
*
* If value < min, then LED is totally OFF.
* If value > max, then LED is blinking freq1/freq1.
* Otherwise frequency of blinking is linearly interpolated between them.
*
* Returns:
* NULL - Error occurred
* -n - Set type to n.
* n - Pointer to newly allocated GSList.
*/
static GSList *parse_make_frequency(gfloat min, gint freq1,
gfloat max, gint freq2, gfloat value) {
GSList *list;
gint t;
/* Sanity check */
if (freq1<=0 || freq2<=0)
return NULL;
if (((max-min)==0) || (max<=min)) /* Both just to be sure (floats) */
return NULL;
/* Yes it would!
* if (freq1<freq2)
* return NULL;
*/
/* All values sane. Just do it. */
/* Check for OFF */
if (value<min)
return (GSList *)GINT_TO_POINTER(-LEDTYPE_OFF);
/* Calc the time (as 1/freq) */
t=((gfloat)(value-min)*freq2+(gfloat)(max-value)*freq1)/(max-min);
if (value>=max)
t=freq2;
/* We're going to BLINK */
list=NULL;
list=g_slist_append(list,GINT_TO_POINTER((gint)t));
/* Unnecessary: */
/* list=g_slist_append(list,GINT_TO_POINTER((gint)t)); */
return list;
}
|