Arduino For Kids Young and Old by Daniel Milligan

Page 136

Wire.write(data); byte transmissionStatus = 0; if (state == FinishSend || state == Complete) { transmissionStatus = Wire.endTransmission(); } return transmissionStatus; }

In the above code, I have written two main methods to send commands and data to the LCD and a third method which will be utilized by each of the other two methods. Because we can send both data and commands to the controller chip, I have two separate methods to handle that with the only difference being the first value written out onto the I2C bus indicating whether the following byte is a command or is data. I am sure there other ways I could have done it to conserve space however I chose this way as it is clear in regard to my intent. I did create a third method, SendByte which is used by each of the other methods in order to reduce the code size when and after data is sent. The other item of interest in this code is the send state SendState enumeration which I am using to track what state I am in terms of sending data to the I2C device. I am doing this for potential speed savings when writing to the end device as some devices can accept a number of commands or data values in one transmission sequence. The Wire library can buffer up to thirty two (32) bytes of data in its transmission queue to send out at one time. As can be seen above, the starting of the send along with the finishing of the send adds overhead and for every single byte of data, this can add up to a lot of activity on the I2C bus unnecessarily. So to alleviate this, I created a simple enumeration in the code to track what state I am in and cut down on the number of bytes being sent over the bus to improve overall throughput. So now that I have given you some of the background of how the Wire library is used, lets dig a little deeper. So how did I know how big the buffer inside of the Wire library was? I looked. Each of the libraries that you can import right out of the box reside on the local machine where the Arduino® application resides. On my Windows™ machine, I found it in the C:\Program Files (x86)\Arduino\libraries\Wire. For the Macintosh™ system it was located inside of the Arduino® package which you can view by Control - Clicking the mouse button and selecting the Show Package Contents option on the context menu that comes up. Doing this should then show a folder Contents which has inside it a number of folders. If you open the folder Resources you should find another folder entitled Java. Inside the Java folder is a file structure very similar to the PC based system including the libraries folder which contains the Wire folder. Inside of this folder, Wire, there is another folder called utility which contains support files for the Wire library. This code is the base two wire code, TWI which is the closest we get to the hardware. While that was a lot of digging around, it is always good to know how the libraries that you are depending on work. In this case, one key issue I had was the amount of data I could queue up before sending. The standard serial library can queue up around 64 bytes however this library, Wire has a maximum buffer size of 32 bytes. Always a good thing to know when trying to send 1024 bytes of


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.