json_t * rpi_i2c_register16_n_post(duda_request_t *dr, json_t *data, int parameter) { int fd = get_fd(dr); if (fd == -1) { return json->create_string("Invalid address."); } if (data->type != cJSON_Number) { return NULL; } int value = data->valueint; if (value < 0 || value > 65535) { return NULL; } if (wiringPiI2CWriteReg16(fd, parameter, value) != 0) { return json->create_string("Write failed."); } return json->create_string("Successful!"); }
static void myAnalogWrite (struct wiringPiNodeStruct *node, int pin, int data) { int chan = pin - node->pinBase ; int reg ; int16_t ndata ; chan &= 3 ; reg = chan + 2 ; /**/ if (data < -32767) ndata = -32767 ; else if (data > 32767) ndata = 32767 ; else ndata = (int16_t)data ; ndata = __bswap_16 (ndata) ; wiringPiI2CWriteReg16 (node->fd, reg, data) ; }
static int myAnalogRead (struct wiringPiNodeStruct *node, int pin) { int chan = pin - node->pinBase ; int16_t result ; uint16_t config = CONFIG_DEFAULT ; chan &= 7 ; // Setup the configuration register // Set PGA/voltage range config &= ~CONFIG_PGA_MASK ; config |= node->data0 ; // Set sample speed config &= ~CONFIG_DR_MASK ; config |= node->data1 ; // Set single-ended channel or differential mode config &= ~CONFIG_MUX_MASK ; switch (chan) { case 0: config |= CONFIG_MUX_SINGLE_0 ; break ; case 1: config |= CONFIG_MUX_SINGLE_1 ; break ; case 2: config |= CONFIG_MUX_SINGLE_2 ; break ; case 3: config |= CONFIG_MUX_SINGLE_3 ; break ; case 4: config |= CONFIG_MUX_DIFF_0_1 ; break ; case 5: config |= CONFIG_MUX_DIFF_2_3 ; break ; case 6: config |= CONFIG_MUX_DIFF_0_3 ; break ; case 7: config |= CONFIG_MUX_DIFF_1_3 ; break ; } // Start a single conversion config |= CONFIG_OS_SINGLE ; config = __bswap_16 (config) ; wiringPiI2CWriteReg16 (node->fd, 1, config) ; // Wait for the conversion to complete for (;;) { result = wiringPiI2CReadReg16 (node->fd, 1) ; result = __bswap_16 (result) ; if ((result & CONFIG_OS_MASK) != 0) break ; delayMicroseconds (100) ; } result = wiringPiI2CReadReg16 (node->fd, 0) ; result = __bswap_16 (result) ; // Sometimes with a 0v input on a single-ended channel the internal 0v reference // can be higher than the input, so you get a negative result... if ( (chan < 4) && (result < 0) ) return 0 ; else return (int)result ; }
/** * (de-)activate I2C-timeout register * @param timeoutFlag */ void Max7312::configTimeout(unsigned char timeoutFlag){ wiringPiI2CWriteReg16(fd, TIMEOUT_REGISTER, timeoutFlag); }
/** * set byte data to higher configuration register * @param portDirection */ void Max7312::configPort2(unsigned char portDirection){ wiringPiI2CWriteReg16(fd, CONFIGURATION_PORT_2, portDirection); _port2_mode = portDirection; }
/** * set byte data to lower PolarityInversionRegister * @param portInversion */ void Max7312::polarityInversionPort2(unsigned char portInversion){ wiringPiI2CWriteReg16(fd, POLARITY_INVERSION_PORT_2, portInversion); }
/** * write byte data to higher port * @param portData */ void Max7312::writePort2(unsigned char portData){ wiringPiI2CWriteReg16(fd, OUTPUT_PORT_2, portData); _port2_data = portData;}
int setPCA9685Duty(int fd , int channel , int on , int off) { int channelpos; channelpos = 0x6 + 4 * channel; wiringPiI2CWriteReg16(fd , channelpos , on & 0x0FFF); wiringPiI2CWriteReg16(fd , channelpos+2 , off & 0x0FFF); }