language.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. }
  68. uint8_t lang_get_count()
  69. {
  70. #ifdef W25X20CL
  71. uint8_t count = 2; //count = 1+n (primary + secondary + all in xflash)
  72. uint32_t addr = 0x00000; //start of xflash
  73. lang_table_header_t header; //table header structure
  74. while (1)
  75. {
  76. w25x20cl_rd_data(addr, (uint8_t*)&header, sizeof(lang_table_header_t)); //read table header from xflash
  77. if (header.magic != LANG_MAGIC) break; //break if magic not valid
  78. addr += header.size; //calc address of next table
  79. count++; //inc counter
  80. }
  81. return count;
  82. #else //W25X20CL
  83. #endif //W25X20CL
  84. }
  85. uint16_t lang_get_code(uint8_t lang)
  86. {
  87. #ifdef W25X20CL
  88. if (lang == LANG_ID_PRI) return LANG_CODE_EN; //primary lang = EN
  89. if (lang == LANG_ID_SEC)
  90. {
  91. uint16_t ui = ((((uint16_t)&_SEC_LANG) + 0x00ff) & 0xff00); //table pointer
  92. if (pgm_read_dword(((uint32_t*)(ui + 0))) != LANG_MAGIC) return LANG_CODE_XX; //magic not valid
  93. return pgm_read_word(((uint32_t*)(ui + 10))); //return lang code from progmem
  94. }
  95. uint32_t addr = 0x00000; //start of xflash
  96. lang_table_header_t header; //table header structure
  97. lang--;
  98. while (1)
  99. {
  100. w25x20cl_rd_data(addr, (uint8_t*)&header, sizeof(lang_table_header_t)); //read table header from xflash
  101. if (header.magic != LANG_MAGIC) break; //break if not valid
  102. if (--lang == 0) return header.code;
  103. addr += header.size; //calc address of next table
  104. }
  105. #else //W25X20CL
  106. #endif //W25X20CL
  107. // if (lang == LANG_ID_SEC)
  108. // {
  109. // uint16_t ui = ((((uint16_t)&_SEC_LANG) + 0x00ff) & 0xff00); //table pointer
  110. // if (pgm_read_dword(((uint32_t*)(ui + 0))) == LANG_MAGIC) //magic num is OK
  111. // return pgm_read_word(((uint16_t*)(ui + 10))); //read language code
  112. // }
  113. return LANG_CODE_XX;
  114. }
  115. const char* lang_get_name_by_code(uint16_t code)
  116. {
  117. switch (code)
  118. {
  119. case LANG_CODE_EN: return _n("English");
  120. case LANG_CODE_CZ: return _n("Cestina");
  121. case LANG_CODE_DE: return _n("Deutsch");
  122. case LANG_CODE_ES: return _n("Espanol");
  123. case LANG_CODE_IT: return _n("Italiano");
  124. case LANG_CODE_PL: return _n("Polski");
  125. }
  126. return _n("??");
  127. // if (lang == LANG_ID_UNDEFINED) lang = lang_selected;
  128. // if (lang == LANG_ID_PRI) return _T(MSG_LANGUAGE_NAME + 2);
  129. // if (lang == LANG_ID_SEC)
  130. // {
  131. // uint16_t ui = ((((uint16_t)&_SEC_LANG) + 0x00ff) & 0xff00); //table pointer
  132. // if (pgm_read_dword(((uint32_t*)(ui + 0))) == LANG_MAGIC) //magic num is OK
  133. // return lang_get_sec_lang_str(MSG_LANGUAGE_NAME);
  134. // }
  135. // return 0;
  136. }
  137. #ifdef DEBUG_SEC_LANG
  138. const char* lang_get_sec_lang_str_by_id(uint16_t id)
  139. {
  140. uint16_t ui = ((((uint16_t)&_SEC_LANG) + 0x00ff) & 0xff00); //table pointer
  141. return ui + pgm_read_word(((uint16_t*)(ui + 16 + id * 2))); //read relative offset and return calculated pointer
  142. }
  143. uint16_t lang_print_sec_lang(FILE* out)
  144. {
  145. printf_P(_n("&_SEC_LANG = 0x%04x\n"), &_SEC_LANG);
  146. printf_P(_n("sizeof(_SEC_LANG) = 0x%04x\n"), sizeof(_SEC_LANG));
  147. uint16_t ptr_lang_table0 = ((uint16_t)(&_SEC_LANG) + 0xff) & 0xff00;
  148. printf_P(_n("&_lang_table0 = 0x%04x\n"), ptr_lang_table0);
  149. uint32_t _lt_magic = pgm_read_dword(((uint32_t*)(ptr_lang_table0 + 0)));
  150. uint16_t _lt_size = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 4)));
  151. uint16_t _lt_count = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 6)));
  152. uint16_t _lt_chsum = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 8)));
  153. uint16_t _lt_resv0 = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 10)));
  154. uint32_t _lt_resv1 = pgm_read_dword(((uint32_t*)(ptr_lang_table0 + 12)));
  155. printf_P(_n(" _lt_magic = 0x%08lx %S\n"), _lt_magic, (_lt_magic==LANG_MAGIC)?_n("OK"):_n("NA"));
  156. printf_P(_n(" _lt_size = 0x%04x (%d)\n"), _lt_size, _lt_size);
  157. printf_P(_n(" _lt_count = 0x%04x (%d)\n"), _lt_count, _lt_count);
  158. printf_P(_n(" _lt_chsum = 0x%04x\n"), _lt_chsum);
  159. printf_P(_n(" _lt_resv0 = 0x%04x\n"), _lt_resv0);
  160. printf_P(_n(" _lt_resv1 = 0x%08lx\n"), _lt_resv1);
  161. if (_lt_magic != LANG_MAGIC) return 0;
  162. puts_P(_n(" strings:\n"));
  163. uint16_t ui = ((((uint16_t)&_SEC_LANG) + 0x00ff) & 0xff00); //table pointer
  164. for (ui = 0; ui < _lt_count; ui++)
  165. fprintf_P(out, _n(" %3d %S\n"), ui, lang_get_sec_lang_str_by_id(ui));
  166. return _lt_count;
  167. }
  168. #endif //DEBUG_SEC_LANG
  169. #endif //(LANG_MODE == 0)
  170. //const char MSG_LANGUAGE_NAME[] PROGMEM_I1 = ISTR("English"); ////c=0 r=0