gobot.io/x/gobot@v1.16.0/platforms/digispark/littleWire.h (about)

     1  #ifndef LITTLEWIRE_H
     2  #define LITTLEWIRE_H
     3  
     4  /*
     5    Cross platform computer interface library for Little Wire project
     6  
     7    http://littlewire.github.io
     8  
     9    Copyright (C) <2013> ihsan Kehribar <ihsan@kehribar.me>
    10    Copyright (C) <2013> Omer Kilic <omerkilic@gmail.com>
    11  
    12    Permission is hereby granted, free of charge, to any person obtaining a copy of
    13    this software and associated documentation files (the "Software"), to deal in
    14    the Software without restriction, including without limitation the rights to
    15    use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
    16    of the Software, and to permit persons to whom the Software is furnished to do
    17    so, subject to the following conditions:
    18  
    19    The above copyright notice and this permission notice shall be included in all
    20    copies or substantial portions of the Software.
    21  
    22    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    23    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    24    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    25    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    26    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    27    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    28    SOFTWARE.
    29  */
    30  
    31  #ifdef _WIN32
    32     #include <lusb0_usb.h>   // this is libusb, see http://libusb.sourceforge.net/
    33  #else
    34     #include <usb.h>       // this is libusb, see http://libusb.sourceforge.net/
    35  #endif
    36  #include "opendevice.h"			// common code moved to separate module
    37  #include "littleWire_util.h"
    38  #include <stdio.h>
    39  
    40  #define	VENDOR_ID 0x1781
    41  #define	PRODUCT_ID 0x0c9f
    42  #define USB_TIMEOUT 5000
    43  #define RX_BUFFER_SIZE 64
    44  
    45  #define INPUT 1
    46  #define OUTPUT 0
    47  
    48  #define ENABLE 1
    49  #define DISABLE 0
    50  
    51  #define HIGH 1
    52  #define LOW	 0
    53  
    54  #define AUTO_CS 1
    55  #define MANUAL_CS 0
    56  
    57  // Voltage ref definition
    58  #define VREF_VCC 0
    59  #define VREF_1100mV 1
    60  #define VREF_2560mV 2 
    61  
    62  // I2C definition
    63  #define END_WITH_STOP 1
    64  #define NO_STOP 0
    65  #define READ 1
    66  #define WRITE 0
    67  
    68  // General Purpose Pins
    69  #define PIN1 1
    70  #define PIN2 2
    71  #define PIN3 5
    72  #define PIN4 0
    73  
    74  // ADC Channels
    75  #define ADC_PIN3 0
    76  #define ADC_PIN2 1
    77  #define ADC_TEMP_SENS 2
    78  
    79  // PWM Pins
    80  #define PWM1 PIN4
    81  #define PWM2 PIN1
    82  
    83  // Aliases
    84  #define ADC0 ADC_PIN3
    85  #define ADC1 ADC_PIN2
    86  #define ADC2 ADC_TEMP_SENS
    87  #define PWMA PWM1
    88  #define PWMB PWM2
    89  
    90  // 'AVR ISP' Pins
    91  #define SCK_PIN PIN2
    92  #define MISO_PIN PIN1
    93  #define MOSI_PIN PIN4
    94  #define RESET_PIN PIN3
    95  
    96  extern unsigned char rxBuffer[RX_BUFFER_SIZE]; /* This has to be unsigned for the data's sake */
    97  extern unsigned char ROM_NO[8];
    98  extern int lwStatus;
    99  
   100  /*! \addtogroup General
   101  *  @brief General library functions
   102  *  @{
   103  */
   104  
   105  typedef usb_dev_handle littleWire;
   106  
   107  typedef struct lwCollection
   108  {
   109    struct usb_device* lw_device;
   110    int serialNumber;  
   111  }lwCollection;
   112  
   113  extern lwCollection lwResults[16];
   114  
   115  extern int lw_totalDevices;
   116  
   117  /**
   118    * Tries to cache all the littleWire devices and stores them in lwResults array. \n
   119    * Don't actually connects to any of the device(s).
   120    *
   121    * @param (none)
   122    * @return Total number of littleWire devices found in the USB system.
   123    */
   124  int littlewire_search();
   125  
   126  /**
   127    * Tries to connect to the spesific littleWire device by array id. 
   128    *
   129    * @param desiredID array index of the lwResults array.
   130    * @return littleWire pointer for healthy connection, NULL for a failed trial.
   131    */
   132  littleWire* littlewire_connect_byID(int desiredID);
   133  
   134  /**
   135    * Tries to connect to the spesific littleWire with a given serial number. \n
   136    * If multiple devices have the same serial number, it connects to the last one it finds 
   137    *
   138    * @param mySerial Serial number of the desired littlewire device.
   139    * @return littleWire pointer for healthy connection, NULL for a failed trial.
   140    */
   141  littleWire* littlewire_connect_bySerialNum(int mySerial);
   142  
   143  /**
   144    * Tries to connect to the first littleWire device that libusb can find. 
   145    *
   146    * @param (none)
   147    * @return littleWire pointer for healthy connection, NULL for a failed trial.
   148    */
   149  littleWire* littleWire_connect();
   150  
   151  /**
   152    * Reads the firmware version of the Little Wire \n
   153    * Format: 0xXY => X: Primary version Y: Minor version
   154    *
   155    * @param (none)
   156    * @return Firmware version
   157    */ 
   158  unsigned char readFirmwareVersion(littleWire* lwHandle);
   159  
   160  /**
   161    * Changes the USB serial number of the Little Wire 
   162    * 
   163    * @param serialNumber Serial number integer value (100-99)
   164    * @return (none)
   165    */
   166  void changeSerialNumber(littleWire* lwHandle,int serialNumber);
   167  
   168  /**
   169    * Sends a custom message to the device. \n
   170    * Useful when developing new features in the firmware.
   171    *
   172    * @param receiveBuffer Returned data buffer
   173    * @param command Firmware command
   174    * @param d1 data[0] for the command
   175    * @param d2 data[1] for the command
   176    * @param d3 data[2] for the command
   177    * @param d4 data[3] for the command
   178    * @return status
   179    */
   180  int customMessage(littleWire* lwHandle,unsigned char* receiveBuffer,unsigned char command,unsigned char d1,unsigned char d2, unsigned char d3, unsigned char d4);
   181  
   182  /**
   183    * Returns the numeric value of the status of the last communication attempt
   184    *
   185    * @param (none)
   186    * @return Numeric value of the status of the last communication attempt
   187    */
   188  int littleWire_error ();
   189  
   190  /**
   191    * Returns the string version of the last communication attempt status if there was an error
   192    *
   193    * @param (none)
   194    * @return String version of the last communication attempt status if there was an error
   195    */
   196  char *littleWire_errorName ();
   197  
   198  /*! @} */
   199  
   200  /*! \addtogroup GPIO
   201  *  @brief GPIO library functions with Arduino-like syntax
   202  *  @{
   203  */
   204  
   205  /**
   206    * Set pin value
   207    *
   208    * @param lwHandle littleWire device pointer
   209    * @param pin Pin name (\b PIN1 , \b PIN2 , \b PIN3 or \b PIN4 )
   210    * @param state Pin state (\b HIGH or \b LOW)
   211    * @return (none)
   212    */
   213  void digitalWrite(littleWire* lwHandle, unsigned char pin, unsigned char state);
   214  
   215  /**
   216    * Set pin as input/output
   217    *
   218    * @param lwHandle littleWire device pointer
   219    * @param pin Pin name (\b PIN1 , \b PIN2 , \b PIN3 or \b PIN4 )
   220    * @param mode Mode of pin (\b INPUT or \b OUTPUT)
   221    * @return (none)
   222    */
   223  void pinMode(littleWire* lwHandle, unsigned char pin, unsigned char mode);
   224  
   225  /**
   226    * Read pin value
   227    *
   228    * @param lwHandle littleWire device pointer
   229    * @param pin Pin name (\b PIN1 , \b PIN2 , \b PIN3 or \b PIN4 )
   230    * @return Pin state (\b HIGH or \b LOW)
   231    */
   232  unsigned char digitalRead(littleWire* lwHandle, unsigned char pin);
   233  
   234  /**
   235    * Sets the state of the internal pullup resistor. 
   236    * \n Call this function after you assign the pin as an input.
   237    *
   238    * @param lwHandle littleWire device pointer
   239    * @param pin Pin name (\b PIN1 , \b PIN2 , \b PIN3 or \b PIN4 )
   240    * @rparam state (\b ENABLE or \b DISABLE )
   241    * @return (none)
   242    */
   243  void internalPullup(littleWire* lwHandle, unsigned char pin, unsigned char state);
   244  
   245  /*! @} */
   246  
   247  /*! \addtogroup ADC
   248  *  @brief Analog to digital converter functions.
   249  *  @{
   250  */
   251  
   252  /**
   253    * Initialize the analog module. VREF_VCC is the standard voltage coming from the USB plug
   254    * \n Others are the Attiny's internal voltage references.
   255    *
   256    * @param lwHandle littleWire device pointer
   257    * @param voltageRef (\b VREF_VCC , \b VREF_110mV or \b VREF_2560mV )
   258    * @return (none)
   259    */
   260  void analog_init(littleWire* lwHandle, unsigned char voltageRef);
   261  
   262  /**
   263    * Read analog voltage. Analog voltage reading from ADC_PIN3 isn't advised (it is a bit noisy) but supported. Use it at your own risk.
   264    * \n For more about internal temperature sensor, look at the Attiny85 datasheet.
   265    *
   266    * @param lwHandle littleWire device pointer
   267    * @param channel Source of ADC reading (\b ADC_PIN2 , \b ADC_PIN3 or \b ADC_TEMP_SENS )
   268    * @return 10 bit ADC result
   269    */
   270  unsigned int analogRead(littleWire* lwHandle, unsigned char channel);
   271  
   272  /*! @} */
   273  
   274  /*! \addtogroup PWM
   275  *  @brief Pulse width modulation functions.
   276  *  @{
   277  */
   278  
   279  /**
   280    * Initialize the PWM module on the Little-Wire 
   281    *
   282    * @param lwHandle littleWire device pointer
   283    * @return (none)
   284    */
   285  void pwm_init(littleWire* lwHandle);
   286  
   287  /**
   288    * Stop the PWM module on the Little-Wire 
   289    *
   290    * @param lwHandle littleWire device pointer
   291    * @return (none)
   292    */
   293  void pwm_stop(littleWire* lwHandle);
   294  
   295  /**
   296    * Update the compare values of the PWM output pins. Resolution is 8 bit.
   297    *
   298    * @param lwHandle littleWire device pointer
   299    * @param channelA Compare value of \b PWMA pin
   300    * @param channelB Compare value of \b PWMB pin
   301    * @return (none)
   302    */
   303  void pwm_updateCompare(littleWire* lwHandle, unsigned char channelA, unsigned char channelB);
   304  
   305  /**
   306    * Update the prescaler of the PWM module. Adjust this value according to your need for speed in PWM output. Default is 1024. Lower prescale means higher frequency PWM output.
   307    *
   308    * @param lwHandle littleWire device pointer
   309    * @param value Presecaler value (\b 1024, \b 256, \b 64, \b 8 or \b 1) 
   310    * @return (none)
   311    */
   312  void pwm_updatePrescaler(littleWire* lwHandle, unsigned int value);
   313  
   314  /*! @} */
   315  
   316  /*! \addtogroup SPI
   317  *  @brief Serial peripheral interface functions.
   318  *  @{
   319  */
   320  
   321  /**
   322    * Initialize the SPI module on the Little-Wire 
   323    *
   324    * @param lwHandle littleWire device pointer
   325    * @return (none)
   326    */
   327  void spi_init(littleWire* lwHandle);
   328  
   329  /**
   330    * Send SPI message(s). SPI Mode is 0. 
   331    *
   332    * @param lwHandle littleWire device pointer
   333    * @param sendBuffer Message array to send
   334    * @param inputBuffer Returned answer message
   335    * @param length Message length - maximum 4
   336    * @param mode \b AUTO_CS or \b MANUAL_CS
   337    * @return (none)
   338    */
   339  void spi_sendMessage(littleWire* lwHandle, unsigned char * sendBuffer, unsigned char * inputBuffer, unsigned char length ,unsigned char mode);
   340  
   341  /**
   342    * Send one byte SPI message over MOSI pin. Slightly slower than the actual one.
   343    * \n There isn't any chip select control involved. Useful for debug console app
   344    *
   345    * @param lwHandle littleWire device pointer
   346    * @param message Message to send
   347    * @return Received SPI message
   348    */
   349  unsigned char debugSpi(littleWire* lwHandle, unsigned char message);
   350  
   351  /**
   352    * Change the SPI message frequency by adjusting delay duration. By default, Little-Wire sends the SPI messages with maximum speed. 
   353    * \n If your hardware can't catch up with the speed, increase the duration value to lower the SPI speed.
   354    *
   355    * @param lwHandle littleWire device pointer
   356    * @param duration Amount of delay. 
   357    * @return (none)
   358    */
   359  void spi_updateDelay(littleWire* lwHandle, unsigned int duration);
   360  
   361  /*! @} */
   362  
   363  /*! \addtogroup I2C
   364    *  @brief Inter IC communication functions.
   365    *  @{
   366    */
   367  
   368  /**
   369    * Initialize the I2C module on the Little-Wire 
   370    *
   371    * @return (none)
   372    */
   373  void i2c_init(littleWire* lwHandle);
   374  
   375  /**
   376    * Start the i2c communication
   377    *
   378    * @param lwHandle littleWire device pointer
   379    * @param address 7 bit slave address.
   380    * @param direction ( \b READ or \b WRITE )
   381    * @return 1 if received ACK
   382    */
   383  unsigned char i2c_start(littleWire* lwHandle, unsigned char address7bit, unsigned char direction);
   384  
   385  /**
   386    * Send byte(s) over i2c bus
   387    *
   388    * @param lwHandle littleWire device pointer
   389    * @param sendBuffer Message array to send
   390    * @param length Message length -> Max = 4
   391    * @param endWithStop Should we send a STOP condition after this buffer? ( \b END_WITH_STOP or \b NO_STOP )
   392    * @return (none)
   393    */
   394  void i2c_write(littleWire* lwHandle, unsigned char* sendBuffer, unsigned char length, unsigned char endWithStop);
   395  
   396  /**
   397    * Read byte(s) over i2c bus
   398    * 
   399    * @param lwHandle littleWire device pointer
   400    * @param readBuffer Returned message array
   401    * @param length Message length -> Max = 8
   402    * @param endWithStop Should we send a STOP condition after this buffer? ( \b END_WITH_STOP or \b NO_STOP )
   403    * @return (none)
   404    */
   405  void i2c_read(littleWire* lwHandle, unsigned char* readBuffer, unsigned char length, unsigned char endWithStop);
   406  
   407  /**
   408    * Update i2c signal delay amount. Tune if neccessary to fit your requirements.
   409    * 
   410    * @param lwHandle littleWire device pointer
   411    * @param duration Delay amount
   412    * @return (none)
   413    */
   414  void i2c_updateDelay(littleWire* lwHandle, unsigned int duration);
   415  
   416  /*! @} */ 
   417  
   418  /*! \addtogroup Onewire
   419   *  @brief Onewire functions.
   420   *  @{
   421   */
   422  
   423  /**
   424    * Send a single bit over onewire bus.
   425    *
   426    * @param lwHandle littleWire device pointer  
   427    * @param bitValue \b 1 or \b 0
   428    * @return (none)
   429    */
   430  void onewire_sendBit(littleWire* lwHandle, unsigned char bitValue);
   431  
   432  /**
   433    * Send a byte over onewire bus.
   434    *
   435    * @param lwHandle littleWire device pointer
   436    * @param messageToSend Message to send
   437    * @return (none)
   438    */
   439  void onewire_writeByte(littleWire* lwHandle, unsigned char messageToSend);
   440  
   441  /**
   442    * Read a byte over onewire bus.
   443    *
   444    * @param lwHandle littleWire device pointer
   445    * @return Read byte
   446    */
   447  unsigned char onewire_readByte(littleWire* lwHandle);
   448  
   449  /**
   450    * Read a single bit over onewire bus
   451    *
   452    * @param lwHandle littleWire device pointer
   453    * @return Read bit ( \b 1 or \b 0 )
   454    */
   455  unsigned char onewire_readBit(littleWire* lwHandle);
   456  
   457  /**
   458    * Send a reset pulse over onewire bus
   459    * 
   460    * @param lwHandle littleWire device pointer
   461    * @return Nonzero if any device presents on the bus
   462    */
   463  unsigned char onewire_resetPulse(littleWire* lwHandle);
   464  
   465  /**
   466    * Start searching for device address on the onewire bus.
   467    * \n Read the 8 byte address from \b ROM_NO array
   468    *
   469    * @param lwHandle littleWire device pointer  
   470    * @return Nonzero if any device found
   471    */
   472  int onewire_firstAddress(littleWire* lwHandle);
   473  
   474  /**
   475    * Try to find the next adress on the onewire bus.
   476    * \n Read the 8 byte address from \b ROM_NO array
   477    *
   478    * @param lwHandle littleWire device pointer  
   479    * @return Nonzero if any new device found
   480    */
   481  int onewire_nextAddress(littleWire* lwHandle);
   482  
   483  /*! @} */ 
   484  
   485  /*! \addtogroup SOFT_PWM
   486    *  @brief Software PWM functions. Designed to be used with RGB LEDs.
   487    *  @{
   488    */
   489  
   490  /**
   491    * Sets the state of the softPWM module
   492    * 
   493    * @param lwHandle littleWire device pointer
   494    * @param state State of the softPWM module ( \b ENABLE or \b DISABLE )
   495    * @return (none)
   496    */
   497  void softPWM_state(littleWire* lwHandle,unsigned char state);
   498  
   499  /**
   500    * Updates the values of softPWM modules
   501    *
   502    * @param lwHandle littleWire device pointer
   503    * @param ch1 Value of channel 1 - \b PIN4
   504    * @param ch2 Value of channel 2 - \b PIN1
   505    * @param ch3 Value of channel 3 - \b PIN2
   506    * @return (none)
   507    */
   508  void softPWM_write(littleWire* lwHandle,unsigned char ch1,unsigned char ch2,unsigned char ch3);
   509  
   510  /*! @} */
   511  
   512  /*! @} */ 
   513  
   514  /*! \addtogroup WS2812
   515    *  @brief WS2812 programmable RGB-LED support
   516    *  @{
   517    */
   518  
   519    /**
   520    * Writes to a WS2812 RGB-LED. This function writes the passed rgb values to a WS2812 led string
   521    * connected to the given pin. Use this if you want to control a single LED.
   522    *
   523    * If RGB values were preloaded with the preload call, the values passed in this call are added
   524    * to the buffer and the entire buffer is written to the LED string. This feature can be used to
   525    * reduce the number of USB transmissions for a string.
   526    *
   527    * @param lwHandle littleWire device pointer
   528    * @param r value of the red channel
   529    * @param g value of the green channel
   530    * @param b value of the blue channel
   531    * @param pin Pin name (\b PIN1 , \b PIN2 , \b PIN3 or \b PIN4 )
   532    * @return (none)
   533    */
   534  void ws2812_write(littleWire* lwHandle, unsigned char pin,unsigned char r,unsigned char g,unsigned char b);
   535  
   536    /**
   537    * This function flushes the contents of the littlewire internal RGB buffer to the LED string.
   538    *
   539    * @param lwHandle littleWire device pointer
   540    * @param r value of the red channel
   541    * @param g value of the green channel
   542    * @param b value of the blue channel
   543    * @return (none)
   544    */
   545  void ws2812_flush(littleWire* lwHandle, unsigned char pin);
   546  
   547    /**
   548    * Preloads a RGB value to the internal buffer. Up to 64 values can be preloaded. Further writes will be ignored
   549    *
   550    * @param lwHandle littleWire device pointer
   551    * @param r value of the red channel
   552    * @param g value of the green channel
   553    * @param b value of the blue channel
   554    * @return (none)
   555    */
   556  void ws2812_preload(littleWire* lwHandle, unsigned char r,unsigned char g,unsigned char b);
   557  
   558    /*! @} */
   559    
   560  
   561  /**
   562  * @mainpage Introduction
   563  
   564  \htmlinclude intro.html
   565  
   566  */
   567  
   568  #endif