ardworker_rcv.ino 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // This code is in the public domain.
  3. //
  4. // License: CC0
  5. //
  6. // Note:
  7. // For Arduino Nano clones, some use a CH340 USB chipset
  8. // which causes problems when trying to upload sketches.
  9. // The solution is to select the 'Nano' board type and then
  10. // select the 'ATmega328P (Old Bootloader)' option to program.
  11. //
  12. // See:
  13. // https://arduino.stackexchange.com/questions/51729/ch340-nano-avrdude-stk500-getsync-not-in-sync-resp-0xa4
  14. // https://arduino.stackexchange.com/questions/804/arduino-nano-uploading-gives-error-avrdude-stk500-recv-programmer-is-not-re
  15. //
  16. #include <Wire.h>
  17. #define MSG_BUF_MAX 128
  18. #define ARDWORKER_I2C_ADDRESS 0x08
  19. char _tbuf[MSG_BUF_MAX];
  20. int i2c_msg_buf_n = 0,
  21. i2c_msg_buf_rdy = 0;
  22. char i2c_msg_buf[MSG_BUF_MAX];
  23. int serial_buf_s=0,
  24. serial_buf_n=0;
  25. char serial_buf[MSG_BUF_MAX];
  26. void setup() {
  27. i2c_msg_buf_rdy = 0;
  28. i2c_msg_buf[0] = '\0';
  29. i2c_msg_buf_n = 0;
  30. serial_buf_s=0;
  31. serial_buf_n=0;
  32. serial_buf[0] = '\0';
  33. Wire.begin(ARDWORKER_I2C_ADDRESS);
  34. Wire.onReceive(receive_i2c_data);
  35. Wire.onRequest(send_i2c_data);
  36. Serial.begin(115200);
  37. }
  38. void loop() {
  39. int i, _b, _cur;
  40. int _ser_print_rdy = 0;
  41. noInterrupts();
  42. if (Serial.available()) {
  43. _b = Serial.read();
  44. if (_b >= 0) {
  45. // Add to our circular serial_buf for later tranport
  46. // through to the i2c line.
  47. //
  48. if (serial_buf_n < MSG_BUF_MAX) {
  49. _cur = (serial_buf_s + serial_buf_n) % MSG_BUF_MAX;
  50. serial_buf[_cur] = (char)_b;
  51. serial_buf_n++;
  52. }
  53. }
  54. }
  55. interrupts();
  56. noInterrupts();
  57. if (i2c_msg_buf_rdy) {
  58. for (i=0; i<MSG_BUF_MAX; i++) {
  59. _tbuf[i] = i2c_msg_buf[i];
  60. }
  61. i2c_msg_buf[0] = '\0';
  62. i2c_msg_buf_n=0;
  63. i2c_msg_buf_rdy=0;
  64. _ser_print_rdy = 1;
  65. }
  66. interrupts();
  67. if (_ser_print_rdy) {
  68. Serial.print(_tbuf);
  69. }
  70. }
  71. void send_i2c_data() {
  72. int res;
  73. if (serial_buf_n > 0) {
  74. res = Wire.write((char)serial_buf[serial_buf_s]);
  75. serial_buf_n--;
  76. serial_buf_s++;
  77. serial_buf_s %= MSG_BUF_MAX;
  78. }
  79. }
  80. void receive_i2c_data(int howMany) {
  81. char c;
  82. while ( Wire.available()) {
  83. c = Wire.read();
  84. i2c_msg_buf[i2c_msg_buf_n++] = c;
  85. if (i2c_msg_buf_n >= MSG_BUF_MAX) {
  86. i2c_msg_buf_n = MSG_BUF_MAX-1;
  87. }
  88. if ((c == '\n') || (c == '\r')) {
  89. i2c_msg_buf[i2c_msg_buf_n] = '\0';
  90. // Our messages are terminated by '\n' and '\r'.
  91. // Once a message is received, shuttle it to the
  92. // serial line.
  93. //
  94. i2c_msg_buf_rdy=1;
  95. }
  96. }
  97. }