language.c 7.1 KB

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