language.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 _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; //translation not found, 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_LANG_SUPPORT //Community language support
  198. #ifdef COMMUNITY_LANG_NL
  199. case LANG_CODE_NL: return _n("Nederlands"); //community contribution
  200. #endif // COMMUNITY_LANG_NL
  201. //Use the 3 lines below as a template and replace 'QR' and 'New language'
  202. //#ifdef COMMUNITY_LANG_QR
  203. // case LANG_CODE_QR: return _n("New language"); //community contribution
  204. //#endif // COMMUNITY_LANG_QR
  205. #endif // COMMUNITY_LANG_SUPPORT
  206. }
  207. return _n("??");
  208. }
  209. void lang_reset(void)
  210. {
  211. lang_selected = 0;
  212. eeprom_update_byte((unsigned char*)EEPROM_LANG, LANG_ID_FORCE_SELECTION);
  213. }
  214. uint8_t lang_is_selected(void)
  215. {
  216. uint8_t lang_eeprom = eeprom_read_byte((unsigned char*)EEPROM_LANG);
  217. return (lang_eeprom != LANG_ID_FORCE_SELECTION) && (lang_eeprom == lang_selected);
  218. }
  219. #ifdef DEBUG_SEC_LANG
  220. #include <stdio.h>
  221. const char* lang_get_sec_lang_str_by_id(uint16_t id)
  222. {
  223. uint16_t ui = _SEC_LANG_TABLE; //table pointer
  224. return ui + pgm_read_word(((uint16_t*)(ui + 16 + id * 2))); //read relative offset and return calculated pointer
  225. }
  226. uint16_t lang_print_sec_lang(FILE* out)
  227. {
  228. printf_P(_n("&_SEC_LANG = 0x%04x\n"), &_SEC_LANG);
  229. printf_P(_n("sizeof(_SEC_LANG) = 0x%04x\n"), sizeof(_SEC_LANG));
  230. uint16_t ptr_lang_table0 = ((uint16_t)(&_SEC_LANG) + 0xff) & 0xff00;
  231. printf_P(_n("&_lang_table0 = 0x%04x\n"), ptr_lang_table0);
  232. uint32_t _lt_magic = pgm_read_dword(((uint32_t*)(ptr_lang_table0 + 0)));
  233. uint16_t _lt_size = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 4)));
  234. uint16_t _lt_count = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 6)));
  235. uint16_t _lt_chsum = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 8)));
  236. uint16_t _lt_resv0 = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 10)));
  237. uint32_t _lt_resv1 = pgm_read_dword(((uint32_t*)(ptr_lang_table0 + 12)));
  238. printf_P(_n(" _lt_magic = 0x%08lx %S\n"), _lt_magic, (_lt_magic==LANG_MAGIC)?_n("OK"):_n("NA"));
  239. printf_P(_n(" _lt_size = 0x%04x (%d)\n"), _lt_size, _lt_size);
  240. printf_P(_n(" _lt_count = 0x%04x (%d)\n"), _lt_count, _lt_count);
  241. printf_P(_n(" _lt_chsum = 0x%04x\n"), _lt_chsum);
  242. printf_P(_n(" _lt_resv0 = 0x%04x\n"), _lt_resv0);
  243. printf_P(_n(" _lt_resv1 = 0x%08lx\n"), _lt_resv1);
  244. if (_lt_magic != LANG_MAGIC) return 0;
  245. puts_P(_n(" strings:\n"));
  246. uint16_t ui = _SEC_LANG_TABLE; //table pointer
  247. for (ui = 0; ui < _lt_count; ui++)
  248. fprintf_P(out, _n(" %3d %S\n"), ui, lang_get_sec_lang_str_by_id(ui));
  249. return _lt_count;
  250. }
  251. #endif //DEBUG_SEC_LANG
  252. #endif //(LANG_MODE == 0)
  253. void lang_boot_update_start(uint8_t lang)
  254. {
  255. uint8_t cnt = lang_get_count();
  256. if ((lang < 2) || (lang > cnt)) return; //only languages from xflash can be selected
  257. bootapp_reboot_user0(lang << 4);
  258. }