gtk2 sam7fc telemetrie application
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.

295 lines
6.6KB

  1. /***************************************************************************
  2. * Copyright (C) 04/2008 by Olaf Rempel *
  3. * razzor@kopf-tisch.de *
  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; version 2 of the License *
  8. * *
  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. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. #include <stdint.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include <glib.h>
  25. #include "tdc_proto.h"
  26. #include "tdc_variable.h"
  27. void tdcvar_get_value(struct tdc_var *var, char *buf, int size, int viewmode)
  28. {
  29. switch ((var->flags & TDC_TYPEMASK) | ((viewmode & 0x01) << 8)) {
  30. case TDC_UNSIGNED:
  31. switch (var->flags & TDC_SIZEMASK) {
  32. case 1: snprintf(buf, size, "%u", var->data_uint8);
  33. break;
  34. case 2: snprintf(buf, size, "%u", var->data_uint16);
  35. break;
  36. case 4: snprintf(buf, size, "%u", var->data_uint32);
  37. break;
  38. case 8: snprintf(buf, size, "%llu", var->data_uint64);
  39. break;
  40. default:
  41. snprintf(buf, size, "???");
  42. break;
  43. }
  44. break;
  45. case TDC_UNSIGNED | 0x100:
  46. switch (var->flags & TDC_SIZEMASK) {
  47. case 1: snprintf(buf, size, "0x%02x", var->data_uint8);
  48. break;
  49. case 2: snprintf(buf, size, "0x%04x", var->data_uint16);
  50. break;
  51. case 4: snprintf(buf, size, "0x%08x", var->data_uint32);
  52. break;
  53. case 8: snprintf(buf, size, "0x%16llx", var->data_uint64);
  54. break;
  55. default:
  56. snprintf(buf, size, "???");
  57. break;
  58. }
  59. break;
  60. case TDC_SIGNED:
  61. switch (var->flags & TDC_SIZEMASK) {
  62. case 1: snprintf(buf, size, "%d", (int8_t)var->data_uint8);
  63. break;
  64. case 2: snprintf(buf, size, "%d", (int16_t)var->data_uint16);
  65. break;
  66. case 4: snprintf(buf, size, "%d", (int32_t)var->data_uint32);
  67. break;
  68. case 8: snprintf(buf, size, "%lld", (int64_t)var->data_uint64);
  69. break;
  70. default:
  71. snprintf(buf, size, "???");
  72. break;
  73. }
  74. break;
  75. case TDC_SIGNED | 0x100:
  76. switch (var->flags & TDC_SIZEMASK) {
  77. case 1: snprintf(buf, size, "0x%02x", (int8_t)var->data_uint8);
  78. break;
  79. case 2: snprintf(buf, size, "0x%04x", (int16_t)var->data_uint16);
  80. break;
  81. case 4: snprintf(buf, size, "0x%08x", (int32_t)var->data_uint32);
  82. break;
  83. case 8: snprintf(buf, size, "0x%16llx", (int64_t)var->data_uint64);
  84. break;
  85. default:
  86. snprintf(buf, size, "???");
  87. break;
  88. }
  89. break;
  90. case TDC_FP:
  91. case TDC_FP | 0x100:
  92. switch (var->flags & TDC_SIZEMASK) {
  93. case sizeof(float):
  94. snprintf(buf, size, "%.8f", var->data_float);
  95. break;
  96. case sizeof(double):
  97. snprintf(buf, size, "%.12f", var->data_double);
  98. break;
  99. default:
  100. snprintf(buf, size, "???");
  101. break;
  102. }
  103. break;
  104. case TDC_FIXED:
  105. case TDC_FIXED | 0x100:
  106. snprintf(buf, size, "???");
  107. break;
  108. }
  109. }
  110. void tdcvar_get_type(struct tdc_var *var, char *buf, int size)
  111. {
  112. int pos;
  113. int width = (var->flags & TDC_SIZEMASK);
  114. switch (var->flags & TDC_TYPEMASK) {
  115. case TDC_UNSIGNED:
  116. pos = snprintf(buf, size, "uint%d_t", width * 8);
  117. break;
  118. case TDC_SIGNED:
  119. pos = snprintf(buf, size, "int%d_t", width * 8);
  120. break;
  121. case TDC_FP:
  122. if (width == sizeof(float))
  123. pos = snprintf(buf, size, "float");
  124. else if (size == sizeof(double))
  125. pos = snprintf(buf, size, "double");
  126. else
  127. pos = snprintf(buf, size, "fp%d_t", width * 8);
  128. break;
  129. case TDC_FIXED:
  130. pos = snprintf(buf, size, "fix%d_t", width * 8);
  131. break;
  132. default:
  133. pos = snprintf(buf, size, "???");
  134. break;
  135. }
  136. if (var->flags & TDC_READONLY)
  137. pos = snprintf(buf + pos, size - pos, " (ro)");
  138. }
  139. int tdcvar_parse_value(struct tdc_var *var, char *data)
  140. {
  141. struct tdc_var tmp;
  142. int width = (var->flags & TDC_SIZEMASK);
  143. switch (var->flags & TDC_TYPEMASK) {
  144. case TDC_UNSIGNED:
  145. if (strchr(data, '-') != NULL)
  146. return -1;
  147. errno = 0;
  148. if (width < 8)
  149. tmp.data_uint32 = strtoul(data, NULL, 0);
  150. else
  151. tmp.data_uint64 = strtoull(data, NULL, 0);
  152. if (errno != 0) {
  153. errno = 0;
  154. return -1;
  155. }
  156. break;
  157. case TDC_SIGNED:
  158. errno = 0;
  159. if (width < 8)
  160. tmp.data_uint32 = strtol(data, NULL, 0);
  161. else
  162. tmp.data_uint64 = strtoll(data, NULL, 0);
  163. if (errno != 0) {
  164. errno = 0;
  165. return -1;
  166. }
  167. break;
  168. case TDC_FP:
  169. errno = 0;
  170. if (width == sizeof(float))
  171. tmp.data_float = strtod(data, NULL);
  172. else
  173. tmp.data_double = strtod(data, NULL);
  174. if (errno != 0) {
  175. errno = 0;
  176. return -1;
  177. }
  178. break;
  179. case TDC_FIXED:
  180. return -1;
  181. break;
  182. default:
  183. break;
  184. }
  185. memcpy(&var->data, &tmp.data, width);
  186. return 0;
  187. }
  188. double tdcvar_get_double(struct tdc_var *var)
  189. {
  190. switch (var->flags & TDC_TYPEMASK) {
  191. case TDC_UNSIGNED:
  192. switch (var->flags & TDC_SIZEMASK) {
  193. case 1: return var->data_uint8;
  194. case 2: return var->data_uint16;
  195. case 4: return var->data_uint32;
  196. case 8: return var->data_uint64;
  197. }
  198. break;
  199. case TDC_SIGNED:
  200. switch (var->flags & TDC_SIZEMASK) {
  201. case 1: return (int8_t)var->data_uint8;
  202. case 2: return (int16_t)var->data_uint16;
  203. case 4: return (int32_t)var->data_uint32;
  204. case 8: return (int64_t)var->data_uint64;
  205. }
  206. break;
  207. case TDC_FP:
  208. case TDC_FP | 0x100:
  209. switch (var->flags & TDC_SIZEMASK) {
  210. case sizeof(float):
  211. return var->data_float;
  212. case sizeof(double):
  213. return var->data_double;
  214. }
  215. break;
  216. }
  217. return 0.0;
  218. }
  219. struct tdc_var * tdcvar_create(int id, uint32_t flags, char *name, int len)
  220. {
  221. struct tdc_var *var = g_malloc0(sizeof(struct tdc_var) + len + 1);
  222. var->id = id & 0xFF;
  223. var->flags = flags;
  224. strncpy(var->name, name, len);
  225. var->name[len] = '\0';
  226. // printf("create_var(%d, 0x%x, '%s', %d)\n", var->id, var->flags, var->name, len);
  227. return var;
  228. }
  229. void tdcvar_update(struct tdc_var *var, uint8_t *data, int len)
  230. {
  231. if ((var->flags & TDC_SIZEMASK) != len)
  232. return;
  233. memcpy(&var->data, data, len);
  234. }
  235. void tdcvar_destroy(struct tdc_var *var)
  236. {
  237. g_free(var);
  238. }