# Shunya Interfaces Core API Shunya Interfaces depends on the core library to provide basic API's to Interact with the Microprocessor Interfaces like GPIO, SPI, UART, I2C. While porting sensors Use these API's for interacting with sensors. ## GPIO API | **API** | **Description** | **Details** | | ------ | ------ | ------ | | `pinmode()` | Sets the direction of the GPIO pin to INPUT or OUTPUT | [Read More](#pinmode) | | `digitalWrite()` | Sets the digital value of the GPIO pin | [Read More](#digitalwrite) | | `digitalRead()` | Read digital value of the GPIO pin | [Read More](#digitalread) | ## Interrupt API | **API** | **Description** | **Details** | | ------ | ------ | ------ | | `attachInterrupt()`| Sets the digital value of the GPIO pin.| [Read More](#attachinterrupt) | ## Delay API | **API** | **Description** | **Details** | | ------ | ------ | ------ | | `delay()` | Code sleeps for given milliseconds | [Read More](#delay) | | `delayMicroseconds()` | Code sleeps for given microseconds | [Read More](#delaymicroseconds) | ## I2C API | **API** | **Description** | **Details** | | ------ | ------ | ------ | | `wireBegin()` | Initializes the I2C device | [Read More](#wirebegin) | | `wireBeginTransmission()` | Starts I2C communication | [Read More](#wirebegintransmission) | | `wireWrite()` | Send data to the I2C device | [Read More](#wirewrite) | | `wireRequestFrom()` | Request Data from the I2C device | [Read More](#wirerequestfrom) | | `wireAvailable()` |Check number of bytes available to read from the device | [Read More](#wireavailable) | | `wireRead()` | Read from the I2C device | [Read More](#wireread) | | `wireEndTransmission()` | End communication to the I2C device | [Read More](#wireendtransmission) | | `wireEnd()` | De-initialize the I2C device | [Read More](#wireend) | ## SPI API | **API** | **Description** | **Details** | | ------ | ------ | ------ | | `spiBegin()` | Initializes the SPI device | [Read More](#spibegin) | | `spiBeginTransaction()` | Starts SPI communication. | [Read More](#spibegintransaction) | | `spiTransfer()` | Send data to the I2C device | [Read More](#spitransfer) | | `spiEndTransaction()` | End the SPI transaction | [Read More](#spiendtransaction) | | `spiEnd()` | End the SPI communication | [Read More](#spiend) | | `spiSetBitOrder()` | Set the bit order to MSB or LSB first | [Read More](#spisetbitorder) | | `spiSetClock()` | Set SPI clock rate | [Read More](#spisetclock) | | `spiSetDataMode()` | Set SPI mode | [Read More](#spisetdatamode) | --- ## GPIO API ### `pinmode()` **Description** : Sets the direction of the GPIO pin to INPUT or OUTPUT **Parameters** * `physicalPin`(int) - Physical pin number of the GPIO * `mode`(int) - GPIO mode either INPUT or OUTPUT **Return-type** : void **Usage** : pinmode(7, OUTPUT); --- ### `digitalWrite()` **Description** : Sets the digital value of the GPIO pin **Parameters** * `physicalPin`(int) - Physical pin number of the GPIO * `value`(int) - value is either HIGH or LOW **Return-type** : void **Usage** : digitalWrite(7, HIGH); --- ### `digitalRead()` **Description** : Read digital value of the GPIO pin **Parameters** * `physicalPin`(int) - Physical pin number of the GPIO **Return-type** : int **Returns** : Return GPIO pin value on SUCCESS, Return -1 on FAILURE **Usage** : result = digitalRead(7); --- ## Delay API ### `delay()` **Description** : Used to produce millisecond delays **Parameters** + `howLong`(unsigned int) - Delay in ms to be produced **Return-type** : void **Usage** : delay(50); --- ### `delayMicroseconds()` **Description**: Used to produce microsecond delays **Parameters** + `howLong`(unsigned int) - Delay in us to be produced **Return-type**: void **Usage**: delayMicroseconds(50); --- ## I2C/TWI API ### `wireBegin()` **Description** : Initializes the I2C device **Parameters** * `bus`(const int) - I2C device node **Usage** : wireBegin(1); //1 is the board I2C 1 --- ### `wireBeginTransmission()` **Description** : Starts I2C communication **Parameters** + `addr`(const int) - I2C device address **Return-type** : void **Usage** : wireBeginTransmission(0x23); //0x23 is the device address --- ### `wireWrite()` **Description** : Send data to the I2C device **Parameters** * `val`(int) - Value to be written to the device. **Return-type** : void **Usage** : wireWrite(1); --- ### `wireRequestFrom()` **Description** : Request Data from the I2C device **Parameters** * `addr`(int) - I2C address * `count`(int) - Number of Bytes to request **Return-type** : int **Returns** : count of the data available or returns -1 on FAILURE **Usage** : wireRequestFrom(0x23,5); //0x23 is the address of the I2C device --- ### `wireAvailable()` **Description** : Check number of bytes available to read from the device **Return-type** : int **Returns** : count of the data available to read or returns -1 on FAILURE **Usage** : ret = wireAvailable(); --- ### `wireRead()` **Description** : Read from the I2C device. **Return-type** : int **Returns** : -1 on FAILURE **Usage** : val = wireRead(); --- ### `wireEndTransmission()` **Description** : End communication to the I2C device **Return-type** : void **Usage** : wireEndTransmission(); --- ### `wireEnd()` **Description** : De-initialize the I2C device **Return-type** : void **Returns** : -1 on FAILURE **Usage** : wireWriteReg8(fd,0x10,0x01); //Write 0x01 in the register whose address is 0x10 --- ## SPI API ### `spiBegin()` **Description** : Initializes the SPI bus. **Return-type** : void **Usage** : spiBegin(); ---- ### `spiBeginTransaction()` **Description** : Starts SPI communication. **Parameters** - clock SPI clock in Hz - bit_order Bit order for SPI - mode SPI mode (can be 0,1,2,3) **Return-type** : void **Usage** : spiBeginTransaction(50000, MSBFIRST, 0); ---- ### `spiTransfer()` **Description** : Write 8 bits (1 byte) of data to the SPI bus. **Parameters** - `val` Data to be written to the SPI device. **Return-type** : Returns the Rx buffer of the SPI bus. **Usage** : spiTransfer(0x5D); ---- ### `spiEndTransaction()` **Description** : Ends the SPI communication. **Return-type** : void **Usage** : spiEndTransaction(); ---- ### `spiEnd()` **Description** : Ends the SPI device. **Return-type** : void **Usage** : spiEnd(); ---- ### `spiSetBitOrder()` **Description** : Set the bit order to MSB or LSB first. **Parameters** - `order` Bit order **Return-type** : void **Usage** : spiSetBitOrder(MSBFIRST); ---- ### `spiSetClock()` **Description** : Set SPI clock rate **Parameters** - `max_speed` Clock rate **Return-type** : void **Usage** : spiSetClock(500000); ---- ### `spiSetDataMode()` **Description** : Set SPI mode **Parameters** - `mode` SPI mode (can be 0,1,2,3) **Return-type** : void **Usage** : spiSetDataMode(0);