Example #1
0
FskErr FskGPIOPlatformSetDirection(FskGPIO gpio, GPIOdirection direction){
	if (gpio->realPin >= FRONTOFFSET){
		int pin = gpio->realPin - FRONTOFFSET;
		int slaveAddy = 0;
		if (pin < 8){
			slaveAddy = 0x20;
		}else{
			slaveAddy = 0x21;
			pin -= 8;
		}
		if (direction == in){
			FskHardwarePinsDoMux(pin, slaveAddy, 4);
		}else if(direction == out){
			FskHardwarePinsDoMux(pin, slaveAddy, 5);
		}
		gpio->direction = direction;
		return kFskErrNone;
	}else{
		FskErr err = kFskErrNone;
		FILE *f = NULL;
		char buffer[40];
		char existingDirection;
    
        if (undefined != direction) {
            snprintf(buffer, 40, "/sys/class/gpio/gpio%d/direction", gpio->realPin);
            f = fopen(buffer, "w+");
            bailIfNoFile(f);
            rewind(f);
            fscanf(f, "%c", &existingDirection);
            rewind(f);
            if (direction == in && existingDirection != 'i'){
                fprintf(f, "in");
            }else if (direction == out && existingDirection != 'o'){
                fprintf(f, "out");
            }
            fclose(f);
            f = NULL;
        }

		if (  ((FskGPIOSysfs)gpio->platform)->valueFile != NULL) fclose(((FskGPIOSysfs)gpio->platform)->valueFile);
		snprintf(buffer, 40, "/sys/class/gpio/gpio%d/value", gpio->realPin);
		if (direction == in){
			f = fopen(buffer, "r+");
		}else if (direction == out){
			f = fopen(buffer, "w+");
		}
		bailIfNoFile(f);
		setbuf(f, NULL);
		((FskGPIOSysfs)gpio->platform)->valueFile = f;

bail:
		if (err){
			gpio->direction = errorDir;
			return err;
		}else{
			gpio->direction = direction;
			return err;
		}
	}
}
FskErr createFrontAnalogNew(FskPinAnalog *pin, SInt32 number, const char *name)
{
	FskErr err;
	createFrontAnalog cfa = NULL;

	err = FskMemPtrNewClear(sizeof(createFrontAnalogRecord), &cfa);
	if (err) return err;

	err = FskPinI2CNew(&cfa->i2c, 0, 0, I2CBUS);
	if (err) {
		FskMemPtrDispose(cfa);
		return err;
	}

	cfa->pin = number - 51;
	cfa->address = (cfa->pin < 8) ? a2dAddr1 : a2dAddr2;
	cfa->pin = 7 - (cfa->pin & 7);
//@@ this is odd.... this makes it an analog pin. seems like we should be checking that it is an analog pin, rather than forcing it
	FskHardwarePinsDoMux(cfa->pin, cfa->address, 3);

	*pin = (FskPinAnalog)cfa;

	return err;
}