language.c 9.4 KB

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