language.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //language.c
  2. #include "language.h"
  3. #include <avr/pgmspace.h>
  4. #include "bootapp.h"
  5. #ifdef W25X20CL
  6. #include "w25x20cl.h"
  7. #endif //W25X20CL
  8. // Currently active language selection.
  9. uint8_t lang_selected = 0;
  10. #if (LANG_MODE == 0) //primary language only
  11. uint8_t lang_select(uint8_t lang) { return 0; }
  12. uint8_t lang_get_count() { return 1; }
  13. uint16_t lang_get_code(uint8_t lang) { return LANG_CODE_EN; }
  14. const char* lang_get_name_by_code(uint16_t code) { return _n("English"); }
  15. #else //(LANG_MODE == 0) //secondary languages in progmem or xflash
  16. //reserved xx kbytes for secondary language table
  17. const char _SEC_LANG[LANG_SIZE_RESERVED] PROGMEM_I2 = "_SEC_LANG";
  18. //lang_table pointer
  19. lang_table_t* lang_table = 0;
  20. const char* lang_get_translation(const char* s)
  21. {
  22. if (lang_selected == 0) return s + 2; //primary language selected, return orig. str.
  23. if (lang_table == 0) return s + 2; //sec. lang table not found, return orig. str.
  24. uint16_t ui = pgm_read_word(((uint16_t*)s)); //read string id
  25. if (ui == 0xffff) return s + 2; //translation not found, return orig. str.
  26. ui = pgm_read_word(((uint16_t*)(((char*)lang_table + 16 + ui*2)))); //read relative offset
  27. if (pgm_read_byte(((uint8_t*)((char*)lang_table + ui))) == 0) //read first character
  28. return s + 2;//zero length string == not translated, return orig. str.
  29. return (const char*)((char*)lang_table + ui); //return calculated pointer
  30. }
  31. uint8_t lang_select(uint8_t lang)
  32. {
  33. if (lang == LANG_ID_PRI) //primary language
  34. {
  35. lang_table = 0;
  36. lang_selected = 0;
  37. return 1;
  38. }
  39. #ifdef W25X20CL
  40. if (lang == LANG_ID_SEC) //current secondary language
  41. {
  42. uint16_t ui = _SEC_LANG_TABLE; //table pointer
  43. if (pgm_read_dword(((uint32_t*)(ui + 0))) != LANG_MAGIC) return 0; //magic not valid
  44. lang_table = ui; // set table pointer
  45. lang_selected = 1; // set language id
  46. return 1;
  47. }
  48. #else //W25X20CL
  49. #endif //W25X20CL
  50. /*
  51. uint16_t ui = (uint16_t)&_SEC_LANG; //pointer to _SEC_LANG reserved space
  52. ui += 0x00ff; //add 1 page
  53. ui &= 0xff00; //align to page
  54. lang_table = ui; //set table pointer
  55. ui = pgm_read_word(((uint16_t*)(((char*)lang_table + 16)))); //read relative offset of first string (language name)
  56. return (const char*)((char*)lang_table + ui); //return calculated pointer
  57. */
  58. return 0;
  59. }
  60. uint8_t lang_get_count()
  61. {
  62. #ifdef W25X20CL
  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. uint32_t addr = 0x00000; //start of xflash
  89. lang--;
  90. while (1)
  91. {
  92. W25X20CL_SPI_ENTER();
  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. uint32_t addr = 0x00000; //start of xflash
  114. lang_table_header_t header; //table header structure
  115. lang--;
  116. while (1)
  117. {
  118. w25x20cl_rd_data(addr, (uint8_t*)&header, sizeof(lang_table_header_t)); //read table header from xflash
  119. if (header.magic != LANG_MAGIC) break; //break if not valid
  120. if (--lang == 0) return header.code;
  121. addr += header.size; //calc address of next table
  122. }
  123. #else //W25X20CL
  124. #endif //W25X20CL
  125. // if (lang == LANG_ID_SEC)
  126. // {
  127. // uint16_t ui = _SEC_LANG_TABLE; //table pointer
  128. // if (pgm_read_dword(((uint32_t*)(ui + 0))) == LANG_MAGIC) //magic num is OK
  129. // return pgm_read_word(((uint16_t*)(ui + 10))); //read language code
  130. // }
  131. return LANG_CODE_XX;
  132. }
  133. const char* lang_get_name_by_code(uint16_t code)
  134. {
  135. switch (code)
  136. {
  137. case LANG_CODE_EN: return _n("English");
  138. case LANG_CODE_CZ: return _n("Cestina");
  139. case LANG_CODE_DE: return _n("Deutsch");
  140. case LANG_CODE_ES: return _n("Espanol");
  141. case LANG_CODE_IT: return _n("Italiano");
  142. case LANG_CODE_PL: return _n("Polski");
  143. }
  144. return _n("??");
  145. }
  146. #ifdef DEBUG_SEC_LANG
  147. const char* lang_get_sec_lang_str_by_id(uint16_t id)
  148. {
  149. uint16_t ui = _SEC_LANG_TABLE; //table pointer
  150. return ui + pgm_read_word(((uint16_t*)(ui + 16 + id * 2))); //read relative offset and return calculated pointer
  151. }
  152. uint16_t lang_print_sec_lang(FILE* out)
  153. {
  154. printf_P(_n("&_SEC_LANG = 0x%04x\n"), &_SEC_LANG);
  155. printf_P(_n("sizeof(_SEC_LANG) = 0x%04x\n"), sizeof(_SEC_LANG));
  156. uint16_t ptr_lang_table0 = ((uint16_t)(&_SEC_LANG) + 0xff) & 0xff00;
  157. printf_P(_n("&_lang_table0 = 0x%04x\n"), ptr_lang_table0);
  158. uint32_t _lt_magic = pgm_read_dword(((uint32_t*)(ptr_lang_table0 + 0)));
  159. uint16_t _lt_size = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 4)));
  160. uint16_t _lt_count = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 6)));
  161. uint16_t _lt_chsum = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 8)));
  162. uint16_t _lt_resv0 = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 10)));
  163. uint32_t _lt_resv1 = pgm_read_dword(((uint32_t*)(ptr_lang_table0 + 12)));
  164. printf_P(_n(" _lt_magic = 0x%08lx %S\n"), _lt_magic, (_lt_magic==LANG_MAGIC)?_n("OK"):_n("NA"));
  165. printf_P(_n(" _lt_size = 0x%04x (%d)\n"), _lt_size, _lt_size);
  166. printf_P(_n(" _lt_count = 0x%04x (%d)\n"), _lt_count, _lt_count);
  167. printf_P(_n(" _lt_chsum = 0x%04x\n"), _lt_chsum);
  168. printf_P(_n(" _lt_resv0 = 0x%04x\n"), _lt_resv0);
  169. printf_P(_n(" _lt_resv1 = 0x%08lx\n"), _lt_resv1);
  170. if (_lt_magic != LANG_MAGIC) return 0;
  171. puts_P(_n(" strings:\n"));
  172. uint16_t ui = _SEC_LANG_TABLE; //table pointer
  173. for (ui = 0; ui < _lt_count; ui++)
  174. fprintf_P(out, _n(" %3d %S\n"), ui, lang_get_sec_lang_str_by_id(ui));
  175. return _lt_count;
  176. }
  177. #endif //DEBUG_SEC_LANG
  178. #endif //(LANG_MODE == 0)
  179. void lang_boot_upgrade_start(uint8_t lang)
  180. {
  181. uint8_t cnt = lang_get_count();
  182. if ((lang < 2) || (lang > cnt)) return; //only languages from xflash can be selected
  183. bootapp_reboot_user0(lang << 4);
  184. }