Introduce __attribute__((unused)) (#defined to UNUSED_ATTR) to mark possibly unused...
[kugel-rb.git] / apps / plugins / euroconverter.c
blobded523acbc5df9f896d907021509f16c78557e36
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2003 Pierre Delore
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "plugin.h"
22 #include "lib/configfile.h"
24 #ifdef HAVE_LCD_CHARCELLS
26 /* Euro converter for the player */
28 Use:
29 + : Digit +1
30 - : Digit -1
31 PLAY : Next digit
32 STOP : Prev digit
33 ON : RESET
34 ON+PLAY : Swap Euro<>Home
35 MENU : Display the Menu
36 Currency -> Allows to choose the currency
37 Exit-> Exit the plugin
39 Notes:
40 I don't use float.
41 I use signed long long (64 bits).
42 A value have 5 digits after the . (123.45 = 12345000)
44 To do:
45 - The Irish currency needs 6 digits after the . to have sufficient precision on big number
48 PLUGIN_HEADER
50 /* Name and path of the config file*/
51 static const char cfg_filename[] = "euroconverter.cfg";
52 #define CFGFILE_VERSION 0 /* Current config file version */
53 #define CFGFILE_MINVERSION 0 /* Minimum config file version to accept */
55 /* typedef for simplifying usage of long long type */
56 typedef long long int longlong_t;
58 /*Pattern for the converter*/
59 static unsigned char pattern_euro[]={0x07, 0x08, 0x1E, 0x10, 0x1E, 0x08, 0x07}; /* € */
60 static unsigned char pattern_home[]={0x04, 0x0A, 0x11, 0x1F, 0x11, 0x11, 0x1F}; /* Home icon*/
62 /* 1 euro = ... (remenber 5 digits after the .)*/
63 static int currency[12]={
64 655957, /*FRF France*/
65 195583, /*DEM Germany*/
66 1376030, /*ATS Austria*/
67 4033990, /*BEF Belgium*/
68 16638600, /*ESP Spain*/
69 594573, /*FIM Finland*/
70 78756, /*IEP Ireland*/
71 193627000, /*ITL Italy*/
72 4033990, /*LUF Luxemburg*/
73 220371, /*NLG Netherlands*/
74 20048200, /*PTE Portugal*/
75 34075100, /*GRD Greece*/
78 /* Number of digit of the currency (for the display) */
79 static int nb_digit[12]={
80 2, /*FRF France*/
81 2, /*DEM Germany*/
82 2, /*ATS Austria*/
83 2, /*BEF Belgium*/
84 0, /*ESP Spain*/
85 2, /*FIM Finland*/
86 2, /*IEP Ireland*/
87 0, /*ITL Italy*/
88 2, /*LUF Luxemburg*/
89 2, /*NLG Netherlands*/
90 0, /*PTE Portugal*/
91 0 /*GRD Greece*/
94 /* max euro to have home currency */
95 static longlong_t max_euro[12]={
96 99999999000LL, /*FRF France 999 999.99 */
97 99999999000LL, /*DEM Germany 999 999.99 */
98 99999999000LL, /*ATS Austria 999 999.99 */
99 99999999000LL, /*BEF Belgium 999 999.99 */
100 99999999000LL, /*ESP Spain 99 999 999 */
101 99999999000LL, /*FIM Finland 999 999.99 */
102 99999999000LL, /*IEP Ireland 999 999.99 */
103 51645690000LL, /*ITL Italy 999 999 999 */
104 99999999000LL, /*LUF Luxemburg 999 999.99 */
105 99999999000LL, /*NLG Netherlands 999 999.99 */
106 99999999000LL, /*PTE Portugal 99 999 999 */
107 29347028000LL /*GRD Greece 99 999 999 */
110 /* max home to have euro currency */
111 /* 92233720300000 Limitation due to the max capacity of long long (2^63)*/
112 static longlong_t max_curr[12]={
113 99999999000LL, /*FRF France 152449.02 */
114 99999999000LL, /*DEM Germany 511291.88 */
115 99999999000LL, /*ATS Austria 72672.83 */
116 99999999000LL, /*BEF Belgium 24789.35 */
117 92233720300000LL,/*ESP Spain 5543358.23 */
118 99999999000LL, /*FIM Finland 168187.92 */
119 9999999900LL, /*IEP Ireland 1269744.51 exact value=1269738.07 */
120 92233720300000LL,/*ITL Italy 476347.41 */
121 99999999000LL, /*LUF Luxemburg 24789.35 */
122 99999999000LL, /*NLG Netherlands 453780.21 */
123 92233720300000LL,/*PTE Portugal 4600598.57 */
124 92233720300000LL /*GRD Greece 2706777.69 */
127 static unsigned char *abbrev_str[12] = {
128 "...FRF...", /*France*/
129 "...DEM...", /*Germany*/
130 "...ATS...", /*Austria*/
131 "...BEF...", /*Belgium*/
132 "...ESP...", /*Spain*/
133 "...FIM...", /*Finland*/
134 "...IEP...", /*Ireland*/
135 "...ITL...", /*Italy*/
136 "...LUF...", /*Luxemburg*/
137 "...NLG...", /*Netherlands*/
138 "...PTE...", /*Portugal*/
139 "...GRD..." /*Greece*/
143 static unsigned long heuro,hhome; /*Handles for the new patterns*/
145 static char *currency_str[12] = {
146 "France",
147 "Germany",
148 "Austria",
149 "Belgium",
150 "Spain",
151 "Finland",
152 "Ireland",
153 "Italy",
154 "Luxemburg",
155 "Netherlands",
156 "Portugal",
157 "Greece"
161 static int country; /*Country selected*/
162 static int cur_pos; /*Cursor position*/
163 static longlong_t inc;
165 /* Persistent settings */
166 static struct configdata config[] = {
167 { TYPE_ENUM, 0, 12, { .int_p = &country }, "country", currency_str }
171 /* 64bits*64 bits with 5 digits after the . */
172 static longlong_t mymul(longlong_t a, longlong_t b)
174 return((a*b)/100000LL);
178 /* 64bits/64 bits with 5 digits after the . */
179 static longlong_t mydiv(longlong_t a, longlong_t b)
181 return((a*100000LL)/b);
185 /* 123.45=12345000 split => i=123 f=45000*/
186 static void split(longlong_t v, longlong_t* i, longlong_t* f)
188 longlong_t t;
190 t=v/100000LL;
191 (*i)=t;
192 (*f)=(v-(t*100000LL));
196 /* result=10^n */
197 static longlong_t pow10(int n)
199 int i;
200 longlong_t r;
202 r=1;
203 for (i=0;i<n;i++)
204 r=r*10LL;
205 return(r);
209 /* round the i.f at n digit after the . */
210 static void round(longlong_t* i, longlong_t* f, int n)
213 longlong_t m;
214 int add=0;
216 m=(int)pow10(5-n-1);
217 if (((*f)/m)%10>=5)
218 add=1;
219 if (n>0)
221 (*f)=((*f)/(int)pow10(5-n))+add;
222 if ((*f)==100LL)
224 (*i)+=1;
225 (*f)=0;
228 else
230 (*i)+=add;
231 (*f)=0;
236 /* Display the imput and the result
237 pos: false : first line
238 : true : second line
240 static void display(longlong_t euro, longlong_t home, bool pos)
242 longlong_t i,f;
243 unsigned char str[20];
244 unsigned char s1[20];
245 unsigned char s2[20];
247 if (pos)
248 { /*Edit the second line*/
249 rb->strcpy(s1," %6d.%02d");
250 if (nb_digit[country]==2)
251 rb->strcpy(s2,"\xee\x84\x90%06d.%02d");
252 else
253 rb->strcpy(s2,"\xee\x84\x90%09d");
255 else
257 rb->strcpy(s1,"\xee\x84\x90%06d.%02d");
258 if (nb_digit[country]==2)
259 rb->strcpy(s2," %6d.%02d");
260 else
261 rb->strcpy(s2," %9d");
264 rb->lcd_remove_cursor();
265 /*First line*/
266 rb->lcd_putc(0,0,heuro);
267 split(euro,&i,&f);
268 if (pos)
269 round(&i,&f,2);
270 rb->snprintf(str,sizeof(str),s1,(int)i,(int)f);
272 if (!pos)
274 rb->lcd_puts(1,0,str);
275 rb->lcd_put_cursor(10-cur_pos,0,0x5F);
277 else
278 rb->lcd_puts_scroll(1,0,str);
280 /*Second line*/
281 rb->lcd_putc(0,1,hhome);
282 split(home,&i,&f);
283 if (!pos)
284 round(&i,&f,nb_digit[country]);
285 rb->snprintf(str,sizeof(str),s2,(int)i,(int)f);
286 if (pos)
288 rb->lcd_puts(1,1,str);
289 rb->lcd_put_cursor(10-cur_pos,1,0x5F);
291 else
292 rb->lcd_puts_scroll(1,1,str);
294 rb->lcd_update();
298 /* Show country Abbreviation */
299 static void show_abbrev(void)
301 rb->splash(HZ*3/4,abbrev_str[country]);
305 /* Save the config to disk */
306 static void save_config(void)
308 configfile_save(cfg_filename, config, 1, CFGFILE_VERSION);
312 /* Load the config from disk */
313 static void load_config(void)
315 configfile_load(cfg_filename, config, 1, CFGFILE_MINVERSION);
319 /*Currency choice*/
320 static void currency_menu(void)
322 int c=country;
324 rb->lcd_clear_display();
325 while (true)
327 rb->lcd_puts(0,0,"Currency:");
328 rb->lcd_puts(0,1,currency_str[c]);
329 rb->lcd_update();
330 switch (rb->button_get(true))
332 case BUTTON_RIGHT|BUTTON_REL:
333 c++;
334 if (c>11)
335 c=0;
336 break;
337 case BUTTON_LEFT|BUTTON_REL:
338 c--;
339 if (c<0)
340 c=11;
341 break;
342 case BUTTON_PLAY|BUTTON_REL:
343 country=c;
344 save_config();
345 return;
346 break;
347 case BUTTON_STOP|BUTTON_REL:
348 return;
354 /* Display the choice menu. */
355 static int euro_menu(void)
357 int c=0;
360 while (true)
362 rb->lcd_clear_display();
363 rb->lcd_puts(0,0," Currency");
364 rb->lcd_puts(0,1," Exit");
365 rb->lcd_putc(0,c,0xe110);
366 rb->lcd_update();
368 switch (rb->button_get(true))
370 case BUTTON_RIGHT|BUTTON_REL:
371 c=1;
372 break;
373 case BUTTON_LEFT|BUTTON_REL:
374 c=0;
375 break;
376 case BUTTON_PLAY|BUTTON_REL:
377 if (c==0)
378 currency_menu();
379 else
380 return 1;
381 break;
382 case BUTTON_STOP|BUTTON_REL:
383 return 0;
389 /* Call when the program end */
390 static void cleanup(UNUSED_ATTR void *parameter)
392 //Restore the old pattern (i don't find another way to do this. An idea?)
393 rb->lcd_unlock_pattern(heuro);
394 rb->lcd_unlock_pattern(hhome);
396 //Clear the screen
397 rb->lcd_clear_display();
398 rb->lcd_update();
402 /* this is the plugin entry point */
403 enum plugin_status plugin_start(UNUSED_ATTR const void* parameter)
405 bool end, pos;
406 longlong_t e,h,old_e,old_h;
407 int button;
409 /* if you don't use the parameter, you can do like
410 this to avoid the compiler warning about it */
412 /*Get the pattern handle*/
413 heuro=rb->lcd_get_locked_pattern();
414 hhome=rb->lcd_get_locked_pattern();
415 rb->lcd_define_pattern(heuro, pattern_euro);
416 rb->lcd_define_pattern(hhome, pattern_home);
418 h=0;
419 e=0;
420 end=false;
421 pos=false;
422 country=0;
423 cur_pos=3;
424 inc=100000;
426 load_config();
428 /*Empty the event queue*/
429 rb->button_clear_queue();
431 display(e,h,false);
432 show_abbrev();
433 display(e,h,false);
435 /*Main loop*/
436 while(end!=true)
438 button = rb->button_get(true);
439 switch (button)
441 case BUTTON_MENU|BUTTON_REL:
442 switch (euro_menu())
444 case 1:
445 end=true;
446 break;
448 if (!pos)
450 if (e>max_euro[country])
451 e=0;
452 cur_pos=3;
454 else
456 if (h>max_curr[country])
457 h=0;
458 if (nb_digit[country]==2)
459 cur_pos=3;
460 else
461 cur_pos=0;
464 display(e,h,pos);
465 break;
467 case BUTTON_ON | BUTTON_PLAY:
468 pos=!pos;
470 case BUTTON_ON | BUTTON_REL:
471 e=0;
472 h=0;
473 if (!pos)
475 cur_pos=3;
476 inc=100000;
478 else
480 inc=100000;
481 if (nb_digit[country]==2)
482 cur_pos=3;
483 else
484 cur_pos=0;
486 show_abbrev();
487 break;
489 case BUTTON_STOP|BUTTON_REL:
490 cur_pos--;
491 if (!pos)
493 if (cur_pos<0)
494 cur_pos=0;
495 if (cur_pos==2)
496 cur_pos=1;
497 if (cur_pos>2)
498 inc=pow10(3+cur_pos-1);
499 else
500 inc=pow10(3+cur_pos);
502 else
504 if (cur_pos<0)
505 cur_pos=0;
506 if (nb_digit[country]==2)
508 if (cur_pos==2)
509 cur_pos=1;
510 if (cur_pos>2)
511 inc=pow10(3+cur_pos-1);
512 else
513 inc=pow10(3+cur_pos);
515 else
516 inc=pow10(5+cur_pos);
519 break;
521 case BUTTON_PLAY|BUTTON_REL:
522 cur_pos++;
523 if (!pos)
525 if (cur_pos>8)
526 cur_pos=8;
527 if (cur_pos==2)
528 cur_pos=3;
529 if (cur_pos>2)
530 inc=pow10(3+cur_pos-1);
531 else
532 inc=pow10(3+cur_pos);
534 else
536 if (cur_pos>8)
537 cur_pos=8;
538 if (nb_digit[country]==2)
540 if (cur_pos==2)
541 cur_pos=3;
542 if (cur_pos>2)
543 inc=pow10(3+cur_pos-1);
544 else
545 inc=pow10(3+cur_pos);
547 else
548 inc=pow10(5+cur_pos);
550 break;
552 case BUTTON_LEFT|BUTTON_REL:
553 case BUTTON_LEFT|BUTTON_REPEAT:
554 if (!pos)
556 e-=inc;
557 if (e<0)
558 e=0;
560 else
562 h-=inc;
563 if (h<0)
564 h=0;
566 break;
568 case BUTTON_RIGHT|BUTTON_REL:
569 case BUTTON_RIGHT|BUTTON_REPEAT:
570 old_e=e;
571 old_h=h;
572 if (!pos)
574 e+=inc;
575 if (e>max_euro[country])
576 e=old_e;
578 else
580 h+=inc;
581 if (h>max_curr[country])
582 h=old_h;
584 break;
586 default:
587 if (rb->default_event_handler_ex(button, euro_exit, NULL)
588 == SYS_USB_CONNECTED)
589 return PLUGIN_USB_CONNECTED;
590 break;
592 /*Display*/
593 if (!pos) /*Euro>home*/
594 h=mymul(e,currency[country]);
595 else /*Home>euro*/
596 e=mydiv(h,currency[country]);
597 display(e,h,pos);
599 euro_exit(NULL);
600 return PLUGIN_OK;
603 #endif