/* Autogenerated by /usr/bin/comp on Thu Aug 30 12:53:58 2007 -- do not edit */
#include "rtapi.h"
#include "rtapi_app.h"
#include "hal.h"
#include "rtapi_string.h"

static int comp_id;

#ifdef MODULE_INFO
MODULE_INFO(emc2, "component:turret:This component controls the turret hardware for Kirk Wallace's Hardinge HNC turret. It moves the turret to the last T word value entered. (Invoked by M6)");
MODULE_INFO(emc2, "pin:tool_change:bit:in:A .hal file should connect this to iocontrol.X.tool-change.:None");
MODULE_INFO(emc2, "pin:match:bit:in:A .hal file should connect this to s32equal.X.out which should be the result of a check for a match between the requested and current tool.:None");
MODULE_INFO(emc2, "pin:tool_changed:bit:out:A .hal file should connect this to iocontrol.X.tool-changed to indicate to EMC when the position change is complete.:None");
MODULE_INFO(emc2, "pin:seek:bit:out:Connect this pin via .hal file to the turret rotate solenoid signal.:None");
MODULE_INFO(emc2, "pin:stop:bit:out:Connect this pin via .hal file to the turret stop solenoid signal.:None");
MODULE_INFO(emc2, "param:state:s32:rw:This parameter holds the value of the current state of the turret change process; idle, seeking, stopping, parking.:None");
MODULE_INFO(emc2, "param:settle:s32:rw:This parameter holds the initial value and then the count down of servo periods to allow settling of the turret mechanism before invoking next state proceedure.:None");
MODULE_INFO(emc2, "param:stop_settle:s32:r:This is the initial settle time in servo periods between activating the stop solenoid and when the turret comes to rest.:None");
MODULE_INFO(emc2, "param:park_settle:s32:r:This is the initial settle time in servo periods between deactivating the turret rotate solenoid and when the turret descends to the park position.:None");
MODULE_INFO(emc2, "funct:_:0:");
#endif // __MODULE_INFO


struct state {
    hal_bit_t *tool_change;
    hal_bit_t *match;
    hal_bit_t *tool_changed;
    hal_bit_t *seek;
    hal_bit_t *stop;
    hal_s32_t state;
    hal_s32_t settle;
    hal_s32_t stop_settle;
    hal_s32_t park_settle;
};

static void _(struct state *inst, long period);
static int get_data_size(void);

static int export(char *prefix, long extra_arg) {
    char buf[HAL_NAME_LEN + 2];
    int r = 0;
    int sz = sizeof(struct state) + get_data_size();
    struct state *inst = hal_malloc(sz);
    memset(inst, 0, sz);
    r = hal_pin_bit_newf(HAL_IN, &(inst->tool_change), comp_id,
        "%s.tool-change", prefix);
    if(r != 0) return r;
    r = hal_pin_bit_newf(HAL_IN, &(inst->match), comp_id,
        "%s.match", prefix);
    if(r != 0) return r;
    r = hal_pin_bit_newf(HAL_OUT, &(inst->tool_changed), comp_id,
        "%s.tool-changed", prefix);
    if(r != 0) return r;
    r = hal_pin_bit_newf(HAL_OUT, &(inst->seek), comp_id,
        "%s.seek", prefix);
    if(r != 0) return r;
    r = hal_pin_bit_newf(HAL_OUT, &(inst->stop), comp_id,
        "%s.stop", prefix);
    if(r != 0) return r;
    r = hal_param_s32_newf(HAL_RW, &(inst->state), comp_id,
        "%s.state", prefix);
    if(r != 0) return r;
    r = hal_param_s32_newf(HAL_RW, &(inst->settle), comp_id,
        "%s.settle", prefix);
    if(r != 0) return r;
    r = hal_param_s32_newf(HAL_RO, &(inst->stop_settle), comp_id,
        "%s.stop-settle", prefix);
    if(r != 0) return r;
    r = hal_param_s32_newf(HAL_RO, &(inst->park_settle), comp_id,
        "%s.park-settle", prefix);
    if(r != 0) return r;
    rtapi_snprintf(buf, HAL_NAME_LEN, "%s", prefix);
    r = hal_export_funct(buf, (void(*)(void *inst, long))_, inst, 0, 0, comp_id);
    if(r != 0) return r;
    return 0;
}
static int count = 1;
RTAPI_MP_INT(count, "number of turret");
int rtapi_app_main(void) {
    int r = 0;
    int i;
    comp_id = hal_init("turret");
    if(comp_id < 0) return comp_id;
    for(i=0; i<count; i++) {
        char buf[HAL_NAME_LEN + 2];
        rtapi_snprintf(buf, HAL_NAME_LEN, "turret.%d", i);
        r = export(buf, i);
        if(r != 0) break;
    }
    if(r) {
        hal_exit(comp_id);
    } else {
        hal_ready(comp_id);
    }
    return r;
}

void rtapi_app_exit(void) {
    hal_exit(comp_id);
}

#define FUNCTION(name) static void name(struct state *inst, long period)
#define EXTRA_SETUP() static int extra_setup(struct state *inst, long extra_arg)
#define EXTRA_CLEANUP() static void extra_cleanup(void)
#define fperiod (period * 1e-9)
#define tool_change (0+*inst->tool_change)
#define match (0+*inst->match)
#define tool_changed (*inst->tool_changed)
#define seek (*inst->seek)
#define stop (*inst->stop)
#define state (inst->state)
#define settle (inst->settle)
#define stop_settle (inst->stop_settle)
#define park_settle (inst->park_settle)


#line 13 "./turret.comp"
MODULE_LICENSE("GPL");
FUNCTION(_) {
    if (tool_change) {
        if (!match) {
            switch (state) {
            case 1:
                seek=1;
                state=2;
                break;
            case 2:
                if (match) {
                    stop=1
                    settle=stop_settle;
                    state=3;
                }
                break;
            case 3:
                --settle;
                if (settle<=0) {
                    seek=0;
                    settle=park_settle;
                    state=4;
                }
                break;
            case 4:
                --settle;
                if (settle<=0) {
                    stop=0;
                    state=1;
                    tool_changed=1;
                }
                break;
            default:
            }
        }
    }
state=1;
}


static int get_data_size(void) { return 0; }
