You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.6KB

  1. // Copyright 2009 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. //
  16. // -----------------------------------------------------------------------------
  17. //
  18. // Resources manager. Support for lookup of values/strings in tables. Since
  19. // one might not want this functionality and just use the plain program memory
  20. // read/write function, an alias for a stripped down version without string
  21. // table lookup is provided (SimpleResourcesManager).
  22. #ifndef AVRLIB_RESOURCES_MANAGER_H_
  23. #define AVRLIB_RESOURCES_MANAGER_H_
  24. #include "avrlib/base.h"
  25. #include <string.h>
  26. #include <avr/pgmspace.h>
  27. namespace avrlib {
  28. template<
  29. const prog_char* const* strings,
  30. const prog_uint16_t* const* lookup_tables>
  31. struct ResourcesTables {
  32. static inline const prog_char* const* string_table() { return strings; }
  33. static inline const prog_uint16_t* const* lookup_table_table() {
  34. return lookup_tables;
  35. }
  36. };
  37. struct NoResourcesTables {
  38. static inline const prog_char* const* string_table() { return NULL; }
  39. static inline const prog_uint16_t* const* lookup_table_table() { return NULL; }
  40. };
  41. template<typename ResourceId = uint8_t, typename Tables = NoResourcesTables>
  42. class ResourcesManager {
  43. public:
  44. static inline void LoadStringResource(ResourceId resource, char* buffer,
  45. uint8_t buffer_size) {
  46. if (!Tables::string_table()) {
  47. return;
  48. }
  49. char* address = (char*)(pgm_read_word(&(Tables::string_table()[resource])));
  50. strncpy_P(buffer, address, buffer_size);
  51. }
  52. template<typename ResultType, typename IndexType>
  53. static inline ResultType Lookup(ResourceId resource, IndexType i) {
  54. if (!Tables::lookup_table_table()) {
  55. return 0;
  56. };
  57. uint16_t* address = (uint16_t*)(
  58. pgm_read_word(&(Tables::lookup_table_table()[resource])));
  59. return ResultType(pgm_read_word(address + i));
  60. }
  61. template<typename ResultType, typename IndexType>
  62. static inline ResultType Lookup(const prog_char* p, IndexType i) {
  63. return ResultType(pgm_read_byte(p + i));
  64. }
  65. template<typename ResultType, typename IndexType>
  66. static inline ResultType Lookup(const prog_uint8_t* p, IndexType i) {
  67. return ResultType(pgm_read_byte(p + i));
  68. }
  69. template<typename ResultType, typename IndexType>
  70. static inline ResultType Lookup(const prog_uint16_t* p, IndexType i) {
  71. return ResultType(pgm_read_word(p + i));
  72. }
  73. template<typename T>
  74. static void Load(const prog_char* p, uint8_t i, T* destination) {
  75. memcpy_P(destination, p + i * sizeof(T), sizeof(T));
  76. }
  77. template<typename T, typename U>
  78. static void Load(const T* p, uint8_t i, U* destination) {
  79. STATIC_ASSERT(sizeof(T) == sizeof(U));
  80. memcpy_P(destination, p + i, sizeof(T));
  81. }
  82. template<typename T>
  83. static void Load(const T* p, uint8_t* destination, uint16_t size) {
  84. memcpy_P(destination, p, size);
  85. }
  86. };
  87. typedef ResourcesManager<> SimpleResourcesManager;
  88. } // namespace avrlib
  89. #endif // AVRLIB_RESOURCES_MANAGER_H_