Manual: change wording slightly in the USB charging section.
[kugel-rb.git] / apps / plugins / euroconverter.c
blobd7aa61f806fb7cd8b953057efb75d7766a278765
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"
23 #include "lib/pluginlib_exit.h"
25 /* Euro converter for the player */
27 Use:
28 + : Digit +1
29 - : Digit -1
30 PLAY : Next digit
31 STOP : Prev digit
32 ON : RESET
33 ON+PLAY : Swap Euro<>Home
34 MENU : Display the Menu
35 Currency -> Allows to choose the currency
36 Exit-> Exit the plugin
38 Notes:
39 I don't use float.
40 I use signed long long (64 bits).
41 A value have 5 digits after the . (123.45 = 12345000)
43 To do:
44 - The Irish currency needs 6 digits after the . to have sufficient precision on big number
49 /* Name and path of the config file*/
50 static const char cfg_filename[] = "euroconverter.cfg";
51 #define CFGFILE_VERSION 0 /* Current config file version */
52 #define CFGFILE_MINVERSION 0 /* Minimum config file version to accept */
54 /* typedef for simplifying usage of long long type */
55 typedef long long int longlong_t;
57 /*Pattern for the converter*/
58 static unsigned char pattern_euro[]={0x07, 0x08, 0x1E, 0x10, 0x1E, 0x08, 0x07}; /* € */
59 static unsigned char pattern_home[]={0x04, 0x0A, 0x11, 0x1F, 0x11, 0x11, 0x1F}; /* Home icon*/
61 /* 1 euro = ... (remenber 5 digits after the .)*/
62 static int currency[12]={
63 655957, /*FRF France*/
64 195583, /*DEM Germany*/
65 1376030, /*ATS Austria*/
66 4033990, /*BEF Belgium*/
67 16638600, /*ESP Spain*/
68 594573, /*FIM Finland*/
69 78756, /*IEP Ireland*/
70 193627000, /*ITL Italy*/
71 4033990, /*LUF Luxemburg*/
72 220371, /*NLG Netherlands*/
73 20048200, /*PTE Portugal*/
74 34075100, /*GRD Greece*/
77 /* Number of digit of the currency (for the display) */
78 static int nb_digit[12]={
79 2, /*FRF France*/
80 2, /*DEM Germany*/
81 2, /*ATS Austria*/
82 2, /*BEF Belgium*/
83 0, /*ESP Spain*/
84 2, /*FIM Finland*/
85 2, /*IEP Ireland*/
86 0, /*ITL Italy*/
87 2, /*LUF Luxemburg*/
88 2, /*NLG Netherlands*/
89 0, /*PTE Portugal*/
90 0 /*GRD Greece*/
93 /* max euro to have home currency */
94 static longlong_t max_euro[12]={
95 99999999000LL, /*FRF France 999 999.99 */
96 99999999000LL, /*DEM Germany 999 999.99 */
97 99999999000LL, /*ATS Austria 999 999.99 */
98 99999999000LL, /*BEF Belgium 999 999.99 */
99 99999999000LL, /*ESP Spain 99 999 999 */
100 99999999000LL, /*FIM Finland 999 999.99 */
101 99999999000LL, /*IEP Ireland 999 999.99 */
102 51645690000LL, /*ITL Italy 999 999 999 */
103 99999999000LL, /*LUF Luxemburg 999 999.99 */
104 99999999000LL, /*NLG Netherlands 999 999.99 */
105 99999999000LL, /*PTE Portugal 99 999 999 */
106 29347028000LL /*GRD Greece 99 999 999 */
109 /* max home to have euro currency */
110 /* 92233720300000 Limitation due to the max capacity of long long (2^63)*/
111 static longlong_t max_curr[12]={
112 99999999000LL, /*FRF France 152449.02 */
113 99999999000LL, /*DEM Germany 511291.88 */
114 99999999000LL, /*ATS Austria 72672.83 */
115 99999999000LL, /*BEF Belgium 24789.35 */
116 92233720300000LL,/*ESP Spain 5543358.23 */
117 99999999000LL, /*FIM Finland 168187.92 */
118 9999999900LL, /*IEP Ireland 1269744.51 exact value=1269738.07 */
119 92233720300000LL,/*ITL Italy 476347.41 */
120 99999999000LL, /*LUF Luxemburg 24789.35 */
121 99999999000LL, /*NLG Netherlands 453780.21 */
122 92233720300000LL,/*PTE Portugal 4600598.57 */
123 92233720300000LL /*GRD Greece 2706777.69 */
126 static unsigned char *abbrev_str[12] = {
127 "...FRF...", /*France*/
128 "...DEM...", /*Germany*/
129 "...ATS...", /*Austria*/
130 "...BEF...", /*Belgium*/
131 "...ESP...", /*Spain*/
132 "...FIM...", /*Finland*/
133 "...IEP...", /*Ireland*/
134 "...ITL...", /*Italy*/
135 "...LUF...", /*Luxemburg*/
136 "...NLG...", /*Netherlands*/
137 "...PTE...", /*Portugal*/
138 "...GRD..." /*Greece*/
142 static unsigned long heuro,hhome; /*Handles for the new patterns*/
144 static char *currency_str[12] = {
145 "France",
146 "Germany",
147 "Austria",
148 "Belgium",
149 "Spain",
150 "Finland",
151 "Ireland",
152 "Italy",
153 "Luxemburg",
154 "Netherlands",
155 "Portugal",
156 "Greece"
160 static int country; /*Country selected*/
161 static int cur_pos; /*Cursor position*/
162 static longlong_t inc;
164 /* Persistent settings */
165 static struct configdata config[] = {
166 { TYPE_ENUM, 0, 12, { .int_p = &country }, "country", currency_str }
170 /* 64bits*64 bits with 5 digits after the . */
171 static longlong_t mymul(longlong_t a, longlong_t b)
173 return((a*b)/100000LL);
177 /* 64bits/64 bits with 5 digits after the . */
178 static longlong_t mydiv(longlong_t a, longlong_t b)
180 return((a*100000LL)/b);
184 /* 123.45=12345000 split => i=123 f=45000*/
185 static void split(longlong_t v, longlong_t* i, longlong_t* f)
187 longlong_t t;
189 t=v/100000LL;
190 (*i)=t;
191 (*f)=(v-(t*100000LL));
195 /* result=10^n */
196 static longlong_t pow10(int n)
198 int i;
199 longlong_t r;
201 r=1;
202 for (i=0;i<n;i++)
203 r=r*10LL;
204 return(r);
208 /* round the i.f at n digit after the . */
209 static void round(longlong_t* i, longlong_t* f, int n)
212 longlong_t m;
213 int add=0;
215 m=(int)pow10(5-n-1);
216 if (((*f)/m)%10>=5)
217 add=1;
218 if (n>0)
220 (*f)=((*f)/(int)pow10(5-n))+add;
221 if ((*f)==100LL)
223 (*i)+=1;
224 (*f)=0;
227 else
229 (*i)+=add;
230 (*f)=0;
235 /* Display the imput and the result
236 pos: false : first line
237 : true : second line
239 static void display(longlong_t euro, longlong_t home, bool pos)
241 longlong_t i,f;
242 unsigned char str[20];
243 unsigned char s1[20];
244 unsigned char s2[20];
246 if (pos)
247 { /*Edit the second line*/
248 rb->strcpy(s1," %6d.%02d");
249 if (nb_digit[country]==2)
250 rb->strcpy(s2,"\xee\x84\x90%06d.%02d");
251 else
252 rb->strcpy(s2,"\xee\x84\x90%09d");
254 else
256 rb->strcpy(s1,"\xee\x84\x90%06d.%02d");
257 if (nb_digit[country]==2)
258 rb->strcpy(s2," %6d.%02d");
259 else
260 rb->strcpy(s2," %9d");
263 rb->lcd_remove_cursor();
264 /*First line*/
265 rb->lcd_putc(0,0,heuro);
266 split(euro,&i,&f);
267 if (pos)
268 round(&i,&f,2);
269 rb->snprintf(str,sizeof(str),s1,(int)i,(int)f);
271 if (!pos)
273 rb->lcd_puts(1,0,str);
274 rb->lcd_put_cursor(10-cur_pos,0,0x5F);
276 else
277 rb->lcd_puts_scroll(1,0,str);
279 /*Second line*/
280 rb->lcd_putc(0,1,hhome);
281 split(home,&i,&f);
282 if (!pos)
283 round(&i,&f,nb_digit[country]);
284 rb->snprintf(str,sizeof(str),s2,(int)i,(int)f);
285 if (pos)
287 rb->lcd_puts(1,1,str);
288 rb->lcd_put_cursor(10-cur_pos,1,0x5F);
290 else
291 rb->lcd_puts_scroll(1,1,str);
293 rb->lcd_update();
297 /* Show country Abbreviation */
298 static void show_abbrev(void)
300 rb->splash(HZ*3/4,abbrev_str[country]);
304 /* Save the config to disk */
305 static void save_config(void)
307 configfile_save(cfg_filename, config, 1, CFGFILE_VERSION);
311 /* Load the config from disk */
312 static void load_config(void)
314 configfile_load(cfg_filename, config, 1, CFGFILE_MINVERSION);
318 /*Currency choice*/
319 static void currency_menu(void)
321 int c=country;
323 rb->lcd_clear_display();
324 while (true)
326 rb->lcd_puts(0,0,"Currency:");
327 rb->lcd_puts(0,1,currency_str[c]);
328 rb->lcd_update();
329 switch (rb->button_get(true))
331 case BUTTON_RIGHT|BUTTON_REL:
332 c++;
333 if (c>11)
334 c=0;
335 break;
336 case BUTTON_LEFT|BUTTON_REL:
337 c--;
338 if (c<0)
339 c=11;
340 break;
341 case BUTTON_PLAY|BUTTON_REL:
342 country=c;
343 save_config();
344 return;
345 break;
346 case BUTTON_STOP|BUTTON_REL:
347 return;
353 /* Display the choice menu. */
354 static int euro_menu(void)
356 int c=0;
359 while (true)
361 rb->lcd_clear_display();
362 rb->lcd_puts(0,0," Currency");
363 rb->lcd_puts(0,1," Exit");
364 rb->lcd_putc(0,c,0xe110);
365 rb->lcd_update();
367 switch (rb->button_get(true))
369 case BUTTON_RIGHT|BUTTON_REL:
370 c=1;
371 break;
372 case BUTTON_LEFT|BUTTON_REL:
373 c=0;
374 break;
375 case BUTTON_PLAY|BUTTON_REL:
376 if (c==0)
377 currency_menu();
378 else
379 return 1;
380 break;
381 case BUTTON_STOP|BUTTON_REL:
382 return 0;
388 /* Call when the program end */
389 static void euro_exit(void)
391 //Restore the old pattern (i don't find another way to do this. An idea?)
392 rb->lcd_unlock_pattern(heuro);
393 rb->lcd_unlock_pattern(hhome);
395 //Clear the screen
396 rb->lcd_clear_display();
397 rb->lcd_update();
401 /* this is the plugin entry point */
402 enum plugin_status plugin_start(const void* parameter)
404 bool end, pos;
405 longlong_t e,h,old_e,old_h;
406 int button;
408 /* if you don't use the parameter, you can do like
409 this to avoid the compiler warning about it */
410 (void)parameter;
411 atexit(euro_exit);
413 /*Get the pattern handle*/
414 heuro=rb->lcd_get_locked_pattern();
415 hhome=rb->lcd_get_locked_pattern();
416 rb->lcd_define_pattern(heuro, pattern_euro);
417 rb->lcd_define_pattern(hhome, pattern_home);
419 h=0;
420 e=0;
421 end=false;
422 pos=false;
423 country=0;
424 cur_pos=3;
425 inc=100000;
427 load_config();
429 /*Empty the event queue*/
430 rb->button_clear_queue();
432 display(e,h,false);
433 show_abbrev();
434 display(e,h,false);
436 /*Main loop*/
437 while(end!=true)
439 button = rb->button_get(true);
440 switch (button)
442 case BUTTON_MENU|BUTTON_REL:
443 switch (euro_menu())
445 case 1:
446 end=true;
447 break;
449 if (!pos)
451 if (e>max_euro[country])
452 e=0;
453 cur_pos=3;
455 else
457 if (h>max_curr[country])
458 h=0;
459 if (nb_digit[country]==2)
460 cur_pos=3;
461 else
462 cur_pos=0;
465 display(e,h,pos);
466 break;
468 case BUTTON_ON | BUTTON_PLAY:
469 pos=!pos;
471 case BUTTON_ON | BUTTON_REL:
472 e=0;
473 h=0;
474 if (!pos)
476 cur_pos=3;
477 inc=100000;
479 else
481 inc=100000;
482 if (nb_digit[country]==2)
483 cur_pos=3;
484 else
485 cur_pos=0;
487 show_abbrev();
488 break;
490 case BUTTON_STOP|BUTTON_REL:
491 cur_pos--;
492 if (!pos)
494 if (cur_pos<0)
495 cur_pos=0;
496 if (cur_pos==2)
497 cur_pos=1;
498 if (cur_pos>2)
499 inc=pow10(3+cur_pos-1);
500 else
501 inc=pow10(3+cur_pos);
503 else
505 if (cur_pos<0)
506 cur_pos=0;
507 if (nb_digit[country]==2)
509 if (cur_pos==2)
510 cur_pos=1;
511 if (cur_pos>2)
512 inc=pow10(3+cur_pos-1);
513 else
514 inc=pow10(3+cur_pos);
516 else
517 inc=pow10(5+cur_pos);
520 break;
522 case BUTTON_PLAY|BUTTON_REL:
523 cur_pos++;
524 if (!pos)
526 if (cur_pos>8)
527 cur_pos=8;
528 if (cur_pos==2)
529 cur_pos=3;
530 if (cur_pos>2)
531 inc=pow10(3+cur_pos-1);
532 else
533 inc=pow10(3+cur_pos);
535 else
537 if (cur_pos>8)
538 cur_pos=8;
539 if (nb_digit[country]==2)
541 if (cur_pos==2)
542 cur_pos=3;
543 if (cur_pos>2)
544 inc=pow10(3+cur_pos-1);
545 else
546 inc=pow10(3+cur_pos);
548 else
549 inc=pow10(5+cur_pos);
551 break;
553 case BUTTON_LEFT|BUTTON_REL:
554 case BUTTON_LEFT|BUTTON_REPEAT:
555 if (!pos)
557 e-=inc;
558 if (e<0)
559 e=0;
561 else
563 h-=inc;
564 if (h<0)
565 h=0;
567 break;
569 case BUTTON_RIGHT|BUTTON_REL:
570 case BUTTON_RIGHT|BUTTON_REPEAT:
571 old_e=e;
572 old_h=h;
573 if (!pos)
575 e+=inc;
576 if (e>max_euro[country])
577 e=old_e;
579 else
581 h+=inc;
582 if (h>max_curr[country])
583 h=old_h;
585 break;
587 default:
588 exit_on_usb(button);
589 break;
591 /*Display*/
592 if (!pos) /*Euro>home*/
593 h=mymul(e,currency[country]);
594 else /*Home>euro*/
595 e=mydiv(h,currency[country]);
596 display(e,h,pos);
598 return PLUGIN_OK;