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.

209 lines
5.2KB

  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 "tdc_parser.h"
  24. #include "tdc_proto.h"
  25. #include "tdc_store.h"
  26. #include "tdc_variable.h"
  27. #include "gui_graph_tab.h"
  28. #include "gui_variable_tab.h"
  29. struct tdc_board {
  30. char name[32];
  31. struct tdc_var *varmap[256];
  32. int refresh_graph_interval;
  33. int refresh_interval;
  34. gint refresh_tag;
  35. };
  36. static struct tdc_board *board_arr[8];
  37. struct tdc_board * tdcstore_get_board(int address)
  38. {
  39. return board_arr[address & 0x7];
  40. }
  41. struct tdc_var * tdcstore_get_var(int address, int id)
  42. {
  43. struct tdc_board *board = tdcstore_get_board(address);
  44. return (board != NULL) ? board->varmap[id & 0xFF] : NULL;
  45. }
  46. static int tdcstore_destroy_var(struct tdc_board *board, int id)
  47. {
  48. struct tdc_var *var = board->varmap[id & 0xFF];
  49. if (var == NULL)
  50. return -1;
  51. if (var->privdata_vartab)
  52. gui_vartab_remove_var(var);
  53. if (var->privdata_graphtab)
  54. gui_graphtab_remove_var(var);
  55. tdcvar_destroy(var);
  56. board->varmap[id & 0xFF] = NULL;
  57. return 0;
  58. }
  59. static int tdcstore_destroy_board(int address)
  60. {
  61. struct tdc_board *board = tdcstore_get_board(address);
  62. if (board == NULL)
  63. return -1;
  64. int i;
  65. for (i = 0; i < 256; i++) {
  66. if (board->varmap[i] == NULL)
  67. continue;
  68. tdcstore_destroy_var(board, i);
  69. }
  70. g_free(board);
  71. board_arr[address & 0x7] = NULL;
  72. return 0;
  73. }
  74. void tdcstore_create_board(int address, char *name)
  75. {
  76. struct tdc_board *old = tdcstore_get_board(address);
  77. if (old != NULL)
  78. tdcstore_destroy_board(address);
  79. struct tdc_board *board = g_malloc0(sizeof(struct tdc_board));
  80. strncpy(board->name, name, 32);
  81. printf("ADDR%d:%s\n", address, board->name);
  82. board_arr[address & 0x7] = board;
  83. }
  84. int tdcstore_create_var(int address, struct tdc_var *var)
  85. {
  86. struct tdc_board *board = tdcstore_get_board(address);
  87. if (board == NULL)
  88. return -1;
  89. struct tdc_var *old = tdcstore_get_var(address, var->id);
  90. if (old != NULL)
  91. tdcstore_destroy_var(board, var->id);
  92. board->varmap[var->id] = var;
  93. gui_vartab_add_var(var);
  94. return 0;
  95. }
  96. int tdcstore_update_var(int address, int id, uint8_t *data, int len)
  97. {
  98. struct tdc_var *var = tdcstore_get_var(address, id);
  99. if (var == NULL)
  100. return -1;
  101. tdcvar_update(var, data, len);
  102. if (var->privdata_vartab)
  103. gui_vartab_update_var(var);
  104. if (var->privdata_graphtab)
  105. gui_graphtab_update_var(var);
  106. return 0;
  107. }
  108. static gint refresh_helper(gpointer data)
  109. {
  110. int i;
  111. int address = (int)data;
  112. for (i = 0; i < 256; i++) {
  113. struct tdc_var *var = tdcstore_get_var(address, i);
  114. /* unknown var, or value updated via refresh_graph */
  115. if ((var == NULL) || (var->flags & TDC_GUI_GRAPH))
  116. continue;
  117. /* error while sending -> bail out, disable timeout */
  118. if (tdcparser_send_getvalue(address, i) < 0)
  119. return FALSE;
  120. }
  121. return TRUE;
  122. }
  123. int tdcstore_refresh_values(int address, int interval)
  124. {
  125. struct tdc_board *board = tdcstore_get_board(address);
  126. if (board == NULL)
  127. return -1;
  128. int old_interval = board->refresh_interval;
  129. if (old_interval != 0)
  130. g_source_remove(board->refresh_tag);
  131. board->refresh_tag = g_timeout_add(interval, refresh_helper, (gpointer)address);
  132. return 0;
  133. }
  134. static void tdcstore_get_bitmap(int address, uint32_t *bitmap)
  135. {
  136. int i, cnt = 0;
  137. uint32_t tmp = 0;
  138. for (i = 0; i < 256; i++) {
  139. struct tdc_var *var = tdcstore_get_var(address, i);
  140. if ((var != NULL) && (var->flags & TDC_GUI_GRAPH))
  141. tmp |= (1 << cnt);
  142. cnt = (cnt +1) % 32;
  143. if (cnt == 0) {
  144. bitmap[i >> 5] = tmp;
  145. tmp = 0;
  146. }
  147. }
  148. }
  149. int tdcstore_graph_refresh(int address, int interval)
  150. {
  151. struct tdc_board *board = tdcstore_get_board(address);
  152. if (board == NULL)
  153. return -1;
  154. if (interval == -1)
  155. interval = board->refresh_graph_interval;
  156. uint32_t bitmap[8];
  157. tdcstore_get_bitmap(address, bitmap);
  158. tdcparser_send_request(address, interval, bitmap);
  159. board->refresh_graph_interval = interval;
  160. return 0;
  161. }
  162. void tdcstore_flush(void)
  163. {
  164. int i;
  165. for (i = 0; i < 8; i++)
  166. tdcstore_destroy_board(i);
  167. }