|
3 | 3 | #include "resistor_logic.h"
|
4 | 4 | #include <math.h>
|
5 | 5 |
|
6 |
| -// Xx_Xx_Xx_Xx_-_Xx\0 = 17 characters |
7 |
| -const char blank_descriptor_R4[14] = " "; |
8 |
| -const char blank_descriptor_R5[17] = " "; |
9 |
| - |
10 |
| -const int RESISTOR_NUMERIC_CHARS = 3; |
11 |
| -const int RESISTOR_MULTIPLIER_UNIT_CHARS = 7; |
12 |
| -const int RESISTOR_TOLERANCE_CHARS = 7; |
13 |
| - |
14 |
| -const int RESISTOR_NUMERIC_POSITION = 0; |
15 |
| -const int RESISTOR_MULTIPLIER_UNIT_POSITION = 4; |
16 |
| -const int RESISTOR_TOLERANCE_POSITION = 14; |
| 6 | +const int CHARS_NUMERIC = 3; |
| 7 | +const int CHARS_MULTIPLIER = 7; |
| 8 | +const int CHARS_TOLERANCE = 7; |
| 9 | +const int CHARS_TEMP_COEFF = 3; |
| 10 | +const int CHARS_CALCULATION = 12; |
| 11 | + |
| 12 | +const char BLANK_CALCULATION[] = " "; // "nnn x 10^nn" |
| 13 | +const char BLANK_TOLERANCE[] = " "; |
| 14 | +const char BLANK_TEMP_COEFF[] = " "; |
| 15 | + |
| 16 | +const int INDEX_NUMERIC = 0; |
| 17 | +const int INDEX_MULTIPLIER = 4; |
| 18 | +const int INDEX_TOLERANCE = 0; |
| 19 | + |
| 20 | +const int NUMERIC_BANDS_PER_RESISTOR[6] = {-1, -1, 2, 2, 3, 3}; |
| 21 | +const int MULTIPLIER_INDEX_PER_RESISTOR[6] = {-1, -1, 2, 2, 3, 3}; |
| 22 | +const int TOLERANCE_INDEX_PER_RESISTOR[6] = {-1, -1, -1, 3, 4, 4}; |
| 23 | +const int TEMP_COEFF_INDEX_PER_RESISTOR[6] = {-1, -1, -1, -1, -1, 5}; |
| 24 | + |
| 25 | +bool has_tolerance(ResistorType rtype) { |
| 26 | + return TOLERANCE_INDEX_PER_RESISTOR[rtype - 1] > -1; |
| 27 | +} |
17 | 28 |
|
18 |
| -// "nnn x 10^nn @ nnnn%"; |
19 |
| -const char blank_calculation[24] = " "; |
| 29 | +bool has_temp_coeff(ResistorType rtype) { |
| 30 | + return TEMP_COEFF_INDEX_PER_RESISTOR[rtype - 1] > -1; |
| 31 | +} |
20 | 32 |
|
21 | 33 | bool is_numeric_band(ResistorType rtype, int index) {
|
22 |
| - return index < (rtype - 2); |
| 34 | + return index < NUMERIC_BANDS_PER_RESISTOR[rtype - 1]; |
23 | 35 | }
|
24 | 36 |
|
25 | 37 | bool is_multiplier_band(ResistorType rtype, int index) {
|
26 |
| - return index == (rtype - 2); |
| 38 | + return index == MULTIPLIER_INDEX_PER_RESISTOR[rtype - 1]; |
27 | 39 | }
|
28 | 40 |
|
29 | 41 | bool is_tolerance_band(ResistorType rtype, int index) {
|
30 |
| - return index == (rtype - 1); |
| 42 | + return index == TOLERANCE_INDEX_PER_RESISTOR[rtype - 1]; |
| 43 | +} |
| 44 | + |
| 45 | +bool is_temp_coefficient_band(ResistorType rtype, int index) { |
| 46 | + return index == TEMP_COEFF_INDEX_PER_RESISTOR[rtype - 1]; |
31 | 47 | }
|
32 | 48 |
|
33 | 49 | bool is_numeric_colour(BandColour colour) {
|
34 | 50 | return colour <= 9;
|
35 | 51 | }
|
36 | 52 |
|
37 |
| -bool is_multiplier_colour() { |
| 53 | +bool is_multiplier_colour(BandColour colour) { |
| 54 | + UNUSED(colour); |
38 | 55 | return true;
|
39 | 56 | }
|
40 | 57 |
|
41 | 58 | bool is_tolerance_colour(BandColour colour) {
|
42 |
| - return colour == BandBrown || colour == BandRed || colour == BandGreen || colour == BandBlue || |
| 59 | + return colour == BandBrown || colour == BandRed || colour == BandOrange || |
| 60 | + colour == BandYellow || colour == BandGreen || colour == BandBlue || |
43 | 61 | colour == BandPurple || colour == BandGray || colour == BandGold ||
|
44 | 62 | colour == BandSilver;
|
45 | 63 | }
|
46 | 64 |
|
| 65 | +bool is_temp_coeff_colour(BandColour colour) { |
| 66 | + return colour == BandBrown || colour == BandRed || colour == BandOrange || |
| 67 | + colour == BandYellow || colour == BandBlue || colour == BandPurple; |
| 68 | +} |
| 69 | + |
47 | 70 | BandColour
|
48 | 71 | alter_resistor_band(ResistorType rtype, int band, BandColour current_colour, int direction) {
|
49 | 72 | int colour = current_colour;
|
50 | 73 | bool accepted = false;
|
51 | 74 | while(!accepted) {
|
52 | 75 | colour += direction;
|
53 | 76 | if(colour > 11) colour = 0;
|
54 |
| - if(colour < 0) colour = 12; |
| 77 | + if(colour < 0) colour = 11; |
55 | 78 | if(is_numeric_band(rtype, band) && is_numeric_colour(colour)) accepted = true;
|
| 79 | + if(is_multiplier_band(rtype, band) && is_multiplier_colour(colour)) accepted = true; |
56 | 80 | if(is_tolerance_band(rtype, band) && is_tolerance_colour(colour)) accepted = true;
|
57 |
| - if(is_multiplier_band(rtype, band) && is_multiplier_colour()) accepted = true; |
| 81 | + if(is_temp_coefficient_band(rtype, band) && is_temp_coeff_colour(colour)) accepted = true; |
58 | 82 | }
|
59 | 83 | return colour;
|
60 | 84 | }
|
61 | 85 |
|
62 |
| -void update_resistance_number(ResistorType rtype, BandColour colours[], char string[], int index) { |
63 |
| - int bands = rtype - 2; |
| 86 | +int decode_resistance_number(ResistorType rtype, BandColour colours[]) { |
| 87 | + int bands = NUMERIC_BANDS_PER_RESISTOR[rtype - 1]; |
64 | 88 | int value = 0;
|
65 | 89 | for(int b = 0; b < bands; b++) {
|
66 | 90 | int pwr = bands - b - 1;
|
67 | 91 | int delta = ((int)pow(10.0, pwr)) * colours[b];
|
68 | 92 | value += delta;
|
69 | 93 | }
|
| 94 | + return value; |
| 95 | +} |
70 | 96 |
|
| 97 | +void update_resistance_number(ResistorType rtype, BandColour colours[], char string[]) { |
| 98 | + int value = decode_resistance_number(rtype, colours); |
71 | 99 | int length = snprintf(NULL, 0, "%d", value);
|
72 | 100 | char* str = malloc(length + 1);
|
73 | 101 | snprintf(str, length + 1, "%d", value);
|
74 | 102 |
|
75 |
| - char* target = string + index; |
| 103 | + char* target = string + INDEX_NUMERIC; |
76 | 104 | strncpy(target, str, length);
|
77 | 105 | free(str);
|
78 | 106 | }
|
79 | 107 |
|
80 |
| -void update_resistance_multiplier_unit(BandColour colour, char string[], int index) { |
81 |
| - char unit[] = "x 10^ "; |
| 108 | +char* decode_resistance_multiplier(BandColour colour) { |
| 109 | + static char unit[] = "x 10^ "; |
82 | 110 | if(colour > 9) {
|
83 | 111 | unit[5] = '-';
|
84 | 112 | unit[6] = (char)(48 + colour - 9);
|
85 | 113 | } else {
|
86 | 114 | unit[5] = (char)(48 + colour);
|
87 |
| - unit[6] = ' '; |
| 115 | + unit[6] = '\0'; |
88 | 116 | }
|
89 |
| - char* target = string + index; |
90 |
| - strncpy(target, unit, RESISTOR_MULTIPLIER_UNIT_CHARS); |
| 117 | + return unit; |
| 118 | +} |
| 119 | + |
| 120 | +void update_resistance_multiplier(ResistorType rtype, BandColour colours[], char string[]) { |
| 121 | + int multiplier_index = MULTIPLIER_INDEX_PER_RESISTOR[rtype - 1]; |
| 122 | + char* unit = decode_resistance_multiplier(colours[multiplier_index]); |
| 123 | + char* target = string + INDEX_MULTIPLIER; |
| 124 | + strncpy(target, unit, CHARS_MULTIPLIER); |
91 | 125 | }
|
92 | 126 |
|
93 |
| -void update_resistance_tolerance(BandColour colour, char string[], int index) { |
94 |
| - char* target = string + index; |
| 127 | +char* decode_resistance_tolerance(BandColour colour) { |
95 | 128 | switch(colour) {
|
96 | 129 | case BandBrown:;
|
97 |
| - char tolerance_brown[8] = "@ 1% "; |
98 |
| - strncpy(target, tolerance_brown, RESISTOR_TOLERANCE_CHARS); |
99 |
| - break; |
| 130 | + return "1%"; |
100 | 131 | case BandRed:;
|
101 |
| - char tolerance_red[8] = "@ 2% "; |
102 |
| - strncpy(target, tolerance_red, RESISTOR_TOLERANCE_CHARS); |
103 |
| - break; |
| 132 | + return "2%"; |
| 133 | + case BandOrange:; |
| 134 | + return "3%"; |
| 135 | + case BandYellow:; |
| 136 | + return "4%"; |
104 | 137 | case BandGreen:;
|
105 |
| - char tolerance_green[8] = "@ 0.5% "; |
106 |
| - strncpy(target, tolerance_green, RESISTOR_TOLERANCE_CHARS); |
107 |
| - break; |
| 138 | + return "0.5%"; |
108 | 139 | case BandBlue:;
|
109 |
| - char tolerance_blue[8] = "@ 0.25%"; |
110 |
| - strncpy(target, tolerance_blue, RESISTOR_TOLERANCE_CHARS); |
111 |
| - break; |
| 140 | + return "0.25%"; |
112 | 141 | case BandPurple:;
|
113 |
| - char tolerance_purple[8] = "@ 0.1% "; |
114 |
| - strncpy(target, tolerance_purple, RESISTOR_TOLERANCE_CHARS); |
115 |
| - break; |
| 142 | + return "0.1%"; |
116 | 143 | case BandGray:;
|
117 |
| - char tolerance_gray[8] = "@ 0.05%"; |
118 |
| - strncpy(target, tolerance_gray, RESISTOR_TOLERANCE_CHARS); |
119 |
| - break; |
| 144 | + return "0.05%"; |
120 | 145 | case BandGold:;
|
121 |
| - char tolerance_gold[8] = "@ 5% "; |
122 |
| - strncpy(target, tolerance_gold, RESISTOR_TOLERANCE_CHARS); |
123 |
| - break; |
| 146 | + return "5%"; |
124 | 147 | case BandSilver:;
|
125 |
| - char tolerance_silver[8] = "@ 10% "; |
126 |
| - strncpy(target, tolerance_silver, RESISTOR_TOLERANCE_CHARS); |
127 |
| - break; |
| 148 | + return "10%"; |
| 149 | + default:; |
| 150 | + return "--"; |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +void update_resistance_calculation(ResistorType rtype, BandColour bands[], char* string) { |
| 155 | + strcpy(string, BLANK_CALCULATION); |
| 156 | + update_resistance_number(rtype, bands, string); |
| 157 | + update_resistance_multiplier(rtype, bands, string); |
| 158 | +} |
| 159 | + |
| 160 | +void update_resistance_tolerance(ResistorType rtype, BandColour colours[], char string[]) { |
| 161 | + strcpy(string, BLANK_TOLERANCE); |
| 162 | + int tolerance_index = TOLERANCE_INDEX_PER_RESISTOR[rtype - 1]; |
| 163 | + char* unit = decode_resistance_tolerance(colours[tolerance_index]); |
| 164 | + char* target = string + INDEX_TOLERANCE; |
| 165 | + strncpy(target, unit, CHARS_TOLERANCE); |
| 166 | +} |
| 167 | + |
| 168 | +char* decode_resistance_temp_coeff(BandColour colour) { |
| 169 | + switch(colour) { |
| 170 | + case BandBrown:; |
| 171 | + return "100"; |
| 172 | + case BandRed:; |
| 173 | + return "50"; |
| 174 | + case BandOrange:; |
| 175 | + return "15"; |
| 176 | + case BandYellow:; |
| 177 | + return "25"; |
| 178 | + case BandBlue:; |
| 179 | + return "10"; |
| 180 | + case BandPurple:; |
| 181 | + return "5"; |
128 | 182 | default:;
|
129 |
| - char tolerance_nk[8] = "@ ????%"; |
130 |
| - strncpy(target, tolerance_nk, RESISTOR_TOLERANCE_CHARS); |
131 |
| - break; |
| 183 | + return "--"; |
132 | 184 | }
|
133 | 185 | }
|
134 | 186 |
|
135 |
| -void update_calculation(ResistorType rtype, BandColour bands[], char string[]) { |
136 |
| - strcpy(string, blank_calculation); |
137 |
| - update_resistance_number(rtype, bands, string, RESISTOR_NUMERIC_POSITION); |
138 |
| - update_resistance_multiplier_unit(bands[rtype - 2], string, RESISTOR_MULTIPLIER_UNIT_POSITION); |
139 |
| - update_resistance_tolerance(bands[rtype - 1], string, RESISTOR_TOLERANCE_POSITION); |
140 |
| -} |
141 |
| - |
142 |
| -void update_resistor_descriptor(ResistorType bands, BandColour resistor_bands[], char descriptor[]) { |
143 |
| - if(bands == Resistor4Band) strcpy(descriptor, blank_descriptor_R4); |
144 |
| - if(bands == Resistor5Band) strcpy(descriptor, blank_descriptor_R5); |
145 |
| - |
146 |
| - for(int i = 0; i < bands; i++) { |
147 |
| - int c = i * 3; |
148 |
| - bool last_band = i == bands - 1; |
149 |
| - if(last_band) { |
150 |
| - descriptor[c] = '-'; |
151 |
| - c += 2; |
152 |
| - } |
153 |
| - switch(resistor_bands[i]) { |
154 |
| - case BandBlack: |
155 |
| - descriptor[c] = 'B'; |
156 |
| - descriptor[c + 1] = 'k'; |
157 |
| - break; |
158 |
| - case BandBrown: |
159 |
| - descriptor[c] = 'B'; |
160 |
| - descriptor[c + 1] = 'r'; |
161 |
| - break; |
162 |
| - case BandRed: |
163 |
| - descriptor[c] = 'R'; |
164 |
| - descriptor[c + 1] = 'e'; |
165 |
| - break; |
166 |
| - case BandOrange: |
167 |
| - descriptor[c] = 'O'; |
168 |
| - descriptor[c + 1] = 'r'; |
169 |
| - break; |
170 |
| - case BandYellow: |
171 |
| - descriptor[c] = 'Y'; |
172 |
| - descriptor[c + 1] = 'e'; |
173 |
| - break; |
174 |
| - case BandGreen: |
175 |
| - descriptor[c] = 'G'; |
176 |
| - descriptor[c + 1] = 'r'; |
177 |
| - break; |
178 |
| - case BandBlue: |
179 |
| - descriptor[c] = 'B'; |
180 |
| - descriptor[c + 1] = 'l'; |
181 |
| - break; |
182 |
| - case BandPurple: |
183 |
| - descriptor[c] = 'P'; |
184 |
| - descriptor[c + 1] = 'u'; |
185 |
| - break; |
186 |
| - case BandGray: |
187 |
| - descriptor[c] = 'G'; |
188 |
| - descriptor[c + 1] = 'y'; |
189 |
| - break; |
190 |
| - case BandWhite: |
191 |
| - descriptor[c] = 'W'; |
192 |
| - descriptor[c + 1] = 'h'; |
193 |
| - break; |
194 |
| - case BandGold: |
195 |
| - descriptor[c] = 'G'; |
196 |
| - descriptor[c + 1] = 'o'; |
197 |
| - break; |
198 |
| - case BandSilver: |
199 |
| - descriptor[c] = 'S'; |
200 |
| - descriptor[c + 1] = 'i'; |
201 |
| - break; |
202 |
| - } |
203 |
| - } // i |
| 187 | +void update_resistance_temp_coeff(ResistorType rtype, BandColour colours[], char string[]) { |
| 188 | + strcpy(string, BLANK_TEMP_COEFF); |
| 189 | + int temp_coeff_index = TEMP_COEFF_INDEX_PER_RESISTOR[rtype - 1]; |
| 190 | + char* unit = decode_resistance_temp_coeff(colours[temp_coeff_index]); |
| 191 | + char* target = string + INDEX_TOLERANCE; |
| 192 | + strncpy(target, unit, CHARS_TEMP_COEFF); |
| 193 | +} |
| 194 | + |
| 195 | +char* get_colour_short_description(BandColour colour) { |
| 196 | + switch(colour) { |
| 197 | + case BandBlack: |
| 198 | + return "Bk"; |
| 199 | + case BandBrown: |
| 200 | + return "Br"; |
| 201 | + case BandRed: |
| 202 | + return "Re"; |
| 203 | + case BandOrange: |
| 204 | + return "Or"; |
| 205 | + case BandYellow: |
| 206 | + return "Ye"; |
| 207 | + case BandGreen: |
| 208 | + return "Gr"; |
| 209 | + case BandBlue: |
| 210 | + return "Bl"; |
| 211 | + case BandPurple: |
| 212 | + return "Pu"; |
| 213 | + case BandGray: |
| 214 | + return "Gy"; |
| 215 | + case BandWhite: |
| 216 | + return "Wh"; |
| 217 | + case BandGold: |
| 218 | + return "Go"; |
| 219 | + case BandSilver: |
| 220 | + return "Si"; |
| 221 | + default: |
| 222 | + return "--"; |
| 223 | + } |
204 | 224 | }
|
0 commit comments