language.c 7.7 KB

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