language.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. //language.c
  2. #include "language.h"
  3. #include <avr/pgmspace.h>
  4. #include <avr/eeprom.h>
  5. #include "bootapp.h"
  6. #include "Configuration.h"
  7. #include "pins.h"
  8. #ifdef XFLASH
  9. #include "xflash.h"
  10. #include "xflash_layout.h"
  11. #endif //XFLASH
  12. // Currently active language selection.
  13. uint8_t lang_selected = 0;
  14. #if (LANG_MODE == 0) //primary language only
  15. uint8_t lang_select(_UNUSED uint8_t lang) { return 0; }
  16. uint8_t lang_get_count() { return 1; }
  17. uint16_t lang_get_code(_UNUSED uint8_t lang) { return LANG_CODE_EN; }
  18. const char* lang_get_name_by_code(_UNUSED uint16_t code) { return _n("English"); }
  19. void lang_reset(void) { }
  20. uint8_t lang_is_selected(void) { return 1; }
  21. #else //(LANG_MODE == 0) //secondary languages in progmem or xflash
  22. //reserved xx kbytes for secondary language table
  23. const char __attribute__((aligned(256))) _SEC_LANG[LANG_SIZE_RESERVED] PROGMEM_I2 = "_SEC_LANG";
  24. //primary language signature
  25. const uint32_t _PRI_LANG_SIGNATURE[1] __attribute__((section(".progmem0"))) = {0xffffffff};
  26. //lang_table pointer
  27. lang_table_t* lang_table = 0;
  28. const char* lang_get_translation(const char* s)
  29. {
  30. if (lang_selected == 0) return s + 2; //primary language selected, return orig. str.
  31. if (lang_table == 0) return s + 2; //sec. lang table not found, return orig. str.
  32. uint16_t ui = pgm_read_word(((uint16_t*)s)); //read string id
  33. if (ui == 0xffff) return s + 2; //id not assigned, return orig. str.
  34. ui = pgm_read_word(((uint16_t*)(((char*)lang_table + 16 + ui*2)))); //read relative offset
  35. if (pgm_read_byte(((uint8_t*)((char*)lang_table + ui))) == 0) //read first character
  36. return s + 2;//zero length string == not translated, return orig. str.
  37. return (const char*)((char*)lang_table + ui); //return calculated pointer
  38. }
  39. uint8_t lang_select(uint8_t lang)
  40. {
  41. if (lang == LANG_ID_PRI) //primary language
  42. {
  43. lang_table = 0;
  44. lang_selected = lang;
  45. }
  46. #ifdef XFLASH
  47. if (lang_get_code(lang) == lang_get_code(LANG_ID_SEC)) lang = LANG_ID_SEC;
  48. if (lang == LANG_ID_SEC) //current secondary language
  49. {
  50. if (pgm_read_dword(((uint32_t*)_SEC_LANG_TABLE)) == LANG_MAGIC) //magic valid
  51. {
  52. if (lang_check(_SEC_LANG_TABLE))
  53. if (pgm_read_dword(((uint32_t*)(_SEC_LANG_TABLE + 12))) == pgm_read_dword(((uint32_t*)(_PRI_LANG_SIGNATURE)))) //signature valid
  54. {
  55. lang_table = (lang_table_t*)(_SEC_LANG_TABLE); // set table pointer
  56. lang_selected = lang; // set language id
  57. }
  58. }
  59. }
  60. #else //XFLASH
  61. if (lang == LANG_ID_SEC)
  62. {
  63. uint16_t table = _SEC_LANG_TABLE;
  64. if (pgm_read_dword(((uint32_t*)table)) == LANG_MAGIC) //magic valid
  65. {
  66. if (lang_check(table))
  67. if (pgm_read_dword(((uint32_t*)(table + 12))) == pgm_read_dword(((uint32_t*)(_PRI_LANG_SIGNATURE)))) //signature valid
  68. {
  69. lang_table = (lang_table_t*)table; // set table pointer
  70. lang_selected = lang; // set language id
  71. }
  72. }
  73. }
  74. #endif //XFLASH
  75. if (lang_selected == lang)
  76. {
  77. eeprom_update_byte((unsigned char*)EEPROM_LANG, lang_selected);
  78. return 1;
  79. }
  80. return 0;
  81. }
  82. uint8_t lang_check(uint16_t addr)
  83. {
  84. uint16_t sum = 0;
  85. uint16_t size = pgm_read_word((uint16_t*)(addr + 4));
  86. uint16_t lt_sum = pgm_read_word((uint16_t*)(addr + 8));
  87. uint16_t i; for (i = 0; i < size; i++)
  88. sum += (uint16_t)pgm_read_byte((uint8_t*)(addr + i)) << ((i & 1)?0:8);
  89. sum -= lt_sum; //subtract checksum
  90. sum = (sum >> 8) | ((sum & 0xff) << 8); //swap bytes
  91. return (sum == lt_sum);
  92. }
  93. uint8_t lang_get_count()
  94. {
  95. if (pgm_read_dword(((uint32_t*)(_PRI_LANG_SIGNATURE))) == 0xffffffff)
  96. return 1; //signature not set - only primary language will be available
  97. #ifdef XFLASH
  98. XFLASH_SPI_ENTER();
  99. uint8_t count = 2; //count = 1+n (primary + secondary + all in xflash)
  100. uint32_t addr = LANG_OFFSET;
  101. lang_table_header_t header; //table header structure
  102. while (1)
  103. {
  104. xflash_rd_data(addr, (uint8_t*)&header, sizeof(lang_table_header_t)); //read table header from xflash
  105. if (header.magic != LANG_MAGIC) break; //break if magic not valid
  106. addr += header.size; //calc address of next table
  107. count++; //inc counter
  108. }
  109. #else //XFLASH
  110. uint16_t table = _SEC_LANG_TABLE;
  111. uint8_t count = 1; //count = 1 (primary)
  112. while (pgm_read_dword(((uint32_t*)table)) == LANG_MAGIC) //magic valid
  113. {
  114. table += pgm_read_word((uint16_t*)(table + 4));
  115. count++;
  116. }
  117. #endif //XFLASH
  118. return count;
  119. }
  120. uint8_t lang_get_header(uint8_t lang, lang_table_header_t* header, uint32_t* offset)
  121. {
  122. if (lang == LANG_ID_PRI) return 0; //primary lang not supported for this function
  123. #ifdef XFLASH
  124. if (lang == LANG_ID_SEC)
  125. {
  126. uint16_t ui = _SEC_LANG_TABLE; //table pointer
  127. memcpy_P(header, (lang_table_t*)(_SEC_LANG_TABLE), sizeof(lang_table_header_t)); //read table header from progmem
  128. if (offset) *offset = ui;
  129. return (header->magic == LANG_MAGIC)?1:0; //return 1 if magic valid
  130. }
  131. XFLASH_SPI_ENTER();
  132. uint32_t addr = LANG_OFFSET;
  133. lang--;
  134. while (1)
  135. {
  136. xflash_rd_data(addr, (uint8_t*)(header), sizeof(lang_table_header_t)); //read table header from xflash
  137. if (header->magic != LANG_MAGIC) break; //break if not valid
  138. if (offset) *offset = addr;
  139. if (--lang == 0) return 1;
  140. addr += header->size; //calc address of next table
  141. }
  142. #else //XFLASH
  143. if (lang == LANG_ID_SEC)
  144. {
  145. uint16_t ui = _SEC_LANG_TABLE; //table pointer
  146. memcpy_P(header, (lang_table_header_t*)ui, sizeof(lang_table_header_t)); //read table header from progmem
  147. if (offset) *offset = ui;
  148. return (header->magic == LANG_MAGIC)?1:0; //return 1 if magic valid
  149. }
  150. #endif //XFLASH
  151. return 0;
  152. }
  153. uint16_t lang_get_code(uint8_t lang)
  154. {
  155. if (lang == LANG_ID_PRI) return LANG_CODE_EN; //primary lang = EN
  156. #ifdef XFLASH
  157. if (lang == LANG_ID_SEC)
  158. {
  159. uint16_t ui = _SEC_LANG_TABLE; //table pointer
  160. if (pgm_read_dword(((uint32_t*)(ui + 0))) != LANG_MAGIC) return LANG_CODE_XX; //magic not valid
  161. return pgm_read_word(((uint32_t*)(ui + 10))); //return lang code from progmem
  162. }
  163. XFLASH_SPI_ENTER();
  164. uint32_t addr = LANG_OFFSET;
  165. lang_table_header_t header; //table header structure
  166. lang--;
  167. while (1)
  168. {
  169. xflash_rd_data(addr, (uint8_t*)&header, sizeof(lang_table_header_t)); //read table header from xflash
  170. if (header.magic != LANG_MAGIC) break; //break if not valid
  171. if (--lang == 0) return header.code;
  172. addr += header.size; //calc address of next table
  173. }
  174. #else //XFLASH
  175. uint16_t table = _SEC_LANG_TABLE;
  176. uint8_t count = 1; //count = 1 (primary)
  177. while (pgm_read_dword((uint32_t*)table) == LANG_MAGIC) //magic valid
  178. {
  179. if (count == lang) return pgm_read_word(((uint16_t*)(table + 10))); //read language code
  180. table += pgm_read_word((uint16_t*)(table + 4));
  181. count++;
  182. }
  183. #endif //XFLASH
  184. return LANG_CODE_XX;
  185. }
  186. const char* lang_get_name_by_code(uint16_t code)
  187. {
  188. switch (code)
  189. {
  190. case LANG_CODE_EN: return _n("English");
  191. case LANG_CODE_CZ: return _n("Cestina");
  192. case LANG_CODE_DE: return _n("Deutsch");
  193. case LANG_CODE_ES: return _n("Espanol");
  194. case LANG_CODE_FR: return _n("Francais");
  195. case LANG_CODE_IT: return _n("Italiano");
  196. case LANG_CODE_PL: return _n("Polski");
  197. #ifdef COMMUNITY_LANGUAGE_SUPPORT //Community language support
  198. #ifdef COMMUNITY_LANG_GROUP1_NL
  199. case LANG_CODE_NL: return _n("Nederlands"); //community Dutch contribution
  200. #endif // COMMUNITY_LANG_GROUP1_NL
  201. #ifdef COMMUNITY_LANG_GROUP1_SV
  202. case LANG_CODE_SV: return _n("Svenska"); //community Swedish contribution
  203. #endif // COMMUNITY_LANG_GROUP1_SV
  204. #ifdef COMMUNITY_LANG_GROUP1_NO
  205. case LANG_CODE_NO: return _n("Norsk"); //community Swedish contribution
  206. #endif // COMMUNITY_LANG_GROUP1_NO
  207. #ifdef COMMUNITY_LANG_GROUP1_DA
  208. case LANG_CODE_DA: return _n("Dansk"); //community Danish contribution
  209. #endif // COMMUNITY_LANG_GROUP1_DA
  210. #ifdef COMMUNITY_LANG_GROUP1_SK
  211. case LANG_CODE_SK: return _n("Slovencina"); //community Slovak contribution
  212. #endif // COMMUNITY_LANG_GROUP1_SK
  213. #ifdef COMMUNITY_LANG_GROUP1_SL
  214. case LANG_CODE_SL: return _n("Slovenscina"); //community Slovanian contribution
  215. #endif // COMMUNITY_LANG_GROUP1_SL
  216. #ifdef COMMUNITY_LANG_GROUP1_HU
  217. case LANG_CODE_HU: return _n("Magyar"); //community Hungarian contribution
  218. #endif // COMMUNITY_LANG_GROUP1_HU
  219. #ifdef COMMUNITY_LANG_GROUP1_LB
  220. case LANG_CODE_LB: return _n("Letzebuergesch"); //community Luxembourgish contribution
  221. #endif // COMMUNITY_LANG_GROUP1_LB
  222. #ifdef COMMUNITY_LANG_GROUP1_HR
  223. case LANG_CODE_HR: return _n("Hrvatski"); //community Croatian contribution
  224. #endif // COMMUNITY_LANG_GROUP1_HR
  225. #ifdef COMMUNITY_LANG_GROUP1_LT
  226. case LANG_CODE_LT: return _n("Lietuviu"); //community Lithuanian contribution
  227. #endif // COMMUNITY_LANG_GROUP1_LT
  228. #ifdef COMMUNITY_LANG_GROUP1_RO
  229. case LANG_CODE_RO: return _n("Romana"); //community Romanian contribution
  230. #endif // COMMUNITY_LANG_GROUP1_RO
  231. //Use the 3 lines below as a template and replace 'QR' and 'New language'
  232. //#ifdef COMMUNITY_LANG_GROUP1_QR
  233. // case LANG_CODE_QR: return _n("New language"); //community contribution
  234. //#endif // COMMUNITY_LANG_GROUP1_QR
  235. #endif // COMMUNITY_LANGUAGE_SUPPORT
  236. }
  237. return _n("??");
  238. }
  239. void lang_reset(void)
  240. {
  241. lang_selected = 0;
  242. eeprom_update_byte((unsigned char*)EEPROM_LANG, LANG_ID_FORCE_SELECTION);
  243. }
  244. uint8_t lang_is_selected(void)
  245. {
  246. uint8_t lang_eeprom = eeprom_read_byte((unsigned char*)EEPROM_LANG);
  247. return (lang_eeprom != LANG_ID_FORCE_SELECTION) && (lang_eeprom == lang_selected);
  248. }
  249. #ifdef DEBUG_SEC_LANG
  250. #include <stdio.h>
  251. const char* lang_get_sec_lang_str_by_id(uint16_t id)
  252. {
  253. uint16_t ui = _SEC_LANG_TABLE; //table pointer
  254. return ui + pgm_read_word(((uint16_t*)(ui + 16 + id * 2))); //read relative offset and return calculated pointer
  255. }
  256. uint16_t lang_print_sec_lang(FILE* out)
  257. {
  258. printf_P(_n("&_SEC_LANG = 0x%04x\n"), &_SEC_LANG);
  259. printf_P(_n("sizeof(_SEC_LANG) = 0x%04x\n"), sizeof(_SEC_LANG));
  260. uint16_t ptr_lang_table0 = ((uint16_t)(&_SEC_LANG) + 0xff) & 0xff00;
  261. printf_P(_n("&_lang_table0 = 0x%04x\n"), ptr_lang_table0);
  262. uint32_t _lt_magic = pgm_read_dword(((uint32_t*)(ptr_lang_table0 + 0)));
  263. uint16_t _lt_size = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 4)));
  264. uint16_t _lt_count = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 6)));
  265. uint16_t _lt_chsum = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 8)));
  266. uint16_t _lt_resv0 = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 10)));
  267. uint32_t _lt_resv1 = pgm_read_dword(((uint32_t*)(ptr_lang_table0 + 12)));
  268. printf_P(_n(" _lt_magic = 0x%08lx %S\n"), _lt_magic, (_lt_magic==LANG_MAGIC)?_n("OK"):_n("NA"));
  269. printf_P(_n(" _lt_size = 0x%04x (%d)\n"), _lt_size, _lt_size);
  270. printf_P(_n(" _lt_count = 0x%04x (%d)\n"), _lt_count, _lt_count);
  271. printf_P(_n(" _lt_chsum = 0x%04x\n"), _lt_chsum);
  272. printf_P(_n(" _lt_resv0 = 0x%04x\n"), _lt_resv0);
  273. printf_P(_n(" _lt_resv1 = 0x%08lx\n"), _lt_resv1);
  274. if (_lt_magic != LANG_MAGIC) return 0;
  275. puts_P(_n(" strings:\n"));
  276. uint16_t ui = _SEC_LANG_TABLE; //table pointer
  277. for (ui = 0; ui < _lt_count; ui++)
  278. fprintf_P(out, _n(" %3d %S\n"), ui, lang_get_sec_lang_str_by_id(ui));
  279. return _lt_count;
  280. }
  281. #endif //DEBUG_SEC_LANG
  282. #endif //(LANG_MODE == 0)
  283. void lang_boot_update_start(uint8_t lang)
  284. {
  285. uint8_t cnt = lang_get_count();
  286. if ((lang < 2) || (lang > cnt)) return; //only languages from xflash can be selected
  287. bootapp_reboot_user0(lang << 3);
  288. }