Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* A component to stream HAL data to an LCD in a user-defined format */
- #include "rtapi.h"
- #include "rtapi_app.h"
- #include "rtapi_string.h"
- #include "hal.h"
- /* module information */
- MODULE_AUTHOR("Andy Pugh");
- MODULE_DESCRIPTION("Hal-to-text component for Mesa 7i73 and similar");
- MODULE_LICENSE("GPL");
- #define MAX_CHAN 16
- #define MAX_CHARS 81
- typedef struct {
- void *pntr;
- char type;
- char fmt[MAX_CHARS];
- } lcd_arg_t;
- typedef struct {
- lcd_arg_t *args;
- int num_args;
- }lcd_page_t;
- typedef struct {
- lcd_page_t *pages;
- int num_pages;
- hal_u32_t *page_num;
- hal_u32_t last_page;
- hal_u32_t *out;
- char buff[MAX_CHARS];
- int c_ptr;
- }lcd_inst_t;
- typedef struct {
- lcd_inst_t *insts;
- int num_insts;
- }lcd_t;
- static int comp_id;
- static lcd_t *lcd;
- static void write(void *arg, long period);
- MODULE_AUTHOR("Andy Pugh");
- MODULE_DESCRIPTION("A component to stream HAL data to an LCD (or similar)");
- MODULE_LICENSE("GPL");
- char *fmt_strings[MAX_CHAN];
- RTAPI_MP_ARRAY_STRING(fmt_strings, MAX_CHAN, "screen formatting scancodes")
- int rtapi_app_main(void){
- int a, c, i, f, f1, k, n, n1, p;
- int retval;
- if (!fmt_strings[0]){
- rtapi_print_msg(RTAPI_MSG_ERR, "The LCD component requires at least one valid format string");
- return -EINVAL;
- }
- comp_id = hal_init("lcd");
- if (comp_id < 0) {
- rtapi_print_msg(RTAPI_MSG_ERR, "LCD: ERROR: hal_init() failed\n");
- return -1;
- }
- // allocate shared memory for data
- lcd = hal_malloc(sizeof(lcd_t));
- if (lcd == 0) {
- rtapi_print_msg(RTAPI_MSG_ERR,
- "lcd component: Out of Memory\n");
- hal_exit(comp_id);
- return -1;
- }
- // Count the instances. Very unlikely to be more than one, but...
- for (lcd->num_insts = 0; fmt_strings[lcd->num_insts];lcd->num_insts++){}
- lcd->insts = hal_malloc(lcd->num_insts * sizeof(lcd_inst_t));
- rtapi_print("Can I print floats like %03.3f\n", 1.2334567);
- for (i = 0; i < lcd->num_insts; i++){
- lcd_inst_t *inst = &lcd->insts[i];
- inst->num_pages = 1;
- // count the pages, demarked by | chars.
- for (f = 0; fmt_strings[i][f]; f++){
- if (fmt_strings[i][f] =='|') inst->num_pages++;
- }
- inst->pages = hal_malloc(inst->num_pages * sizeof(lcd_page_t));
- //second pass, splitting out individual format specifiers
- f1 = k = p = 0;
- for (f = 0; fmt_strings[i][f]; f++){
- if (fmt_strings[i][f + 1] =='%') inst->pages[p].num_args++;
- if (fmt_strings[i][f + 1] =='|' || fmt_strings[i][f + 1] == 0) {
- if (inst->pages[p].num_args == 0) inst->pages[p].num_args = 1;
- inst->pages[p].args = hal_malloc(inst->pages[p].num_args
- * sizeof(lcd_arg_t));
- a = c = 0;
- n1 = f1;
- for (n = f1; n < f; n++){
- if (fmt_strings[i][n + 1] == '%') c++;
- if (c == 2){
- c = 1;
- retval = snprintf(inst->pages[p].args[a].fmt,
- n - n1 + 2,
- "%s",
- fmt_strings[i] + n1);
- n1 = n + 1;
- a++;
- }
- }
- // don't forget the trailing non-format stuff
- retval = snprintf(inst->pages[p].args[a].fmt,
- n - n1 + 2,
- "%s",
- fmt_strings[i] + n1);
- f1 = f + 2;
- // Now work out what numerical type each format is
- for (a = 0 ; a < inst->pages[p].num_args; a++){
- int flg = 0;
- n = -1;
- inst->pages[p].args[a].type = 't'; // tag as text initially
- while (flg != 2 && inst->pages[p].args[a].fmt[++n]){
- if (inst->pages[p].args[a].fmt[n] == '%') flg = 1;
- if (flg){
- switch (inst->pages[p].args[a].fmt[n]){
- case 'a': // all the float formats
- case 'A':
- case 'e':
- case 'E':
- case 'f':
- case 'g':
- case 'G':
- retval = hal_pin_float_newf(HAL_IN,
- (hal_float_t**)&(inst->pages[p].args[a].pntr), comp_id,
- "lcd.%02i.page.%02i.arg.%02i",
- i, p, a);
- if (retval != 0) {
- return retval;
- }
- inst->pages[p].args[a].type = 'f';
- flg = 2;
- break;
- case 'c': // all the unsigned formats
- case 'o':
- case 'u':
- case 'x':
- case 'X':
- retval = hal_pin_u32_newf(HAL_IN,
- (hal_u32_t **)&(inst->pages[p].args[a].pntr), comp_id,
- "lcd.%02i.page.%02i.arg.%02i",
- i, p, a);
- if (retval != 0) {
- return retval;
- }
- inst->pages[p].args[a].type = 'u';
- flg = 2;
- break;
- case 'd': // all the signed formats
- case 'i':
- retval = hal_pin_s32_newf(HAL_IN,
- (hal_s32_t **)&(inst->pages[p].args[a].pntr), comp_id,
- "lcd.%02i.page.%02i.arg.%02i",
- i, p, a);
- if (retval != 0) {
- return retval;
- }
- inst->pages[p].args[a].type = 's';
- flg = 2;
- break;
- }
- }
- }
- }
- p++; // increment the page index
- }
- }
- }
- retval = hal_export_funct("lcd", write, lcd, 1, 0, comp_id); //needs fp?
- if (retval < 0) {
- rtapi_print_msg(RTAPI_MSG_ERR, "LCD: ERROR: function export failed\n");
- return -1;
- }
- for (i = 0; i < lcd->num_insts; i++){
- retval = hal_pin_u32_newf(HAL_IN, &(lcd->insts[i].page_num), comp_id,
- "lcd.%02i.page_num", i);
- if (retval != 0) {
- return retval;
- }
- lcd->insts[i].last_page = -1; // force screen refresh
- retval = hal_pin_u32_newf(HAL_OUT, &(lcd->insts[i].out), comp_id,
- "lcd.%02i.out",i);
- if (retval != 0) {
- return retval;
- }
- }
- return 0;
- }
- void write(void *arg, long period){
- lcd_t *lcd;
- int i;
- int retval;
- lcd = arg;
- for (i = 0; i < lcd->num_insts; i++){
- lcd_inst_t *inst = &lcd->insts[i];
- if (*inst->page_num != inst->last_page){
- *inst->out = 0x1A; // clear screen
- inst->last_page = *inst->page_num;
- memset(inst->buff, 0, MAX_CHARS);
- return;
- }
- if (inst->buff[inst->c_ptr]){
- *inst->out = inst->buff[inst->c_ptr++];
- }
- else
- {
- int a;
- char temp[80];
- lcd_page_t page = inst->pages[*inst->page_num];
- *inst->out = 0x1e;
- inst->c_ptr = 0;
- memset(inst->buff, 0, MAX_CHARS);
- for (a = 0; a < page.num_args; a++){
- switch (page.args[a].type){
- case 'f':
- retval = sprintf(temp,
- page.args[a].fmt,
- *((hal_float_t*)page.args[a].pntr));
- break;
- case 'u':
- retval = sprintf(temp,
- page.args[a].fmt,
- *((hal_u32_t*)page.args[a].pntr));
- break;
- case 's':
- retval = sprintf(temp,
- page.args[a].fmt,
- *((hal_s32_t*)page.args[a].pntr));
- break;
- case 't':
- retval = sprintf(temp,
- "%s",
- page.args[a].fmt);
- break;
- }
- retval = sprintf(inst->buff, "%s%s", inst->buff, temp);
- }
- if (dbg++ < 10) rtapi_print("%s/n",inst->buff);
- }
- }
- }
- void rtapi_app_exit(void)
- {
- hal_exit(comp_id);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement