language.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. static 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. return (const char*)((char*)lang_table + ui + 16); //return calculated pointer
  36. }
  37. const char* lang_select(unsigned char lang)
  38. {
  39. #if (LANG_MODE == 0) //primary language only
  40. return 0;
  41. #else //(LANG_MODE == 0)
  42. if (lang == 0) //primary language
  43. {
  44. lang_table = 0;
  45. lang_selected = 0;
  46. return;
  47. }
  48. uint16_t ui = (uint16_t)&_SEC_LANG; //pointer to _SEC_LANG reserved space
  49. ui += 0x0100; //add 1 page
  50. ui &= 0xff00; //align to page
  51. lang_table = ui; //set table pointer
  52. ui = pgm_read_word(((uint16_t*)(((char*)lang_table + 16)))); //read relative offset of first string (language name)
  53. return (const char*)((char*)lang_table + ui + 16); //return calculated pointer
  54. #endif //(LANG_MODE == 0)
  55. }