Fix some incorrect colour-to-resistance results. The price is that sometimes the...
[maemo-rb.git] / apps / plugins / resistor.c
blobc5d9cb5fdd4ae2401313e73adc9426b81573b266
1 /* === Rockbox Resistor code/value calculator ===
2 [insert relevant/useful information here]
3 TODO:
4 [ ] Own numeric keypad
5 */
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"
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
42 #endif
44 #else /* HAVE_LCD_COLOR */
46 #define USE_TEXT_ONLY
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
56 #ifndef USE_TEXT_ONLY
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 */
72 enum 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,
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 };
91 /* All in mW */
93 #ifndef LCD_RGBPACK
94 /* Warning: dirty kludge */
95 #define LCD_RGBPACK(x,y,z) 0
96 #endif
98 static struct band_data
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[] =
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;
131 static int lineno;
133 static char *get_power_rating_str(int in_rating)
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";
159 static int powi(int num, int exp)
161 int i, product = 1;
162 for (i = 0; i < exp; i++) {
163 product *= num; }
165 return product;
168 static enum color get_band_rtoc(int in_val)
170 int return_color = 0;
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;
203 return return_color;
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)
220 unsigned int fg;
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);
232 } else {
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);
240 } else {
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);
248 } else {
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);
256 } else {
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);
263 rb->lcd_update();
264 return;
266 #endif
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);
281 rb->lcd_update();
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,
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;
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,
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;
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,
404 false);
405 switch(band_selection) {
406 case 0: /* Black */
407 band_color_selection = RES_BLACK;
408 break;
409 case 1: /* Brown */
410 band_color_selection = RES_BROWN;
411 break;
412 case 2: /* Red */
413 band_color_selection = RES_RED;
414 break;
415 case 3: /* Orange */
416 band_color_selection = RES_ORANGE;
417 break;
418 case 4: /* Yellow */
419 band_color_selection= RES_YELLOW;
420 break;
421 case 5: /* Green */
422 band_color_selection = RES_GREEN;
423 break;
424 case 6: /* Blue */
425 band_color_selection = RES_BLUE;
426 break;
427 case 7: /* Silver */
428 band_color_selection = RES_SILVER;
429 break;
430 case 8: /* Gold */
431 band_color_selection= RES_GOLD;
432 break;
433 default:
434 band_color_selection = RES_INVALID;
435 break;
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,
448 false);
449 switch(band_selection) {
450 case 0: /* Gold */
451 band_color_selection = RES_GOLD;
452 break;
453 case 1: /* Brown */
454 band_color_selection = RES_BROWN;
455 break;
456 case 2: /* Red */
457 band_color_selection = RES_RED;
458 break;
459 case 3: /* Silver */
460 band_color_selection = RES_SILVER;
461 break;
462 case 4: /* (none) */
463 band_color_selection = RES_NONE;
464 break;
465 default:
466 band_color_selection = RES_INVALID;
467 break;
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 */
479 /* -- */
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 */
485 /* -- */
486 "", "V = IR",
487 "", "I = V/R",
488 "", "and",
489 "", "R = I/V", "", "",
490 "Where", "V", "=", "voltage", "I", "=", "current", "(in", "amps)",
491 "and", "R", "=", "resistance", "(measured", "in", "Ohms)", "", "",
492 /* 28 */
493 /* -- */
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 */
499 /* -- */
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 */
505 /* -- */
506 "Resistors", "in", "parallel:", "", /* 4 */
507 /* -- */
508 "1/Rtotal", "=", "1/R1", "+", "1/R2", "...", "+", "1/Rn", "", /* 9*/
509 /* -- */
510 "", "Resistors", "in", "series:", "", /* 5 */
511 /* -- */
512 "Rtotal", "=", "R1", "+", "R2", "...", "+", "Rn", "", /* 9 */
513 /* -- */
514 "", "How to use this calculator", "", /* 3 */
515 /* -- */
516 "This", "calculator", "has", "three", "modes:", "",
517 "Resistance", "to", "coulor", "codes,", "",
518 "Colour", "codes", "to", "resistance", "",
519 "and", "LED", "resistance", "calculator", "", "",
520 /* -- */
521 "At", "this", "time", "there", "is", "only", "support", "for",
522 "four-", "band", "resistors.", "", "",
523 /* -- */
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.", "", "",
528 /* -- */
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.", "","",
537 /* -- */
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 },
557 LAST_STYLE_ITEM
560 display_text(ARRAYLEN(helpfile_text), helpfile_text, formatting,
561 NULL, true);
562 return;
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;
570 bool quit = false;
571 char kbd_buffer [5];
572 char fwd_kbd_buffer [5];
573 int input_voltage, led_voltage = 0;
575 int resistance = 0;
576 int rounded_resistance = 0;
577 int temp;
578 int power_rating_in = 0;
579 int rounded_power_rating = 0;
580 int out_int = 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)");
603 while(!quit) {
604 int ret;
605 ret = voltage_menu_selection = rb->do_menu(&voltage_menu,
606 &voltage_menu_selection, NULL, false);
607 if(ret<0) break;
608 ret = fwd_current_selection = rb->do_menu(&fwd_current_menu,
609 &fwd_current_selection, NULL, false);
610 if(ret<0) break;
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) {
621 input_voltage *= 10;
623 else { input_voltage *= 100; }
625 switch(voltage_menu_selection) {
626 case 0: /* 2v */
627 led_voltage = 200;
628 break;
629 case 1: /* 1.5v */
630 led_voltage = 150;
631 break;
632 case 2: /* 2.1 */
633 led_voltage = 210;
634 break;
635 case 3:
636 led_voltage = 220;
637 break;
638 case 4:
639 led_voltage = 330;
640 break;
641 case 5:
642 led_voltage = 460;
643 break;
645 switch(fwd_current_selection) {
646 case 0: /* 20mA */
647 foreward_current = 2; /* 20mA * 100 */
648 break;
649 case 1:
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);
655 break;
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.");
670 break;
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));
677 /* perfect match */
678 break;
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 */
684 break;
686 else { break; }
690 if(rounded_resistance == 0)
692 rb->splash(HZ, "Problem: Input voltage too high.");
693 break;
695 power_rating_in = ((input_voltage/100)*(input_voltage/100)*1000 / rounded_resistance);
696 /* in mW */
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]);
700 break;
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];
705 break;
707 else { break; }
710 get_power_rating_str(rounded_power_rating);
712 power_ten=0;
713 temp=rounded_resistance;
714 while(temp>=100)
716 temp/=10;
717 power_ten++;
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);
730 #endif
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);
752 rb->lcd_update();
754 button_press = rb->button_get(true);
755 switch(button_press) {
756 case PLA_SELECT:
757 break;
758 default:
759 quit = true;
760 backlight_use_settings();
761 break;
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();
774 int menu_selection;
775 int menu_selection_tol;
776 int button_press;
777 int i;
778 bool quit = false;
779 char kbd_buffer [10];
780 int kbd_input_int;
781 int temp;
782 int in_resistance_int;
784 int power_ten;
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;
794 char out_str[20];
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%");
805 while(!quit) {
806 int ret;
807 ret=menu_selection = rb->do_menu(&r_to_c_menu, &menu_selection,
808 NULL, false);
809 if(ret<0) break;
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,
815 NULL, false);
816 if(ret<0) break;
818 switch(menu_selection_tol) {
819 case 0: /* 5% */
820 fourth_band = RES_GOLD;
821 break;
822 case 1: /* 10% */
823 fourth_band = RES_SILVER;
824 break;
825 case 2: /* 1% */
826 fourth_band = RES_BROWN;
827 break;
828 case 3: /* 2% */
829 fourth_band = RES_RED;
830 break;
831 case 4: /* 20% */
832 fourth_band = RES_NONE;
833 break;
836 kbd_input_int = rb->atoi(kbd_buffer);
837 in_resistance_int = kbd_input_int;
839 switch(menu_selection) {
840 case 0:
841 units_used = RES_BLACK;
842 break;
843 case 1: /* KOhms */
844 units_used = RES_RED;
845 kbd_input_int *= 1000;
846 break;
847 case 2: /* MOhms */
848 units_used = RES_GREEN;
849 kbd_input_int *= 1000000;
850 break;
853 power_ten=0;
854 temp=kbd_input_int;
855 while(temp>=100)
857 temp/=10;
858 power_ten++;
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);
871 #endif
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);
878 rb->lcd_update();
880 button_press = rb->button_get(true);
881 switch(button_press) {
882 case PLA_SELECT:
883 break;
884 default:
885 quit = true;
886 backlight_use_settings();
887 break;
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();
899 bool quit = false;
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();
914 while(!quit) {
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,
928 second_band,
929 third_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);
935 #endif
936 draw_resistor_text(first_band, second_band, third_band, fourth_band);
938 if(total_resistance_centiunits % 100 == 0)
940 /* No decimals */
941 rb->snprintf(total_resistance_str, sizeof(total_resistance_str),
942 "Resistance: %d %s",
943 total_resistance_centiunits/100,
944 unit_abbrev);
946 else
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,
952 unit_abbrev);
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));
959 rb->lcd_update();
961 button_input = rb->button_get(true);
962 switch(button_input) {
963 case PLA_RIGHT:
964 break;
965 case PLA_EXIT:
966 case PLA_SELECT:
967 default:
968 quit = true;
969 backlight_use_settings();
970 break;
973 display->set_viewport(&text_vp);
974 rb->lcd_stop_scroll();
975 display->set_viewport(&screen_vp);
976 rb->lcd_clear_display();
977 return;
980 enum plugin_status plugin_start(const void* nothing)
982 (void)nothing;
983 rb->lcd_clear_display();
984 rb->lcd_update();
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;
997 #endif
999 MENUITEM_STRINGLIST(main_menu, "Resistor Code Calculator:", NULL,
1000 "Colours -> Resistance", "Resistance -> Colours",
1001 "LED resistor calculator", "Help", "Exit");
1002 while (!menuquit) {
1003 display->set_viewport(&screen_vp);
1004 main_menu_selection = rb->do_menu(&main_menu, &main_menu_selection,
1005 NULL, false);
1006 switch(main_menu_selection) {
1007 case 0:
1008 color_to_resistance();
1009 break;
1010 case 1:
1011 resistance_to_color();
1012 break;
1013 case 2:
1014 led_resistance_calc();
1015 break;
1016 case 3:
1017 display_helpfile();
1018 break;
1019 case 4:
1020 menuquit = true;
1021 break;
1022 case MENU_ATTACHED_USB:
1023 return PLUGIN_USB_CONNECTED;
1026 return PLUGIN_OK;