Пример #1
0
void fxErrorMessage(txMachine* the, txInteger theCode, txString theBuffer, txSize theSize)
{
    const char *name = FskInstrumentationGetErrorString((FskErr)theCode);
    
    if (name && ('(' != name[0]) && (1 + FskStrLen(name)) < (unsigned long)theSize)
        FskStrCopy(theBuffer, name);
    else
        FskStrNumToStr(theCode, theBuffer, theSize);
}
Пример #2
0
void xs_i2c_readBlock(xsMachine *the)
{
	FskErr err;
	xsI2C i2c = xsGetHostData(xsThis);
	int argc = xsToInteger(xsArgc), i;
	int format = 2;
    SInt32 dataSize = xsToInteger(xsArg(0)), readCount;
	UInt8 data[32];

	xsThrowIfNULL(i2c);

    DBG_I2C("xs_i2c_readBlock\n");

	if ((dataSize > 32) || (dataSize <= 0))
		xsThrowDiagnosticIfFskErr(kFskErrInvalidParameter, "I2C readBlock invalid size %d. %s", (int)dataSize, i2c->diagnosticID);

	FskPinI2CSetAddress(i2c->pin, i2c->address);
	err = FskPinI2CReadBytes(i2c->pin, dataSize, &readCount, data);
    if (err) {
        xsTraceDiagnostic("I2C readBlock failed with error %s %s.", FskInstrumentationGetErrorString(err), i2c->diagnosticID);
        goto bail;
    }

    if (argc > 1) {
        int t = xsTypeOf(xsArg(1));
        if ((xsNumberType == t) || (t == xsIntegerType))
            format = xsToInteger(xsArg(1));
        else {
            char *formatString = xsToString(xsArg(1));
            if (0 == FskStrCompare(formatString, "Buffer"))
                format = 2;
            else if (0 == FskStrCompare(formatString, "Chunk"))
                format = 0;
            else if (0 == FskStrCompare(formatString, "Array"))
                format = 1;
        }
    }
    
    if (2 == format) {
        xsResult = xsArrayBuffer(data, readCount);
    }
    else if (0 == format) {
        xsResult = xsNew1(xsGlobal, xsID("Chunk"), xsInteger(readCount));
        FskMemMove(xsGetHostData(xsResult), data, readCount);
    }
    else if (1 == format) {
        xsResult = xsNew1(xsGlobal, xsID("Array"), xsInteger(readCount));
        for (i = 0; i < readCount; i++)
            xsSet(xsResult, i, xsInteger(data[i]));
    }
    
bail:
    if (err)
		xsError(err);
}
Пример #3
0
void xs_pwm_read(xsMachine* the)
{
	FskErr err;
	double value;
    FskPinPWM pwm = xsGetHostData(xsThis);
    if (!pwm) return;

	err = FskPinPWMGetDutyCycle(pwm, &value);
	xsThrowDiagnosticIfFskErr(err, "PWM read of pin %d failed with error %d.", (int)-1, FskInstrumentationGetErrorString(err));

	xsResult = xsNumber(value);
}
Пример #4
0
void xs_a2d_init(xsMachine* the)
{
    FskErr err;
    FskPinAnalog a2d;
    SInt32 pin = xsToInteger(xsGet(xsThis, xsID("pin")));

    if (xsGetHostData(xsThis))
        xsThrowDiagnosticIfFskErr(kFskErrBadState, "Analog pin %d already initialized.", (int)pin);

    err = FskPinAnalogNew(&a2d, pin, NULL);
    xsThrowDiagnosticIfFskErr(err, "Analog init failed with error %s on pin %d.", FskInstrumentationGetErrorString(err), (int)pin);

    xsSetHostData(xsThis, a2d);
}
Пример #5
0
void xs_i2c_init(xsMachine *the)
{
    xsI2C i2c = NULL;
    FskErr err;
    int address;
    int sdaPin = 0, clkPin = 0, sdaDev = 0, clkDev = 0;
	FskPinI2C pin = NULL;

    if (xsGetHostData(xsThis))
        xsThrowDiagnosticIfFskErr(kFskErrBadState, "I2C pin already initialized (SDA pin %d).", i2c->sdaPin);

    address = xsToInteger(xsGet(xsThis, xsID("address")));
    xsResult = xsGet(xsThis, xsID("bus"));
    if (xsUndefinedType == xsTypeOf(xsResult)) {
        sdaPin = xsToInteger(xsGet(xsThis, xsID("sda")));
        clkPin = xsToInteger(xsGet(xsThis, xsID("clock")));

		err = FskPinI2CNew(&pin, sdaPin, clkPin, kFskPinI2CNoBus);
    }
    else {
        sdaDev = xsToInteger(xsResult);
		err = FskPinI2CNew(&pin, 0, 0, sdaDev);
	}

    xsThrowDiagnosticIfFskErr(err, "I2C open failed %s (SDA pin %d, CLK pin %d).", FskInstrumentationGetErrorString(err), sdaPin, clkPin);

    err = FskMemPtrNewClear(sizeof(xsI2CRecord), &i2c);
    if (err) {
        FskPinI2CDispose(pin);
        xsError(err);
    }
    
    xsSetHostData(xsThis, i2c);

	i2c->pin = pin;
    i2c->sdaPin = sdaPin;
    i2c->clkPin = clkPin;
    i2c->sdaDev = sdaDev;
    i2c->clkDev = clkDev;
    i2c->address = (UInt8)address;
    i2c->bus = sdaDev;

#if SUPPORT_XS_DEBUG
    if (0 != sdaPin)
        snprintf(i2c->diagnosticID, sizeof(i2c->diagnosticID), "(Address 0x%x, SDA pin %d, CLK pin %d)", address, sdaPin, clkPin);
    else
        snprintf(i2c->diagnosticID, sizeof(i2c->diagnosticID), "(Address 0x%x, Bus %d)", address, sdaDev);
#endif
}
Пример #6
0
void xs_i2c_writeByte(xsMachine *the)
{
    xsI2C i2c = xsGetHostData(xsThis);
    FskErr err;
    UInt8 byte = (UInt8)xsToInteger(xsArg(0));

    DBG_I2C("xs_i2c_writeByte: %#x\n", byte);

	xsThrowIfNULL(i2c);

    FskPinI2CSetAddress(i2c->pin, i2c->address);

    err = FskPinI2CWriteByte(i2c->pin, byte);
    xsThrowDiagnosticIfFskErr(err, "I2C writeByte failed with error %s %s.", FskInstrumentationGetErrorString(err), i2c->diagnosticID);
}
Пример #7
0
void xs_i2c_writeQuickSMB(xsMachine *the)
{
    xsI2C i2c = xsGetHostData(xsThis);
    FskErr err;
    uint8_t byte = (uint8_t)xsToInteger(xsArg(0));

	DBG_I2C("xs_i2c_writeQuickSMB Writing quick byte SMB %u\n", byte);

	xsThrowIfNULL(i2c);

    FskPinI2CSetAddress(i2c->pin, i2c->address);

//@@    err = FskI2CWriteQuickSMB(i2c->bus, byte);
err = kFskErrOperationFailed;		//@@
    xsThrowDiagnosticIfFskErr(err, "I2C writeQuickSMB failed with error %s %s.", FskInstrumentationGetErrorString(err), i2c->diagnosticID);
}
Пример #8
0
void xs_i2c_writeBlock(xsMachine* the)
{
	FskErr err;
	xsI2C i2c = xsGetHostData(xsThis);
	int argc = xsToInteger(xsArgc), i;
	UInt8 buffer[32], *bufPtr = buffer;

	xsThrowIfNULL(i2c);

    DBG_I2C("xs_i2c_writeBlock\n");

	for (i = 0; i < argc; i++)
		bufPtr = writeOne(the, i2c, &xsArg(i), bufPtr, buffer + sizeof(buffer));

	FskPinI2CSetAddress(i2c->pin, i2c->address);

	err = FskPinI2CWriteBytes(i2c->pin, bufPtr - buffer, buffer);
    xsThrowDiagnosticIfFskErr(err, "I2C FskI2CWriteBlock failed with error %s %s.", FskInstrumentationGetErrorString(err), i2c->diagnosticID);
}
Пример #9
0
void xs_pwm_write(xsMachine* the)
{
    SInt32 argc;
	FskErr err;
    double dutyCyclePercent;
    double dutyCycle;
    double period;
    FskPinPWM pwm = xsGetHostData(xsThis);
    if (!pwm) return;

    argc = xsToInteger(xsArgc);

    if (argc == 1){
        dutyCyclePercent = xsToNumber(xsArg(0));
        err = FskPinPWMSetDutyCycle(pwm, dutyCyclePercent);
    }else if(argc >= 2){
        dutyCycle = xsToNumber(xsArg(0));
        period = xsToNumber(xsArg(1));
        err = FskPinPWMSetDutyCycleAndPeriod(pwm, dutyCycle, period);
    }

	xsThrowDiagnosticIfFskErr(err, "PWM write of pin %d failed with error %d.", (int)-1, FskInstrumentationGetErrorString(err));
}
Пример #10
0
void xs_pwm_init(xsMachine* the)
{
    FskErr err;
    FskPinPWM pwm;
    SInt32 pin = 0;
    char *pinName = NULL;

    xsVars(1);

    pwm = xsGetHostData(xsThis);
    if (pwm)
        xsThrowDiagnosticIfFskErr(kFskErrBadState, "PWM pin %d already initialized.", (int)pin);

    xsVar(0) = xsGet(xsThis, xsID("pin"));
    if (xsStringType == xsTypeOf(xsVar(0)))
        pinName = xsToString(xsVar(0));
    else
        pin = xsToInteger(xsVar(0));

	err = FskPinPWMNew(&pwm, pin, pinName);
    xsThrowDiagnosticIfFskErr(err, "PWM initialization of pin %d failed with error %d.", (int)pin, FskInstrumentationGetErrorString(err));

    xsSetHostData(xsThis, pwm);
}
Пример #11
0
void xs_gpio_init(xsMachine* the)
{
    FskErr err;
    FskGPIO gpio;
    SInt32 pin = 0;
	GPIOdirection dir;
    char *pinName = NULL;

    xsVars(1);

    if ((gpio = xsGetHostData(xsThis)))
        xsThrowDiagnosticIfFskErr(kFskErrOperationFailed, "Digital pin %d already initialized.", (int)gpio->pinNum);

    dir = stringToDirection(the, xsToString(xsGet(xsThis, xsID("_direction"))), gpio);

    xsVar(0) = xsGet(xsThis, xsID("_pin"));
    if (xsStringType == xsTypeOf(xsVar(0)))
        pinName = xsToString(xsVar(0));
    else
        pin = xsToInteger(xsVar(0));

    err = FskGPIONew(&gpio, pin, pinName, dir);
    xsThrowDiagnosticIfFskErr(err, "Digital pin %d initialization failed with error %s.", pin, FskInstrumentationGetErrorString(err));

    xsSetHostData(xsThis, gpio);
}
Пример #12
0
void xs_gpio_read(xsMachine* the)
{
    FskGPIO gpio = xsGetHostData(xsThis);
    if (gpio) {
    	int value = FskGPIOPlatformRead(gpio);
        if ((0 == value) || (1 == value))
            xsResult = xsInteger(value);
        else
            xsThrowDiagnosticIfFskErr((FskErr)value, "Digital pin %d read error %s.", (int)gpio->pinNum, FskInstrumentationGetErrorString((FskErr)value));
    }
}
Пример #13
0
void xs_gpio_write(xsMachine* the)
{
    FskGPIO gpio = xsGetHostData(xsThis);
    if (gpio) {
        SInt32 value = xsToInteger(xsArg(0));
        FskErr err = FskGPIOPlatformWrite(gpio, value ? 1 : 0);
        xsThrowDiagnosticIfFskErr(err, "Digital pin %d write error %s.", (int)gpio->pinNum, FskInstrumentationGetErrorString(err));
    }
}
Пример #14
0
void xs_i2c_writeBlockDataSMB(xsMachine* the)
{
    FskErr err;
    xsI2C i2c = xsGetHostData(xsThis);
    int argc = xsToInteger(xsArgc), i;
    UInt8 command = (UInt8)xsToInteger(xsArg(0));
    unsigned char buffer[32], *bufPtr = buffer;

	xsThrowIfNULL(i2c);

    DBG_I2C("xs_i2c_writeBlockDataSMB\n");

	FskPinI2CSetAddress(i2c->pin, i2c->address);

    for (i = 1; i < argc; i++)
        bufPtr = writeOne(the, i2c, &xsArg(i), bufPtr, buffer + sizeof(buffer));

    err = FskPinI2CWriteDataBytes(i2c->pin, command, (SInt32)(bufPtr - buffer), buffer);
    xsThrowDiagnosticIfFskErr(err, "I2C writeBlockDataSMB register %d failed with error %s %s.", (int)command, FskInstrumentationGetErrorString(err), i2c->diagnosticID);
}
Пример #15
0
void xs_i2c_writeWordDataSMB(xsMachine *the)
{
    xsI2C i2c = xsGetHostData(xsThis);
    FskErr err;
    UInt8 command = (UInt8)xsToInteger(xsArg(0));
    UInt16 word = (uint16_t)xsToInteger(xsArg(1));

	DBG_I2C("xs_i2c_writeWordDataSMB write word %u from %u\n", word, command);

	xsThrowIfNULL(i2c);

    FskPinI2CSetAddress(i2c->pin, i2c->address);

    err = FskPinI2CWriteDataWord(i2c->pin, command, word);
    xsThrowDiagnosticIfFskErr(err, "I2C writeWordDataSMB register %d failed with error %s %s.", (int)command, FskInstrumentationGetErrorString(err), i2c->diagnosticID);
}