TinyWireM.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. TinyWireM.cpp - a wrapper class for TWI/I2C Master library for the ATtiny on Arduino
  3. 1/21/2011 BroHogan - brohoganx10 at gmail dot com
  4. **** See TinyWireM.h for Credits and Usage information ****
  5. This library is free software; you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the Free Software
  7. Foundation; either version 2.1 of the License, or any later version.
  8. This program is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  10. PARTICULAR PURPOSE. See the GNU General Public License for more details.
  11. */
  12. extern "C" {
  13. //#include "USI_TWI_Master.h"
  14. //#include <USI_TWI_Master.h>
  15. //#include <USI_TWI_Master\USI_TWI_Master.h>
  16. //#include <USI_TWI_Master/USI_TWI_Master.h>
  17. }
  18. #include "USI_TWI_Master.h"
  19. #include "TinyWireM.h"
  20. // Initialize Class Variables //////////////////////////////////////////////////
  21. uint8_t USI_TWI::USI_Buf[USI_BUF_SIZE]; // holds I2C send and receive data
  22. uint8_t USI_TWI::USI_BufIdx = 0; // current number of bytes in the send buff
  23. uint8_t USI_TWI::USI_LastRead = 0; // number of bytes read so far
  24. uint8_t USI_TWI::USI_BytesAvail = 0; // number of bytes requested but not read
  25. // Constructors ////////////////////////////////////////////////////////////////
  26. USI_TWI::USI_TWI(){
  27. }
  28. // Public Methods //////////////////////////////////////////////////////////////
  29. //int USI_TWI::peek(){}
  30. //void USI_TWI::flush(){}
  31. void USI_TWI::begin(){ // initialize I2C lib
  32. USI_TWI_Master_Initialise();
  33. }
  34. void USI_TWI::beginTransmission(uint8_t slaveAddr){ // setup address & write bit
  35. USI_BufIdx = 0;
  36. USI_Buf[USI_BufIdx] = (slaveAddr<<TWI_ADR_BITS) | USI_SEND;
  37. }
  38. size_t USI_TWI::write(uint8_t data){ // buffers up data to send
  39. if (USI_BufIdx >= USI_BUF_SIZE-1) return 0; // dont blow out the buffer
  40. USI_BufIdx++; // inc for next byte in buffer
  41. USI_Buf[USI_BufIdx] = data;
  42. return 1;
  43. }
  44. uint8_t USI_TWI::endTransmission() {
  45. return endTransmission(1);
  46. }
  47. uint8_t USI_TWI::endTransmission(uint8_t stop){ // actually sends the buffer
  48. bool xferOK = false;
  49. uint8_t errorCode = 0;
  50. xferOK = USI_TWI_Start_Read_Write(USI_Buf,USI_BufIdx+1); // core func that does the work
  51. USI_BufIdx = 0;
  52. if (xferOK) {
  53. if (stop) {
  54. errorCode = USI_TWI_Master_Stop();
  55. if (errorCode == 0) {
  56. errorCode = USI_TWI_Get_State_Info();
  57. return errorCode;
  58. }
  59. }
  60. return 0;
  61. }
  62. else { // there was an error
  63. errorCode = USI_TWI_Get_State_Info(); // this function returns the error number
  64. return errorCode;
  65. }
  66. }
  67. uint8_t USI_TWI::requestFrom(uint8_t slaveAddr, uint8_t numBytes){ // setup for receiving from slave
  68. bool xferOK = false;
  69. uint8_t errorCode = 0;
  70. USI_LastRead = 0;
  71. USI_BytesAvail = numBytes; // save this off in a global
  72. numBytes++; // add extra byte to transmit header
  73. USI_Buf[0] = (slaveAddr<<TWI_ADR_BITS) | USI_RCVE; // setup address & Rcve bit
  74. xferOK = USI_TWI_Start_Read_Write(USI_Buf,numBytes); // core func that does the work
  75. // USI_Buf now holds the data read
  76. if (xferOK) {
  77. errorCode = USI_TWI_Master_Stop();
  78. if (errorCode == 0) {
  79. errorCode = USI_TWI_Get_State_Info();
  80. return errorCode;
  81. }
  82. return 0;
  83. }
  84. else { // there was an error
  85. errorCode = USI_TWI_Get_State_Info(); // this function returns the error number
  86. return errorCode;
  87. }
  88. }
  89. int USI_TWI::read(){ // returns the bytes received one at a time
  90. USI_LastRead++; // inc first since first uint8_t read is in USI_Buf[1]
  91. return USI_Buf[USI_LastRead];
  92. }
  93. int USI_TWI::available(){ // the bytes available that haven't been read yet
  94. return USI_BytesAvail - (USI_LastRead);
  95. }
  96. // Preinstantiate Objects //////////////////////////////////////////////////////
  97. USI_TWI TinyWireM = USI_TWI();