comparison Discovery/Src/tMenuEdit.c @ 776:45b8f3c2acce

Add support for a configurable compass declination in a range of -99 to 99 degrees. This allows the displayed compass reading to be configured to take into account the compass declination (difference between the measured magnetic North direction and the actual geographical North direction) that has different values for different locations around the globe. See https://magnetic-declination.com/ for more information. (mikeller)
author heinrichsweikamp
date Thu, 18 May 2023 09:49:17 +0200
parents 6169309d6eb9
children c31237d20491
comparison
equal deleted inserted replaced
775:46c6d2380d4e 776:45b8f3c2acce
25 /// You should have received a copy of the GNU General Public License 25 /// You should have received a copy of the GNU General Public License
26 /// along with this program. If not, see <http://www.gnu.org/licenses/>. 26 /// along with this program. If not, see <http://www.gnu.org/licenses/>.
27 ////////////////////////////////////////////////////////////////////////////// 27 //////////////////////////////////////////////////////////////////////////////
28 28
29 /* Includes ------------------------------------------------------------------*/ 29 /* Includes ------------------------------------------------------------------*/
30 #include <stdlib.h>
31
30 #include "tMenuEdit.h" 32 #include "tMenuEdit.h"
31 33
32 #include "externLogbookFlash.h" 34 #include "externLogbookFlash.h"
33 #include "gfx_fonts.h" 35 #include "gfx_fonts.h"
34 #include "tHome.h" 36 #include "tHome.h"
58 60
59 typedef struct 61 typedef struct
60 { 62 {
61 char orgText[32]; 63 char orgText[32];
62 char newText[32]; 64 char newText[32];
63 uint16_t input[4]; 65 uint32_t input[4];
64 uint16_t coord[3]; 66 uint16_t coord[3];
65 int8_t begin[4], size[4]; 67 int8_t begin[4], size[4];
66 tFont *fontUsed; 68 tFont *fontUsed;
67 uint32_t callerID; 69 uint32_t callerID;
68 uint8_t maintype; 70 uint8_t maintype;
78 FIELD_TOGGLE, 80 FIELD_TOGGLE,
79 FIELD_ON_OFF, 81 FIELD_ON_OFF,
80 FIELD_UDIGIT, 82 FIELD_UDIGIT,
81 FIELD_2DIGIT, 83 FIELD_2DIGIT,
82 FIELD_3DIGIT, 84 FIELD_3DIGIT,
85 FIELD_SDIGIT,
83 FIELD_FLOAT, 86 FIELD_FLOAT,
84 FIELD_END 87 FIELD_END
85 } SField; 88 } SField;
86 89
87 /* Private variables ---------------------------------------------------------*/ 90 /* Private variables ---------------------------------------------------------*/
614 switch(ident[id].maintype) 617 switch(ident[id].maintype)
615 { 618 {
616 case FIELD_NUMBERS: 619 case FIELD_NUMBERS:
617 write_buttonTextline(TXT2BYTE_ButtonMinus,TXT2BYTE_ButtonEnter,TXT2BYTE_ButtonPlus); 620 write_buttonTextline(TXT2BYTE_ButtonMinus,TXT2BYTE_ButtonEnter,TXT2BYTE_ButtonPlus);
618 621
619 if(ident[id].subtype == FIELD_UDIGIT) 622 switch (ident[id].subtype) {
620 { 623 case FIELD_UDIGIT:
621 if((newContent >= '0') && (newContent <= '9')) 624 if((newContent >= '0') && (newContent <= '9'))
622 ident[id].newText[ident[id].begin[block] + subBlockPosition] = newContent; 625 ident[id].newText[ident[id].begin[block] + subBlockPosition] = newContent;
623 626
624 mark_new_digit_of_actual_id_block_and_subBlock(); 627 mark_new_digit_of_actual_id_block_and_subBlock();
625 } 628
626 else if(ident[id].subtype == FIELD_3DIGIT) 629 break;
627 { 630 case FIELD_3DIGIT:
628 if((newContent >= '0') && (newContent <= '0'+200)) 631 if((newContent >= '0') && (newContent <= '0'+200))
629 { 632 {
630 split_Content_to_Digit_helper( newContent, &digit100, &digit10, &digit1); 633 split_Content_to_Digit_helper( newContent, &digit100, &digit10, &digit1);
631 ident[id].newText[ident[id].begin[block] + 0] = '0' + digit100; 634 ident[id].newText[ident[id].begin[block] + 0] = '0' + digit100;
632 ident[id].newText[ident[id].begin[block] + 1] = '0' + digit10; 635 ident[id].newText[ident[id].begin[block] + 1] = '0' + digit10;
633 ident[id].newText[ident[id].begin[block] + 2] = '0' + digit1; 636 ident[id].newText[ident[id].begin[block] + 2] = '0' + digit1;
634 mark_new_3digit_of_actual_id_block(); 637 mark_new_3digit_of_actual_id_block();
635 } 638 }
636 } 639
637 else // FIELD_2DIGIT 640 break;
638 { 641 case FIELD_2DIGIT:
639 if((newContent >= '0') && (newContent <= '0'+99)) 642 if((newContent >= '0') && (newContent <= '0'+99))
640 { 643 {
641 ident[id].newText[ident[id].begin[block]] = '0' + (newContent - '0')/10; 644 ident[id].newText[ident[id].begin[block]] = '0' + (newContent - '0')/10;
642 ident[id].newText[ident[id].begin[block] + 1] = '0' + ((newContent - '0') - (10*((newContent - '0')/10))); 645 ident[id].newText[ident[id].begin[block] + 1] = '0' + ((newContent - '0') - (10*((newContent - '0')/10)));
643 mark_new_2digit_of_actual_id_block(); 646 mark_new_2digit_of_actual_id_block();
644 } 647 }
648
649 break;
650 case FIELD_SDIGIT:
651 if ((subBlockPosition == 0 && (newContent == '+' || newContent == '-')) || (subBlockPosition > 0 && newContent >= '0' && newContent <= '9')) {
652 ident[id].newText[ident[id].begin[block] + subBlockPosition] = newContent;
653 }
654
655 mark_new_digit_of_actual_id_block_and_subBlock();
656
657 break;
645 } 658 }
646 break; 659 break;
647 case FIELD_BUTTON: 660 case FIELD_BUTTON:
648 set_globalState(menuID); 661 set_globalState(menuID);
649 break; 662 break;
756 else 769 else
757 if((ident[id].maintype == FIELD_NUMBERS) && (ident[id].subtype == FIELD_2DIGIT) && (action == ACTION_BUTTON_ENTER) &&(newContent >= '0') && (newContent <= '0' + 99)) 770 if((ident[id].maintype == FIELD_NUMBERS) && (ident[id].subtype == FIELD_2DIGIT) && (action == ACTION_BUTTON_ENTER) &&(newContent >= '0') && (newContent <= '0' + 99))
758 { 771 {
759 ident[id].newText[ident[id].begin[block] + 0] = '0' + (newContent - '0')/10; 772 ident[id].newText[ident[id].begin[block] + 0] = '0' + (newContent - '0')/10;
760 ident[id].newText[ident[id].begin[block] + 1] = '0' + ((newContent - '0') - (10*((newContent - '0')/10))); 773 ident[id].newText[ident[id].begin[block] + 1] = '0' + ((newContent - '0') - (10*((newContent - '0')/10)));
774 } else if (ident[id].maintype == FIELD_NUMBERS && ident[id].subtype == FIELD_SDIGIT && action == ACTION_BUTTON_ENTER && subBlockPosition == 0) {
775 if (newContent == '+' || newContent == '-') {
776 ident[id].newText[ident[id].begin[block] + subBlockPosition] = newContent;
777 }
761 } 778 }
762 else 779 else
763 if((ident[id].maintype == FIELD_NUMBERS) && (action == ACTION_BUTTON_ENTER) && (newContent >= '0') && (newContent <= '9')) 780 if((ident[id].maintype == FIELD_NUMBERS) && (action == ACTION_BUTTON_ENTER) && (newContent >= '0') && (newContent <= '9'))
764 ident[id].newText[ident[id].begin[block] + subBlockPosition] = newContent; 781 ident[id].newText[ident[id].begin[block] + subBlockPosition] = newContent;
765 782
811 *outDigit1 = tempDigit; 828 *outDigit1 = tempDigit;
812 829
813 return CopyContent; 830 return CopyContent;
814 } 831 }
815 832
833
834 static void checkUpdateSDigit(uint8_t newContent)
835 {
836 if ((subBlockPosition == 0 && (newContent == '+' || newContent == '-')) || (subBlockPosition > 0 && newContent >= '0' && newContent <= '9')) {
837 ident[id].newText[ident[id].begin[block] + subBlockPosition] = newContent;
838 }
839 }
840
841
816 void upMenuEditFieldDigit(void) 842 void upMenuEditFieldDigit(void)
817 { 843 {
818 uint8_t newContent; 844 uint8_t newContent;
819 uint8_t digit100; 845 uint8_t digit100;
820 uint8_t digit10; 846 uint8_t digit10;
841 ident[id].newText[ident[id].begin[block] + 1] = '0' + ((newContent - '0') - (10*((newContent - '0')/10))); 867 ident[id].newText[ident[id].begin[block] + 1] = '0' + ((newContent - '0') - (10*((newContent - '0')/10)));
842 mark_new_2digit_of_actual_id_block(); 868 mark_new_2digit_of_actual_id_block();
843 return; 869 return;
844 } 870 }
845 871
846 if((ident[id].maintype == FIELD_NUMBERS) && (newContent >= '0') && (newContent <= '9')) 872 if (ident[id].maintype == FIELD_NUMBERS && ident[id].subtype == FIELD_SDIGIT) {
873 checkUpdateSDigit(newContent);
874 } else if (ident[id].maintype == FIELD_NUMBERS && newContent >= '0' && newContent <= '9') {
847 ident[id].newText[ident[id].begin[block] + subBlockPosition] = newContent; 875 ident[id].newText[ident[id].begin[block] + subBlockPosition] = newContent;
876 }
848 877
849 mark_new_digit_of_actual_id_block_and_subBlock(); 878 mark_new_digit_of_actual_id_block_and_subBlock();
850 } 879 }
851 880
852 881
878 ident[id].newText[ident[id].begin[block] + 1] = '0' + ((newContent - '0') - (10*((newContent - '0')/10))); 907 ident[id].newText[ident[id].begin[block] + 1] = '0' + ((newContent - '0') - (10*((newContent - '0')/10)));
879 mark_new_2digit_of_actual_id_block(); 908 mark_new_2digit_of_actual_id_block();
880 return; 909 return;
881 } 910 }
882 911
883 if((ident[id].maintype == FIELD_NUMBERS) && (newContent >= '0') && (newContent <= '9')) 912 if (ident[id].maintype == FIELD_NUMBERS && ident[id].subtype == FIELD_SDIGIT) {
913 checkUpdateSDigit(newContent);
914 } else if (ident[id].maintype == FIELD_NUMBERS && newContent >= '0' && newContent <= '9') {
884 ident[id].newText[ident[id].begin[block] + subBlockPosition] = newContent; 915 ident[id].newText[ident[id].begin[block] + subBlockPosition] = newContent;
916 }
885 917
886 mark_new_digit_of_actual_id_block_and_subBlock(); 918 mark_new_digit_of_actual_id_block_and_subBlock();
887 } 919 }
888 920
889 921
890 void evaluateNewString(uint32_t editID, uint32_t *pNewValue1, uint32_t *pNewValue2, uint32_t *pNewValue3, uint32_t *pNewValue4) 922 void evaluateNewString(uint32_t editID, uint32_t *pNewValue1, uint32_t *pNewValue2, uint32_t *pNewValue3, uint32_t *pNewValue4)
891 { 923 {
892 if(editID != ident[id].callerID) 924 if(editID != ident[id].callerID)
893 return; 925 return;
926
927 bool isSigned = ident[id].maintype == FIELD_NUMBERS && ident[id].subtype == FIELD_SDIGIT;
894 928
895 uint8_t i, digitCount, digit; 929 uint8_t i, digitCount, digit;
896 uint32_t sum[4], multiplier; 930 uint32_t sum[4], multiplier;
897 931
898 for(i=0;i<4;i++) 932 for(i=0;i<4;i++)
903 { 937 {
904 multiplier = 1; 938 multiplier = 1;
905 for(digitCount = 1; digitCount < ident[id].size[i]; digitCount++) 939 for(digitCount = 1; digitCount < ident[id].size[i]; digitCount++)
906 multiplier *= 10; 940 multiplier *= 10;
907 941
942 bool isNegative = false;
908 for(digitCount = 0; digitCount < ident[id].size[i]; digitCount++) 943 for(digitCount = 0; digitCount < ident[id].size[i]; digitCount++)
909 { 944 {
910 digit = ident[id].newText[ident[id].begin[i] + digitCount]; 945 digit = ident[id].newText[ident[id].begin[i] + digitCount];
911 946
912 if(digit > '0') 947 if (isSigned && digitCount == 0) {
913 digit -= '0'; 948 if (digit == '-') {
914 else 949 isNegative = true;
915 digit = 0; 950 }
916 951 } else {
917 if(digit > 9) 952 if(digit > '0')
918 digit = 9; 953 digit -= '0';
919 954 else
920 sum[i] += digit * multiplier; 955 digit = 0;
956
957 if(digit > 9)
958 digit = 9;
959
960 sum[i] += digit * multiplier;
961 }
921 962
922 if(multiplier >= 10) 963 if(multiplier >= 10)
923 multiplier /= 10; 964 multiplier /= 10;
924 else 965 else
925 multiplier = 0; 966 multiplier = 0;
926 } 967 }
968
969 if (isSigned) {
970 int32_t value = sum[i];
971 if (isNegative) {
972 value = -value;
973 }
974 sum[i] = ((input_u)value).uint32;
975 }
976
927 i++; 977 i++;
928 } 978 }
929 979
930 *pNewValue1 = sum[0]; 980 *pNewValue1 = sum[0];
931 *pNewValue2 = sum[1]; 981 *pNewValue2 = sum[1];
1178 { 1228 {
1179 create_newText_for_actual_Id_and_field_select(); 1229 create_newText_for_actual_Id_and_field_select();
1180 return; 1230 return;
1181 } 1231 }
1182 1232
1233 bool isSigned = ident[id].maintype == FIELD_NUMBERS && ident[id].subtype == FIELD_SDIGIT;
1234
1183 uint8_t i, digitCount; 1235 uint8_t i, digitCount;
1184 uint32_t remainder, digit, divider; 1236 uint32_t remainder, digit, divider;
1185 1237
1186 i = 0; 1238 i = 0;
1187 while( ident[id].size[i] && (i < 4)) 1239 while( ident[id].size[i] && (i < 4))
1188 { 1240 {
1189 remainder = ident[id].input[i]; 1241 bool isNegative = false;
1242 if (isSigned) {
1243 int32_t value = ((input_u)ident[id].input[i]).int32;
1244 if (value < 0) {
1245 isNegative = true;
1246 }
1247 remainder = abs(value);
1248 } else {
1249 remainder = ident[id].input[i];
1250 }
1190 divider = 1; 1251 divider = 1;
1191 1252
1192 for(digitCount = 1; digitCount < ident[id].size[i]; digitCount++) 1253 for(digitCount = 1; digitCount < ident[id].size[i]; digitCount++)
1193 divider *= 10; 1254 divider *= 10;
1194 1255
1195 for(digitCount = 0; digitCount < ident[id].size[i]; digitCount++) 1256 for(digitCount = 0; digitCount < ident[id].size[i]; digitCount++)
1196 { 1257 {
1197 digit = remainder / divider; 1258 if (isSigned && digitCount == 0) {
1198 remainder -= digit * divider; 1259 ident[id].newText[ident[id].begin[i] + digitCount] = isNegative ? '-' : '+';
1260 } else {
1261 digit = remainder / divider;
1262 remainder -= digit * divider;
1263 if(digit < 10)
1264 ident[id].newText[ident[id].begin[i] + digitCount] = digit + '0';
1265 else
1266 ident[id].newText[ident[id].begin[i] + digitCount] = 'x';
1267 }
1268
1199 divider /= 10; 1269 divider /= 10;
1200 if(digit < 10)
1201 ident[id].newText[ident[id].begin[i] + digitCount] = digit + '0';
1202 else
1203 ident[id].newText[ident[id].begin[i] + digitCount] = 'x';
1204 } 1270 }
1205 i++; 1271 i++;
1206 } 1272 }
1207 } 1273 }
1208 1274
1308 void write_field_3digit(uint32_t editID, uint16_t XleftGimpStyle, uint16_t XrightGimpStyle, uint16_t YtopGimpStyle, const tFont *Font, const char *text, uint32_t int1, uint32_t int2, uint32_t int3, uint32_t int4) 1374 void write_field_3digit(uint32_t editID, uint16_t XleftGimpStyle, uint16_t XrightGimpStyle, uint16_t YtopGimpStyle, const tFont *Font, const char *text, uint32_t int1, uint32_t int2, uint32_t int3, uint32_t int4)
1309 { 1375 {
1310 write_field_udigit_and_2digit(FIELD_3DIGIT, editID,XleftGimpStyle,XrightGimpStyle,YtopGimpStyle,Font,text,int1,int2,int3,int4); 1376 write_field_udigit_and_2digit(FIELD_3DIGIT, editID,XleftGimpStyle,XrightGimpStyle,YtopGimpStyle,Font,text,int1,int2,int3,int4);
1311 } 1377 }
1312 1378
1313 /* 1379
1314 void write_field_sdigit(uint32_t editID, uint16_t XleftGimpStyle, uint16_t XrightGimpStyle, uint16_t YtopGimpStyle, const tFont *Font, const char *text, int32_t int1, int32_t int2, int32_t int3, int32_t int4) 1380 void write_field_sdigit(uint32_t editID, uint16_t XleftGimpStyle, uint16_t XrightGimpStyle, uint16_t YtopGimpStyle, const tFont *Font, const char *text, int32_t int1, int32_t int2, int32_t int3, int32_t int4)
1315 { 1381 {
1316 } 1382 write_field_udigit_and_2digit(FIELD_SDIGIT, editID, XleftGimpStyle, XrightGimpStyle, YtopGimpStyle, Font, text, ((input_u)int1).uint32, ((input_u)int2).uint32, ((input_u)int3).uint32, ((input_u)int4).uint32);
1317 */ 1383 }
1384
1318 1385
1319 void write_field_select(uint32_t editID, uint16_t XleftGimpStyle, uint16_t XrightGimpStyle, uint16_t YtopGimpStyle, const tFont *Font, const char *text, uint8_t int1, uint8_t int2, uint8_t int3, uint8_t int4) 1386 void write_field_select(uint32_t editID, uint16_t XleftGimpStyle, uint16_t XrightGimpStyle, uint16_t YtopGimpStyle, const tFont *Font, const char *text, uint8_t int1, uint8_t int2, uint8_t int3, uint8_t int4)
1320 { 1387 {
1321 if(id >= 9) 1388 if(id >= 9)
1322 return; 1389 return;