language.c 6.8 KB

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