Advertisement
Guest User

Untitled

a guest
Sep 26th, 2012
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.19 KB | None | 0 0
  1. /* A component to stream HAL data to an LCD in a user-defined format */
  2. #include "rtapi.h"
  3. #include "rtapi_app.h"
  4. #include "rtapi_string.h"
  5. #include "hal.h"
  6.  
  7. /* module information */
  8. MODULE_AUTHOR("Andy Pugh");
  9. MODULE_DESCRIPTION("Hal-to-text component for Mesa 7i73 and similar");
  10. MODULE_LICENSE("GPL");
  11.  
  12. #define MAX_CHAN 16
  13. #define MAX_CHARS 81
  14.  
  15. typedef struct {
  16. void *pntr;
  17. char type;
  18. char fmt[MAX_CHARS];
  19. } lcd_arg_t;
  20.  
  21. typedef struct {
  22. lcd_arg_t *args;
  23. int num_args;
  24. }lcd_page_t;
  25.  
  26. typedef struct {
  27. lcd_page_t *pages;
  28. int num_pages;
  29. hal_u32_t *page_num;
  30. hal_u32_t last_page;
  31. hal_u32_t *out;
  32. char buff[MAX_CHARS];
  33. int c_ptr;
  34. }lcd_inst_t;
  35.  
  36. typedef struct {
  37. lcd_inst_t *insts;
  38. int num_insts;
  39. }lcd_t;
  40.  
  41. static int comp_id;
  42. static lcd_t *lcd;
  43. static void write(void *arg, long period);
  44.  
  45. MODULE_AUTHOR("Andy Pugh");
  46. MODULE_DESCRIPTION("A component to stream HAL data to an LCD (or similar)");
  47. MODULE_LICENSE("GPL");
  48.  
  49. char *fmt_strings[MAX_CHAN];
  50. RTAPI_MP_ARRAY_STRING(fmt_strings, MAX_CHAN, "screen formatting scancodes")
  51.  
  52. int rtapi_app_main(void){
  53. int a, c, i, f, f1, k, n, n1, p;
  54. int retval;
  55.  
  56. if (!fmt_strings[0]){
  57. rtapi_print_msg(RTAPI_MSG_ERR, "The LCD component requires at least one valid format string");
  58. return -EINVAL;
  59. }
  60.  
  61. comp_id = hal_init("lcd");
  62. if (comp_id < 0) {
  63. rtapi_print_msg(RTAPI_MSG_ERR, "LCD: ERROR: hal_init() failed\n");
  64. return -1;
  65. }
  66.  
  67. // allocate shared memory for data
  68. lcd = hal_malloc(sizeof(lcd_t));
  69. if (lcd == 0) {
  70. rtapi_print_msg(RTAPI_MSG_ERR,
  71. "lcd component: Out of Memory\n");
  72. hal_exit(comp_id);
  73. return -1;
  74. }
  75.  
  76. // Count the instances. Very unlikely to be more than one, but...
  77. for (lcd->num_insts = 0; fmt_strings[lcd->num_insts];lcd->num_insts++){}
  78. lcd->insts = hal_malloc(lcd->num_insts * sizeof(lcd_inst_t));
  79. rtapi_print("Can I print floats like %03.3f\n", 1.2334567);
  80.  
  81. for (i = 0; i < lcd->num_insts; i++){
  82. lcd_inst_t *inst = &lcd->insts[i];
  83. inst->num_pages = 1;
  84.  
  85. // count the pages, demarked by | chars.
  86. for (f = 0; fmt_strings[i][f]; f++){
  87. if (fmt_strings[i][f] =='|') inst->num_pages++;
  88. }
  89. inst->pages = hal_malloc(inst->num_pages * sizeof(lcd_page_t));
  90.  
  91. //second pass, splitting out individual format specifiers
  92. f1 = k = p = 0;
  93. for (f = 0; fmt_strings[i][f]; f++){
  94.  
  95. if (fmt_strings[i][f + 1] =='%') inst->pages[p].num_args++;
  96.  
  97. if (fmt_strings[i][f + 1] =='|' || fmt_strings[i][f + 1] == 0) {
  98.  
  99. if (inst->pages[p].num_args == 0) inst->pages[p].num_args = 1;
  100.  
  101. inst->pages[p].args = hal_malloc(inst->pages[p].num_args
  102. * sizeof(lcd_arg_t));
  103. a = c = 0;
  104. n1 = f1;
  105. for (n = f1; n < f; n++){
  106. if (fmt_strings[i][n + 1] == '%') c++;
  107. if (c == 2){
  108. c = 1;
  109. retval = snprintf(inst->pages[p].args[a].fmt,
  110. n - n1 + 2,
  111. "%s",
  112. fmt_strings[i] + n1);
  113. n1 = n + 1;
  114. a++;
  115. }
  116. }
  117.  
  118. // don't forget the trailing non-format stuff
  119. retval = snprintf(inst->pages[p].args[a].fmt,
  120. n - n1 + 2,
  121. "%s",
  122. fmt_strings[i] + n1);
  123. f1 = f + 2;
  124.  
  125. // Now work out what numerical type each format is
  126. for (a = 0 ; a < inst->pages[p].num_args; a++){
  127. int flg = 0;
  128. n = -1;
  129. inst->pages[p].args[a].type = 't'; // tag as text initially
  130. while (flg != 2 && inst->pages[p].args[a].fmt[++n]){
  131.  
  132. if (inst->pages[p].args[a].fmt[n] == '%') flg = 1;
  133.  
  134. if (flg){
  135. switch (inst->pages[p].args[a].fmt[n]){
  136. case 'a': // all the float formats
  137. case 'A':
  138. case 'e':
  139. case 'E':
  140. case 'f':
  141. case 'g':
  142. case 'G':
  143. retval = hal_pin_float_newf(HAL_IN,
  144. (hal_float_t**)&(inst->pages[p].args[a].pntr), comp_id,
  145. "lcd.%02i.page.%02i.arg.%02i",
  146. i, p, a);
  147. if (retval != 0) {
  148. return retval;
  149. }
  150. inst->pages[p].args[a].type = 'f';
  151. flg = 2;
  152. break;
  153.  
  154. case 'c': // all the unsigned formats
  155. case 'o':
  156. case 'u':
  157. case 'x':
  158. case 'X':
  159. retval = hal_pin_u32_newf(HAL_IN,
  160. (hal_u32_t **)&(inst->pages[p].args[a].pntr), comp_id,
  161. "lcd.%02i.page.%02i.arg.%02i",
  162. i, p, a);
  163. if (retval != 0) {
  164. return retval;
  165. }
  166. inst->pages[p].args[a].type = 'u';
  167. flg = 2;
  168. break;
  169.  
  170. case 'd': // all the signed formats
  171. case 'i':
  172. retval = hal_pin_s32_newf(HAL_IN,
  173. (hal_s32_t **)&(inst->pages[p].args[a].pntr), comp_id,
  174. "lcd.%02i.page.%02i.arg.%02i",
  175. i, p, a);
  176. if (retval != 0) {
  177. return retval;
  178. }
  179. inst->pages[p].args[a].type = 's';
  180. flg = 2;
  181. break;
  182. }
  183.  
  184. }
  185.  
  186. }
  187. }
  188. p++; // increment the page index
  189. }
  190. }
  191. }
  192.  
  193. retval = hal_export_funct("lcd", write, lcd, 1, 0, comp_id); //needs fp?
  194. if (retval < 0) {
  195. rtapi_print_msg(RTAPI_MSG_ERR, "LCD: ERROR: function export failed\n");
  196. return -1;
  197. }
  198.  
  199. for (i = 0; i < lcd->num_insts; i++){
  200. retval = hal_pin_u32_newf(HAL_IN, &(lcd->insts[i].page_num), comp_id,
  201. "lcd.%02i.page_num", i);
  202. if (retval != 0) {
  203. return retval;
  204. }
  205. lcd->insts[i].last_page = -1; // force screen refresh
  206. retval = hal_pin_u32_newf(HAL_OUT, &(lcd->insts[i].out), comp_id,
  207. "lcd.%02i.out",i);
  208. if (retval != 0) {
  209. return retval;
  210. }
  211. }
  212.  
  213. return 0;
  214. }
  215.  
  216. void write(void *arg, long period){
  217. lcd_t *lcd;
  218. int i;
  219. int retval;
  220.  
  221. lcd = arg;
  222.  
  223. for (i = 0; i < lcd->num_insts; i++){
  224. lcd_inst_t *inst = &lcd->insts[i];
  225. if (*inst->page_num != inst->last_page){
  226. *inst->out = 0x1A; // clear screen
  227. inst->last_page = *inst->page_num;
  228. memset(inst->buff, 0, MAX_CHARS);
  229. return;
  230. }
  231.  
  232. if (inst->buff[inst->c_ptr]){
  233. *inst->out = inst->buff[inst->c_ptr++];
  234. }
  235. else
  236. {
  237. int a;
  238. char temp[80];
  239. lcd_page_t page = inst->pages[*inst->page_num];
  240. *inst->out = 0x1e;
  241. inst->c_ptr = 0;
  242. memset(inst->buff, 0, MAX_CHARS);
  243. for (a = 0; a < page.num_args; a++){
  244. switch (page.args[a].type){
  245. case 'f':
  246. retval = sprintf(temp,
  247. page.args[a].fmt,
  248. *((hal_float_t*)page.args[a].pntr));
  249. break;
  250. case 'u':
  251. retval = sprintf(temp,
  252. page.args[a].fmt,
  253. *((hal_u32_t*)page.args[a].pntr));
  254. break;
  255. case 's':
  256. retval = sprintf(temp,
  257. page.args[a].fmt,
  258. *((hal_s32_t*)page.args[a].pntr));
  259.  
  260. break;
  261. case 't':
  262. retval = sprintf(temp,
  263. "%s",
  264. page.args[a].fmt);
  265. break;
  266. }
  267. retval = sprintf(inst->buff, "%s%s", inst->buff, temp);
  268. }
  269. if (dbg++ < 10) rtapi_print("%s/n",inst->buff);
  270. }
  271. }
  272. }
  273.  
  274.  
  275. void rtapi_app_exit(void)
  276. {
  277. hal_exit(comp_id);
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement