Orient3dData ofApp::readOrient3d() { Orient3dData res; res.bearing = (int)wiringPiI2CReadReg8(myOrient3d, 0x01); res.pitch = (int) wiringPiI2CReadReg8(myOrient3d, 0x04); res.roll = (int) wiringPiI2CReadReg8(myOrient3d, 0x05); return res; }
int main(){ int fd = wiringPiI2CSetup(0x68); int secs = wiringPiI2CReadReg8(fd, 0x00); int mins = wiringPiI2CReadReg8(fd, 0x01); int hours = wiringPiI2CReadReg8(fd, 0x02); printf("The RTC time is %2d:%02d:%02d\n", hours, mins, secs); return 0; }
/** Verify the I2C connection. * Make sure the device is connected and responds as expected. * @return True if connection is valid, false otherwise */ bool HMC5883L::testConnection() { //if (I2Cdev::readBytes(devAddr, HMC5883L_RA_ID_A, 3, buffer) == 3) { //if (wiringPiI2CReadReg8(devAddr, HMC5883L_RA_ID_A, 3, buffer) == 3) { char rxA = wiringPiI2CReadReg8(devAddr, HMC5883L_RA_ID_A); char rxB = wiringPiI2CReadReg8(devAddr, HMC5883L_RA_ID_B); char rxC = wiringPiI2CReadReg8(devAddr, HMC5883L_RA_ID_C); if (rxA == 'H' && rxB == '4' && rxC == '3') return true; else return false; }
double readEncoder(int fd) { int byte0 = wiringPiI2CReadReg8(fd, 0x41); int byte1 = wiringPiI2CReadReg8(fd, 0x40); int byte2 = wiringPiI2CReadReg8(fd, 0x43); int byte3 = wiringPiI2CReadReg8(fd, 0x42); //printf("Encoder = {%d, %d, %d, %d}\r\n", byte0, byte1, byte2, byte3); double regVal1 = (byte3 << 24) + (byte2 << 16) + (byte1 << 8) + byte0; return regVal1; }
double read_word_2c(adr,reg){ uint8_t high=wiringPiI2CReadReg8(adr,reg); uint8_t low=wiringPiI2CReadReg8(adr,reg); uint16_t val=(high<<8)+low; // lectura de dos palabras if(val>=0x8000){ // complemento a dos si es necesario return(double)(-((65535-val)+1)); }else{ return(double)(val); } }
void BtnAppuiLong (int Mcp, int *EtatLed) { while ((wiringPiI2CReadReg8 (Mcp, GPIO) - *EtatLed) == BTNG) { delay (20); } while ((wiringPiI2CReadReg8 (Mcp, GPIO) - *EtatLed) == BTND) { delay (20) ; } }
void Temperature::receiveTemperature() { static const int mpu6050RDS = 0x41; // MPU6050_RA_TEMP_OUT_H (0x41) static const double aScale = 163.47; quint8 msb = wiringPiI2CReadReg8(m_fd, mpu6050RDS); quint8 lsb = wiringPiI2CReadReg8(m_fd, mpu6050RDS+1); double temperature = msb << 8 | lsb; qDebug()<<Q_FUNC_INFO<<temperature; temperature=temperature / 340.0 - aScale; emit changeTemperature(temperature); }
int Mcp23017::readPort(quint8 port) { int ret; ret = -2; if (port == PORTA) { ret = wiringPiI2CReadReg8(fd, MCP23017_GPIOA); } else if (port == PORTB) { ret = wiringPiI2CReadReg8(fd, MCP23017_GPIOB); } return ret; }
void* blinkerThread(void* param) { struct threadParams * p = (struct threadParams *) param; int tid = pthread_self() ; int pin6=6, pin7=7; int bit6 = 1 << (pin6 & 7) ; int bit7 = 1 << (pin7 & 7) ; int fd = p->fd ; int value = LOW; // int old = p->old ; printf("%d: Blinker thread started...\n", tid) ; wiringPiI2CWriteReg8 (fd, MCP23x17_IOCON, 0b01100100) ; // mirror interrupts, disable sequential mode, open drain unsigned int nInt = wiringPiI2CReadReg8 (fd, MCP23x17_IOCON ); // Read printf("IOCONA="); bin_prnt_byte(nInt); wiringPiI2CWriteReg8 (fd, MCP23x17_IODIRA, 0x0) ; // Set direction as output - 5 B ports nInt = wiringPiI2CReadReg8 (fd, MCP23x17_IODIRA ); // Read printf("IODIRA="); bin_prnt_byte(nInt); wiringPiI2CWriteReg8 (fd, MCP23x17_IPOLA, 0x0) ; // Set POLARITY bit same as GPIO nInt = wiringPiI2CReadReg8 (fd, MCP23x17_IPOLA ); // Read printf("IPOLA="); bin_prnt_byte(nInt); p->old = wiringPiI2CReadReg8 (fd, MCP23x17_OLATA ); // Read printf("OLATA="); bin_prnt_byte(nInt); printf ("Port A setup completed.\n") ; while (g_ShouldRun) { while(!g_EndBlink) { if (value == LOW) p->old &= (~bit6) ; else p->old |= bit6 ; wiringPiI2CWriteReg8 (fd, MCP23x17_GPIOA, p->old); delay(1000); if (value == LOW) p->old &= (~bit7) ; else p->old |= bit7 ; wiringPiI2CWriteReg8 (fd, MCP23x17_GPIOA, p->old); value = !value ; //printf ("Value=") ; bin_prnt_byte(old) ; } wiringPiI2CWriteReg8 (fd, MCP23x17_GPIOA, 0); delay (1000); //grace sleep } printf("%d: Blinker thread: Cleaning and exiting...\n", tid) ; wiringPiI2CWriteReg8 (fd, MCP23x17_GPIOA, 0); return NULL; }
int readLidar (int lidFd1) { wiringPiI2CWriteReg8(lidFd1,0x00,0x04); while (wiringPiI2CReadReg8(lidFd1,0x01) ==1 ) { printf("Lidar is not ready yet\n"); } usleep(20000); int a = wiringPiI2CReadReg8(lidFd1,0x0f); int b = wiringPiI2CReadReg8(lidFd1,0x10); return b + (a<<8) ; }
void main(int argc,char* argv[]) { int fd,i2cAddress,j,k; if(argc<2) { printf("No address entered\n"); return; } i2cAddress=atoi(argv[1]); if((fd = wiringPiI2CSetup(i2cAddress))<0) { printf("could not connect to slave address\n"); return; } printf("setup done\n"); while(1) { for(j=0;j<16;j++) { printf("Sending %d\t",j); wiringPiI2CWriteReg8(fd,0,j); k=wiringPiI2CReadReg8(fd,2); printf("data read %d\n",k); delay(1000); } } }
int htu21dSetup (const int pinBase) { int fd ; struct wiringPiNodeStruct *node ; uint8_t data ; int status ; if ((fd = wiringPiI2CSetup (I2C_ADDRESS)) < 0) return FALSE ; node = wiringPiNewNode (pinBase, 2) ; node->fd = fd ; node->analogRead = myAnalogRead ; // Send a reset code to it: data = 0xFE ; if (write (fd, &data, 1) != 1) return FALSE ; delay (15) ; // Read the status register to check it's really there status = wiringPiI2CReadReg8 (fd, 0xE7) ; return (status == 0x02) ? TRUE : FALSE ; }
int adc_read_0(int fd) { int value; value = 0xff - wiringPiI2CReadReg8(fd, 0x32); return value; }
void gpiocontrollWriteA(int pin, bool val){ if(useWiringPi){ int read = wiringPiI2CReadReg8(expander,PORTA); if (val == true) wiringPiI2CWriteReg8(expander, PORTA, read | pin); else wiringPiI2CWriteReg8(expander, PORTA, read ^ pin); } else qDebug()<<"Write Port A Portexpander Bit:"<<pin<<":"<<val; }
word init(){ //Setup I2C word i2c = wiringPiI2CSetup(ADAFRUIT_MOTORHAT); //Setup PWM setAllPWM(i2c, 0, 0); wiringPiI2CWriteReg8(i2c, PWM_MODE2, PWM_OUTDRV); wiringPiI2CWriteReg8(i2c, PWM_MODE1, PWM_ALLCALL); delay(5); word mode1 = wiringPiI2CReadReg8(i2c, PWM_MODE1) & ~PWM_SLEEP; wiringPiI2CWriteReg8(i2c, PWM_MODE1, mode1); delay(5); //Set PWM frequency word prescale = (int)(25000000.0 / 4096.0 / PWM_FREQUENCY - 1.0); word oldmode = wiringPiI2CReadReg8(i2c, PWM_MODE1); word newmode = oldmode & (0x7F | 0x10); wiringPiI2CWriteReg8(i2c, PWM_MODE1, newmode); wiringPiI2CWriteReg8(i2c, PWM_PRESCALE, prescale); wiringPiI2CWriteReg8(i2c, PWM_MODE1, oldmode); delay(5); wiringPiI2CWriteReg8(i2c, PWM_MODE1, oldmode | 0x80); return i2c; }
json_t * rpi_i2c_register8_n_get(duda_request_t *dr, int parameter) { int fd = get_fd(dr); if (fd == -1) { return json->create_null(); } return json->create_number((double)wiringPiI2CReadReg8(fd, parameter)); }
int pcaHlReadAll(){ // Auslesen der Gesamt Helligkeit if(useWiringPi){ int HlSumme = 0; for (int var = 1; var < 9; ++var) { // List alle werte aus und Addiert sie HlSumme += wiringPiI2CReadReg8(pcaHL,var); } return HlSumme / 8; // Rückgabewert Summe geteilt durch 8 } else qDebug()<<"Read Hauptlicht Summe:"; return 100; }
/** Get 3-axis heading measurements. * In the event the ADC reading overflows or underflows for the given channel, * or if there is a math overflow during the bias measurement, this data * register will contain the value -4096. This register value will clear when * after the next valid measurement is made. Note that this method automatically * clears the appropriate bit in the MODE register if Single mode is active. * @param x 16-bit signed integer container for X-axis heading * @param y 16-bit signed integer container for Y-axis heading * @param z 16-bit signed integer container for Z-axis heading * @see HMC5883L_RA_DATAX_H */ void HMC5883L::getHeading(int16_t *x, int16_t *y, int16_t *z) { //I2Cdev::readBytes(devAddr, HMC5883L_RA_DATAX_H, 6, buffer); for (int i = 0 ; i < 5; i++) buffer[i] = wiringPiI2CReadReg8(devAddr, HMC5883L_RA_DATAX_H); if (mode == HMC5883L_MODE_SINGLE) wiringPiI2CWriteReg8(devAddr, HMC5883L_RA_MODE, HMC5883L_MODE_SINGLE << (HMC5883L_MODEREG_BIT - HMC5883L_MODEREG_LENGTH + 1)); *x = (((int16_t)buffer[0]) << 8) | buffer[1]; *y = (((int16_t)buffer[4]) << 8) | buffer[5]; *z = (((int16_t)buffer[2]) << 8) | buffer[3]; }
void ADXL345::setRange(int range_flag) { int value; value = wiringPiI2CReadReg8(FD_ADDR, DATA_FORMAT); value &= ~0x0F; value |= range_flag; value |= 0x08; // printf("set range value : %d",value); wiringPiI2CWriteReg8(FD_ADDR, DATA_FORMAT, value); }
static int myDigitalRead (struct wiringPiNodeStruct *node, int pin) { int mask, value ; mask = 1 << ((pin - node->pinBase) & 7) ; value = wiringPiI2CReadReg8 (node->fd, MCP23x08_GPIO) ; if ((value & mask) == 0) return LOW ; else return HIGH ; }
int Mcp23017::ISRB() { int ret; ret = wiringPiI2CReadReg8(fd, MCP23017_INTCAPB); qDebug("intcapb value = %X",ret); emit interrupt_B(ret); return ret; }
/** Get Z-axis heading measurement. * @return 16-bit signed integer with Z-axis heading * @see HMC5883L_RA_DATAZ_H */ int16_t HMC5883L::getHeadingZ() { // each axis read requires that ALL axis registers be read, even if only // one is used; this was not done ineffiently in the code by accident //I2Cdev::readBytes(devAddr, HMC5883L_RA_DATAX_H, 6, buffer); //if (mode == HMC5883L_MODE_SINGLE) I2Cdev::writeByte(devAddr, HMC5883L_RA_MODE, HMC5883L_MODE_SINGLE << (HMC5883L_MODEREG_BIT - HMC5883L_MODEREG_LENGTH + 1)); /* for (int i = 0 ; i < 5; i++) buffer[i] = wiringPiI2CReadReg8(devAddr, HMC5883L_RA_DATAX_H); if (mode == HMC5883L_MODE_SINGLE) wiringPiI2CWriteReg8(devAddr, HMC5883L_RA_MODE, HMC5883L_MODE_SINGLE << (HMC5883L_MODEREG_BIT - HMC5883L_MODEREG_LENGTH + 1)); return (((int16_t)buffer[2]) << 8) | buffer[3]; */ wiringPiI2CReadReg8(devAddr, HMC5883L_RA_DATAX_H); wiringPiI2CReadReg8(devAddr, HMC5883L_RA_DATAX_H+1); wiringPiI2CReadReg8(devAddr, HMC5883L_RA_DATAY_H); wiringPiI2CReadReg8(devAddr, HMC5883L_RA_DATAY_H+1); unsigned short msb = wiringPiI2CReadReg8(devAddr, HMC5883L_RA_DATAZ_H); unsigned short lsb = wiringPiI2CReadReg8(devAddr, HMC5883L_RA_DATAZ_H+1); unsigned short val = (((int16_t)msb) << 8) | lsb; return val; if (val >= 0x8000) return -((65535 - val) + 1); else return val; }
int mcp23017Setup (const int pinBase, const int i2cAddress) { int fd ; struct wiringPiNodeStruct *node ; if ((fd = wiringPiI2CSetup (i2cAddress)) < 0) return fd ; wiringPiI2CWriteReg8 (fd, MCP23x17_IOCON, IOCON_INIT) ; node = wiringPiNewNode (pinBase, 16) ; node->fd = fd ; node->pinMode = myPinMode ; node->pullUpDnControl = myPullUpDnControl ; node->digitalRead = myDigitalRead ; node->digitalWrite = myDigitalWrite ; node->data2 = wiringPiI2CReadReg8 (fd, MCP23x17_OLATA) ; node->data3 = wiringPiI2CReadReg8 (fd, MCP23x17_OLATB) ; return 0 ; }
int main() { // int mfd = wiringPiI2CSetup(0x71); //0x72 is multiplexer channel 1 int fd = wiringPiI2CSetup(0x13); //0x13 is determined by i2cdetect -y 1 // wiringPiSetup(); int loop; int pin = 0; wiringPiI2CWriteReg8(fd, 0x80, 0xa8); if(fd >= 0) { loop = 1; } else { loop = 0; } int ver = wiringPiI2CReadReg8(fd, 0x81); printf("version:%d\n", ver); if(ver == 0x11) { printf("Device found\n"); wiringPiI2CWriteReg8(fd, 0x83, 0x0a); //Should increase range of the sensor pinMode(LED, OUTPUT); digitalWrite(LED, 1); while(loop == 1) { wiringPiI2CWriteReg8(fd, 0x80, 0xa8); int lowByte = wiringPiI2CReadReg8(fd, 0x88); int highByte = wiringPiI2CReadReg8(fd, 0x87); int reading = (highByte << 8) | lowByte; /* if(reading >= 3000) { digitalWrite(LED, 0); } else { digitalWrite(LED, 1); }*/ delay(80); printf("%d\n",reading); } } else { printf("Device not found\n"); } }
static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode) { int mask, old, reg ; reg = MCP23x08_IODIR ; mask = 1 << (pin - node->pinBase) ; old = wiringPiI2CReadReg8 (node->fd, reg) ; if (mode == OUTPUT) old &= (~mask) ; else old |= mask ; wiringPiI2CWriteReg8 (node->fd, reg, old) ; }
int main(void) { int device = wiringPiI2CSetup(address); if (device < 0) { return 1; } int data = wiringPiI2CReadReg8(device, 0x01); printf("%d\n", data); return 0; }
int Mcp23017::readPin(quint8 port, quint8 pin) { int ret; if (port == PORTA) { ret = wiringPiI2CReadReg8(fd, MCP23017_GPIOA); if (ret & (1 << pin)) { return 1; } else { return 0; } } else if (port == PORTB) { ret = wiringPiI2CReadReg8(fd, MCP23017_GPIOB); if (ret & (1 << pin)) { return 1; } else { return 0; } } return -1; }
static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode) { int mask, old, reg ; reg = MCP23x08_GPPU ; mask = 1 << (pin - node->pinBase) ; old = wiringPiI2CReadReg8 (node->fd, reg) ; if (mode == PUD_UP) old |= mask ; else old &= (~mask) ; wiringPiI2CWriteReg8 (node->fd, reg, old) ; }
int setPCA9685Freq(int fd , float freq) { float prescaleval; int prescale , oldmode , newmode; freq = 0.9 * freq; prescaleval = 25000000.0; prescaleval /= 4096.0; prescaleval /= freq; prescaleval -= 1.0; prescale = prescaleval + 0.5; oldmode = wiringPiI2CReadReg8(fd,0x00); newmode = (oldmode & 0x7F)|0x10; wiringPiI2CWriteReg8(fd , 0x00 , newmode); wiringPiI2CWriteReg8(fd , 0xFE , prescale); wiringPiI2CWriteReg8(fd , 0x00 , oldmode); sleep(0.005); wiringPiI2CWriteReg8(fd , 0x00 , oldmode | 0xA1); }
int cPwmBoard::setDrive(int mode) { piLock(0); int regval = wiringPiI2CReadReg8(pwmFd,MODE2); if (mode == OPEN_DRAIN) { #ifdef PWM_DEBUG std::cout<<"PWM | Setting drive to Open Drain"<<std::endl; #endif #ifdef LOGGING_FULL logfile << "PWM | Setting drive to Open Drain" << std::endl; #endif regval = bitLow(2,regval); //Bit 2 = 0 } else if (mode == TOTEM_POLE) { #ifdef PWM_DEBUG std::cout<<"PWM | Setting drive to Totem Pole"<<std::endl; #endif #ifdef LOGGING_FULL logfile << "PWM | Setting drive to Totem Pole" << std::endl; #endif regval = bitHigh(2,regval); //Bit 2 = 1 } if(wiringPiI2CWriteReg8(pwmFd,MODE2,regval)<0) { #ifdef PWM_DEBUG std::cout<<"PWM | Setting drive mode failed (MODE2)"<<std::endl; #endif #ifdef LOGGING_FULL logfile << "PWM | Setting drive mode failed (MODE2)" << std::endl; #endif return -1; } piUnlock(0); return 0; }