1 /* === Rockbox Resistor code/value calculator ===
2 [insert relevant/useful information here]
8 #include "lib/display_text.h"
9 #include "lib/pluginlib_actions.h"
10 #include "lib/picture.h"
11 #include "lib/helper.h"
13 /* Defining player-specific constants */
15 #if defined(HAVE_LCD_COLOR)
16 #define RESISTOR_BMP_X ((LCD_WIDTH - BMPWIDTH_resistor) / 2)
18 #if LCD_WIDTH >= 320 && LCD_HEIGHT >= 240 /* iPod video or larger */
19 #define RESISTOR_BMP_Y 3
21 #elif LCD_WIDTH >= 240 && LCD_HEIGHT >= 320 /* Onda, mostly */
22 #define RESISTOR_BMP_Y 3
24 #elif LCD_WIDTH >= 220 && LCD_HEIGHT >= 176 /* Fuze or larger */
25 #define RESISTOR_BMP_Y 15
27 #elif LCD_WIDTH >= 176 && LCD_HEIGHT >= 220 /* e200 or larger */
28 #define RESISTOR_BMP_Y 11
30 #elif LCD_WIDTH >= 176 && LCD_HEIGHT >= 132 /* ipod nano or larger */
31 #define RESISTOR_BMP_Y 7
33 #elif LCD_WIDTH >= 160 && LCD_HEIGHT >= 128 /* H10 or larger */
34 #define RESISTOR_BMP_Y 3
36 #elif LCD_WIDTH >= 128 && LCD_HEIGHT >= 128 /* GoGear */
37 #define RESISTOR_BMP_Y 3
39 #else /* Small screens */
40 #define RESISTOR_BMP_Y 0
44 #else /* HAVE_LCD_COLOR */
47 #endif /* HAVE_LCD_COLOR */
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
54 #define INITIAL_TEXT_Y 0
57 /* (below is for color targets */
59 #include "pluginbitmaps/resistor.h"
61 #define band_width (BMPWIDTH_resistor/15)
62 #define band_height (BMPHEIGHT_resistor*9/10)
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)
70 #endif /* USE_TEXT_ONLY */
89 static int common_values
[] = { 0, 1, 10, 15, 22, 27, 33, 39, 47, 51, 68, 82 };
90 static int power_ratings
[] = { 125, 250, 500, 1000, 2000, 3000, 5000, 10000, 50000 };
94 /* Warning: dirty kludge */
95 #define LCD_RGBPACK(x,y,z) 0
98 static struct band_data
103 int resistance_value
;
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, -1, 0, -1 },
117 { RES_GREY
, "Grey", LCD_RGBPACK(192, 192, 192), 8, -1, 0, -1 },
118 { RES_WHITE
, "White", LCD_RGBPACK(255, 255, 255), 9, -1, 0, -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 }
124 static char *unit_abbrev
;
126 static struct viewport screen_vp
;
127 static struct viewport bitmap_vp
;
128 static struct viewport text_vp
;
129 static struct screen
*display
;
133 static char *get_power_rating_str(int in_rating
)
159 static int powi(int num
, int exp
)
162 for (i
= 0; i
< exp
; i
++) {
168 static enum color
get_band_rtoc(int in_val
)
170 int return_color
= 0;
173 return_color
= RES_BLACK
;
176 return_color
= RES_BROWN
;
179 return_color
= RES_RED
;
182 return_color
= RES_ORANGE
;
185 return_color
= RES_YELLOW
;
188 return_color
= RES_GREEN
;
191 return_color
= RES_BLUE
;
194 return_color
= RES_VIOLET
;
197 return_color
= RES_GREY
;
200 return_color
= RES_WHITE
;
206 static char *get_tolerance_str(enum color color
)
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
;
214 #ifndef USE_TEXT_ONLY
215 static void draw_resistor(enum color firstband_color
,
216 enum color secondband_color
,
217 enum color thirdband_color
,
218 enum color fourthband_color
)
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
);
227 fg
= rb
->lcd_get_foreground();
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
);
233 rb
->lcd_set_foreground(LCD_BLACK
);
234 rb
->lcd_drawrect(first_band_x
, universal_y
, band_width
, band_height
);
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
);
241 rb
->lcd_set_foreground(LCD_BLACK
);
242 rb
->lcd_drawrect(second_band_x
, universal_y
, band_width
, band_height
);
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
);
249 rb
->lcd_set_foreground(LCD_BLACK
);
250 rb
->lcd_drawrect(third_band_x
, universal_y
, band_width
, band_height
);
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
);
257 rb
->lcd_set_foreground(LCD_BLACK
);
258 rb
->lcd_drawrect(fourth_band_x
, universal_y
, band_width
, band_height
);
261 rb
->lcd_set_foreground(fg
);
268 static void draw_resistor_text(enum color firstband_color
,
269 enum color secondband_color
,
270 enum color thirdband_color
,
271 enum color fourthband_color
)
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
);
285 static int calculate_resistance(enum color first_band
,
286 enum color second_band
,
287 enum color third_band
)
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
;
294 unit_abbrev
= band_data
[third_band
].unit
;
296 return total_resistance_centiunits
;
299 static enum color
do_first_band_menu(void)
301 int band_selection
= 0;
302 enum color band_color_selection
= 0;
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
,
309 switch(band_selection
) {
311 band_color_selection
= RES_BLACK
;
314 band_color_selection
= RES_BROWN
;
317 band_color_selection
= RES_RED
;
320 band_color_selection
= RES_ORANGE
;
323 band_color_selection
= RES_YELLOW
;
326 band_color_selection
= RES_GREEN
;
329 band_color_selection
= RES_BLUE
;
332 band_color_selection
= RES_VIOLET
;
335 band_color_selection
= RES_GREY
;
338 band_color_selection
= RES_WHITE
;
341 band_color_selection
= RES_INVALID
;
344 return band_color_selection
;
347 static enum color
do_second_band_menu(void)
349 int band_selection
= 0;
350 enum color band_color_selection
= 0;
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
,
357 switch(band_selection
) {
359 band_color_selection
= RES_BLACK
;
362 band_color_selection
= RES_BROWN
;
365 band_color_selection
= RES_RED
;
368 band_color_selection
= RES_ORANGE
;
371 band_color_selection
= RES_YELLOW
;
374 band_color_selection
= RES_GREEN
;
377 band_color_selection
= RES_BLUE
;
380 band_color_selection
= RES_VIOLET
;
383 band_color_selection
= RES_GREY
;
386 band_color_selection
= RES_WHITE
;
389 band_color_selection
= RES_INVALID
;
392 return band_color_selection
;
395 static enum color
do_third_band_menu(void)
397 int band_selection
= 0;
398 enum color band_color_selection
= 0;
400 MENUITEM_STRINGLIST(colors_menu_third
, "Third band colour:", NULL
,
401 "Black", "Brown", "Red", "Orange", "Yellow",
402 "Green", "Blue", "Silver", "Gold");
403 band_selection
= rb
->do_menu(&colors_menu_third
, &band_selection
, NULL
,
405 switch(band_selection
) {
407 band_color_selection
= RES_BLACK
;
410 band_color_selection
= RES_BROWN
;
413 band_color_selection
= RES_RED
;
416 band_color_selection
= RES_ORANGE
;
419 band_color_selection
= RES_YELLOW
;
422 band_color_selection
= RES_GREEN
;
425 band_color_selection
= RES_BLUE
;
428 band_color_selection
= RES_SILVER
;
431 band_color_selection
= RES_GOLD
;
434 band_color_selection
= RES_INVALID
;
437 return band_color_selection
;
440 static enum color
do_fourth_band_menu(void)
442 int band_selection
= 0;
443 enum color band_color_selection
= 0;
445 MENUITEM_STRINGLIST(colors_menu_fourth
, "Fourth band colour:", NULL
,
446 "Gold", "Brown", "Red", "Silver", "(none)");
447 band_selection
= rb
->do_menu(&colors_menu_fourth
, &band_selection
, NULL
,
449 switch(band_selection
) {
451 band_color_selection
= RES_GOLD
;
454 band_color_selection
= RES_BROWN
;
457 band_color_selection
= RES_RED
;
460 band_color_selection
= RES_SILVER
;
463 band_color_selection
= RES_NONE
;
466 band_color_selection
= RES_INVALID
;
469 return band_color_selection
;
472 static void display_helpfile(void)
474 rb
->lcd_clear_display();
475 /* some information obtained from wikipedia */
476 static char * helpfile_text
[] = {
477 "Resistor Calculator Helpfile", "", "",
478 "About resistors:", "", /* 7 */
480 "A", "resistor", "is", "a ", "two-terminal", "electronic",
481 "component", "that", "produces", "a", "voltage", "across", "its",
482 "terminals", "that", "is", "proportional", "to", "the", "electric",
483 "current", "passing", "through", "it", "in", "accordance", "to",
484 "Ohm's", "Law:", "", /* 29 */
489 "", "R = I/V", "", "",
490 "Where", "V", "=", "voltage", "I", "=", "current", "(in", "amps)",
491 "and", "R", "=", "resistance", "(measured", "in", "Ohms)", "", "",
494 "The", "primary", "characteristics", "of", "a", "resistor", "are",
495 "the", "resistance,", "the", "tolerance,", "and", "the", "maximum",
496 "working", "voltage", "and", "the", "power", "rating.", "At",
497 "this", "time,", "this", "calculator", "only", "utilises", "the",
498 "resistance", "and", "tolerance.", "", "", /* 33 */
500 "The", "Ohm", "is", "the", "SI", "unit", "of", "resistance,", "and",
501 "common", "multiples", "of", "that", "include", "the", "kiloohm",
502 "(KOhm", "-", "1x10^3)", "and", "the", "megaohm", "(MOhm",
503 "-", "1x10^6),", "both", "of", "which", "are", "supported", "by",
504 "this", "calculator.", "", "", /* 34 */
506 "Resistors", "in", "parallel:", "", /* 4 */
508 "1/Rtotal", "=", "1/R1", "+", "1/R2", "...", "+", "1/Rn", "", /* 9*/
510 "", "Resistors", "in", "series:", "", /* 5 */
512 "Rtotal", "=", "R1", "+", "R2", "...", "+", "Rn", "", /* 9 */
514 "", "How to use this calculator", "", /* 3 */
516 "This", "calculator", "has", "three", "modes:", "",
517 "Resistance", "to", "coulor", "codes,", "",
518 "Colour", "codes", "to", "resistance", "",
519 "and", "LED", "resistance", "calculator", "", "",
521 "At", "this", "time", "there", "is", "only", "support", "for",
522 "four-", "band", "resistors.", "", "",
524 "In", "Colour", "to", "Resistance", "mode", "use", "the", "menus",
525 "to", "input", "(in", "order)", "the", "bands", "of", "the",
526 "resistor", "for", "which", "you", "would", "like", "to", "know",
527 "the", "resistance.", "", "",
529 "In", "Resistance", "to", "Colour", "mode,", "use", "the", "menus",
530 "to", "select", "which", "unit", "to", "use", "(choose", "from", "Ohms,",
531 "KiloOhms", "and", "MegaOhms)", "and", "the", "on-screen", "keyboard",
532 "to", "input", "the", "value", "of", "the", "resistor", "that", "you",
533 "would", "like", "to", "know", "the", "colour", "codes", "of.",
534 "Output", "will", "be", "both", "graphical", "(with", "bands", "of",
535 "the", "resistor", "shown", "in", "their", "corresponding", "colours",
536 "-", "colour", "targets", "only)", "and", "textually.", "","",
538 "LED", "resistor", "calculator", "mode", "is", "used", "to", "determine",
539 "the", "resistor", "necessary", "to", "light", "a", "LED", "safely",
540 "at", "a", "given", "voltage.", "First,", "select", "the", "voltage",
541 "that", "the", "LED", "will", "use", "(the", "first", "option", "is",
542 "the", "most", "common", "and", "is", "a", "safe", "guess)", "and", "the",
543 "current", "that", "it", "will", "draw", "(likewise", "with", "the",
544 "first", "option).", "Then", "use", "the", "onscreen", "keyboard", "to",
545 "type", "in", "the", "supply", "voltage", "and,", "if", "selected,",
546 "the", "custom", "foreward", "current.", "",
547 "Disclaimer:", "this",
548 "calculator", "produces", "safe", "estimates,", "but", "use", "your",
549 "own", "judgement", "when", "using", "these", "output", "values.",
550 "Power", "rating", "and", "displayed", "resistance", "are", "rounded",
551 "up", "to", "the", "nearest", "common", "value."
553 static struct style_text formatting
[] = {
554 { 0, TEXT_CENTER
|TEXT_UNDERLINE
},
555 { 3, TEXT_UNDERLINE
},
556 { 159, TEXT_UNDERLINE
},
560 display_text(ARRAYLEN(helpfile_text
), helpfile_text
, formatting
,
565 static void led_resistance_calc(void)
567 backlight_force_on();
568 int voltage_menu_selection
, button_press
, j
, k
, l
, foreward_current
= 0;
569 int fwd_current_selection
= 0;
572 char fwd_kbd_buffer
[5];
573 int input_voltage
, led_voltage
= 0;
576 int rounded_resistance
= 0;
578 int power_rating_in
= 0;
579 int rounded_power_rating
= 0;
581 char current_out_str
[16];
582 char true_current_out_str
[40];
583 char rounded_resistance_out_str
[40];
584 char power_rating_out_str
[40];
586 int power_ten
, first_band_int
, second_band_int
= 0;
588 enum color first_band
;
589 enum color second_band
;
590 enum color multiplier
;
591 enum color fourth_band
= RES_NONE
;
593 rb
->lcd_clear_display();
595 MENUITEM_STRINGLIST(voltage_menu
, "Select LED voltage:", NULL
,
596 "2v (Common red, orange)", "1.5v (IR)", "2.1v (Yellow)",
597 "2.2v (Green)", "3.3v (True green, blue, white, UV)",
598 "4.6v (Blue - 430nm)");
599 MENUITEM_STRINGLIST(fwd_current_menu
, "Select foreward current:", NULL
,
600 "20mA - Most common for 5mm and 3mm LEDs - select if unsure.",
601 "Key in other (only if already known)");
605 ret
= voltage_menu_selection
= rb
->do_menu(&voltage_menu
,
606 &voltage_menu_selection
, NULL
, false);
608 ret
= fwd_current_selection
= rb
->do_menu(&fwd_current_menu
,
609 &fwd_current_selection
, NULL
, false);
611 rb
->lcd_clear_display();
614 rb
->splash(HZ
*2, "(First) Input the supply voltage:");
615 memset(kbd_buffer
,0,sizeof(kbd_buffer
));
616 rb
->kbd_input(kbd_buffer
, sizeof(kbd_buffer
));
617 input_voltage
= rb
->atoi(kbd_buffer
);
618 if(input_voltage
== 0) break;
620 if(input_voltage
!= (int)input_voltage
) {
623 else { input_voltage
*= 100; }
625 switch(voltage_menu_selection
) {
645 switch(fwd_current_selection
) {
647 foreward_current
= 2; /* 20mA * 100 */
650 rb
->lcd_clear_display();
651 rb
->splash(HZ
*2, "Input the foreward current, in mA");
652 memset(fwd_kbd_buffer
,0,sizeof(fwd_kbd_buffer
));
653 rb
->kbd_input(fwd_kbd_buffer
, sizeof(fwd_kbd_buffer
));
654 foreward_current
= ((rb
->atoi(fwd_kbd_buffer
))/10);
658 if(foreward_current
== 0) break;
660 rb
->lcd_clear_display();
662 resistance
= (input_voltage
- led_voltage
) / foreward_current
;
663 out_int
= resistance
;
665 int total_common_values
= 11;
666 int total_power_values
= 9;
668 if(led_voltage
> input_voltage
) {
669 rb
->splash(HZ
, "Problem: LED voltage is higher than the source.");
673 for(j
= 0; j
< total_common_values
; j
++) {
674 for(k
= 1; k
< 5; k
++) {
675 if( resistance
== (common_values
[j
] * powi(10, k
))) {
676 rounded_resistance
= (common_values
[j
] * powi(10, k
));
680 else if(resistance
>= (common_values
[j
] * powi(10, k
)) &&
681 resistance
<= (common_values
[j
+1] * powi(10, k
))) {
682 rounded_resistance
= (common_values
[j
+1] * powi(10, k
));
683 /* the higher resistance, to be safe */
690 if(rounded_resistance
== 0)
692 rb
->splash(HZ
, "Problem: Input voltage too high.");
695 power_rating_in
= ((input_voltage
/100)*(input_voltage
/100)*1000 / rounded_resistance
);
697 for(l
= 0; l
< total_power_values
; l
++) {
698 if((int)power_rating_in
== power_ratings
[l
]) {
699 rounded_power_rating
= (power_ratings
[l
]);
702 else if(power_rating_in
>= power_ratings
[l
] &&
703 power_rating_in
<= power_ratings
[l
+1]) {
704 rounded_power_rating
= power_ratings
[l
+1];
710 get_power_rating_str(rounded_power_rating
);
713 temp
=rounded_resistance
;
719 first_band_int
=temp
/10;
720 second_band_int
=temp
%10;
722 first_band
= get_band_rtoc(first_band_int
);
723 second_band
= get_band_rtoc(second_band_int
);
724 multiplier
= get_band_rtoc(power_ten
);
726 rb
->lcd_clear_display();
727 lineno
= INITIAL_TEXT_Y
;
728 #ifndef USE_TEXT_ONLY
729 draw_resistor(first_band
, second_band
, multiplier
, fourth_band
);
731 draw_resistor_text(first_band
, second_band
, multiplier
, fourth_band
);
733 rb
->snprintf(current_out_str
, sizeof(current_out_str
), "%d mA",
734 (foreward_current
*10));
736 rb
->snprintf(true_current_out_str
, sizeof(true_current_out_str
),
737 "Input: %dv, %d Ohms @ %s", (input_voltage
/100),
738 out_int
, current_out_str
);
739 rb
->snprintf(rounded_resistance_out_str
,
740 sizeof(rounded_resistance_out_str
),
741 "Rounded/displayed: [%d %s]", rounded_resistance
,
742 band_data
[multiplier
].unit
);
743 rb
->snprintf(power_rating_out_str
, sizeof(power_rating_out_str
),
744 "Recommended: %s or greater",
745 get_power_rating_str(rounded_power_rating
));
747 display
->set_viewport(&text_vp
);
748 rb
->lcd_puts_scroll(resistance_val_x
, lineno
++, true_current_out_str
);
749 rb
->lcd_puts_scroll(resistance_val_x
, lineno
++, rounded_resistance_out_str
);
750 rb
->lcd_puts_scroll(resistance_val_x
, lineno
++, power_rating_out_str
);
754 button_press
= rb
->button_get(true);
755 switch(button_press
) {
760 backlight_use_settings();
764 display
->set_viewport(&text_vp
);
765 rb
->lcd_stop_scroll();
766 display
->set_viewport(&screen_vp
);
767 rb
->lcd_clear_display();
771 static void resistance_to_color(void)
773 backlight_force_on();
775 int menu_selection_tol
;
779 char kbd_buffer
[10];
782 int in_resistance_int
;
785 int first_band_int
= 0;
786 int second_band_int
= 0;
788 enum color first_band
;
789 enum color second_band
;
790 enum color multiplier
;
791 enum color fourth_band
= 0;
792 enum color units_used
= 0;
796 for(i
=0; i
<=10; i
++) { kbd_buffer
[i
] = 0; }
797 /* This cleans out the mysterious garbage that appears */
798 rb
->lcd_clear_display();
799 rb
->splash(HZ
/2, "Resistance to Colour");
800 MENUITEM_STRINGLIST(r_to_c_menu
, "Select unit to use:", NULL
,
801 "Ohms", "Kiloohms (KOhms)", "Megaohms (MOhms)");
802 MENUITEM_STRINGLIST(r_to_c_menu_tol
, "Tolerance to display:", NULL
,
803 "5%", "10%", "1%", "2%", "20%");
807 ret
=menu_selection
= rb
->do_menu(&r_to_c_menu
, &menu_selection
,
811 rb
->kbd_input(kbd_buffer
, sizeof(kbd_buffer
));
812 /* As stated above somewhere, we (I) need to make a calculator-like
813 keypad, that keyboard isn't all that fun to use. */
814 ret
= rb
->do_menu(&r_to_c_menu_tol
, &menu_selection_tol
,
818 switch(menu_selection_tol
) {
820 fourth_band
= RES_GOLD
;
823 fourth_band
= RES_SILVER
;
826 fourth_band
= RES_BROWN
;
829 fourth_band
= RES_RED
;
832 fourth_band
= RES_NONE
;
836 kbd_input_int
= rb
->atoi(kbd_buffer
);
837 in_resistance_int
= kbd_input_int
;
839 switch(menu_selection
) {
841 units_used
= RES_BLACK
;
844 units_used
= RES_RED
;
845 kbd_input_int
*= 1000;
848 units_used
= RES_GREEN
;
849 kbd_input_int
*= 1000000;
860 first_band_int
=temp
/10;
861 second_band_int
=temp
%10;
863 first_band
= get_band_rtoc(first_band_int
);
864 second_band
= get_band_rtoc(second_band_int
);
865 multiplier
= get_band_rtoc(power_ten
);
867 rb
->lcd_clear_display();
868 lineno
= INITIAL_TEXT_Y
;
869 #ifndef USE_TEXT_ONLY
870 draw_resistor(first_band
, second_band
, multiplier
, fourth_band
);
872 draw_resistor_text(first_band
, second_band
, multiplier
, fourth_band
);
874 rb
->snprintf(out_str
, sizeof(out_str
), "Input: %d %s", in_resistance_int
,
875 band_data
[units_used
].unit
);
876 display
->set_viewport(&text_vp
);
877 rb
->lcd_puts_scroll(r_to_c_out_str_x
, lineno
++, out_str
);
880 button_press
= rb
->button_get(true);
881 switch(button_press
) {
886 backlight_use_settings();
890 display
->set_viewport(&text_vp
);
891 rb
->lcd_stop_scroll();
892 display
->set_viewport(&screen_vp
);
893 rb
->lcd_clear_display();
896 static void color_to_resistance(void)
898 backlight_force_on();
900 int button_input
= 0;
902 /* The colors of the bands */
903 enum color first_band
= 0;
904 enum color second_band
= 0;
905 enum color third_band
= 0;
906 enum color fourth_band
= 0;
908 int total_resistance_centiunits
= 0;
909 char total_resistance_str
[35];
911 rb
->splash(HZ
/2, "Colour to resistance");
912 rb
->lcd_clear_display();
915 first_band
= do_first_band_menu();
916 if(first_band
==RES_INVALID
) break;
918 second_band
= do_second_band_menu();
919 if(second_band
==RES_INVALID
) break;
921 third_band
= do_third_band_menu();
922 if(third_band
==RES_INVALID
) break;
924 fourth_band
= do_fourth_band_menu();
925 if(third_band
==RES_INVALID
) break;
927 total_resistance_centiunits
= calculate_resistance(first_band
,
931 rb
->lcd_clear_display();
932 lineno
= INITIAL_TEXT_Y
;
933 #ifndef USE_TEXT_ONLY
934 draw_resistor(first_band
, second_band
, third_band
, fourth_band
);
936 draw_resistor_text(first_band
, second_band
, third_band
, fourth_band
);
938 if(total_resistance_centiunits
% 100 == 0)
941 rb
->snprintf(total_resistance_str
, sizeof(total_resistance_str
),
943 total_resistance_centiunits
/100,
948 rb
->snprintf(total_resistance_str
, sizeof(total_resistance_str
),
949 "Resistance: %d.%2.2d %s",
950 total_resistance_centiunits
/100,
951 total_resistance_centiunits
%100,
954 display
->set_viewport(&text_vp
);
955 rb
->lcd_puts_scroll(total_resistance_str_x
, lineno
++,
956 total_resistance_str
);
957 rb
->lcd_puts_scroll(tolerance_str_x
, lineno
++,
958 get_tolerance_str(fourth_band
));
961 button_input
= rb
->button_get(true);
962 switch(button_input
) {
969 backlight_use_settings();
973 display
->set_viewport(&text_vp
);
974 rb
->lcd_stop_scroll();
975 display
->set_viewport(&screen_vp
);
976 rb
->lcd_clear_display();
980 enum plugin_status
plugin_start(const void* nothing
)
983 rb
->lcd_clear_display();
985 int main_menu_selection
= 0;
986 bool menuquit
= false;
988 display
= rb
->screens
[0];
989 rb
->viewport_set_defaults(&screen_vp
,0);
990 rb
->viewport_set_defaults(&text_vp
,0);
991 rb
->viewport_set_defaults(&bitmap_vp
,0);
992 #ifndef USE_TEXT_ONLY
993 bitmap_vp
.y
= RESISTOR_BMP_Y
+ screen_vp
.y
;
994 bitmap_vp
.height
= BMPHEIGHT_resistor
;
995 text_vp
.y
= bitmap_vp
.y
+ bitmap_vp
.height
;
996 text_vp
.height
= screen_vp
.height
- text_vp
.y
;
999 MENUITEM_STRINGLIST(main_menu
, "Resistor Code Calculator:", NULL
,
1000 "Colours -> Resistance", "Resistance -> Colours",
1001 "LED resistor calculator", "Help", "Exit");
1003 display
->set_viewport(&screen_vp
);
1004 main_menu_selection
= rb
->do_menu(&main_menu
, &main_menu_selection
,
1006 switch(main_menu_selection
) {
1008 color_to_resistance();
1011 resistance_to_color();
1014 led_resistance_calc();
1022 case MENU_ATTACHED_USB
:
1023 return PLUGIN_USB_CONNECTED
;