language.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #ifdef W25X20CL
  8. #include "w25x20cl.h"
  9. #endif //W25X20CL
  10. // Currently active language selection.
  11. uint8_t lang_selected = 0;
  12. #if (LANG_MODE == 0) //primary language only
  13. uint8_t lang_select(uint8_t lang) { return 0; }
  14. uint8_t lang_get_count() { return 1; }
  15. uint16_t lang_get_code(uint8_t lang) { return LANG_CODE_EN; }
  16. const char* lang_get_name_by_code(uint16_t code) { return _n("English"); }
  17. #else //(LANG_MODE == 0) //secondary languages in progmem or xflash
  18. //reserved xx kbytes for secondary language table
  19. const char _SEC_LANG[LANG_SIZE_RESERVED] PROGMEM_I2 = "_SEC_LANG";
  20. //lang_table pointer
  21. lang_table_t* lang_table = 0;
  22. const char* lang_get_translation(const char* s)
  23. {
  24. if (lang_selected == 0) return s + 2; //primary language selected, return orig. str.
  25. if (lang_table == 0) return s + 2; //sec. lang table not found, return orig. str.
  26. uint16_t ui = pgm_read_word(((uint16_t*)s)); //read string id
  27. if (ui == 0xffff) return s + 2; //translation not found, return orig. str.
  28. ui = pgm_read_word(((uint16_t*)(((char*)lang_table + 16 + ui*2)))); //read relative offset
  29. if (pgm_read_byte(((uint8_t*)((char*)lang_table + ui))) == 0) //read first character
  30. return s + 2;//zero length string == not translated, return orig. str.
  31. return (const char*)((char*)lang_table + ui); //return calculated pointer
  32. }
  33. uint8_t lang_select(uint8_t lang)
  34. {
  35. if (lang == LANG_ID_PRI) //primary language
  36. {
  37. lang_table = 0;
  38. lang_selected = lang;
  39. }
  40. #ifdef W25X20CL
  41. if (lang_get_code(lang) == lang_get_code(LANG_ID_SEC)) lang = LANG_ID_SEC;
  42. if (lang == LANG_ID_SEC) //current secondary language
  43. {
  44. if (pgm_read_dword(((uint32_t*)_SEC_LANG_TABLE)) == LANG_MAGIC) //magic valid
  45. {
  46. lang_table = _SEC_LANG_TABLE; // set table pointer
  47. lang_selected = lang; // set language id
  48. }
  49. }
  50. #else //W25X20CL
  51. #endif //W25X20CL
  52. if (lang_selected == lang)
  53. {
  54. eeprom_update_byte((unsigned char*)EEPROM_LANG, lang_selected);
  55. return 1;
  56. }
  57. return 0;
  58. }
  59. uint8_t lang_get_count()
  60. {
  61. #ifdef W25X20CL
  62. W25X20CL_SPI_ENTER();
  63. uint8_t count = 2; //count = 1+n (primary + secondary + all in xflash)
  64. uint32_t addr = 0x00000; //start of xflash
  65. lang_table_header_t header; //table header structure
  66. while (1)
  67. {
  68. w25x20cl_rd_data(addr, (uint8_t*)&header, sizeof(lang_table_header_t)); //read table header from xflash
  69. if (header.magic != LANG_MAGIC) break; //break if magic not valid
  70. addr += header.size; //calc address of next table
  71. count++; //inc counter
  72. }
  73. return count;
  74. #else //W25X20CL
  75. #endif //W25X20CL
  76. }
  77. uint8_t lang_get_header(uint8_t lang, lang_table_header_t* header, uint32_t* offset)
  78. {
  79. if (lang == LANG_ID_PRI) return 0; //primary lang not supported for this function
  80. #ifdef W25X20CL
  81. if (lang == LANG_ID_SEC)
  82. {
  83. uint16_t ui = _SEC_LANG_TABLE; //table pointer
  84. memcpy_P(header, ui, sizeof(lang_table_header_t)); //read table header from progmem
  85. if (offset) *offset = ui;
  86. return (header == LANG_MAGIC)?1:0; //return 1 if magic valid
  87. }
  88. W25X20CL_SPI_ENTER();
  89. uint32_t addr = 0x00000; //start of xflash
  90. lang--;
  91. while (1)
  92. {
  93. w25x20cl_rd_data(addr, header, sizeof(lang_table_header_t)); //read table header from xflash
  94. if (header->magic != LANG_MAGIC) break; //break if not valid
  95. if (offset) *offset = addr;
  96. if (--lang == 0) return 1;
  97. addr += header->size; //calc address of next table
  98. }
  99. return 0;
  100. #else //W25X20CL
  101. #endif //W25X20CL
  102. }
  103. uint16_t lang_get_code(uint8_t lang)
  104. {
  105. if (lang == LANG_ID_PRI) return LANG_CODE_EN; //primary lang = EN
  106. #ifdef W25X20CL
  107. if (lang == LANG_ID_SEC)
  108. {
  109. uint16_t ui = _SEC_LANG_TABLE; //table pointer
  110. if (pgm_read_dword(((uint32_t*)(ui + 0))) != LANG_MAGIC) return LANG_CODE_XX; //magic not valid
  111. return pgm_read_word(((uint32_t*)(ui + 10))); //return lang code from progmem
  112. }
  113. W25X20CL_SPI_ENTER();
  114. uint32_t addr = 0x00000; //start of xflash
  115. lang_table_header_t header; //table header structure
  116. lang--;
  117. while (1)
  118. {
  119. w25x20cl_rd_data(addr, (uint8_t*)&header, sizeof(lang_table_header_t)); //read table header from xflash
  120. if (header.magic != LANG_MAGIC) break; //break if not valid
  121. if (--lang == 0) return header.code;
  122. addr += header.size; //calc address of next table
  123. }
  124. #else //W25X20CL
  125. #endif //W25X20CL
  126. // if (lang == LANG_ID_SEC)
  127. // {
  128. // uint16_t ui = _SEC_LANG_TABLE; //table pointer
  129. // if (pgm_read_dword(((uint32_t*)(ui + 0))) == LANG_MAGIC) //magic num is OK
  130. // return pgm_read_word(((uint16_t*)(ui + 10))); //read language code
  131. // }
  132. return LANG_CODE_XX;
  133. }
  134. const char* lang_get_name_by_code(uint16_t code)
  135. {
  136. switch (code)
  137. {
  138. case LANG_CODE_EN: return _n("English");
  139. case LANG_CODE_CZ: return _n("Cestina");
  140. case LANG_CODE_DE: return _n("Deutsch");
  141. case LANG_CODE_ES: return _n("Espanol");
  142. case LANG_CODE_IT: return _n("Italiano");
  143. case LANG_CODE_PL: return _n("Polski");
  144. }
  145. return _n("??");
  146. }
  147. void lang_reset(void)
  148. {
  149. lang_selected = 0;
  150. eeprom_update_byte((unsigned char*)EEPROM_LANG, LANG_ID_FORCE_SELECTION);
  151. }
  152. uint8_t lang_is_selected(void)
  153. {
  154. uint8_t lang_eeprom = eeprom_read_byte((unsigned char*)EEPROM_LANG);
  155. return (lang_eeprom != LANG_ID_FORCE_SELECTION) && (lang_eeprom == lang_selected);
  156. }
  157. #ifdef DEBUG_SEC_LANG
  158. const char* lang_get_sec_lang_str_by_id(uint16_t id)
  159. {
  160. uint16_t ui = _SEC_LANG_TABLE; //table pointer
  161. return ui + pgm_read_word(((uint16_t*)(ui + 16 + id * 2))); //read relative offset and return calculated pointer
  162. }
  163. uint16_t lang_print_sec_lang(FILE* out)
  164. {
  165. printf_P(_n("&_SEC_LANG = 0x%04x\n"), &_SEC_LANG);
  166. printf_P(_n("sizeof(_SEC_LANG) = 0x%04x\n"), sizeof(_SEC_LANG));
  167. uint16_t ptr_lang_table0 = ((uint16_t)(&_SEC_LANG) + 0xff) & 0xff00;
  168. printf_P(_n("&_lang_table0 = 0x%04x\n"), ptr_lang_table0);
  169. uint32_t _lt_magic = pgm_read_dword(((uint32_t*)(ptr_lang_table0 + 0)));
  170. uint16_t _lt_size = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 4)));
  171. uint16_t _lt_count = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 6)));
  172. uint16_t _lt_chsum = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 8)));
  173. uint16_t _lt_resv0 = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 10)));
  174. uint32_t _lt_resv1 = pgm_read_dword(((uint32_t*)(ptr_lang_table0 + 12)));
  175. printf_P(_n(" _lt_magic = 0x%08lx %S\n"), _lt_magic, (_lt_magic==LANG_MAGIC)?_n("OK"):_n("NA"));
  176. printf_P(_n(" _lt_size = 0x%04x (%d)\n"), _lt_size, _lt_size);
  177. printf_P(_n(" _lt_count = 0x%04x (%d)\n"), _lt_count, _lt_count);
  178. printf_P(_n(" _lt_chsum = 0x%04x\n"), _lt_chsum);
  179. printf_P(_n(" _lt_resv0 = 0x%04x\n"), _lt_resv0);
  180. printf_P(_n(" _lt_resv1 = 0x%08lx\n"), _lt_resv1);
  181. if (_lt_magic != LANG_MAGIC) return 0;
  182. puts_P(_n(" strings:\n"));
  183. uint16_t ui = _SEC_LANG_TABLE; //table pointer
  184. for (ui = 0; ui < _lt_count; ui++)
  185. fprintf_P(out, _n(" %3d %S\n"), ui, lang_get_sec_lang_str_by_id(ui));
  186. return _lt_count;
  187. }
  188. #endif //DEBUG_SEC_LANG
  189. #endif //(LANG_MODE == 0)
  190. void lang_boot_update_start(uint8_t lang)
  191. {
  192. uint8_t cnt = lang_get_count();
  193. if ((lang < 2) || (lang > cnt)) return; //only languages from xflash can be selected
  194. bootapp_reboot_user0(lang << 4);
  195. }