Skip to content

Commit 0fd09ca

Browse files
authored
Merge pull request #11 from instantiator/006-new-graphics
006 - new graphics
2 parents 8609448 + fa5c022 commit 0fd09ca

12 files changed

+277
-208
lines changed

resistors/images/box_8x22.png

1.33 KB
Loading

resistors/images/r3.png

2.92 KB
Loading

resistors/images/r4.png

2.98 KB
Loading

resistors/images/r5.png

2.98 KB
Loading

resistors/images/r6.png

3.02 KB
Loading

resistors/src/app_state.c

+15-2
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,31 @@ void app_init_resistor(App* app, ResistorType rtype) {
3333
app->state->resistor_type = rtype;
3434
app->state->edit_selection = 0;
3535
switch(rtype) {
36-
case Resistor4Band:
36+
case R3:
37+
app->state->resistor_bands[0] = BandRed;
38+
app->state->resistor_bands[1] = BandOrange;
39+
app->state->resistor_bands[2] = BandYellow;
40+
break;
41+
case R4:
42+
app->state->resistor_bands[0] = BandRed;
43+
app->state->resistor_bands[1] = BandOrange;
44+
app->state->resistor_bands[2] = BandYellow;
45+
app->state->resistor_bands[3] = BandGreen;
46+
break;
47+
case R5:
3748
app->state->resistor_bands[0] = BandRed;
3849
app->state->resistor_bands[1] = BandOrange;
3950
app->state->resistor_bands[2] = BandYellow;
4051
app->state->resistor_bands[3] = BandGreen;
52+
app->state->resistor_bands[4] = BandBlue;
4153
break;
42-
case Resistor5Band:
54+
case R6:
4355
app->state->resistor_bands[0] = BandRed;
4456
app->state->resistor_bands[1] = BandOrange;
4557
app->state->resistor_bands[2] = BandYellow;
4658
app->state->resistor_bands[3] = BandGreen;
4759
app->state->resistor_bands[4] = BandBlue;
60+
app->state->resistor_bands[5] = BandPurple;
4861
break;
4962
default:
5063
FURI_LOG_E(TAG, "Unknown resistor type in app_init_resistor");

resistors/src/app_state.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
typedef struct AppState {
88
uint8_t edit_selection;
99
ResistorType resistor_type;
10-
BandColour resistor_bands[5];
10+
BandColour resistor_bands[6];
1111
} AppState;
1212

1313
typedef struct App {

resistors/src/resistor_logic.c

+146-126
Original file line numberDiff line numberDiff line change
@@ -3,202 +3,222 @@
33
#include "resistor_logic.h"
44
#include <math.h>
55

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+
}
1728

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+
}
2032

2133
bool is_numeric_band(ResistorType rtype, int index) {
22-
return index < (rtype - 2);
34+
return index < NUMERIC_BANDS_PER_RESISTOR[rtype - 1];
2335
}
2436

2537
bool is_multiplier_band(ResistorType rtype, int index) {
26-
return index == (rtype - 2);
38+
return index == MULTIPLIER_INDEX_PER_RESISTOR[rtype - 1];
2739
}
2840

2941
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];
3147
}
3248

3349
bool is_numeric_colour(BandColour colour) {
3450
return colour <= 9;
3551
}
3652

37-
bool is_multiplier_colour() {
53+
bool is_multiplier_colour(BandColour colour) {
54+
UNUSED(colour);
3855
return true;
3956
}
4057

4158
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 ||
4361
colour == BandPurple || colour == BandGray || colour == BandGold ||
4462
colour == BandSilver;
4563
}
4664

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+
4770
BandColour
4871
alter_resistor_band(ResistorType rtype, int band, BandColour current_colour, int direction) {
4972
int colour = current_colour;
5073
bool accepted = false;
5174
while(!accepted) {
5275
colour += direction;
5376
if(colour > 11) colour = 0;
54-
if(colour < 0) colour = 12;
77+
if(colour < 0) colour = 11;
5578
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;
5680
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;
5882
}
5983
return colour;
6084
}
6185

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];
6488
int value = 0;
6589
for(int b = 0; b < bands; b++) {
6690
int pwr = bands - b - 1;
6791
int delta = ((int)pow(10.0, pwr)) * colours[b];
6892
value += delta;
6993
}
94+
return value;
95+
}
7096

97+
void update_resistance_number(ResistorType rtype, BandColour colours[], char string[]) {
98+
int value = decode_resistance_number(rtype, colours);
7199
int length = snprintf(NULL, 0, "%d", value);
72100
char* str = malloc(length + 1);
73101
snprintf(str, length + 1, "%d", value);
74102

75-
char* target = string + index;
103+
char* target = string + INDEX_NUMERIC;
76104
strncpy(target, str, length);
77105
free(str);
78106
}
79107

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^ ";
82110
if(colour > 9) {
83111
unit[5] = '-';
84112
unit[6] = (char)(48 + colour - 9);
85113
} else {
86114
unit[5] = (char)(48 + colour);
87-
unit[6] = ' ';
115+
unit[6] = '\0';
88116
}
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);
91125
}
92126

93-
void update_resistance_tolerance(BandColour colour, char string[], int index) {
94-
char* target = string + index;
127+
char* decode_resistance_tolerance(BandColour colour) {
95128
switch(colour) {
96129
case BandBrown:;
97-
char tolerance_brown[8] = "@ 1% ";
98-
strncpy(target, tolerance_brown, RESISTOR_TOLERANCE_CHARS);
99-
break;
130+
return "1%";
100131
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%";
104137
case BandGreen:;
105-
char tolerance_green[8] = "@ 0.5% ";
106-
strncpy(target, tolerance_green, RESISTOR_TOLERANCE_CHARS);
107-
break;
138+
return "0.5%";
108139
case BandBlue:;
109-
char tolerance_blue[8] = "@ 0.25%";
110-
strncpy(target, tolerance_blue, RESISTOR_TOLERANCE_CHARS);
111-
break;
140+
return "0.25%";
112141
case BandPurple:;
113-
char tolerance_purple[8] = "@ 0.1% ";
114-
strncpy(target, tolerance_purple, RESISTOR_TOLERANCE_CHARS);
115-
break;
142+
return "0.1%";
116143
case BandGray:;
117-
char tolerance_gray[8] = "@ 0.05%";
118-
strncpy(target, tolerance_gray, RESISTOR_TOLERANCE_CHARS);
119-
break;
144+
return "0.05%";
120145
case BandGold:;
121-
char tolerance_gold[8] = "@ 5% ";
122-
strncpy(target, tolerance_gold, RESISTOR_TOLERANCE_CHARS);
123-
break;
146+
return "5%";
124147
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";
128182
default:;
129-
char tolerance_nk[8] = "@ ????%";
130-
strncpy(target, tolerance_nk, RESISTOR_TOLERANCE_CHARS);
131-
break;
183+
return "--";
132184
}
133185
}
134186

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+
}
204224
}

0 commit comments

Comments
 (0)