language.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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_SPI_ENTER();
  102. w25x20cl_rd_data(addr, header, sizeof(lang_table_header_t)); //read table header from xflash
  103. if (header->magic != LANG_MAGIC) break; //break if not valid
  104. if (offset) *offset = addr;
  105. if (--lang == 0) return 1;
  106. addr += header->size; //calc address of next table
  107. }
  108. return 0;
  109. #else //W25X20CL
  110. #endif //W25X20CL
  111. }
  112. uint16_t lang_get_code(uint8_t lang)
  113. {
  114. if (lang == LANG_ID_PRI) return LANG_CODE_EN; //primary lang = EN
  115. #ifdef W25X20CL
  116. if (lang == LANG_ID_SEC)
  117. {
  118. uint16_t ui = ((((uint16_t)&_SEC_LANG) + 0x00ff) & 0xff00); //table pointer
  119. if (pgm_read_dword(((uint32_t*)(ui + 0))) != LANG_MAGIC) return LANG_CODE_XX; //magic not valid
  120. return pgm_read_word(((uint32_t*)(ui + 10))); //return lang code from progmem
  121. }
  122. uint32_t addr = 0x00000; //start of xflash
  123. lang_table_header_t header; //table header structure
  124. lang--;
  125. while (1)
  126. {
  127. w25x20cl_rd_data(addr, (uint8_t*)&header, sizeof(lang_table_header_t)); //read table header from xflash
  128. if (header.magic != LANG_MAGIC) break; //break if not valid
  129. if (--lang == 0) return header.code;
  130. addr += header.size; //calc address of next table
  131. }
  132. #else //W25X20CL
  133. #endif //W25X20CL
  134. // if (lang == LANG_ID_SEC)
  135. // {
  136. // uint16_t ui = ((((uint16_t)&_SEC_LANG) + 0x00ff) & 0xff00); //table pointer
  137. // if (pgm_read_dword(((uint32_t*)(ui + 0))) == LANG_MAGIC) //magic num is OK
  138. // return pgm_read_word(((uint16_t*)(ui + 10))); //read language code
  139. // }
  140. return LANG_CODE_XX;
  141. }
  142. const char* lang_get_name_by_code(uint16_t code)
  143. {
  144. switch (code)
  145. {
  146. case LANG_CODE_EN: return _n("English");
  147. case LANG_CODE_CZ: return _n("Cestina");
  148. case LANG_CODE_DE: return _n("Deutsch");
  149. case LANG_CODE_ES: return _n("Espanol");
  150. case LANG_CODE_IT: return _n("Italiano");
  151. case LANG_CODE_PL: return _n("Polski");
  152. }
  153. return _n("??");
  154. // if (lang == LANG_ID_UNDEFINED) lang = lang_selected;
  155. // if (lang == LANG_ID_PRI) return _T(MSG_LANGUAGE_NAME + 2);
  156. // if (lang == LANG_ID_SEC)
  157. // {
  158. // uint16_t ui = ((((uint16_t)&_SEC_LANG) + 0x00ff) & 0xff00); //table pointer
  159. // if (pgm_read_dword(((uint32_t*)(ui + 0))) == LANG_MAGIC) //magic num is OK
  160. // return lang_get_sec_lang_str(MSG_LANGUAGE_NAME);
  161. // }
  162. // return 0;
  163. }
  164. #ifdef DEBUG_SEC_LANG
  165. const char* lang_get_sec_lang_str_by_id(uint16_t id)
  166. {
  167. uint16_t ui = ((((uint16_t)&_SEC_LANG) + 0x00ff) & 0xff00); //table pointer
  168. return ui + pgm_read_word(((uint16_t*)(ui + 16 + id * 2))); //read relative offset and return calculated pointer
  169. }
  170. uint16_t lang_print_sec_lang(FILE* out)
  171. {
  172. printf_P(_n("&_SEC_LANG = 0x%04x\n"), &_SEC_LANG);
  173. printf_P(_n("sizeof(_SEC_LANG) = 0x%04x\n"), sizeof(_SEC_LANG));
  174. uint16_t ptr_lang_table0 = ((uint16_t)(&_SEC_LANG) + 0xff) & 0xff00;
  175. printf_P(_n("&_lang_table0 = 0x%04x\n"), ptr_lang_table0);
  176. uint32_t _lt_magic = pgm_read_dword(((uint32_t*)(ptr_lang_table0 + 0)));
  177. uint16_t _lt_size = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 4)));
  178. uint16_t _lt_count = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 6)));
  179. uint16_t _lt_chsum = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 8)));
  180. uint16_t _lt_resv0 = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 10)));
  181. uint32_t _lt_resv1 = pgm_read_dword(((uint32_t*)(ptr_lang_table0 + 12)));
  182. printf_P(_n(" _lt_magic = 0x%08lx %S\n"), _lt_magic, (_lt_magic==LANG_MAGIC)?_n("OK"):_n("NA"));
  183. printf_P(_n(" _lt_size = 0x%04x (%d)\n"), _lt_size, _lt_size);
  184. printf_P(_n(" _lt_count = 0x%04x (%d)\n"), _lt_count, _lt_count);
  185. printf_P(_n(" _lt_chsum = 0x%04x\n"), _lt_chsum);
  186. printf_P(_n(" _lt_resv0 = 0x%04x\n"), _lt_resv0);
  187. printf_P(_n(" _lt_resv1 = 0x%08lx\n"), _lt_resv1);
  188. if (_lt_magic != LANG_MAGIC) return 0;
  189. puts_P(_n(" strings:\n"));
  190. uint16_t ui = ((((uint16_t)&_SEC_LANG) + 0x00ff) & 0xff00); //table pointer
  191. for (ui = 0; ui < _lt_count; ui++)
  192. fprintf_P(out, _n(" %3d %S\n"), ui, lang_get_sec_lang_str_by_id(ui));
  193. return _lt_count;
  194. }
  195. #endif //DEBUG_SEC_LANG
  196. #endif //(LANG_MODE == 0)
  197. //const char MSG_LANGUAGE_NAME[] PROGMEM_I1 = ISTR("English"); ////c=0 r=0