/*This extract from my library will give you an idea about writing to and reading pins*/ int set_common(hal_type_t type, void *d_ptr, QString& value) { // This function assumes that the mutex is held int retval = 0; double fval = 0; long lval = 0; unsigned long ulval = 0; switch (type) { case HAL_BIT: if( (value.contains("TRUE") || (value.contains("1")))) *(hal_bit_t *) (d_ptr) = 1; else if( (value.contains("FALSE") || (value.contains("0")))) *(hal_bit_t *) (d_ptr) = 0; else retval = -EINVAL; break; case HAL_FLOAT: fval = value.toFloat(); *((hal_float_t *) (d_ptr)) = fval; break; case HAL_S32: lval = value.toLong(); *((hal_s32_t *) (d_ptr)) = lval; break; case HAL_U32: ulval = value.toULong(); *((hal_u32_t *) (d_ptr)) = ulval; break; default: /* Shouldn't get here, but just in case... */ retval = -EINVAL; } return retval; } int get_common(hal_type_t type, void *d_ptr, QString& value) { // This function assumes that the mutex is held int retval = 0; int bitval = 0; double fval = 0; long lval = 0; unsigned long ulval = 0; QString str; switch (type) { case HAL_BIT: bitval = *(hal_bit_t *) (d_ptr); str.sprintf("%d", bitval); break; case HAL_FLOAT: fval = *((hal_float_t *) (d_ptr)); str.sprintf("%f", fval); break; case HAL_S32: lval = *((hal_s32_t *) (d_ptr)); str.sprintf("%ld", lval); break; case HAL_U32: ulval = *((hal_u32_t *) (d_ptr)); str.sprintf("%lu", ulval); break; default: /* Shouldn't get here, but just in case... */ retval = -EINVAL; } //qDebug() << "str string = " << str; value = str; //qDebug() << "value string = " << value; return 0; } /*The mutex code is something like*/ // get mutex before accessing shared data rtapi_mutex_get(&(hal_data->mutex)); ... get or set the value ... rtapi_mutex_give(&(hal_data->mutex)); /*When you create any pin you have to malloc the memory required to hold the data it can carry, that is the pointer. As I said my libraries were for interfacing with Qt, I created various typedefs*/ // These are used as the basic data units for any pins created // via the functions in this class typedef struct { hal_bit_t *pin; } bit_data_t; typedef struct { hal_s32_t *pin; } s32_data_t; typedef struct { hal_float_t *pin; } float_data_t; /*and held the pins created in a local arrays for ease of access*/ // Access arrays bit_data_t* bit_data[ARRAY_SIZE]; s32_data_t* s32_data[ARRAY_SIZE]; float_data_t* float_data[ARRAY_SIZE]; hal_bit_t* bit_param_data[ARRAY_SIZE]; hal_s32_t* s32_param_data[ARRAY_SIZE]; hal_float_t* float_param_data[ARRAY_SIZE]; // if(strcmp((char*)float_name[y], name) == 0) finds name char bit_name [ARRAY_SIZE][NAME_LEN]; char s32_name [ARRAY_SIZE][NAME_LEN]; char float_name [ARRAY_SIZE][NAME_LEN]; char bit_param_name [ARRAY_SIZE][NAME_LEN]; char s32_param_name [ARRAY_SIZE][NAME_LEN]; char float_param_name [ARRAY_SIZE][NAME_LEN]; int bit_index; // index to the relevant array, left at last entry so size is known int s32_index; int float_index; int bit_param_index; int s32_param_index; int float_param_index; /*and this is routine to create a float pin and eneter it into the relevant array*/ int HAL_Access::createFloatPin(QString& name, bool in) { int retval; QStrncpy(lname, name, name.length()); if(float_index >= ARRAY_SIZE) { rtapi_print_msg(RTAPI_MSG_ERR,"ERROR: pin %s creation not possible, local array full\n", lname); return -1; } else { float_data_t *data = (float_data_t*)hal_malloc(sizeof(float_data_t)); if (data == 0) { rtapi_print_msg(RTAPI_MSG_ERR, "ERROR: hal_malloc() for pin %s creation failed\n", lname); cleanUp(); } else { float_data[float_index] = data; strcpy((char*)float_name[float_index++], lname); } retval = hal_pin_float_newf(in ? HAL_IN : HAL_OUT, &(data->pin), comp_id, "%s.%s", prefix, lname); if (retval < 0) { rtapi_print_msg(RTAPI_MSG_ERR,"ERROR: pin %s export failed with err=%i\n", lname, retval); cleanUp(); return -1; } return 0; } } /*If you write a simple comp file that creates a do nothing component with a couple of pins, run comp filename to generate a .c file you can then see the pure C method of creating a pin.*/ struct __comp_state { struct __comp_state *_next; hal_float_t *out; hal_float_t *in0; hal_float_t *in1; }; struct __comp_state *__comp_inst=0; struct __comp_state *__comp_first_inst=0, *__comp_last_inst=0; static void _(struct __comp_state *__comp_inst, long period); static int __comp_get_data_size(void); #undef TRUE #define TRUE (1) #undef FALSE #define FALSE (0) #undef true #define true (1) #undef false #define false (0) static int export(char *prefix, long extra_arg) { char buf[HAL_NAME_LEN + 1]; int r = 0; int sz = sizeof(struct __comp_state) + __comp_get_data_size(); struct __comp_state *inst = hal_malloc(sz); memset(inst, 0, sz); r = hal_pin_float_newf(HAL_OUT, &(inst->out), comp_id, "%s.out", prefix); if(r != 0) return r; r = hal_pin_float_newf(HAL_IN, &(inst->in0), comp_id, "%s.in0", prefix); if(r != 0) return r; r = hal_pin_float_newf(HAL_IN, &(inst->in1), comp_id, "%s.in1", prefix); if(r != 0) return r; rtapi_snprintf(buf, sizeof(buf), "%s", prefix); r = hal_export_funct(buf, (void(*)(void *inst, long))_, inst, 1, 0, comp_id); if(r != 0) return r; if(__comp_last_inst) __comp_last_inst->_next = inst; __comp_last_inst = inst; if(!__comp_first_inst) __comp_first_inst = inst; return 0; }