diff options
author | Frank Gevaerts <frank@gevaerts.be> | 2010-10-03 13:57:18 +0000 |
---|---|---|
committer | Frank Gevaerts <frank@gevaerts.be> | 2010-10-03 13:57:18 +0000 |
commit | c80dd635abb959646ed8c3262df591dfb189cd57 (patch) | |
tree | 2db4fcffd334103da83257824d6726d5a08d168d | |
parent | 3e9f797f6e3676e537a82308ef3b3509006346bd (diff) | |
download | rockbox-c80dd635abb959646ed8c3262df591dfb189cd57.tar.gz rockbox-c80dd635abb959646ed8c3262df591dfb189cd57.zip |
Resistor calculator: simplify layout code, add scrolling, and handle error cases
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@28203 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r-- | apps/plugins/resistor.c | 434 |
1 files changed, 202 insertions, 232 deletions
diff --git a/apps/plugins/resistor.c b/apps/plugins/resistor.c index 3053c4f4ac..d2645a792e 100644 --- a/apps/plugins/resistor.c +++ b/apps/plugins/resistor.c | |||
@@ -38,7 +38,6 @@ TODO: | |||
38 | 38 | ||
39 | #else /* Small screens */ | 39 | #else /* Small screens */ |
40 | #define RESISTOR_BMP_Y 0 | 40 | #define RESISTOR_BMP_Y 0 |
41 | /* And along with the tiny screen comes a whole bunch of exceptions */ | ||
42 | 41 | ||
43 | #endif /* HAVE_LCD_COLOR */ | 42 | #endif /* HAVE_LCD_COLOR */ |
44 | 43 | ||
@@ -47,29 +46,15 @@ TODO: | |||
47 | #define USE_TEXT_ONLY | 46 | #define USE_TEXT_ONLY |
48 | #endif | 47 | #endif |
49 | 48 | ||
50 | #ifdef USE_TEXT_ONLY | 49 | #define total_resistance_str_x 1 |
51 | #define resistance_val_x 0 | 50 | #define tolerance_str_x 1 |
52 | #define resistance_val_y 1 | 51 | #define resistance_val_x 1 |
53 | |||
54 | #if LCD_HEIGHT <= 64 | ||
55 | #define total_resistance_str_x 0 | ||
56 | #define total_resistance_str_y 17 | ||
57 | #define tolerance_str_x 0 | ||
58 | #define tolerance_str_y 32 | ||
59 | #define r_to_c_out_str_y (total_resistance_str_y + 15) | ||
60 | #define r_to_c_out_str_x 1 | 52 | #define r_to_c_out_str_x 1 |
61 | #else | ||
62 | #define total_resistance_str_x 0 | ||
63 | #define total_resistance_str_y 25 | ||
64 | #define tolerance_str_x 0 | ||
65 | #define tolerance_str_y 45 | ||
66 | #define r_to_c_out_str_x 0 | ||
67 | #define r_to_c_out_str_y 45 | ||
68 | #endif /* LCD_HEIGHT = <= 64 */ | ||
69 | 53 | ||
70 | #else /* USE_TEXT_ONLY */ | 54 | #define INITIAL_TEXT_Y 0 |
71 | /* (below is for color targets */ | ||
72 | 55 | ||
56 | #ifndef USE_TEXT_ONLY | ||
57 | /* (below is for color targets */ | ||
73 | 58 | ||
74 | #include "pluginbitmaps/resistor.h" | 59 | #include "pluginbitmaps/resistor.h" |
75 | 60 | ||
@@ -80,34 +65,7 @@ TODO: | |||
80 | #define second_band_x (3*BMPWIDTH_resistor/8 + RESISTOR_BMP_X - band_width/2) | 65 | #define second_band_x (3*BMPWIDTH_resistor/8 + RESISTOR_BMP_X - band_width/2) |
81 | #define third_band_x (BMPWIDTH_resistor/2 + RESISTOR_BMP_X - band_width/2) | 66 | #define third_band_x (BMPWIDTH_resistor/2 + RESISTOR_BMP_X - band_width/2) |
82 | #define fourth_band_x (3*BMPWIDTH_resistor/4 + RESISTOR_BMP_X - band_width/2) | 67 | #define fourth_band_x (3*BMPWIDTH_resistor/4 + RESISTOR_BMP_X - band_width/2) |
83 | #define universal_y (RESISTOR_BMP_Y+(BMPHEIGHT_resistor)/2 - band_height/2) | 68 | #define universal_y ((BMPHEIGHT_resistor)/2 - band_height/2) |
84 | |||
85 | #if LCD_WIDTH <= 128 | ||
86 | #define total_resistance_str_x 1 | ||
87 | #define tolerance_str_x 1 | ||
88 | #define resistance_val_x 1 | ||
89 | #define r_to_c_out_str_x 1 | ||
90 | #else /* LCD_WIDTHT <= 128 */ | ||
91 | #define total_resistance_str_x (LCD_WIDTH/14) | ||
92 | #define tolerance_str_x (LCD_WIDTH/14) | ||
93 | #define resistance_val_x (LCD_WIDTH/14) | ||
94 | #define r_to_c_out_str_x (LCD_WIDTH/14) | ||
95 | #endif | ||
96 | |||
97 | |||
98 | #if LCD_HEIGHT <= 128 | ||
99 | #define total_resistance_str_y (BMPHEIGHT_resistor + RESISTOR_BMP_Y + 3) | ||
100 | #define tolerance_str_y total_resistance_str_y + 20 | ||
101 | #define resistance_val_y total_resistance_str_y + 20 | ||
102 | #define r_to_c_out_str_y total_resistance_str_y + 30 | ||
103 | #else /* LCD_HEIGHT <= 128 */ | ||
104 | #define total_resistance_str_y (2*RESISTOR_BMP_Y + BMPHEIGHT_resistor) | ||
105 | #define tolerance_str_y (total_resistance_str_y + 15) | ||
106 | #define resistance_val_y (total_resistance_str_y + 15) | ||
107 | #define r_to_c_out_str_y (total_resistance_str_y + 25) | ||
108 | /* tolerance_str and resistance_val will never be shown at the same time */ | ||
109 | |||
110 | #endif /* LCD_HEIGHT <= 128 */ | ||
111 | 69 | ||
112 | #endif /* USE_TEXT_ONLY */ | 70 | #endif /* USE_TEXT_ONLY */ |
113 | 71 | ||
@@ -125,6 +83,7 @@ enum color { | |||
125 | RES_GOLD, | 83 | RES_GOLD, |
126 | RES_SILVER, | 84 | RES_SILVER, |
127 | RES_NONE, | 85 | RES_NONE, |
86 | RES_INVALID = -1, | ||
128 | }; | 87 | }; |
129 | 88 | ||
130 | int common_values[] = { 0, 1, 10, 15, 22, 27, 33, 39, 47, 51, 68, 82 }; | 89 | int common_values[] = { 0, 1, 10, 15, 22, 27, 33, 39, 47, 51, 68, 82 }; |
@@ -171,6 +130,13 @@ int r_to_c_third_band; | |||
171 | 130 | ||
172 | char str [4][7]; | 131 | char str [4][7]; |
173 | 132 | ||
133 | struct viewport screen_vp; | ||
134 | struct viewport bitmap_vp; | ||
135 | struct viewport text_vp; | ||
136 | struct screen *display; | ||
137 | |||
138 | int lineno; | ||
139 | |||
174 | void get_power_rating_str(int in_rating) | 140 | void get_power_rating_str(int in_rating) |
175 | { | 141 | { |
176 | switch(in_rating) { | 142 | switch(in_rating) { |
@@ -270,25 +236,6 @@ void get_tolerance_str(enum color color) | |||
270 | band_data[color].tolerance); | 236 | band_data[color].tolerance); |
271 | } | 237 | } |
272 | 238 | ||
273 | void draw_resistor_text(enum color firstband_color, | ||
274 | enum color secondband_color, | ||
275 | enum color thirdband_color, | ||
276 | enum color fourthband_color) | ||
277 | { | ||
278 | char resistance_vals_str[64]; | ||
279 | rb->snprintf(resistance_vals_str, sizeof(resistance_vals_str), | ||
280 | "%s - %s - %s - %s", band_data[firstband_color].name, | ||
281 | band_data[secondband_color].name, | ||
282 | band_data[thirdband_color].name, | ||
283 | band_data[fourthband_color].name); | ||
284 | #if LCD_HEIGHT == 80 /* c200 exception */ | ||
285 | rb->lcd_putsxy(resistance_val_x, resistance_val_y-12, resistance_vals_str); | ||
286 | #else /* everything else */ | ||
287 | rb->lcd_putsxy(resistance_val_x, resistance_val_y, resistance_vals_str); | ||
288 | #endif | ||
289 | rb->lcd_update(); | ||
290 | } | ||
291 | |||
292 | #ifndef USE_TEXT_ONLY | 239 | #ifndef USE_TEXT_ONLY |
293 | void draw_resistor(enum color firstband_color, | 240 | void draw_resistor(enum color firstband_color, |
294 | enum color secondband_color, | 241 | enum color secondband_color, |
@@ -296,7 +243,8 @@ void draw_resistor(enum color firstband_color, | |||
296 | enum color fourthband_color) | 243 | enum color fourthband_color) |
297 | { | 244 | { |
298 | rb->lcd_clear_display(); | 245 | rb->lcd_clear_display(); |
299 | rb->lcd_bitmap_transparent(resistor, RESISTOR_BMP_X, RESISTOR_BMP_Y, | 246 | display->set_viewport(&bitmap_vp); |
247 | rb->lcd_bitmap_transparent(resistor, RESISTOR_BMP_X, 0, | ||
300 | BMPWIDTH_resistor, BMPHEIGHT_resistor); | 248 | BMPWIDTH_resistor, BMPHEIGHT_resistor); |
301 | 249 | ||
302 | if(firstband_color != RES_NONE) { | 250 | if(firstband_color != RES_NONE) { |
@@ -336,24 +284,24 @@ void draw_resistor(enum color firstband_color, | |||
336 | rb->lcd_update(); | 284 | rb->lcd_update(); |
337 | return; | 285 | return; |
338 | } | 286 | } |
339 | #else | 287 | #endif |
340 | 288 | ||
341 | void draw_resistor(enum color firstband_color, | 289 | void draw_resistor_text(enum color firstband_color, |
342 | enum color secondband_color, | 290 | enum color secondband_color, |
343 | enum color thirdband_color, | 291 | enum color thirdband_color, |
344 | enum color fourthband_color) | 292 | enum color fourthband_color) |
345 | { | 293 | { |
346 | char resistance_vals_str[64]; | 294 | char resistance_vals_str[64]; |
347 | rb->snprintf(resistance_vals_str, sizeof(resistance_vals_str), | 295 | display->set_viewport(&text_vp); |
296 | rb->snprintf(resistance_vals_str, sizeof(resistance_vals_str), | ||
348 | "%s - %s - %s - %s", band_data[firstband_color].name, | 297 | "%s - %s - %s - %s", band_data[firstband_color].name, |
349 | band_data[secondband_color].name, | 298 | band_data[secondband_color].name, |
350 | band_data[thirdband_color].name, | 299 | band_data[thirdband_color].name, |
351 | band_data[fourthband_color].name); | 300 | band_data[fourthband_color].name); |
352 | rb->lcd_clear_display(); | 301 | rb->lcd_puts_scroll(resistance_val_x, lineno++, resistance_vals_str); |
353 | rb->lcd_puts_scroll(resistance_val_x, resistance_val_y, resistance_vals_str); | 302 | rb->lcd_update(); |
354 | rb->lcd_update(); | ||
355 | } | 303 | } |
356 | #endif | 304 | |
357 | 305 | ||
358 | int calculate_resistance(enum color first_band, | 306 | int calculate_resistance(enum color first_band, |
359 | enum color second_band, | 307 | enum color second_band, |
@@ -414,6 +362,9 @@ enum color do_first_band_menu(void) | |||
414 | case 9: /* White */ | 362 | case 9: /* White */ |
415 | band_color_selection = RES_WHITE; | 363 | band_color_selection = RES_WHITE; |
416 | break; | 364 | break; |
365 | default: | ||
366 | band_color_selection = RES_INVALID; | ||
367 | break; | ||
417 | } | 368 | } |
418 | return band_color_selection; | 369 | return band_color_selection; |
419 | } | 370 | } |
@@ -458,7 +409,10 @@ enum color do_second_band_menu(void) | |||
458 | break; | 409 | break; |
459 | case 9: /* White */ | 410 | case 9: /* White */ |
460 | band_color_selection = RES_WHITE; | 411 | band_color_selection = RES_WHITE; |
461 | break; | 412 | break; |
413 | default: | ||
414 | band_color_selection = RES_INVALID; | ||
415 | break; | ||
462 | } | 416 | } |
463 | return band_color_selection; | 417 | return band_color_selection; |
464 | } | 418 | } |
@@ -501,6 +455,9 @@ enum color do_third_band_menu(void) | |||
501 | case 8: /* Gold */ | 455 | case 8: /* Gold */ |
502 | band_color_selection= RES_GOLD; | 456 | band_color_selection= RES_GOLD; |
503 | break; | 457 | break; |
458 | default: | ||
459 | band_color_selection = RES_INVALID; | ||
460 | break; | ||
504 | } | 461 | } |
505 | return band_color_selection; | 462 | return band_color_selection; |
506 | } | 463 | } |
@@ -530,13 +487,15 @@ enum color do_fourth_band_menu(void) | |||
530 | case 4: /* (none) */ | 487 | case 4: /* (none) */ |
531 | band_color_selection = RES_NONE; | 488 | band_color_selection = RES_NONE; |
532 | break; | 489 | break; |
490 | default: | ||
491 | band_color_selection = RES_INVALID; | ||
492 | break; | ||
533 | } | 493 | } |
534 | return band_color_selection; | 494 | return band_color_selection; |
535 | } | 495 | } |
536 | 496 | ||
537 | void display_helpfile(void) | 497 | void display_helpfile(void) |
538 | { | 498 | { |
539 | rb->splash(HZ/2, "Helpfile"); | ||
540 | rb->lcd_clear_display(); | 499 | rb->lcd_clear_display(); |
541 | /* some information obtained from wikipedia */ | 500 | /* some information obtained from wikipedia */ |
542 | static char * helpfile_text[] = { | 501 | static char * helpfile_text[] = { |
@@ -631,7 +590,7 @@ void display_helpfile(void) | |||
631 | void led_resistance_calc(void) | 590 | void led_resistance_calc(void) |
632 | { | 591 | { |
633 | backlight_force_on(); | 592 | backlight_force_on(); |
634 | int voltage_menu_selection, button_press, i, j, k, l, foreward_current = 0; | 593 | int voltage_menu_selection, button_press, j, k, l, foreward_current = 0; |
635 | int fwd_current_selection = 0; | 594 | int fwd_current_selection = 0; |
636 | bool quit = false; | 595 | bool quit = false; |
637 | char kbd_buffer [5]; | 596 | char kbd_buffer [5]; |
@@ -655,7 +614,6 @@ void led_resistance_calc(void) | |||
655 | enum color multiplier; | 614 | enum color multiplier; |
656 | enum color fourth_band = RES_NONE; | 615 | enum color fourth_band = RES_NONE; |
657 | 616 | ||
658 | rb->splash(HZ/2, "LED resistor calculator"); | ||
659 | rb->lcd_clear_display(); | 617 | rb->lcd_clear_display(); |
660 | 618 | ||
661 | MENUITEM_STRINGLIST(voltage_menu, "Select LED voltage:", NULL, | 619 | MENUITEM_STRINGLIST(voltage_menu, "Select LED voltage:", NULL, |
@@ -667,15 +625,22 @@ void led_resistance_calc(void) | |||
667 | "Key in other (only if already known)"); | 625 | "Key in other (only if already known)"); |
668 | 626 | ||
669 | while(!quit) { | 627 | while(!quit) { |
670 | voltage_menu_selection = rb->do_menu(&voltage_menu, | 628 | int ret; |
629 | ret = voltage_menu_selection = rb->do_menu(&voltage_menu, | ||
671 | &voltage_menu_selection, NULL, false); | 630 | &voltage_menu_selection, NULL, false); |
672 | fwd_current_selection = rb->do_menu(&fwd_current_menu, | 631 | if(ret<0) break; |
632 | ret = fwd_current_selection = rb->do_menu(&fwd_current_menu, | ||
673 | &fwd_current_selection, NULL, false); | 633 | &fwd_current_selection, NULL, false); |
634 | if(ret<0) break; | ||
674 | rb->lcd_clear_display(); | 635 | rb->lcd_clear_display(); |
636 | |||
637 | |||
675 | rb->splash(HZ*2, "(First) Input the supply voltage:"); | 638 | rb->splash(HZ*2, "(First) Input the supply voltage:"); |
676 | for(i = 0; i < 5; i++) {kbd_buffer[i] = 0; fwd_kbd_buffer[i] = 0;} | 639 | memset(kbd_buffer,0,sizeof(kbd_buffer)); |
677 | rb->kbd_input(kbd_buffer, sizeof(kbd_buffer)); | 640 | rb->kbd_input(kbd_buffer, sizeof(kbd_buffer)); |
678 | input_voltage = rb->atoi(kbd_buffer); | 641 | input_voltage = rb->atoi(kbd_buffer); |
642 | if(input_voltage == 0) break; | ||
643 | |||
679 | if(input_voltage != (int)input_voltage) { | 644 | if(input_voltage != (int)input_voltage) { |
680 | input_voltage *= 10; | 645 | input_voltage *= 10; |
681 | } | 646 | } |
@@ -708,12 +673,14 @@ void led_resistance_calc(void) | |||
708 | case 1: | 673 | case 1: |
709 | rb->lcd_clear_display(); | 674 | rb->lcd_clear_display(); |
710 | rb->splash(HZ*2, "Input the foreward current, in mA"); | 675 | rb->splash(HZ*2, "Input the foreward current, in mA"); |
676 | memset(fwd_kbd_buffer,0,sizeof(fwd_kbd_buffer)); | ||
711 | rb->kbd_input(fwd_kbd_buffer, sizeof(fwd_kbd_buffer)); | 677 | rb->kbd_input(fwd_kbd_buffer, sizeof(fwd_kbd_buffer)); |
712 | |||
713 | foreward_current = ((rb->atoi(fwd_kbd_buffer))/10); | 678 | foreward_current = ((rb->atoi(fwd_kbd_buffer))/10); |
714 | break; | 679 | break; |
715 | } | 680 | } |
716 | 681 | ||
682 | if(foreward_current == 0) break; | ||
683 | |||
717 | rb->lcd_clear_display(); | 684 | rb->lcd_clear_display(); |
718 | 685 | ||
719 | resistance = (input_voltage - led_voltage) / foreward_current; | 686 | resistance = (input_voltage - led_voltage) / foreward_current; |
@@ -724,147 +691,113 @@ void led_resistance_calc(void) | |||
724 | 691 | ||
725 | if(led_voltage > input_voltage) { | 692 | if(led_voltage > input_voltage) { |
726 | rb->splash(HZ, "Problem: LED voltage is higher than the source."); | 693 | rb->splash(HZ, "Problem: LED voltage is higher than the source."); |
694 | break; | ||
727 | } | 695 | } |
728 | else { | ||
729 | 696 | ||
730 | for(j = 0; j < total_common_values; j++) { | 697 | for(j = 0; j < total_common_values; j++) { |
731 | for(k = 1; k < 5; k++) { | 698 | for(k = 1; k < 5; k++) { |
732 | if( resistance == (common_values[j] * powi(10, k))) { | 699 | if( resistance == (common_values[j] * powi(10, k))) { |
733 | rounded_resistance = (common_values[j] * powi(10, k)); | 700 | rounded_resistance = (common_values[j] * powi(10, k)); |
734 | /* perfect match */ | 701 | /* perfect match */ |
735 | break; | ||
736 | } | ||
737 | else if(resistance >= (common_values[j] * powi(10, k)) && | ||
738 | resistance <= (common_values[j+1] * powi(10, k))) { | ||
739 | rounded_resistance = (common_values[j+1] * powi(10, k)); | ||
740 | /* the higher resistance, to be safe */ | ||
741 | break; | ||
742 | } | ||
743 | else { break; } | ||
744 | } | ||
745 | } | ||
746 | |||
747 | power_rating_in = ((input_voltage/100)*(input_voltage/100)*1000 / rounded_resistance); | ||
748 | /* in mW */ | ||
749 | for(l = 0; l < total_power_values; l++) { | ||
750 | if((int)power_rating_in == power_ratings[l]) { | ||
751 | rounded_power_rating = (power_ratings[l]); | ||
752 | break; | 702 | break; |
753 | } | 703 | } |
754 | else if(power_rating_in >= power_ratings[l] && | 704 | else if(resistance >= (common_values[j] * powi(10, k)) && |
755 | power_rating_in <= power_ratings[l+1]) { | 705 | resistance <= (common_values[j+1] * powi(10, k))) { |
756 | rounded_power_rating = power_ratings[l+1]; | 706 | rounded_resistance = (common_values[j+1] * powi(10, k)); |
707 | /* the higher resistance, to be safe */ | ||
757 | break; | 708 | break; |
758 | } | 709 | } |
759 | else { break; } | 710 | else { break; } |
760 | } | 711 | } |
712 | } | ||
761 | 713 | ||
762 | get_power_rating_str(rounded_power_rating); | 714 | if(rounded_resistance == 0) |
763 | 715 | { | |
764 | power_ten = get_power_ten(rounded_resistance); | 716 | rb->splash(HZ, "Problem: Input voltage too high."); |
765 | if(rounded_resistance / powi(10, power_ten) == 1) { | 717 | break; |
766 | while(rounded_resistance /powi(10, power_ten) == 1) { | 718 | } |
767 | power_ten--; | 719 | power_rating_in = ((input_voltage/100)*(input_voltage/100)*1000 / rounded_resistance); |
768 | } | 720 | /* in mW */ |
721 | for(l = 0; l < total_power_values; l++) { | ||
722 | if((int)power_rating_in == power_ratings[l]) { | ||
723 | rounded_power_rating = (power_ratings[l]); | ||
724 | break; | ||
769 | } | 725 | } |
770 | 726 | else if(power_rating_in >= power_ratings[l] && | |
771 | if(rounded_resistance/powi(10, power_ten) != (int)rounded_resistance) { | 727 | power_rating_in <= power_ratings[l+1]) { |
772 | power_ten--; } | 728 | rounded_power_rating = power_ratings[l+1]; |
773 | rounded_resistance /= powi(10, power_ten); | 729 | break; |
774 | |||
775 | if(rounded_resistance < 10) { | ||
776 | first_band_int = rounded_resistance; } | ||
777 | else { first_band_int = rounded_resistance /10; } | ||
778 | second_band_int += rounded_resistance % 10; | ||
779 | |||
780 | if(first_band_int == 10) { | ||
781 | first_band_int /= 10; | ||
782 | second_band_int = 0; | ||
783 | power_ten++; | ||
784 | } | 730 | } |
731 | else { break; } | ||
732 | } | ||
785 | 733 | ||
786 | if(first_band_int > 10) { | 734 | get_power_rating_str(rounded_power_rating); |
787 | int temp; | 735 | |
788 | temp = first_band_int /10; | 736 | power_ten = get_power_ten(rounded_resistance); |
789 | second_band_int = first_band_int % 10; | 737 | if(rounded_resistance / powi(10, power_ten) == 1) { |
790 | first_band_int = temp; | 738 | while(rounded_resistance /powi(10, power_ten) == 1) { |
739 | power_ten--; | ||
791 | } | 740 | } |
792 | rounded_resistance *= 10; | 741 | } |
793 | |||
794 | if(rounded_resistance >= 1000) { | ||
795 | rounded_resistance /= 10; } | ||
796 | /*kludge, maybe. But it fixes the problem (100 ohms graphically, | ||
797 | 1000 ohms in text displayed */ | ||
798 | |||
799 | first_band = get_band_rtoc(first_band_int); | ||
800 | second_band = get_band_rtoc(second_band_int); | ||
801 | multiplier = get_band_rtoc(power_ten); | ||
802 | 742 | ||
803 | rb->lcd_clear_display(); | 743 | if(rounded_resistance/powi(10, power_ten) != (int)rounded_resistance) { |
804 | draw_resistor(first_band, second_band, multiplier, fourth_band); | 744 | power_ten--; } |
805 | #ifdef HAVE_LCD_COLOR | 745 | rounded_resistance /= powi(10, power_ten); |
806 | draw_resistor_text(first_band, second_band, multiplier, fourth_band); | 746 | |
807 | #endif | 747 | if(rounded_resistance < 10) { |
748 | first_band_int = rounded_resistance; } | ||
749 | else { first_band_int = rounded_resistance /10; } | ||
750 | second_band_int += rounded_resistance % 10; | ||
751 | |||
752 | if(first_band_int == 10) { | ||
753 | first_band_int /= 10; | ||
754 | second_band_int = 0; | ||
755 | power_ten++; | ||
756 | } | ||
757 | |||
758 | if(first_band_int > 10) { | ||
759 | int temp; | ||
760 | temp = first_band_int /10; | ||
761 | second_band_int = first_band_int % 10; | ||
762 | first_band_int = temp; | ||
763 | } | ||
764 | rounded_resistance *= 10; | ||
808 | 765 | ||
809 | if(fwd_current_selection == 0) { | 766 | if(rounded_resistance >= 1000) { |
810 | rb->snprintf(current_out_str, sizeof(current_out_str), "20mA"); | 767 | rounded_resistance /= 10; } |
811 | } | 768 | /*kludge, maybe. But it fixes the problem (100 ohms graphically, |
812 | else if(fwd_current_selection == 1) { | 769 | 1000 ohms in text displayed */ |
813 | rb->snprintf(current_out_str, sizeof(current_out_str), "%d mA", | 770 | |
814 | (foreward_current*10)); | 771 | first_band = get_band_rtoc(first_band_int); |
815 | } | 772 | second_band = get_band_rtoc(second_band_int); |
816 | 773 | multiplier = get_band_rtoc(power_ten); | |
817 | #if (LCD_HEIGHT >= 128) | 774 | |
818 | rb->snprintf(true_current_out_str, sizeof(true_current_out_str), | 775 | rb->lcd_clear_display(); |
819 | "Input: %dv, %d Ohms @ %s", (input_voltage/100), | 776 | lineno = INITIAL_TEXT_Y; |
820 | out_int, current_out_str); | 777 | #ifndef USE_TEXT_ONLY |
821 | rb->snprintf(rounded_resistance_out_str, | 778 | draw_resistor(first_band, second_band, multiplier, fourth_band); |
822 | sizeof(rounded_resistance_out_str), | 779 | #endif |
823 | "Rounded/displayed: [%d %s]", rounded_resistance, | 780 | draw_resistor_text(first_band, second_band, multiplier, fourth_band); |
824 | band_data[multiplier].unit); | 781 | |
825 | rb->snprintf(power_rating_out_str, sizeof(power_rating_out_str), | 782 | rb->snprintf(current_out_str, sizeof(current_out_str), "%d mA", |
826 | "Reccomended: %s or greater", power_rating_str); | 783 | (foreward_current*10)); |
827 | #if (LCD_HEIGHT == 128) /* Fixes an issue with the M:Robe 100 */ | 784 | |
828 | rb->lcd_putsxy(1, resistance_val_y+25, true_current_out_str); | 785 | rb->snprintf(true_current_out_str, sizeof(true_current_out_str), |
829 | rb->lcd_putsxy(1, resistance_val_y+40, rounded_resistance_out_str); | 786 | "Input: %dv, %d Ohms @ %s", (input_voltage/100), |
830 | rb->lcd_putsxy(1, resistance_val_y+55, power_rating_out_str); | 787 | out_int, current_out_str); |
831 | #else /* LCD_HEIGHT == 128 */ | 788 | rb->snprintf(rounded_resistance_out_str, |
832 | rb->lcd_putsxy(1, resistance_val_y+15, true_current_out_str); | 789 | sizeof(rounded_resistance_out_str), |
833 | rb->lcd_putsxy(1, resistance_val_y+27, rounded_resistance_out_str); | 790 | "Rounded/displayed: [%d %s]", rounded_resistance, |
834 | rb->lcd_putsxy(1, resistance_val_y+39, power_rating_out_str); | 791 | band_data[multiplier].unit); |
835 | #endif /* LCD_HEIGHT == 128 */ | 792 | rb->snprintf(power_rating_out_str, sizeof(power_rating_out_str), |
836 | 793 | "Recommended: %s or greater", power_rating_str); | |
837 | #else /* (LCD_HEIGHT >= 128) */ | 794 | |
838 | rb->snprintf(true_current_out_str, sizeof(true_current_out_str), | 795 | display->set_viewport(&text_vp); |
839 | "Input:%dv, %d Ohms @ %s", (input_voltage/100), | 796 | rb->lcd_puts_scroll(resistance_val_x, lineno++, true_current_out_str); |
840 | out_int, current_out_str); | 797 | rb->lcd_puts_scroll(resistance_val_x, lineno++, rounded_resistance_out_str); |
841 | rb->snprintf(rounded_resistance_out_str, | 798 | rb->lcd_puts_scroll(resistance_val_x, lineno++, power_rating_out_str); |
842 | sizeof(rounded_resistance_out_str), "Rounded: [%d %s]", | 799 | |
843 | rounded_resistance, band_data[multiplier].unit); | 800 | rb->lcd_update(); |
844 | rb->snprintf(power_rating_out_str, sizeof(power_rating_out_str), | ||
845 | "Reccommended: %s", power_rating_str); | ||
846 | |||
847 | #if (LCD_HEIGHT == 80) /* For c200 */ | ||
848 | rb->lcd_putsxy(0, resistance_val_y, true_current_out_str); | ||
849 | rb->lcd_putsxy(0, resistance_val_y+10, rounded_resistance_out_str); | ||
850 | rb->lcd_putsxy(0, resistance_val_y+20, power_rating_out_str); | ||
851 | #else | ||
852 | rb->lcd_puts(resistance_val_x, resistance_val_y+2, | ||
853 | true_current_out_str); | ||
854 | rb->lcd_puts(resistance_val_x, resistance_val_y+3, | ||
855 | rounded_resistance_out_str); | ||
856 | rb->lcd_puts(resistance_val_x, resistance_val_y+4, | ||
857 | power_rating_out_str); | ||
858 | #endif /* LCD_HEIGHT == 80 */ | ||
859 | |||
860 | #endif /* (LCD_HEIGHT >= 128) */ | ||
861 | /* I have created these separate routines for the different LCDs | ||
862 | instead of using lcd_puts_scroll() because it is too buggy | ||
863 | to use more than once apparently - the scrolling text is not | ||
864 | removed by lcd_clear_display() and can still be seen in the | ||
865 | main menu.*/ | ||
866 | rb->lcd_update(); | ||
867 | } | ||
868 | 801 | ||
869 | button_press = rb->button_get(true); | 802 | button_press = rb->button_get(true); |
870 | switch(button_press) { | 803 | switch(button_press) { |
@@ -876,6 +809,10 @@ void led_resistance_calc(void) | |||
876 | break; | 809 | break; |
877 | } | 810 | } |
878 | } | 811 | } |
812 | display->set_viewport(&text_vp); | ||
813 | rb->lcd_stop_scroll(); | ||
814 | display->set_viewport(&screen_vp); | ||
815 | rb->lcd_clear_display(); | ||
879 | } | 816 | } |
880 | 817 | ||
881 | 818 | ||
@@ -913,14 +850,18 @@ void resistance_to_color(void) | |||
913 | "5%", "10%", "1%", "2%", "20%"); | 850 | "5%", "10%", "1%", "2%", "20%"); |
914 | 851 | ||
915 | while(!quit) { | 852 | while(!quit) { |
916 | menu_selection = rb->do_menu(&r_to_c_menu, &menu_selection, | 853 | int ret; |
854 | ret=menu_selection = rb->do_menu(&r_to_c_menu, &menu_selection, | ||
917 | NULL, false); | 855 | NULL, false); |
856 | if(ret<0) break; | ||
918 | 857 | ||
919 | rb->kbd_input(kbd_buffer, sizeof(kbd_buffer)); | 858 | rb->kbd_input(kbd_buffer, sizeof(kbd_buffer)); |
920 | /* As stated above somewhere, we (I) need to make a calculator-like | 859 | /* As stated above somewhere, we (I) need to make a calculator-like |
921 | keypad, that keyboard isn't all that fun to use. */ | 860 | keypad, that keyboard isn't all that fun to use. */ |
922 | menu_selection_tol = rb->do_menu(&r_to_c_menu_tol, &menu_selection_tol, | 861 | ret = rb->do_menu(&r_to_c_menu_tol, &menu_selection_tol, |
923 | NULL, false); | 862 | NULL, false); |
863 | if(ret<0) break; | ||
864 | |||
924 | switch(menu_selection_tol) { | 865 | switch(menu_selection_tol) { |
925 | case 0: /* 5% */ | 866 | case 0: /* 5% */ |
926 | fourth_band = RES_GOLD; | 867 | fourth_band = RES_GOLD; |
@@ -990,17 +931,16 @@ void resistance_to_color(void) | |||
990 | multiplier = get_band_rtoc(power_ten); | 931 | multiplier = get_band_rtoc(power_ten); |
991 | 932 | ||
992 | rb->lcd_clear_display(); | 933 | rb->lcd_clear_display(); |
934 | lineno = INITIAL_TEXT_Y; | ||
935 | #ifndef USE_TEXT_ONLY | ||
993 | draw_resistor(first_band, second_band, multiplier, fourth_band); | 936 | draw_resistor(first_band, second_band, multiplier, fourth_band); |
994 | 937 | #endif | |
995 | #ifdef HAVE_LCD_COLOR /* This seems backwards, but is really only | 938 | draw_resistor_text(first_band, second_band, multiplier, fourth_band); |
996 | necessary on color targets */ | ||
997 | draw_resistor_text(first_band, second_band, | ||
998 | multiplier, fourth_band); | ||
999 | #endif | ||
1000 | 939 | ||
1001 | rb->snprintf(out_str, sizeof(out_str), "Input: %d %s", in_resistance_int, | 940 | rb->snprintf(out_str, sizeof(out_str), "Input: %d %s", in_resistance_int, |
1002 | band_data[units_used].unit); | 941 | band_data[units_used].unit); |
1003 | rb->lcd_putsxy(r_to_c_out_str_x, r_to_c_out_str_y, out_str); | 942 | display->set_viewport(&text_vp); |
943 | rb->lcd_puts_scroll(r_to_c_out_str_x, lineno++, out_str); | ||
1004 | rb->lcd_update(); | 944 | rb->lcd_update(); |
1005 | 945 | ||
1006 | button_press = rb->button_get(true); | 946 | button_press = rb->button_get(true); |
@@ -1013,6 +953,10 @@ void resistance_to_color(void) | |||
1013 | break; | 953 | break; |
1014 | } | 954 | } |
1015 | } | 955 | } |
956 | display->set_viewport(&text_vp); | ||
957 | rb->lcd_stop_scroll(); | ||
958 | display->set_viewport(&screen_vp); | ||
959 | rb->lcd_clear_display(); | ||
1016 | } | 960 | } |
1017 | 961 | ||
1018 | void color_to_resistance(void) | 962 | void color_to_resistance(void) |
@@ -1035,18 +979,29 @@ void color_to_resistance(void) | |||
1035 | 979 | ||
1036 | while(!quit) { | 980 | while(!quit) { |
1037 | first_band = do_first_band_menu(); | 981 | first_band = do_first_band_menu(); |
982 | if(first_band==RES_INVALID) break; | ||
983 | |||
1038 | second_band = do_second_band_menu(); | 984 | second_band = do_second_band_menu(); |
985 | if(second_band==RES_INVALID) break; | ||
986 | |||
1039 | third_band = do_third_band_menu(); | 987 | third_band = do_third_band_menu(); |
988 | if(third_band==RES_INVALID) break; | ||
989 | |||
1040 | fourth_band = do_fourth_band_menu(); | 990 | fourth_band = do_fourth_band_menu(); |
991 | if(third_band==RES_INVALID) break; | ||
1041 | 992 | ||
1042 | total_resistance_centiunits = calculate_resistance(first_band, | 993 | total_resistance_centiunits = calculate_resistance(first_band, |
1043 | second_band, | 994 | second_band, |
1044 | third_band); | 995 | third_band); |
1045 | get_tolerance_str(fourth_band); | 996 | get_tolerance_str(fourth_band); |
997 | |||
998 | rb->lcd_clear_display(); | ||
999 | lineno = INITIAL_TEXT_Y; | ||
1000 | #ifndef USE_TEXT_ONLY | ||
1046 | draw_resistor(first_band, second_band, third_band, fourth_band); | 1001 | draw_resistor(first_band, second_band, third_band, fourth_band); |
1047 | #ifndef USE_TEXT_ONLY | 1002 | #endif |
1048 | rb->lcd_set_foreground(LCD_WHITE); | 1003 | draw_resistor_text(first_band, second_band, third_band, fourth_band); |
1049 | #endif | 1004 | |
1050 | if(total_resistance_centiunits % 100 == 0) | 1005 | if(total_resistance_centiunits % 100 == 0) |
1051 | { | 1006 | { |
1052 | /* No decimals */ | 1007 | /* No decimals */ |
@@ -1063,9 +1018,10 @@ void color_to_resistance(void) | |||
1063 | total_resistance_centiunits%100, | 1018 | total_resistance_centiunits%100, |
1064 | unit_abbrev); | 1019 | unit_abbrev); |
1065 | } | 1020 | } |
1066 | rb->lcd_putsxy(total_resistance_str_x, total_resistance_str_y, | 1021 | display->set_viewport(&text_vp); |
1022 | rb->lcd_puts_scroll(total_resistance_str_x, lineno++, | ||
1067 | total_resistance_str); | 1023 | total_resistance_str); |
1068 | rb->lcd_putsxy(tolerance_str_x, tolerance_str_y, tolerance_str); | 1024 | rb->lcd_puts_scroll(tolerance_str_x, lineno++, tolerance_str); |
1069 | rb->lcd_update(); | 1025 | rb->lcd_update(); |
1070 | 1026 | ||
1071 | button_input = rb->button_get(true); | 1027 | button_input = rb->button_get(true); |
@@ -1080,6 +1036,10 @@ void color_to_resistance(void) | |||
1080 | break; | 1036 | break; |
1081 | } | 1037 | } |
1082 | } | 1038 | } |
1039 | display->set_viewport(&text_vp); | ||
1040 | rb->lcd_stop_scroll(); | ||
1041 | display->set_viewport(&screen_vp); | ||
1042 | rb->lcd_clear_display(); | ||
1083 | return; | 1043 | return; |
1084 | } | 1044 | } |
1085 | 1045 | ||
@@ -1090,12 +1050,23 @@ enum plugin_status plugin_start(const void* nothing) | |||
1090 | rb->lcd_update(); | 1050 | rb->lcd_update(); |
1091 | int main_menu_selection = 0; | 1051 | int main_menu_selection = 0; |
1092 | bool menuquit = false; | 1052 | bool menuquit = false; |
1093 | int event; | 1053 | |
1094 | 1054 | display = rb->screens[0]; | |
1055 | rb->viewport_set_defaults(&screen_vp,0); | ||
1056 | rb->viewport_set_defaults(&text_vp,0); | ||
1057 | rb->viewport_set_defaults(&bitmap_vp,0); | ||
1058 | #ifndef USE_TEXT_ONLY | ||
1059 | bitmap_vp.y = RESISTOR_BMP_Y + screen_vp.y; | ||
1060 | bitmap_vp.height = BMPHEIGHT_resistor; | ||
1061 | text_vp.y = bitmap_vp.y + bitmap_vp.height; | ||
1062 | text_vp.height = screen_vp.height - text_vp.y; | ||
1063 | #endif | ||
1064 | |||
1095 | MENUITEM_STRINGLIST(main_menu, "Resistor Code Calculator:", NULL, | 1065 | MENUITEM_STRINGLIST(main_menu, "Resistor Code Calculator:", NULL, |
1096 | "Colours -> Resistance", "Resistance -> Colours", | 1066 | "Colours -> Resistance", "Resistance -> Colours", |
1097 | "LED resistor calculator", "Help", "Exit"); | 1067 | "LED resistor calculator", "Help", "Exit"); |
1098 | while (!menuquit) { | 1068 | while (!menuquit) { |
1069 | display->set_viewport(&screen_vp); | ||
1099 | main_menu_selection = rb->do_menu(&main_menu, &main_menu_selection, | 1070 | main_menu_selection = rb->do_menu(&main_menu, &main_menu_selection, |
1100 | NULL, false); | 1071 | NULL, false); |
1101 | switch(main_menu_selection) { | 1072 | switch(main_menu_selection) { |
@@ -1114,10 +1085,9 @@ enum plugin_status plugin_start(const void* nothing) | |||
1114 | case 4: | 1085 | case 4: |
1115 | menuquit = true; | 1086 | menuquit = true; |
1116 | break; | 1087 | break; |
1088 | case MENU_ATTACHED_USB: | ||
1089 | return PLUGIN_USB_CONNECTED; | ||
1117 | } | 1090 | } |
1118 | event = rb->button_get(true); | ||
1119 | if(rb->default_event_handler(event) == SYS_USB_CONNECTED) { | ||
1120 | return PLUGIN_USB_CONNECTED; } | ||
1121 | } | 1091 | } |
1122 | return PLUGIN_OK; | 1092 | return PLUGIN_OK; |
1123 | } | 1093 | } |