Index: apps/plugins/CATEGORIES
===================================================================
--- apps/plugins/CATEGORIES	(revision 27856)
+++ apps/plugins/CATEGORIES	(working copy)
@@ -73,6 +73,7 @@
 properties,viewers
 random_folder_advance_config,apps
 remote_control,apps
+resistor,apps
 reversi,games
 robotfindskitten,games
 rockblox,games
Index: apps/plugins/bitmaps/native/SOURCES
===================================================================
--- apps/plugins/bitmaps/native/SOURCES	(revision 27856)
+++ apps/plugins/bitmaps/native/SOURCES	(working copy)
@@ -886,5 +886,13 @@
 #endif
 #endif /* Complex condition for pitch detector */
 
+/* Resistor Calculator */
+#if (LCD_WIDTH == 176) && (LCD_DEPTH == 16) /* Sansa e200 and iPod Nano */
+resistor.176x220x16.bmp
+#elif (LCD_WIDTH == 220) && (LCD_DEPTH == 16) /* Sansa Fuze */
+resistor.220x176x16.bmp
+#else
+resistor.112x64x1.bmp
+#endif
 
 #endif /* HAVE_LCD_BITMAP */
Index: apps/plugins/SOURCES
===================================================================
--- apps/plugins/SOURCES	(revision 27856)
+++ apps/plugins/SOURCES	(working copy)
@@ -150,6 +150,8 @@
 xobox.c
 spacerocks.c
 
+resistor.c
+
 /* Plugins needing the grayscale lib on low-depth LCDs */
 fire.c
 plasma.c
Index: apps/plugins/resistor.c
===================================================================
--- apps/plugins/resistor.c	(revision 0)
+++ apps/plugins/resistor.c	(revision 0)
@@ -0,0 +1,1213 @@
+/* === Rockbox Resistor code/value calculator ===
+TODO:
+[in progress, note #2] Entire keyed-in resistance to colors
+[partial - Note#1] Fix bugs regarding calculations
+[ in progress] Support more targets, even greyscale/BW targets
+	
+[note #1]: I know that Brown-Black-Brown is NOT 0 Ohms at all, and I see no
+           fault in my code that would cause that to happen. This is proboably
+           my fault somewhere.
+[note #2]: I have already spent a great deal of time experimenting with
+           routines to allow keyed-in values, but would like to take
+           a break from dealing with it. I believe someone else should take
+           a stab at it, or at least help me with the process, I'm getting
+           kind of sick at looking at the words 'resistor', 'resistance',
+           'band' all day for the past month.
+*/
+ 
+#include "plugin.h"
+#include "lib/display_text.h"
+#include "lib/pluginlib_actions.h"
+#include "lib/picture.h"
+
+
+/* Defining player-specific constants */
+
+#if (CONFIG_KEYPAD == SANSA_M200_PAD) || \
+    (CONFIG_KEYPAD == SANSA_CLIP_PAD)
+#include "pluginbitmaps/resistor.h"
+/* This isn't used at all, but it seems critical to the compiler */
+#define    resistance_val_x         0
+#define    resistance_val_y         1
+#define    tolerance_str_x          1
+#define    tolerance_str_y          60
+#define    total_resistance_str_x   1
+#define    total_resistance_str_y   40
+#define    resistance_txt_x         1
+#define    resistance_txt_y         40
+#define    r_to_c_out_str_x         1
+#define    r_to_c_out_str_y         50
+
+#elif (CONFIG_LCD == LCD_FUZE)
+#include "pluginbitmaps/resistor.h"
+#define    first_band_x             56
+#define    second_band_x            86
+#define    third_band_x             116
+#define    fourth_band_x            162
+#define    universal_y              19
+#define    band_width               8
+#define    band_height              58
+#define    RESISTOR_BMP_X           12
+#define    RESISTOR_BMP_Y           15
+#define    RESISTOR_BMP_WIDTH       200
+#define    RESISTOR_BMP_HEIGHT      66
+#define    resistance_txt_x         6
+#define    resistance_txt_y         90
+#define    total_resistance_str_x   15
+#define    total_resistance_str_y   100
+#define    tolerance_str_x          15
+#define    tolerance_str_y          110
+#define    r_to_c_out_str_x         15
+#define    r_to_c_out_str_y         140
+
+#elif (CONFIG_LCD == LCD_X5) /* Sansa e200. There really ought to be
+                                   a separate entry in config.h */
+#include "pluginbitmaps/resistor.h"
+#define    first_band_x            40
+#define    second_band_x           72
+#define    third_band_x            94
+#define    fourth_band_x           120
+#define    universal_y             14
+#define    band_width              11
+#define    band_height             49
+#define    RESISTOR_BMP_X          3
+#define    RESISTOR_BMP_Y          11
+#define    RESISTOR_BMP_WIDTH      170
+#define    RESISTOR_BMP_HEIGHT     56
+#define    total_resistance_str_x  13
+#define    total_resistance_str_y  140
+#define    resistance_txt_x        6
+#define    resistance_txt_y        80
+#define    tolerance_str_x         13
+#define    tolerance_str_y         150
+#define    r_to_c_out_str_x        13
+#define    r_to_c_out_str_y        150
+
+#elif (CONFIG_LCD == IPODNANO) /* Note: Shares e200's BMP file. */
+#include "pluginbitmaps/resistor.h"
+#define    first_band_x            40
+#define    second_band_x           72
+#define    third_band_x            94
+#define    fourth_band_x           120
+#define    universal_y             10
+#define    band_width              11
+#define    band_height             50
+#define    RESISTOR_BMP_X          3
+#define    RESISTOR_BMP_Y          7
+#define    RESISTOR_BMP_WIDTH      170
+#define    RESISTOR_BMP_HEIGHT     56
+#define    total_resistance_str_x  13
+#define    total_resistance_str_y  90
+#define    resistance_txt_x        6
+#define    resistance_txt_y        80
+#define    tolerance_str_x         13
+#define    tolerance_str_y         100
+#define    r_to_c_out_str_x        13
+#define    r_to_c_out_str_y        100
+
+#else
+#error Player target not yet supported
+#endif
+
+/* Colors of bands */
+#ifdef HAVE_LCD_COLOR
+#define    RES_BLACK      LCD_RGBPACK(0, 0, 0)
+#define    RES_BLACK_2    LCD_RGBPACK(10, 10, 10)
+#define    RES_BROWN      LCD_RGBPACK(118, 78, 0)
+#define    RES_RED        LCD_RGBPACK(255, 0, 0)
+#define    RES_ORANGE     LCD_RGBPACK(255, 199, 76)
+#define    RES_YELLOW     LCD_RGBPACK(255, 255, 0)
+#define    RES_GREEN      LCD_RGBPACK(0, 128, 0)
+#define    RES_BLUE       LCD_RGBPACK(0, 0, 255)
+#define    RES_VIOLET     LCD_RGBPACK(153, 51, 255)
+#define    RES_GREY       LCD_RGBPACK(192, 192, 192)
+#define    RES_WHITE      LCD_RGBPACK(255, 255, 255)
+#define    RES_WHITE_2    LCD_RGBPACK(245, 245, 245)
+#define    RES_GOLD       LCD_RGBPACK(146, 146, 0)
+#define    RES_SILVER     LCD_RGBPACK(213, 213, 213)
+#define    RES_NONE       666
+#else
+#define    RES_BLACK    0
+#define    RES_BROWN    1
+#define    RES_RED      2
+#define    RES_ORANGE   3
+#define    RES_YELLOW   4
+#define    RES_GREEN    5
+#define    RES_BLUE     6
+#define    RES_VIOLET   7
+#define    RES_GREY     8
+#define    RES_WHITE    9
+#define    RES_GOLD     10
+#define    RES_SILVER   11
+#define    RES_NONE     12
+#endif
+
+/* Values of first and second bands */
+#define    RES_BLACK_VALUE    0
+#define    RES_BROWN_VALUE    10
+#define    RES_RED_VALUE      20
+#define    RES_ORANGE_VALUE   30
+#define    RES_YELLOW_VALUE   40
+#define    RES_GREEN_VALUE    50
+#define    RES_BLUE_VALUE     60
+#define    RES_VIOLET_VALUE   70
+#define    RES_GREY_VALUE     80
+#define    RES_WHITE_VALUE    90
+
+#define    RES_BLACK_SECONDARY_VALUE    0
+#define    RES_BROWN_SECONDARY_VALUE    1
+#define    RES_RED_SECONDARY_VALUE      2
+#define    RES_ORANGE_SECONDARY_VALUE   3
+#define    RES_YELLOW_SECONDARY_VALUE   4
+#define    RES_GREEN_SECONDARY_VALUE    5
+#define    RES_BLUE_SECONDARY_VALUE     6
+#define    RES_VIOLET_SECONDARY_VALUE   7
+#define    RES_GREY_SECONDARY_VALUE     8
+#define    RES_WHITE_SECONDARY_VALUE    9
+
+/* Values of third band (multiplier) */
+#define    RES_BLACK_MUL    1
+#define    RES_BROWN_MUL    10
+#define    RES_RED_MUL      100
+#define    RES_ORANGE_MUL   1000
+#define    RES_YELLOW_MUL   10000
+#define    RES_GREEN_MUL    100000
+#define    RES_BLUE_MUL     1000000
+#define    RES_GOLD_MUL     -1
+#define    RES_SILVER_MUL   -2
+
+/* Values of fourth band (tolerance) */
+#define    RES_BROWN_TOL   1
+#define    RES_RED_TOL     2
+#define    RES_GOLD_TOL    5
+#define    RES_SILVER_TOL  10
+#define    RES_NONE_TOL    20
+
+/* For use in the color_to_resistance_menu() function */
+#define    firstband_m    0
+#define    secondband_m   1
+#define    thirdband_m    2
+#define    fourthband_m   3
+
+/* Units */
+#define    UNIT_OHMS      0
+#define    UNIT_KOHMS     1
+#define    UNIT_MOHMS     2
+
+PLUGIN_HEADER
+
+char unit_abbrev [5]; /* 5 chars */
+char tolerance_str [14]; /* 14 chars */				
+int r_to_c_firstband_int;
+int r_to_c_secondband_int;
+int r_to_c_thirdband_int;
+int r_to_c_firstband_color;
+int r_to_c_secondband_color;
+int r_to_c_thirdband_color;
+
+char first_str [7];
+char second_str [7];
+char third_str [7];
+char fourth_str [7];
+
+/* get_first_band_int() */
+int temp_val = 0;
+
+void get_unit_str(int in_unit)
+{
+    int i;
+    for (i = 0; i < 5; i++) {unit_abbrev[i] = 0;}
+    
+    switch(in_unit) {
+        case UNIT_OHMS:
+            rb->snprintf(unit_abbrev, sizeof(unit_abbrev), "Ohms");
+            break;
+        case UNIT_KOHMS:
+            rb->snprintf(unit_abbrev, sizeof(unit_abbrev), "KOhms");
+            break;
+        case UNIT_MOHMS:
+            rb->snprintf(unit_abbrev, sizeof(unit_abbrev), "MOhms");
+            break;
+        }
+}
+
+int powi(int num, int exp)
+{
+    int i, product = 1;
+    for (i = 0; i < exp; i++) {
+    product *= num; }
+
+    return product;
+}
+
+int get_power_ten(float in_val)
+{
+    int power = 0;
+    if(in_val <= 9 && in_val >= 0) { power = 0; }
+    else if(in_val <= 99 && in_val >= 10) {power = 1;}
+    else if(in_val <= 999 && in_val >= 100) {power = 2;}
+    else if(in_val <= 9999 && in_val >= 1000) {power = 3;}
+    else if(in_val <= 99999 && in_val >= 10000) {power = 4;}
+    else if(in_val <= 999999 && in_val >= 100000) {power = 5;}
+    else if(in_val <= 9999999 && in_val >= 1000000) {power = 6;}   
+    return power;
+}
+
+            
+int get_band_color_rtoc(int in_val)
+{
+    int return_color;
+    switch(in_val) {
+        case 0:
+            return_color = RES_BLACK;
+            break;
+        case 1:
+            return_color = RES_BROWN;
+            break;
+        case 2:
+            return_color = RES_RED;
+            break;
+        case 3:
+            return_color = RES_ORANGE;
+            break;
+        case 4:
+            return_color = RES_YELLOW;
+            break;
+        case 5:
+            return_color = RES_GREEN;
+            break;
+        case 6:
+            return_color = RES_BLUE;
+            break;
+        case 7:
+            return_color = RES_VIOLET;
+            break;
+        case 8:
+            return_color = RES_GREY;
+            break;
+        case 9:
+            return_color = RES_WHITE;
+            break;
+        }
+    return return_color;
+}
+
+void get_tolerance_str(int band_value)
+{
+    int i;
+    for(i = 0; i <= 14; i++) {tolerance_str[i] = 0; }
+    /* clears out previous string/garbage */
+    switch(band_value) {
+        case RES_BROWN_TOL:
+            rb->snprintf(tolerance_str, sizeof(tolerance_str), "%s", 
+            "1% tolerance");
+            break;
+        case RES_RED_TOL:
+            rb->snprintf(tolerance_str, sizeof(tolerance_str), "%s", 
+            "2% tolerance");
+            break;
+        case RES_GOLD_TOL:
+            rb->snprintf(tolerance_str, sizeof(tolerance_str), "%s", 
+            "5% tolerance");
+            break;
+        case RES_SILVER_TOL:
+            rb->snprintf(tolerance_str, sizeof(tolerance_str), "%s", 
+            "10% tolerance");
+            break;
+        case RES_NONE_TOL:
+            rb->snprintf(tolerance_str, sizeof(tolerance_str), "%s", 
+            "20% tolerance");
+            break;
+         }
+    return;
+}
+    
+void get_color_str(int in_value, int out_string)
+{
+    switch(in_value) {
+        case RES_BLACK:
+        	switch(out_string) {
+        	    case 1:
+        	        rb->snprintf(first_str, sizeof(first_str), "%s", "Black");
+        	        break;
+        	    case 2:
+        	        rb->snprintf(second_str, sizeof(second_str), "%s", "Black");
+        	        break;
+        	    case 3:
+        	        rb->snprintf(third_str, sizeof(third_str), "%s", "Black");
+        	        break;
+        	    case 4:
+        	        rb->snprintf(fourth_str, sizeof(fourth_str), "%s", "Black");
+        	        break;
+        	    }
+        	break;
+        case RES_BROWN:
+        	switch(out_string) {
+        	    case 1:
+        	        rb->snprintf(first_str, sizeof(first_str), "%s", "Brown");
+        	        break;
+        	    case 2:
+        	        rb->snprintf(second_str, sizeof(second_str), "%s", "Brown");
+        	        break;
+        	    case 3:
+        	        rb->snprintf(third_str, sizeof(third_str), "%s", "Brown");
+        	        break;
+        	    case 4:
+        	        rb->snprintf(fourth_str, sizeof(fourth_str), "%s", "Brown");
+        	        break;
+        	    }
+        	break;
+        case RES_RED:
+        	switch(out_string) {
+        	    case 1:
+        	        rb->snprintf(first_str, sizeof(first_str), "%s", "Red");
+        	        break;
+        	    case 2:
+        	        rb->snprintf(second_str, sizeof(second_str), "%s", "Red");
+        	        break;
+        	    case 3:
+        	        rb->snprintf(third_str, sizeof(third_str), "%s", "Red");
+        	        break;
+        	    case 4:
+        	        rb->snprintf(fourth_str, sizeof(fourth_str), "%s", "Red");
+        	        break;
+        	    }
+        	break;
+        case RES_ORANGE:
+        	switch(out_string) {
+        	    case 1:
+        	        rb->snprintf(first_str, sizeof(first_str), "%s", "Orange");
+        	        break;
+        	    case 2:
+        	        rb->snprintf(second_str, sizeof(second_str), "%s", "Orange");
+        	        break;
+        	    case 3:
+        	        rb->snprintf(third_str, sizeof(third_str), "%s", "Orange");
+        	        break;
+        	    case 4:
+        	        rb->snprintf(fourth_str, sizeof(fourth_str), "%s", "Orange");
+        	        break;
+        	    }
+        	break;
+        case RES_YELLOW:
+        	switch(out_string) {
+        	    case 1:
+        	        rb->snprintf(first_str, sizeof(first_str), "%s", "Yellow");
+        	        break;
+        	    case 2:
+        	        rb->snprintf(second_str, sizeof(second_str), "%s", "Yellow");
+        	        break;
+        	    case 3:
+        	        rb->snprintf(third_str, sizeof(third_str), "%s", "Yellow");
+        	        break;
+        	    case 4:
+        	        rb->snprintf(fourth_str, sizeof(fourth_str), "%s", "Yellow");
+        	        break;
+        	    }
+        	break;
+        case RES_GREEN:
+        	switch(out_string) {
+        	    case 1:
+        	        rb->snprintf(first_str, sizeof(first_str), "%s", "Green");
+        	        break;
+        	    case 2:
+        	        rb->snprintf(second_str, sizeof(second_str), "%s", "Green");
+        	        break;
+        	    case 3:
+        	        rb->snprintf(third_str, sizeof(third_str), "%s", "Green");
+        	        break;
+        	    case 4:
+        	        rb->snprintf(fourth_str, sizeof(fourth_str), "%s", "Green");
+        	        break;
+        	    }
+        	break;
+        case RES_BLUE:
+        	switch(out_string) {
+        	    case 1:
+        	        rb->snprintf(first_str, sizeof(first_str), "%s", "Blue");
+        	        break;
+        	    case 2:
+        	        rb->snprintf(second_str, sizeof(second_str), "%s", "Blue");
+        	        break;
+        	    case 3:
+        	        rb->snprintf(third_str, sizeof(third_str), "%s", "Blue");
+        	        break;
+        	    case 4:
+        	        rb->snprintf(fourth_str, sizeof(fourth_str), "%s", "Blue");
+        	        break;
+        	    }
+        	break;
+        case RES_VIOLET:
+        	switch(out_string) {
+        	    case 1:
+        	        rb->snprintf(first_str, sizeof(first_str), "%s", "Violet");
+        	        break;
+        	    case 2:
+        	        rb->snprintf(second_str, sizeof(second_str), "%s", "Violet");
+        	        break;
+        	    case 3:
+        	        rb->snprintf(third_str, sizeof(third_str), "%s", "Violet");
+        	        break;
+        	    case 4:
+        	        rb->snprintf(fourth_str, sizeof(fourth_str), "%s", "Violet");
+        	        break;
+        	    }
+        	break;
+        case RES_GREY:
+        	switch(out_string) {
+        	    case 1:
+        	        rb->snprintf(first_str, sizeof(first_str), "%s", "Grey");
+        	        break;
+        	    case 2:
+        	        rb->snprintf(second_str, sizeof(second_str), "%s", "Grey");
+        	        break;
+        	    case 3:
+        	        rb->snprintf(third_str, sizeof(third_str), "%s", "Grey");
+        	        break;
+        	    case 4:
+        	        rb->snprintf(fourth_str, sizeof(fourth_str), "%s", "Grey");
+        	        break;
+        	    }
+        	break;
+        case RES_WHITE:
+        	switch(out_string) {
+        	    case 1:
+        	        rb->snprintf(first_str, sizeof(first_str), "%s", "White");
+        	        break;
+        	    case 2:
+        	        rb->snprintf(second_str, sizeof(second_str), "%s", "White");
+        	        break;
+        	    case 3:
+        	        rb->snprintf(third_str, sizeof(third_str), "%s", "White");
+        	        break;
+        	    case 4:
+        	        rb->snprintf(fourth_str, sizeof(fourth_str), "%s", "White");
+        	        break;
+        	    }
+        	break;
+        case RES_GOLD:
+        	switch(out_string) {
+        	    case 1:
+        	        rb->snprintf(first_str, sizeof(first_str), "%s", "Gold");
+        	        break;
+        	    case 2:
+        	        rb->snprintf(second_str, sizeof(second_str), "%s", "Gold");
+        	        break;
+        	    case 3:
+        	        rb->snprintf(third_str, sizeof(third_str), "%s", "Gold");
+        	        break;
+        	    case 4:
+        	        rb->snprintf(fourth_str, sizeof(fourth_str), "%s", "Gold");
+        	        break;
+        	    }
+        	break;
+        case RES_SILVER:
+        	switch(out_string) {
+        	    case 1:
+        	        rb->snprintf(first_str, sizeof(first_str), "%s", "Silver");
+        	        break;
+        	    case 2:
+        	        rb->snprintf(second_str, sizeof(second_str), "%s", "Silver");
+        	        break;
+        	    case 3:
+        	        rb->snprintf(third_str, sizeof(third_str), "%s", "Silver");
+        	        break;
+        	    case 4:
+        	        rb->snprintf(fourth_str, sizeof(fourth_str), "%s", "Silver");
+        	        break;
+        	    }
+        	break;
+        case RES_NONE:
+        	switch(out_string) {
+        	    case 1:
+        	        rb->snprintf(first_str, sizeof(first_str), "%s", "None");
+        	        break;
+        	    case 2:
+        	        rb->snprintf(second_str, sizeof(second_str), "%s", "None");
+        	        break;
+        	    case 3:
+        	        rb->snprintf(third_str, sizeof(third_str), "%s", "None");
+        	        break;
+        	    case 4:
+        	        rb->snprintf(fourth_str, sizeof(fourth_str), "%s", "None");
+        	        break;
+        	    }
+        	break;
+        }
+}
+
+void draw_resistor_text(int firstband_color, int secondband_color,
+                        int thirdband_color, int fourthband_color)
+{  
+    /* This is basically the text output used in the black and white targets,
+       except it can be used in conjunction with the graphical resistor on 
+       color targets for clarity purposes. (Orange on the Fuze screen DOES
+       look different than orange on the e200's screen.)  */
+   char resistance_vals_str [35];
+   get_color_str(firstband_color, 1);
+   get_color_str(secondband_color, 2);
+   get_color_str(thirdband_color, 3);
+   get_color_str(fourthband_color, 4);
+   rb->snprintf(resistance_vals_str, sizeof(resistance_vals_str),
+         "%s - %s - %s - %s", first_str, second_str, third_str, fourth_str);
+   #ifdef HAVE_LCD_COLOR
+   rb->lcd_set_foreground(LCD_WHITE);
+   #endif
+   rb->lcd_putsxy(resistance_txt_x, resistance_txt_y, resistance_vals_str);
+}
+
+#ifdef HAVE_LCD_COLOR
+void draw_resistor(int firstband_color, int secondband_color, 
+                   int thirdband_color, int fourthband_color)
+{
+    rb->lcd_clear_display();
+    rb->lcd_bitmap_transparent(resistor, RESISTOR_BMP_X, RESISTOR_BMP_Y, 
+                               RESISTOR_BMP_WIDTH, RESISTOR_BMP_HEIGHT);
+    
+    if(firstband_color != RES_NONE) {    
+    rb->lcd_set_foreground(firstband_color);
+    rb->lcd_fillrect(first_band_x, universal_y, band_width, band_height); }
+    
+    if(secondband_color != RES_NONE) {      
+    rb->lcd_set_foreground(secondband_color);
+    rb->lcd_fillrect(second_band_x, universal_y, band_width, band_height); }
+            
+    if(thirdband_color != RES_NONE) {        
+    rb->lcd_set_foreground(thirdband_color);
+    rb->lcd_fillrect(third_band_x, universal_y, band_width, band_height); }
+            
+    if(fourthband_color != RES_NONE) {         
+    rb->lcd_set_foreground(fourthband_color);
+    rb->lcd_fillrect(fourth_band_x, universal_y, band_width, band_height); }
+            
+    rb->lcd_update();
+    rb->lcd_set_foreground(LCD_WHITE); /* just putting it back */
+    return;
+}
+#else
+/* I am aware that this is the least elegant way to do this, I am just
+   out of ideas for the time being, and this works for now. Help? */
+
+void draw_resistor(int firstband_color, int secondband_color,
+                      int thirdband_color, int fourthband_color)
+{
+    char resistance_vals_str [35];
+    get_color_str(firstband_color, 1);
+    get_color_str(secondband_color, 2);
+    get_color_str(thirdband_color, 3);
+    get_color_str(fourthband_color, 4);
+    rb->snprintf(resistance_vals_str, sizeof(resistance_vals_str), 
+    "%s - %s - %s - %s", first_str, second_str, third_str, fourth_str);
+    rb->lcd_clear_display();
+    rb->lcd_puts_scroll(resistance_val_x, resistance_val_y, resistance_vals_str);
+    rb->lcd_update(); 
+}
+#endif
+
+int calculate_resistance(int firstband, int secondband, int thirdband)
+{
+    double total_resistance = 0;
+    int total_resistance_return = 0;
+    total_resistance = (firstband + secondband) * thirdband;
+    if(total_resistance >= 1000000) { 
+    	total_resistance_return = (total_resistance / 1000000);
+    	rb->snprintf(unit_abbrev, sizeof(unit_abbrev), "%s", "MOhms"); }
+    else if(total_resistance >= 1000) {
+        total_resistance_return = (total_resistance / 1000);
+        rb->snprintf(unit_abbrev, sizeof(unit_abbrev), "%s", "KOhms"); }
+    else { total_resistance_return = total_resistance_return; 
+            rb->snprintf(unit_abbrev, sizeof(unit_abbrev), "%s", "Ohms"); }
+          
+    return total_resistance_return;
+}
+    
+int get_band_value(int band_color, int band_number)
+{
+    int band_value = 0;
+    switch(band_number) {
+        case 0:
+            switch(band_color) {
+                case RES_BLACK:
+                    band_value = RES_BLACK_VALUE;
+                    break;
+                case RES_BROWN:
+                    band_value = RES_BROWN_VALUE;
+                    break;
+                case RES_RED:
+                    band_value = RES_RED_VALUE;
+                    break;
+                case RES_ORANGE:
+                    band_value = RES_ORANGE_VALUE;
+                    break;
+                case RES_YELLOW:
+                    band_value = RES_YELLOW_VALUE;
+                    break;
+                case RES_GREEN:
+                    band_value = RES_GREEN_VALUE;
+                    break;
+                case RES_BLUE:
+                    band_value = RES_BLUE_VALUE;
+                    break;
+                case RES_VIOLET:
+                    band_value = RES_VIOLET_VALUE;
+                    break;
+                case RES_GREY:
+                    band_value = RES_GREY_VALUE;
+                    break;
+                case RES_WHITE:
+                    band_value = RES_WHITE_VALUE;
+                    break;
+                }
+            break;
+        case 1:
+            switch(band_color) {
+                case RES_BLACK:
+                    band_value = RES_BLACK_SECONDARY_VALUE;
+                    break;
+                case RES_BROWN:
+                    band_value = RES_BROWN_SECONDARY_VALUE;
+                    break;
+                case RES_RED:
+                    band_value = RES_RED_SECONDARY_VALUE;
+                    break;
+               case RES_ORANGE:
+                   band_value = RES_ORANGE_SECONDARY_VALUE;
+                   break;
+               case RES_YELLOW:
+                   band_value = RES_YELLOW_SECONDARY_VALUE;
+                   break;
+               case RES_GREEN:
+                   band_value = RES_GREEN_SECONDARY_VALUE;
+                   break;
+               case RES_BLUE:
+                   band_value = RES_BLUE_SECONDARY_VALUE;
+                   break;
+               case RES_VIOLET:
+                   band_value = RES_VIOLET_SECONDARY_VALUE;
+                   break;
+               case RES_GREY:
+                   band_value = RES_GREY_SECONDARY_VALUE;
+                   break;
+               case RES_WHITE:
+                   band_value = RES_WHITE_SECONDARY_VALUE;
+                   break;
+               }
+            break;
+        case 2:
+            switch(band_color) {
+                case RES_BLACK:
+                    band_value = RES_BLACK_MUL;
+                    break;
+                case RES_BROWN:
+                    band_value = RES_BROWN_MUL;
+                    break;
+                case RES_RED:
+                    band_value = RES_RED_MUL;
+                    break;
+                case RES_ORANGE:
+                    band_value = RES_ORANGE_MUL;
+                    break;
+                case RES_YELLOW:
+                    band_value = RES_YELLOW_MUL;
+                    break;
+                case RES_GREEN:
+                    band_value = RES_GREEN_MUL;
+                    break;
+                case RES_BLUE:
+                    band_value = RES_BLUE_MUL;
+                    break;
+                case RES_SILVER:
+                    band_value = RES_SILVER_MUL;
+                    break;
+                case RES_GOLD:
+                    band_value = RES_GOLD_MUL;
+                    break;
+                }
+            break;
+        case 3:
+            switch(band_color) {
+                case RES_RED:
+                    band_value = RES_RED_TOL;
+                    break;
+                case RES_BROWN:
+                    band_value = RES_BROWN_TOL;
+                    break;
+                case RES_NONE:
+                    band_value = RES_NONE_TOL;
+                    break;
+                case RES_GOLD:
+                    band_value = RES_GOLD_TOL;
+                    break;
+                case RES_SILVER:
+                    band_value = RES_SILVER_TOL;
+                    break;
+                }
+            break;
+        }
+    return band_value;
+}
+    
+int do_first_band_menu(void)
+{
+    int band_selection, band_color_selection = 0;
+            
+    MENUITEM_STRINGLIST(colors_menu_first, "First band colour:", NULL, 
+                        "Black", "Brown", "Red", "Orange", "Yellow",
+                         "Green", "Blue", "Violet", "Grey", "White");
+    band_selection = rb->do_menu(&colors_menu_first, &band_selection, NULL, 
+                                false);
+    switch(band_selection) {
+        case 0: /* Black */
+            band_color_selection = RES_BLACK;
+            break;
+        case 1: /* Brown */
+            band_color_selection = RES_BROWN;
+            break;
+        case 2: /* Red */
+            band_color_selection = RES_RED;
+            break;
+        case 3: /* Orange */
+            band_color_selection = RES_ORANGE;
+            break;
+        case 4: /* Yellow */
+            band_color_selection = RES_YELLOW;
+            break;
+        case 5: /* Green */
+            band_color_selection = RES_GREEN;
+            break;
+        case 6: /* Blue */
+            band_color_selection = RES_BLUE;
+            break;
+        case 7: /* Violet */
+            band_color_selection = RES_VIOLET;
+            break;
+        case 8: /* Grey */
+            band_color_selection = RES_GREY;
+            break;
+        case 9: /* White */
+            band_color_selection = RES_WHITE;
+            break;
+        }
+    return band_color_selection;
+}
+            
+int do_second_band_menu(void) 
+{
+    int band_selection, band_color_selection = 0;
+            
+    MENUITEM_STRINGLIST(colors_menu_second, "Second band colour:", NULL, 
+                        "Black", "Brown", "Red", "Orange", "Yellow", 
+                        "Green", "Blue", "Violet", "Grey", "White");
+    band_selection = rb->do_menu(&colors_menu_second, &band_selection, NULL, 
+                                false);
+    switch(band_selection) {
+        case 0: /* Black */
+            band_color_selection = RES_BLACK;
+            break;
+        case 1: /* Brown */
+            band_color_selection = RES_BROWN;
+            break;
+        case 2: /* Red */
+            band_color_selection = RES_RED;
+            break;
+        case 3: /* Orange */
+            band_color_selection = RES_ORANGE;
+            break;
+        case 4: /* Yellow */
+            band_color_selection = RES_YELLOW;
+            break;
+        case 5: /* Green */
+            band_color_selection = RES_GREEN;
+            break;
+        case 6: /* Blue */
+            band_color_selection = RES_BLUE;
+            break;
+        case 7: /* Violet */
+            band_color_selection = RES_VIOLET;
+            break;
+        case 8: /* Grey */
+            band_color_selection = RES_GREY;
+            break;
+        case 9: /* White */
+            band_color_selection = RES_WHITE;
+        break;
+                    }
+    return band_color_selection;
+}
+    
+int do_third_band_menu(void) 
+{
+    int band_selection, band_color_selection = 0;
+            
+    MENUITEM_STRINGLIST(colors_menu_third, "Third band colour:", NULL, 
+                        "Black", "Brown", "Red", "Orange", "Yellow",
+                         "Green", "Blue", "Silver", "Gold");
+    band_selection = rb->do_menu(&colors_menu_third, &band_selection, NULL, 
+                                false);
+    switch(band_selection) {
+        case 0: /* Black */
+            band_color_selection = RES_BLACK;
+            break;
+        case 1: /* Brown */
+            band_color_selection = RES_BROWN;
+            break;
+        case 2: /* Red */
+            band_color_selection = RES_RED;
+            break;
+        case 3: /* Orange */
+            band_color_selection = RES_ORANGE;
+            break;
+        case 4: /* Yellow */
+            band_color_selection= RES_YELLOW;
+            break;
+        case 5: /* Green */
+            band_color_selection = RES_GREEN;
+            break;
+        case 6: /* Blue */
+            band_color_selection = RES_BLUE;
+            break;
+        case 7: /* Silver */
+            band_color_selection = RES_SILVER;
+            break;
+        case 8: /* Gold */
+            band_color_selection= RES_GOLD;
+            break;
+                    }
+    return band_color_selection;
+}
+            
+int do_fourth_band_menu(void) 
+{
+    int band_selection, band_color_selection = 0;
+            
+    MENUITEM_STRINGLIST(colors_menu_fourth, "Fourth band colour:", NULL, 
+                        "Gold", "Brown", "Red", "Silver", "(none)");
+    band_selection = rb->do_menu(&colors_menu_fourth, &band_selection, NULL, 
+                                false);
+    switch(band_selection) {
+        case 0: /* Gold */
+            band_color_selection = RES_GOLD;
+            break;
+        case 1: /* Brown */
+            band_color_selection = RES_BROWN;
+            break;
+        case 2: /* Red */
+            band_color_selection = RES_RED;
+            break;
+        case 3: /* Silver */
+            band_color_selection = RES_SILVER;
+            break;
+        case 4: /* (none) */
+            band_color_selection = RES_NONE;
+            break;
+                    }
+    return band_color_selection;
+}
+    
+void display_helpfile(void)
+{
+    rb->splash(HZ/2, "Helpfile");
+    rb->lcd_clear_display();
+    /* some information obtained from wikipedia */
+    static char * helpfile_text[] = {
+        "Resistor Calculator Helpfile", "", "",
+        "About resistors:", "", /* 7 */
+        /* -- */
+        "A", "resistor", "is", "a ", "two-terminal", "electronic", 
+        "component", "that", "produces", "a", "voltage", "across", "its", 
+        "terminals", "that", "is", "proportional", "to", "the", "electric",
+        "current", "passing", "through", "it", "in", "accordance", "to",
+        "Ohm's", "Law:", "", /* 29 */
+        /* -- */
+        "", "V = IR",
+        "", "I = V/R",
+        "", "and",
+        "", "R = I/V", "", "",
+        "Where", "V", "=", "voltage", "I", "=", "current", "(in", "amps)", 
+        "and", "R", "=", "resistance", "(measured", "in", "Ohms)", "", "",
+         /* 28 */
+        /* -- */
+        "The", "primary", "characteristics", "of", "a", "resistor", "are",
+        "the", "resistance,", "the", "tolerance,", "and", "the", "maximum",
+        "working", "voltage", "and", "the", "power", "rating.", "At", 
+        "this", "time,", "this", "calculator", "only", "utilizes", "the",
+        "resistance", "and", "tolerance.", "", "", /* 33 */
+        /* -- */
+        "The", "Ohm", "is", "the", "SI", "unit", "of", "resistance,", "and",
+        "common", "multiples", "of", "that", "include", "the", "kiliohm", 
+        "(KOhm", "-", "1x10^3)", "and", "the", "megaohm", "(MOhm",
+        "-", "1x10^6),", "both", "of", "which", "are", "supported", "by",
+        "this", "calculator.", "", "", /* 34 */
+        /* -- */
+        "Resistors", "in", "parallel:", "", /* 4 */
+        /* -- */
+        "1/Rtotal", "=", "1/R1", "+", "1/R2", "...", "+", "1/Rn", "", /* 9*/
+        /* -- */
+        "", "Resistors", "in", "series:", "", /* 5 */
+        /* -- */
+        "Rtotal", "=", "R1", "+", "R2", "...", "+", "Rn", "", /* 9 */
+        /* -- */
+        "", "How to use this calculator", "", /* 3 */
+        /* -- */
+        "This", "calculator", "has", "two", "modes:", "",
+        "Resistance", "to", "coulor", "codes", "", "and", "",
+        "Color", "codes", "to", "resistance", "(although", "this", "part",
+        "is", "not", "yet", "completed.)","", "",
+        /* -- */
+        "*At", "this", "time,", "there", "is", "only", "support", "for", 
+        "5,", "10,", "and", "20%","tolerances,", "and", "4", "bands.","","",
+        /* -- */
+        "In", "Colour", "to", "Resistance", "mode", "use", "the", "menus",
+        "to", "input", "(in", "order)", "the", "bands", "of", "the",
+        "resistor", "for", "which", "you", "would", "like", "to", "know",
+        "the", "resistance.", "", "",
+        /* -- */
+        "In", "Resistance", "to", "Colour", "mode,", "is", "not", "yet",
+        "finished", "but", "plans", "are", "for", "a", "calculator", "-",
+        "like", "keypad", "for", "input", "and", "output", "both", 
+        "visually", "on", "a", "drawn", "resistor", "and", "textually."
+        };
+    static struct style_text formatting[] = {
+        { 0, TEXT_CENTER|TEXT_UNDERLINE },
+        { 3, TEXT_UNDERLINE },
+        { 159, TEXT_UNDERLINE },
+        LAST_STYLE_ITEM
+        };
+            
+    display_text(ARRAYLEN(helpfile_text), helpfile_text, formatting,
+                 NULL, true);
+            
+    return;
+}
+        
+void resistance_to_color(void) 
+{
+    int menu_selection = 0;
+    int menu_selection_tol = 0;
+    int button_press = 0;
+    int units_used = 0;
+    bool quit = false;
+    int i;
+    char kbd_buffer [10];
+    int kbd_input_int = 0;
+    
+    int in_resistance_int;
+    int power_ten;
+    int first_band_int = 0;
+    int second_band_int = 0;
+
+    int first_band_color;
+    int second_band_color;
+    int multiplier_color;
+    int fourth_band_color;
+    
+    char out_str[20];
+    /*
+    char out_str_1[15];
+    char out_str_2[15];
+    char out_str_3[15]; */
+    
+    for(i=0; i<=10; i++) { kbd_buffer[i] = 0; }
+    /* This cleans out the mysterious garbage that appears */
+    rb->lcd_clear_display();
+    rb->splash(HZ/2, "Resistance to Colour");
+    MENUITEM_STRINGLIST(r_to_c_menu, "Select unit to use:", NULL, 
+                        "Ohms", "Kiliohms (KOhms)", "Megaohms (MOhms)");
+    MENUITEM_STRINGLIST(r_to_c_menu_tol, "Tolerance to display:", NULL,
+                        "5%", "10%", "1%", "2%", "20%")
+    while(!quit) {
+        menu_selection = rb->do_menu(&r_to_c_menu, &menu_selection,
+                                     NULL, false);
+        switch(menu_selection) {
+            case 0:
+                units_used = UNIT_OHMS;
+                break;
+            case 1:
+                units_used = UNIT_KOHMS;
+                break;
+            case 2:
+                units_used = UNIT_MOHMS;
+                break;
+            }
+        rb->kbd_input(kbd_buffer, sizeof(kbd_buffer));
+        /* As stated above somewhere, we (I) need to make a calculator-like
+           keypad, that keyboard isn't all that fun as it is. */
+        menu_selection_tol = rb->do_menu(&r_to_c_menu_tol, &menu_selection_tol,
+                                         NULL, false);
+        switch(menu_selection_tol) {
+            case 0: /* 5% */
+                fourth_band_color = RES_GOLD;
+                break;
+            case 1: /* 10% */
+                fourth_band_color = RES_SILVER;
+                break;
+            case 2:
+                fourth_band_color = RES_BROWN;
+                break;
+            case 3:
+                fourth_band_color = RES_RED;
+                break;
+            case 4:
+                fourth_band_color = RES_NONE;
+                break;
+            }
+        kbd_input_int = atoi(kbd_buffer);
+        in_resistance_int = kbd_input_int;
+        if(units_used == UNIT_KOHMS) { kbd_input_int *= 1000; }
+        else if(units_used == UNIT_MOHMS) { kbd_input_int *= 1000000; }
+        
+        power_ten = get_power_ten(kbd_input_int);
+        if(kbd_input_int / powi(10, power_ten) == 1) {
+            while(kbd_input_int /powi(10, power_ten) == 1) {
+                power_ten--;
+                }
+            }
+        if(kbd_input_int / powi(10, power_ten) != (int)kbd_input_int) {
+            power_ten--; }
+        kbd_input_int /= powi(10, power_ten);
+        
+        if(kbd_input_int < 10) {
+            first_band_int = kbd_input_int; }
+        else { first_band_int = kbd_input_int /10; }
+        second_band_int += kbd_input_int % 10;
+        
+        if(first_band_int == 10) {
+            first_band_int /= 10;
+            second_band_int = 0;
+            power_ten++;
+            }
+        
+        if(first_band_int > 10) {
+            int temp;
+            temp = first_band_int /10;
+            second_band_int = first_band_int % 10;
+            first_band_int = temp;
+            }
+                    
+        first_band_color = get_band_color_rtoc(first_band_int);
+        second_band_color = get_band_color_rtoc(second_band_int);
+        multiplier_color = get_band_color_rtoc(power_ten);
+        
+        rb->lcd_clear_display();
+        draw_resistor(first_band_color, second_band_color, multiplier_color,
+                      fourth_band_color);
+        
+        #ifdef HAVE_LCD_COLOR /* This seems backwards, but is really only 
+                                necessary on color targets */
+        draw_resistor_text(first_band_color, second_band_color,
+                           multiplier_color, fourth_band_color);
+        #endif
+        
+        get_unit_str(units_used);
+        rb->snprintf(out_str, sizeof(out_str), "Input: %d %s", 
+                     in_resistance_int, unit_abbrev);
+        /* These were for debugging purposes. */
+        /* rb->snprintf(out_str_1, sizeof(out_str_1), "first = %d",
+                        first_band_int);
+        rb->snprintf(out_str_2, sizeof(out_str_2), "second = %d", 
+                     second_band_int);
+        rb->snprintf(out_str_3, sizeof(out_str_3), "mult = %d", power_ten);
+        rb->lcd_putsxy(13, 150, out_str_1);
+        rb->lcd_putsxy(13, 160, out_str_2);
+        rb->lcd_putsxy(13, 170, out_str_3); */
+        rb->lcd_putsxy(r_to_c_out_str_x, r_to_c_out_str_y, out_str); 
+        rb->lcd_update();
+
+        button_press = rb->button_get(true);
+        switch(button_press) {
+            case PLA_SELECT:
+                break;
+            default:
+                quit = true;
+                break;
+            }
+        }            
+    return;
+}
+    
+void color_to_resistance(void) 
+{
+    bool quit = false;
+    int button_input = 0;
+    /* The colors of the bands */
+    int first_band, second_band, third_band, fourth_band = 0;
+    /* The values of the bands */
+    int first_band_value, second_band_value, third_band_value, 
+        fourth_band_value = 0;
+    int total_resistance = 0;
+    char total_resistance_str [35];
+            
+    rb->splash(HZ/2, "Colour to resistance");
+    rb->lcd_clear_display();
+            
+    while(!quit) {
+        first_band = do_first_band_menu();
+        second_band = do_second_band_menu();
+        third_band = do_third_band_menu();
+        fourth_band = do_fourth_band_menu();
+                    
+        first_band_value = get_band_value(first_band, 0);
+        second_band_value = get_band_value(second_band, 1);
+        third_band_value = get_band_value(third_band, 2);
+        fourth_band_value = get_band_value(fourth_band, 3);
+                    
+        total_resistance = calculate_resistance(first_band_value, 
+                                                second_band_value,
+                                                third_band_value);
+        get_tolerance_str(fourth_band_value);      
+        draw_resistor(first_band, second_band, third_band, fourth_band);
+        rb->snprintf(total_resistance_str, sizeof(total_resistance_str), 
+                     "The resistance: %d %s", total_resistance,
+                     unit_abbrev);
+        rb->lcd_putsxy(total_resistance_str_x, total_resistance_str_y,
+                       total_resistance_str);
+        rb->lcd_putsxy(tolerance_str_x, tolerance_str_y, tolerance_str);
+        rb->lcd_update();
+                    
+        button_input = rb->button_get(true);
+        switch(button_input) {
+            case PLA_RIGHT:
+                break;
+            case PLA_EXIT:
+            case PLA_SELECT:
+            default:
+                quit = true;
+                break;
+            }                   
+        }
+    return;
+}
+
+enum plugin_status plugin_start(const void* nothing) 
+{
+    (void)nothing;
+    rb->lcd_clear_display();
+    rb->lcd_update();
+    int main_menu_selection = 0;
+    bool menuquit = false;
+        
+    MENUITEM_STRINGLIST(main_menu, "Resistor Code Calculator:", NULL, 
+                        "Colours -> Resistance", "Resistance -> Colours", 
+                        "Help", "Exit");
+    while (!menuquit) {
+        main_menu_selection = rb->do_menu(&main_menu, &main_menu_selection, 
+                                          NULL, false);
+        switch(main_menu_selection) {
+            case 0:
+                color_to_resistance();
+                break;
+            case 1:
+                resistance_to_color();
+                break;
+            case 2:
+                display_helpfile();
+                break;
+            case 3:
+                menuquit = true;
+                break;
+            }
+        }
+    return PLUGIN_OK;
+}
