resistor_logic.c 6.5 KB

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