A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita
audio
rust
zig
deno
mpris
rockbox
mpd
1/* === Rockbox Resistor code/value calculator ===
2[insert relevant/useful information here]
3TODO:
4[ ] Own numeric keypad
5*/
6
7#include "plugin.h"
8#include "lib/display_text.h"
9#include "lib/pluginlib_actions.h"
10#include "lib/picture.h"
11#include "lib/helper.h"
12
13/* Defining player-specific constants */
14
15#if defined(HAVE_LCD_COLOR)
16#define RESISTOR_BMP_X ((LCD_WIDTH - BMPWIDTH_resistor) / 2)
17
18#if LCD_WIDTH >= 320 && LCD_HEIGHT >= 240 /* iPod video or larger */
19#define RESISTOR_BMP_Y 3
20
21#elif LCD_WIDTH >= 240 && LCD_HEIGHT >= 320 /* Onda, mostly */
22#define RESISTOR_BMP_Y 3
23
24#elif LCD_WIDTH >= 220 && LCD_HEIGHT >= 176 /* Fuze or larger */
25#define RESISTOR_BMP_Y 15
26
27#elif LCD_WIDTH >= 176 && LCD_HEIGHT >= 220 /* e200 or larger */
28#define RESISTOR_BMP_Y 11
29
30#elif LCD_WIDTH >= 176 && LCD_HEIGHT >= 132 /* ipod nano or larger */
31#define RESISTOR_BMP_Y 7
32
33#elif LCD_WIDTH >= 160 && LCD_HEIGHT >= 128 /* H10 or larger */
34#define RESISTOR_BMP_Y 3
35
36#elif LCD_WIDTH >= 128 && LCD_HEIGHT >= 128 /* GoGear */
37#define RESISTOR_BMP_Y 3
38
39#else /* Small screens */
40#define RESISTOR_BMP_Y 0
41
42#endif
43
44#else /* HAVE_LCD_COLOR */
45
46#define USE_TEXT_ONLY
47#endif /* HAVE_LCD_COLOR */
48
49#define total_resistance_str_x 1
50#define tolerance_str_x 1
51#define resistance_val_x 1
52#define r_to_c_out_str_x 1
53
54#define INITIAL_TEXT_Y 0
55
56#ifndef USE_TEXT_ONLY
57/* (below is for color targets */
58
59#include "pluginbitmaps/resistor.h"
60
61#define band_width (BMPWIDTH_resistor/15)
62#define band_height (BMPHEIGHT_resistor*9/10)
63
64#define first_band_x (BMPWIDTH_resistor/4 + RESISTOR_BMP_X - band_width/2)
65#define second_band_x (3*BMPWIDTH_resistor/8 + RESISTOR_BMP_X - band_width/2)
66#define third_band_x (BMPWIDTH_resistor/2 + RESISTOR_BMP_X - band_width/2)
67#define fourth_band_x (3*BMPWIDTH_resistor/4 + RESISTOR_BMP_X - band_width/2)
68#define universal_y ((BMPHEIGHT_resistor)/2 - band_height/2)
69
70#endif /* USE_TEXT_ONLY */
71
72enum color {
73 RES_BLACK,
74 RES_BROWN,
75 RES_RED,
76 RES_ORANGE,
77 RES_YELLOW,
78 RES_GREEN,
79 RES_BLUE,
80 RES_VIOLET,
81 RES_GREY,
82 RES_WHITE,
83 RES_GOLD,
84 RES_SILVER,
85 RES_NONE,
86 RES_INVALID = -1,
87};
88
89static int common_values[] = { 0, 1, 10, 15, 22, 27, 33, 39, 47, 51, 68, 82 };
90static int power_ratings[] = { 125, 250, 500, 1000, 2000, 3000, 5000, 10000, 50000 };
91/* All in mW */
92
93#ifndef LCD_RGBPACK
94/* Warning: dirty kludge */
95#define LCD_RGBPACK(x,y,z) 0
96#endif
97
98static struct band_data
99{
100 enum color color;
101 char *name;
102 int color_value;
103 int resistance_value;
104 int multiplier;
105 char *unit;
106 int tolerance;
107} band_data[] =
108{
109 { RES_BLACK, "Black", LCD_RGBPACK(0, 0, 0), 0, 100, "Ohms",-1 },
110 { RES_BROWN, "Brown", LCD_RGBPACK(118, 78, 0), 1, 1000, "Ohms", 1 },
111 { RES_RED, "Red", LCD_RGBPACK(255, 0, 0), 2, 10000, "Ohms", 2 },
112 { RES_ORANGE, "Orange", LCD_RGBPACK(255, 199, 76), 3, 100, "KOhms",-1 },
113 { RES_YELLOW, "Yellow", LCD_RGBPACK(255, 255, 0), 4, 1000, "KOhms",-1 },
114 { RES_GREEN, "Green", LCD_RGBPACK(0, 128, 0), 5, 10000, "KOhms",-1 },
115 { RES_BLUE, "Blue", LCD_RGBPACK(0, 0, 255), 6, 100, "MOhms",-1 },
116 { RES_VIOLET, "Violet", LCD_RGBPACK(153, 51, 255), 7, 1000, "MOhms",-1 },
117 { RES_GREY, "Grey", LCD_RGBPACK(192, 192, 192), 8, 10000, "MOhms",-1 },
118 { RES_WHITE, "White", LCD_RGBPACK(255, 255, 255), 9, 100, "GOhms",-1 },
119 { RES_GOLD, "Gold", LCD_RGBPACK(146, 146, 0), -1, 10, "Ohms", 5 },
120 { RES_SILVER, "Silver", LCD_RGBPACK(213, 213, 213),-1, 1, "Ohms", 10 },
121 { RES_NONE, "[None]", -1 ,-1, -1, 0, 20 }
122};
123
124static char *unit_abbrev;
125
126static struct viewport screen_vp;
127static struct viewport bitmap_vp;
128static struct viewport text_vp;
129static struct screen *display;
130
131static int lineno;
132
133static char *get_power_rating_str(int in_rating)
134{
135 switch(in_rating) {
136 case 125:
137 return "1/8 Watt";
138 case 250:
139 return "1/4 Watt";
140 case 500:
141 return "1/2 Watt";
142 case 1000:
143 return "1 Watt";
144 case 2000:
145 return "2 Watt";
146 case 3000:
147 return "3 Watt";
148 case 5000:
149 return "5 Watt";
150 case 10000:
151 return "10 Watt";
152 case 500000:
153 return "50 Watt";
154 default:
155 return "Unknown";
156 }
157}
158
159static int powi(int num, int exp)
160{
161 int i, product = 1;
162 for (i = 0; i < exp; i++) {
163 product *= num; }
164
165 return product;
166}
167
168static enum color get_band_rtoc(int in_val)
169{
170 int return_color = RES_INVALID;
171 switch(in_val) {
172 case 0:
173 return_color = RES_BLACK;
174 break;
175 case 1:
176 return_color = RES_BROWN;
177 break;
178 case 2:
179 return_color = RES_RED;
180 break;
181 case 3:
182 return_color = RES_ORANGE;
183 break;
184 case 4:
185 return_color = RES_YELLOW;
186 break;
187 case 5:
188 return_color = RES_GREEN;
189 break;
190 case 6:
191 return_color = RES_BLUE;
192 break;
193 case 7:
194 return_color = RES_VIOLET;
195 break;
196 case 8:
197 return_color = RES_GREY;
198 break;
199 case 9:
200 return_color = RES_WHITE;
201 break;
202 }
203 return return_color;
204}
205
206static char *get_tolerance_str(enum color color)
207{
208 static char tolerance_str [14];
209 rb->snprintf(tolerance_str, sizeof(tolerance_str), "%d%% tolerance",
210 band_data[color].tolerance);
211 return tolerance_str;
212}
213
214#ifndef USE_TEXT_ONLY
215static void draw_resistor(enum color firstband_color,
216 enum color secondband_color,
217 enum color thirdband_color,
218 enum color fourthband_color)
219{
220 unsigned int fg;
221
222 rb->lcd_clear_display();
223 display->set_viewport(&bitmap_vp);
224 rb->lcd_bitmap_transparent(resistor, RESISTOR_BMP_X, 0,
225 BMPWIDTH_resistor, BMPHEIGHT_resistor);
226
227 fg = rb->lcd_get_foreground();
228
229 if(firstband_color != RES_NONE) {
230 rb->lcd_set_foreground(band_data[firstband_color].color_value);
231 rb->lcd_fillrect(first_band_x, universal_y, band_width, band_height);
232 } else {
233 rb->lcd_set_foreground(LCD_BLACK);
234 rb->lcd_drawrect(first_band_x, universal_y, band_width, band_height);
235 }
236
237 if(secondband_color != RES_NONE) {
238 rb->lcd_set_foreground(band_data[secondband_color].color_value);
239 rb->lcd_fillrect(second_band_x, universal_y, band_width, band_height);
240 } else {
241 rb->lcd_set_foreground(LCD_BLACK);
242 rb->lcd_drawrect(second_band_x, universal_y, band_width, band_height);
243 }
244
245 if(thirdband_color != RES_NONE) {
246 rb->lcd_set_foreground(band_data[thirdband_color].color_value);
247 rb->lcd_fillrect(third_band_x, universal_y, band_width, band_height);
248 } else {
249 rb->lcd_set_foreground(LCD_BLACK);
250 rb->lcd_drawrect(third_band_x, universal_y, band_width, band_height);
251 }
252
253 if(fourthband_color != RES_NONE) {
254 rb->lcd_set_foreground(band_data[fourthband_color].color_value);
255 rb->lcd_fillrect(fourth_band_x, universal_y, band_width, band_height);
256 } else {
257 rb->lcd_set_foreground(LCD_BLACK);
258 rb->lcd_drawrect(fourth_band_x, universal_y, band_width, band_height);
259 }
260
261 rb->lcd_set_foreground(fg);
262
263 rb->lcd_update();
264 return;
265}
266#endif
267
268static void draw_resistor_text(enum color firstband_color,
269 enum color secondband_color,
270 enum color thirdband_color,
271 enum color fourthband_color)
272{
273 char resistance_vals_str[64];
274 display->set_viewport(&text_vp);
275 rb->snprintf(resistance_vals_str, sizeof(resistance_vals_str),
276 "%s - %s - %s - %s", band_data[firstband_color].name,
277 band_data[secondband_color].name,
278 band_data[thirdband_color].name,
279 band_data[fourthband_color].name);
280 rb->lcd_puts_scroll(resistance_val_x, lineno++, resistance_vals_str);
281 rb->lcd_update();
282}
283
284
285static int calculate_resistance(enum color first_band,
286 enum color second_band,
287 enum color third_band)
288{
289 int tens = band_data[first_band].resistance_value;
290 int units = band_data[second_band].resistance_value;
291 int multiplier = band_data[third_band].multiplier;
292 int total_resistance_centiunits = (10 * tens + units ) * multiplier;
293
294 unit_abbrev = band_data[third_band].unit;
295
296 return total_resistance_centiunits;
297}
298
299static enum color do_first_band_menu(void)
300{
301 int band_selection = 0;
302 enum color band_color_selection = 0;
303
304 MENUITEM_STRINGLIST(colors_menu_first, "First band colour:", NULL,
305 "Black", "Brown", "Red", "Orange", "Yellow",
306 "Green", "Blue", "Violet", "Grey", "White");
307 band_selection = rb->do_menu(&colors_menu_first, &band_selection, NULL,
308 false);
309 switch(band_selection) {
310 case 0: /* Black */
311 band_color_selection = RES_BLACK;
312 break;
313 case 1: /* Brown */
314 band_color_selection = RES_BROWN;
315 break;
316 case 2: /* Red */
317 band_color_selection = RES_RED;
318 break;
319 case 3: /* Orange */
320 band_color_selection = RES_ORANGE;
321 break;
322 case 4: /* Yellow */
323 band_color_selection = RES_YELLOW;
324 break;
325 case 5: /* Green */
326 band_color_selection = RES_GREEN;
327 break;
328 case 6: /* Blue */
329 band_color_selection = RES_BLUE;
330 break;
331 case 7: /* Violet */
332 band_color_selection = RES_VIOLET;
333 break;
334 case 8: /* Grey */
335 band_color_selection = RES_GREY;
336 break;
337 case 9: /* White */
338 band_color_selection = RES_WHITE;
339 break;
340 default:
341 band_color_selection = RES_INVALID;
342 break;
343 }
344 return band_color_selection;
345}
346
347static enum color do_second_band_menu(void)
348{
349 int band_selection = 0;
350 enum color band_color_selection = 0;
351
352 MENUITEM_STRINGLIST(colors_menu_second, "Second band colour:", NULL,
353 "Black", "Brown", "Red", "Orange", "Yellow",
354 "Green", "Blue", "Violet", "Grey", "White");
355 band_selection = rb->do_menu(&colors_menu_second, &band_selection, NULL,
356 false);
357 switch(band_selection) {
358 case 0: /* Black */
359 band_color_selection = RES_BLACK;
360 break;
361 case 1: /* Brown */
362 band_color_selection = RES_BROWN;
363 break;
364 case 2: /* Red */
365 band_color_selection = RES_RED;
366 break;
367 case 3: /* Orange */
368 band_color_selection = RES_ORANGE;
369 break;
370 case 4: /* Yellow */
371 band_color_selection = RES_YELLOW;
372 break;
373 case 5: /* Green */
374 band_color_selection = RES_GREEN;
375 break;
376 case 6: /* Blue */
377 band_color_selection = RES_BLUE;
378 break;
379 case 7: /* Violet */
380 band_color_selection = RES_VIOLET;
381 break;
382 case 8: /* Grey */
383 band_color_selection = RES_GREY;
384 break;
385 case 9: /* White */
386 band_color_selection = RES_WHITE;
387 break;
388 default:
389 band_color_selection = RES_INVALID;
390 break;
391 }
392 return band_color_selection;
393}
394
395static enum color do_third_band_menu(void)
396{
397 int band_selection = 0;
398 enum color band_color_selection = 0;
399
400 MENUITEM_STRINGLIST(colors_menu_third, "Third band colour:", NULL,
401 "Black", "Brown", "Red", "Orange", "Yellow",
402 "Green", "Blue", "Violet", "Grey", "White",
403 "Silver", "Gold");
404 band_selection = rb->do_menu(&colors_menu_third, &band_selection, NULL,
405 false);
406 switch(band_selection) {
407 case 0: /* Black */
408 band_color_selection = RES_BLACK;
409 break;
410 case 1: /* Brown */
411 band_color_selection = RES_BROWN;
412 break;
413 case 2: /* Red */
414 band_color_selection = RES_RED;
415 break;
416 case 3: /* Orange */
417 band_color_selection = RES_ORANGE;
418 break;
419 case 4: /* Yellow */
420 band_color_selection= RES_YELLOW;
421 break;
422 case 5: /* Green */
423 band_color_selection = RES_GREEN;
424 break;
425 case 6: /* Blue */
426 band_color_selection = RES_BLUE;
427 break;
428 case 7: /* Violet */
429 band_color_selection = RES_VIOLET;
430 break;
431 case 8: /* Grey */
432 band_color_selection = RES_GREY;
433 break;
434 case 9: /* White */
435 band_color_selection = RES_WHITE;
436 break;
437 case 10: /* Silver */
438 band_color_selection = RES_SILVER;
439 break;
440 case 11: /* Gold */
441 band_color_selection= RES_GOLD;
442 break;
443 default:
444 band_color_selection = RES_INVALID;
445 break;
446 }
447 return band_color_selection;
448}
449
450static enum color do_fourth_band_menu(void)
451{
452 int band_selection = 0;
453 enum color band_color_selection = 0;
454
455 MENUITEM_STRINGLIST(colors_menu_fourth, "Fourth band colour:", NULL,
456 "Gold", "Brown", "Red", "Silver", "(none)");
457 band_selection = rb->do_menu(&colors_menu_fourth, &band_selection, NULL,
458 false);
459 switch(band_selection) {
460 case 0: /* Gold */
461 band_color_selection = RES_GOLD;
462 break;
463 case 1: /* Brown */
464 band_color_selection = RES_BROWN;
465 break;
466 case 2: /* Red */
467 band_color_selection = RES_RED;
468 break;
469 case 3: /* Silver */
470 band_color_selection = RES_SILVER;
471 break;
472 case 4: /* (none) */
473 band_color_selection = RES_NONE;
474 break;
475 default:
476 band_color_selection = RES_INVALID;
477 break;
478 }
479 return band_color_selection;
480}
481
482static void display_helpfile(void)
483{
484 rb->lcd_clear_display();
485 /* some information obtained from wikipedia */
486 static char * helpfile_text[] = {
487 "Resistor Calculator Helpfile", "", "",
488 "About resistors:", "", /* 7 */
489 /* -- */
490 "A", "resistor", "is", "a ", "two-terminal", "electronic",
491 "component", "that", "produces", "a", "voltage", "across", "its",
492 "terminals", "that", "is", "proportional", "to", "the", "electric",
493 "current", "passing", "through", "it", "in", "accordance", "to",
494 "Ohm's", "Law:", "", /* 29 */
495 /* -- */
496 "", "V = IR",
497 "", "I = V/R",
498 "", "and",
499 "", "R = V/I", "", "",
500 "Where", "V", "=", "voltage, ", "I", "=", "current", "(in", "amps)",
501 "and", "R", "=", "resistance", "(measured", "in", "Ohms).", "", "",
502 /* 28 */
503 /* -- */
504 "The", "primary", "characteristics", "of", "a", "resistor", "are",
505 "the", "resistance,", "the", "tolerance,", "and", "the", "maximum",
506 "working", "voltage", "and", "the", "power", "rating.", "At",
507 "this", "time,", "this", "calculator", "only", "utilises", "the",
508 "resistance", "and", "tolerance.", "", "", /* 33 */
509 /* -- */
510 "The", "Ohm", "is", "the", "SI", "unit", "of", "resistance,", "and",
511 "common", "multiples", "of", "that", "include", "the", "kiloohm",
512 "(KOhm", "-", "1x10^3)", "and", "the", "megaohm", "(MOhm",
513 "-", "1x10^6),", "both", "of", "which", "are", "supported", "by",
514 "this", "calculator.", "", "", /* 34 */
515 /* -- */
516 "Resistors", "in", "parallel:", "", /* 4 */
517 /* -- */
518 "1/Rtotal", "=", "1/R1", "+", "1/R2", "...", "+", "1/Rn", "", /* 9*/
519 /* -- */
520 "", "Resistors", "in", "series:", "", /* 5 */
521 /* -- */
522 "Rtotal", "=", "R1", "+", "R2", "...", "+", "Rn", "", /* 9 */
523 /* -- */
524 "", "How to use this calculator", "", /* 3 */
525 /* -- */
526 "This", "calculator", "has", "three", "modes:", "",
527 "Resistance", "to", "colour", "codes,", "",
528 "Colour", "codes", "to", "resistance", "",
529 "and", "LED", "resistance", "calculator", "", "",
530 /* -- */
531 "At", "this", "time", "there", "is", "only", "support", "for",
532 "four-", "band", "resistors.", "", "",
533 /* -- */
534 "In", "Colour", "to", "Resistance", "mode", "use", "the", "menus",
535 "to", "input", "(in", "order)", "the", "bands", "of", "the",
536 "resistor", "for", "which", "you", "would", "like", "to", "know",
537 "the", "resistance.", "", "",
538 /* -- */
539 "In", "Resistance", "to", "Colour", "mode,", "use", "the", "menus",
540 "to", "select", "which", "unit", "to", "use", "(choose", "from", "Ohms,",
541 "KiloOhms", "and", "MegaOhms)", "and", "the", "on-screen", "keyboard",
542 "to", "input", "the", "value", "of", "the", "resistor", "that", "you",
543 "would", "like", "to", "know", "the", "colour", "codes", "of.",
544 "Output", "will", "be", "both", "graphical", "(with", "bands", "of",
545 "the", "resistor", "shown", "in", "their", "corresponding", "colours",
546 "-", "colour", "targets", "only)", "and", "textually.", "","",
547 /* -- */
548 "LED", "resistor", "calculator", "mode", "is", "used", "to", "determine",
549 "the", "resistor", "necessary", "to", "light", "a", "LED", "safely",
550 "at", "a", "given", "voltage.", "First,", "select", "the", "voltage",
551 "that", "the", "LED", "will", "use", "(the", "first", "option", "is",
552 "the", "most", "common", "and", "is", "a", "safe", "guess)", "and", "the",
553 "current", "that", "it", "will", "draw", "(likewise", "with", "the",
554 "first", "option).", "Then", "use", "the", "onscreen", "keyboard", "to",
555 "type", "in", "the", "supply", "voltage", "and,", "if", "selected,",
556 "the", "custom", "foreward", "current.", "",
557 "Disclaimer:", "this",
558 "calculator", "produces", "safe", "estimates,", "but", "use", "your",
559 "own", "judgement", "when", "using", "these", "output", "values.",
560 "Power", "rating", "and", "displayed", "resistance", "are", "rounded",
561 "up", "to", "the", "nearest", "common", "value."
562 };
563 static struct style_text formatting[] = {
564 { 0, TEXT_CENTER|TEXT_UNDERLINE },
565 { 3, TEXT_UNDERLINE },
566 { 159, TEXT_UNDERLINE },
567 LAST_STYLE_ITEM
568 };
569
570 display_text(ARRAYLEN(helpfile_text), helpfile_text, formatting,
571 NULL, true);
572 return;
573}
574
575static void led_resistance_calc(void)
576{
577 backlight_ignore_timeout();
578
579 int voltage_menu_selection, button_press, j, k, l, foreward_current = 0;
580 int fwd_current_selection = 0;
581 bool quit = false;
582 char kbd_buffer [5];
583 char fwd_kbd_buffer [5];
584 int input_voltage, led_voltage = 0;
585
586 int resistance = 0;
587 int rounded_resistance = 0;
588 int temp;
589 int power_rating_in = 0;
590 int rounded_power_rating = 0;
591 int out_int = 0;
592 char current_out_str [16];
593 char true_current_out_str [40];
594 char rounded_resistance_out_str [40];
595 char power_rating_out_str [40];
596
597 int power_ten, first_band_int, second_band_int = 0;
598
599 enum color first_band;
600 enum color second_band;
601 enum color multiplier;
602 enum color fourth_band = RES_NONE;
603
604 rb->lcd_clear_display();
605
606 MENUITEM_STRINGLIST(voltage_menu, "Select LED voltage:", NULL,
607 "2v (Common red, orange)", "1.5v (IR)", "2.1v (Yellow)",
608 "2.2v (Green)", "3.3v (True green, blue, white, UV)",
609 "4.6v (Blue - 430nm)");
610 MENUITEM_STRINGLIST(fwd_current_menu, "Select foreward current:", NULL,
611 "20mA - Most common for 5mm and 3mm LEDs - select if unsure.",
612 "Key in other (only if already known)");
613
614 while(!quit) {
615 int ret;
616 ret = voltage_menu_selection = rb->do_menu(&voltage_menu,
617 &voltage_menu_selection, NULL, false);
618 if(ret<0) break;
619 ret = fwd_current_selection = rb->do_menu(&fwd_current_menu,
620 &fwd_current_selection, NULL, false);
621 if(ret<0) break;
622 rb->lcd_clear_display();
623
624
625 rb->splash(HZ*2, "(First) Input the supply voltage:");
626 memset(kbd_buffer,0,sizeof(kbd_buffer));
627 rb->kbd_input(kbd_buffer, sizeof(kbd_buffer), NULL);
628 input_voltage = rb->atoi(kbd_buffer);
629 if(input_voltage == 0) break;
630
631 if(input_voltage != (int)input_voltage) {
632 input_voltage *= 10;
633 }
634 else { input_voltage *= 100; }
635
636 switch(voltage_menu_selection) {
637 case 0: /* 2v */
638 led_voltage = 200;
639 break;
640 case 1: /* 1.5v */
641 led_voltage = 150;
642 break;
643 case 2: /* 2.1 */
644 led_voltage = 210;
645 break;
646 case 3:
647 led_voltage = 220;
648 break;
649 case 4:
650 led_voltage = 330;
651 break;
652 case 5:
653 led_voltage = 460;
654 break;
655 }
656 switch(fwd_current_selection) {
657 case 0: /* 20mA */
658 foreward_current = 2; /* 20mA * 100 */
659 break;
660 case 1:
661 rb->lcd_clear_display();
662 rb->splash(HZ*2, "Input the foreward current, in mA");
663 memset(fwd_kbd_buffer,0,sizeof(fwd_kbd_buffer));
664 rb->kbd_input(fwd_kbd_buffer, sizeof(fwd_kbd_buffer), NULL);
665 foreward_current = ((rb->atoi(fwd_kbd_buffer))/10);
666 break;
667 }
668
669 if(foreward_current == 0) break;
670
671 rb->lcd_clear_display();
672
673 resistance = (input_voltage - led_voltage) / foreward_current;
674 out_int = resistance;
675
676 int total_common_values = 11;
677 int total_power_values = 9;
678
679 if(led_voltage > input_voltage) {
680 rb->splash(HZ, "Problem: LED voltage is higher than the source.");
681 break;
682 }
683
684 for(j = 0; j < total_common_values; j++) {
685 for(k = 1; k < 5; k++) {
686 if( resistance == (common_values[j] * powi(10, k))) {
687 rounded_resistance = (common_values[j] * powi(10, k));
688 /* perfect match */
689 break;
690 }
691 else if(resistance >= (common_values[j] * powi(10, k)) &&
692 resistance <= (common_values[j+1] * powi(10, k))) {
693 rounded_resistance = (common_values[j+1] * powi(10, k));
694 /* the higher resistance, to be safe */
695 break;
696 }
697 else { break; }
698 }
699 }
700
701 if(rounded_resistance == 0)
702 {
703 rb->splash(HZ, "Problem: Input voltage too high.");
704 break;
705 }
706 power_rating_in = ((input_voltage/100)*(input_voltage/100)*1000 / rounded_resistance);
707 /* in mW */
708 for(l = 0; l < total_power_values; l++) {
709 if((int)power_rating_in == power_ratings[l]) {
710 rounded_power_rating = (power_ratings[l]);
711 break;
712 }
713 else if(power_rating_in >= power_ratings[l] &&
714 power_rating_in <= power_ratings[l+1]) {
715 rounded_power_rating = power_ratings[l+1];
716 break;
717 }
718 else { break; }
719 }
720
721 get_power_rating_str(rounded_power_rating);
722
723 power_ten=0;
724 temp=rounded_resistance;
725 while(temp>=100) {
726 temp/=10;
727 power_ten++;
728 }
729 first_band_int=temp/10;
730 second_band_int=temp%10;
731
732 first_band = get_band_rtoc(first_band_int);
733 second_band = get_band_rtoc(second_band_int);
734 multiplier = get_band_rtoc(power_ten);
735
736 rb->lcd_clear_display();
737 lineno = INITIAL_TEXT_Y;
738#ifndef USE_TEXT_ONLY
739 draw_resistor(first_band, second_band, multiplier, fourth_band);
740#endif
741 draw_resistor_text(first_band, second_band, multiplier, fourth_band);
742
743 rb->snprintf(current_out_str, sizeof(current_out_str), "%d mA",
744 (foreward_current*10));
745
746 rb->snprintf(true_current_out_str, sizeof(true_current_out_str),
747 "Input: %dv, %d Ohms @ %s", (input_voltage/100),
748 out_int, current_out_str);
749 rb->snprintf(rounded_resistance_out_str,
750 sizeof(rounded_resistance_out_str),
751 "Rounded/displayed: [%d %s]", rounded_resistance,
752 band_data[multiplier].unit);
753 rb->snprintf(power_rating_out_str, sizeof(power_rating_out_str),
754 "Recommended: %s or greater",
755 get_power_rating_str(rounded_power_rating));
756
757 display->set_viewport(&text_vp);
758 rb->lcd_puts_scroll(resistance_val_x, lineno++, true_current_out_str);
759 rb->lcd_puts_scroll(resistance_val_x, lineno++, rounded_resistance_out_str);
760 rb->lcd_puts_scroll(resistance_val_x, lineno++, power_rating_out_str);
761
762 rb->lcd_update();
763
764 while ((button_press = rb->button_get(true)) & BUTTON_REL);
765 switch(button_press) {
766 case PLA_SELECT:
767 break;
768 default:
769 quit = true;
770
771 backlight_use_settings();
772
773 break;
774 }
775 }
776 display->set_viewport(&text_vp);
777 rb->lcd_scroll_stop();
778 display->set_viewport(&screen_vp);
779 rb->lcd_clear_display();
780}
781
782
783static void resistance_to_color(void)
784{
785 backlight_ignore_timeout();
786
787 int menu_selection;
788 int menu_selection_tol;
789 int button_press;
790 bool quit = false;
791 char kbd_buffer [10];
792 int kbd_input_int;
793 int temp;
794 int in_resistance_int;
795
796 int power_ten=0;
797 int first_band_int = 0;
798 int second_band_int = 0;
799
800 enum color first_band;
801 enum color second_band;
802 enum color multiplier;
803 enum color fourth_band = 0;
804 enum color units_used = 0;
805
806 char out_str[20];
807
808 memset(kbd_buffer,0,sizeof(kbd_buffer));
809 /* This cleans out the mysterious garbage that appears */
810 rb->lcd_clear_display();
811 rb->splash(HZ/2, "Resistance to Colour");
812 MENUITEM_STRINGLIST(r_to_c_menu, "Select unit to use:", NULL,
813 "Ohms", "Kiloohms (KOhms)", "Megaohms (MOhms)",
814 "Gigaohms (GOhms)");
815 MENUITEM_STRINGLIST(r_to_c_menu_tol, "Tolerance to display:", NULL,
816 "5%", "10%", "1%", "2%", "20%");
817
818 while(!quit) {
819 int ret;
820 ret=menu_selection = rb->do_menu(&r_to_c_menu, &menu_selection,
821 NULL, false);
822 if(ret<0) break;
823
824 rb->kbd_input(kbd_buffer, sizeof(kbd_buffer), NULL);
825 /* As stated above somewhere, we (I) need to make a calculator-like
826 keypad, that keyboard isn't all that fun to use. */
827 ret = rb->do_menu(&r_to_c_menu_tol, &menu_selection_tol,
828 NULL, false);
829 if(ret<0) break;
830
831 switch(menu_selection_tol) {
832 case 0: /* 5% */
833 fourth_band = RES_GOLD;
834 break;
835 case 1: /* 10% */
836 fourth_band = RES_SILVER;
837 break;
838 case 2: /* 1% */
839 fourth_band = RES_BROWN;
840 break;
841 case 3: /* 2% */
842 fourth_band = RES_RED;
843 break;
844 case 4: /* 20% */
845 fourth_band = RES_NONE;
846 break;
847 }
848
849 kbd_input_int = rb->atoi(kbd_buffer);
850 in_resistance_int = kbd_input_int;
851
852 switch(menu_selection) {
853 case 0:
854 power_ten=0;
855 units_used = RES_BLACK;
856 break;
857 case 1: /* KOhms */
858 power_ten=3;
859 units_used = RES_ORANGE;
860 break;
861 case 2: /* MOhms */
862 power_ten=6;
863 units_used = RES_BLUE;
864 break;
865 case 3: /* GOhms */
866 power_ten=9;
867 units_used = RES_WHITE;
868 break;
869 }
870
871 temp=kbd_input_int;
872 while(temp>=100) {
873 temp/=10;
874 power_ten++;
875 }
876 first_band_int=temp/10;
877 second_band_int=temp%10;
878
879 first_band = get_band_rtoc(first_band_int);
880 second_band = get_band_rtoc(second_band_int);
881 multiplier = get_band_rtoc(power_ten);
882
883 if( first_band == RES_INVALID
884 || second_band == RES_INVALID
885 || multiplier == RES_INVALID) {
886 rb->splashf(HZ, "%d %s can not be represented",
887 in_resistance_int,band_data[units_used].unit);
888 return;
889 }
890
891 rb->lcd_clear_display();
892 lineno = INITIAL_TEXT_Y;
893#ifndef USE_TEXT_ONLY
894 draw_resistor(first_band, second_band, multiplier, fourth_band);
895#endif
896 draw_resistor_text(first_band, second_band, multiplier, fourth_band);
897
898 rb->snprintf(out_str, sizeof(out_str), "Input: %d %s", in_resistance_int,
899 band_data[units_used].unit);
900 display->set_viewport(&text_vp);
901 rb->lcd_puts_scroll(r_to_c_out_str_x, lineno++, out_str);
902 rb->lcd_update();
903
904 button_press = rb->button_get(true);
905 switch(button_press) {
906 case PLA_SELECT:
907 break;
908 default:
909 quit = true;
910
911 backlight_use_settings();
912
913 break;
914 }
915 }
916 display->set_viewport(&text_vp);
917 rb->lcd_scroll_stop();
918 display->set_viewport(&screen_vp);
919 rb->lcd_clear_display();
920}
921
922static void color_to_resistance(void)
923{
924 backlight_ignore_timeout();
925
926 bool quit = false;
927 int button_input = 0;
928
929 /* The colors of the bands */
930 enum color first_band = 0;
931 enum color second_band = 0;
932 enum color third_band = 0;
933 enum color fourth_band = 0;
934
935 int total_resistance_centiunits = 0;
936 char total_resistance_str [35];
937
938 rb->splash(HZ/2, "Colour to resistance");
939 rb->lcd_clear_display();
940
941 while(!quit) {
942 first_band = do_first_band_menu();
943 if(first_band==RES_INVALID) break;
944
945 second_band = do_second_band_menu();
946 if(second_band==RES_INVALID) break;
947
948 third_band = do_third_band_menu();
949 if(third_band==RES_INVALID) break;
950
951 fourth_band = do_fourth_band_menu();
952 if(fourth_band==RES_INVALID) break;
953
954 total_resistance_centiunits = calculate_resistance(first_band,
955 second_band,
956 third_band);
957
958 rb->lcd_clear_display();
959 lineno = INITIAL_TEXT_Y;
960#ifndef USE_TEXT_ONLY
961 draw_resistor(first_band, second_band, third_band, fourth_band);
962#endif
963 draw_resistor_text(first_band, second_band, third_band, fourth_band);
964
965 if(total_resistance_centiunits % 100 == 0) {
966 /* No decimals */
967 rb->snprintf(total_resistance_str, sizeof(total_resistance_str),
968 "Resistance: %d %s",
969 total_resistance_centiunits/100,
970 unit_abbrev);
971 }
972 else {
973 rb->snprintf(total_resistance_str, sizeof(total_resistance_str),
974 "Resistance: %d.%2.2d %s",
975 total_resistance_centiunits/100,
976 total_resistance_centiunits%100,
977 unit_abbrev);
978 }
979 display->set_viewport(&text_vp);
980 rb->lcd_puts_scroll(total_resistance_str_x, lineno++,
981 total_resistance_str);
982 rb->lcd_puts_scroll(tolerance_str_x, lineno++,
983 get_tolerance_str(fourth_band));
984 rb->lcd_update();
985
986 button_input = rb->button_get(true);
987 switch(button_input) {
988 case PLA_RIGHT:
989 break;
990 case PLA_EXIT:
991 case PLA_SELECT:
992 default:
993 quit = true;
994 backlight_use_settings();
995 break;
996 }
997 }
998 display->set_viewport(&text_vp);
999 rb->lcd_scroll_stop();
1000 display->set_viewport(&screen_vp);
1001 rb->lcd_clear_display();
1002 return;
1003}
1004
1005enum plugin_status plugin_start(const void* nothing)
1006{
1007 (void)nothing;
1008 rb->lcd_clear_display();
1009 rb->lcd_update();
1010 int main_menu_selection = 0;
1011 bool menuquit = false;
1012
1013 display = rb->screens[0];
1014 rb->viewport_set_defaults(&screen_vp,0);
1015 rb->viewport_set_defaults(&text_vp,0);
1016 rb->viewport_set_defaults(&bitmap_vp,0);
1017#ifndef USE_TEXT_ONLY
1018 bitmap_vp.y = RESISTOR_BMP_Y + screen_vp.y;
1019 bitmap_vp.height = BMPHEIGHT_resistor;
1020 text_vp.y = bitmap_vp.y + bitmap_vp.height;
1021 text_vp.height = screen_vp.height - text_vp.y;
1022#endif
1023
1024 MENUITEM_STRINGLIST(main_menu, "Resistor Code Calculator:", NULL,
1025 "Colours -> Resistance", "Resistance -> Colours",
1026 "LED resistor calculator", "Help", "Exit");
1027 while (!menuquit) {
1028 display->set_viewport(&screen_vp);
1029 main_menu_selection = rb->do_menu(&main_menu, &main_menu_selection,
1030 NULL, false);
1031 switch(main_menu_selection) {
1032 case 0:
1033 color_to_resistance();
1034 break;
1035 case 1:
1036 resistance_to_color();
1037 break;
1038 case 2:
1039 led_resistance_calc();
1040 break;
1041 case 3:
1042 display_helpfile();
1043 break;
1044 case 4:
1045 menuquit = true;
1046 break;
1047 case MENU_ATTACHED_USB:
1048 return PLUGIN_USB_CONNECTED;
1049 }
1050 }
1051 return PLUGIN_OK;
1052}