123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
-
-
- #include "Marlin.h"
-
- #if ENABLED(EXPERIMENTAL_I2CBUS)
-
- #include "twibus.h"
-
- #include <Wire.h>
-
- TWIBus::TWIBus() {
- Wire.begin();
- this->reset();
- }
-
- void TWIBus::reset() {
- this->addr = 0;
- this->buffer_s = 0;
- this->buffer[0] = 0x00;
- }
-
- void TWIBus::address(uint8_t addr) {
- this->addr = addr;
-
- #if ENABLED(DEBUG_TWIBUS)
- debug(PSTR("sendto"), this->addr);
- #endif
- }
-
- void TWIBus::addbyte(char c) {
- if (buffer_s >= sizeof(this->buffer)) return;
- this->buffer[this->buffer_s++] = c;
-
- #if ENABLED(DEBUG_TWIBUS)
- debug(PSTR("addbyte"), this->buffer[this->buffer_s - 1]);
- #endif
- }
-
- void TWIBus::send() {
- if (!this->addr) return;
-
- #if ENABLED(DEBUG_TWIBUS)
- debug(PSTR("send()"));
- #endif
-
- Wire.beginTransmission(this->addr);
- Wire.write(this->buffer, this->buffer_s);
- Wire.endTransmission();
-
-
- this->reset();
- }
-
- void TWIBus::reqbytes(uint8_t bytes) {
- if (!this->addr) return;
-
- #if ENABLED(DEBUG_TWIBUS)
- debug(PSTR("reqbytes"), bytes);
- #endif
-
- millis_t t = millis() + this->timeout;
- Wire.requestFrom(this->addr, bytes);
-
-
- while (Wire.available() < bytes) {
- if (ELAPSED(millis(), t)) break;
- else continue;
- }
-
- SERIAL_ECHO_START;
- SERIAL_ECHOPAIR("i2c-reply: from:", this->addr);
- SERIAL_ECHOPAIR(" bytes:", Wire.available());
- SERIAL_ECHOPGM (" data:");
-
-
-
- uint8_t wba = Wire.available();
- for (int i = 0; i < wba; i++) SERIAL_CHAR(Wire.read());
- SERIAL_EOL;
-
-
- this->reset();
- }
-
- #if ENABLED(DEBUG_TWIBUS)
-
- void TWIBus::debug(const char func[], int32_t val) {
- if (DEBUGGING(INFO)) {
- SERIAL_ECHOPGM("TWIBus::");
- serialprintPGM(func);
- if (val >= 0) SERIAL_ECHOPAIR(": ", val);
- SERIAL_EOL;
- }
- }
-
- #endif
-
- #endif
|