language.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //language.c
  2. #include "language.h"
  3. #include <inttypes.h>
  4. #include <avr/pgmspace.h>
  5. // Currectly active language selection.
  6. unsigned char lang_selected = 0;
  7. #if (LANG_MODE == 0) //primary language only
  8. #else //(LANG_MODE == 0)
  9. //reserved xx kbytes for secondary language table
  10. const char _SEC_LANG[LANG_SIZE_RESERVED] PROGMEM_I2 = "_SEC_LANG";
  11. #endif //(LANG_MODE == 0)
  12. //lang_table_t structure - 16byte header
  13. typedef struct
  14. {
  15. struct
  16. {
  17. uint32_t magic;
  18. uint16_t size;
  19. uint16_t count;
  20. uint16_t checksum;
  21. uint16_t reserved0;
  22. uint32_t reserved1;
  23. } header;
  24. uint16_t table[];
  25. } lang_table_t;
  26. //lang_table pointer
  27. lang_table_t* lang_table = 0;
  28. const char* lang_get_translation(const char* s)
  29. {
  30. if (lang_selected == 0) return s + 2; //primary language selected
  31. if (lang_table == 0) return s + 2; //sec. lang table not found
  32. uint16_t ui = pgm_read_word(((uint16_t*)s)); //read string id
  33. if (ui == 0xffff) return s + 2; //translation not found
  34. ui = pgm_read_word(((uint16_t*)(((char*)lang_table + 16 + ui*2)))); //read relative offset
  35. if (pgm_read_byte(((uint8_t*)((char*)lang_table + ui))) == 0)
  36. return s + 2;//not translated string
  37. return (const char*)((char*)lang_table + ui); //return calculated pointer
  38. }
  39. const char* lang_get_sec_lang_str(const char* s)
  40. {
  41. uint16_t ui = (uint16_t)&_SEC_LANG; //pointer to _SEC_LANG reserved space
  42. ui += 0x00ff; //add 1 page
  43. ui &= 0xff00; //align to page
  44. lang_table_t* _lang_table = ui; //table pointer
  45. ui = pgm_read_word(((uint16_t*)s)); //read string id
  46. if (ui == 0xffff) return s + 2; //translation not found
  47. ui = pgm_read_word(((uint16_t*)(((char*)_lang_table + 16 + ui*2)))); //read relative offset
  48. return (const char*)((char*)_lang_table + ui); //return calculated pointer
  49. }
  50. const char* lang_select(unsigned char lang)
  51. {
  52. #if (LANG_MODE == 0) //primary language only
  53. return 0;
  54. #else //(LANG_MODE == 0)
  55. if (lang == 0) //primary language
  56. {
  57. lang_table = 0;
  58. lang_selected = 0;
  59. return;
  60. }
  61. uint16_t ui = (uint16_t)&_SEC_LANG; //pointer to _SEC_LANG reserved space
  62. ui += 0x00ff; //add 1 page
  63. ui &= 0xff00; //align to page
  64. lang_table = ui; //set table pointer
  65. ui = pgm_read_word(((uint16_t*)(((char*)lang_table + 16)))); //read relative offset of first string (language name)
  66. return (const char*)((char*)lang_table + ui); //return calculated pointer
  67. #endif //(LANG_MODE == 0)
  68. }
  69. unsigned char lang_get_count()
  70. {
  71. uint16_t ui = (uint16_t)&_SEC_LANG; //pointer to _SEC_LANG reserved space
  72. ui += 0x00ff; //add 1 page
  73. ui &= 0xff00; //align to page
  74. lang_table_t* _lang_table = ui; //table pointer
  75. if (pgm_read_dword(((uint32_t*)(_lang_table + 0))) == 0x4bb45aa5) return 2;
  76. return 1;
  77. }
  78. const char* lang_get_name(unsigned char lang)
  79. {
  80. if (lang == 0) return MSG_LANGUAGE_NAME + 2;
  81. return lang_get_sec_lang_str(MSG_LANGUAGE_NAME);
  82. }
  83. const char MSG_LANGUAGE_NAME[] PROGMEM_I1 = ISTR("English"); ////c=0 r=0