#include "Arduino.h" #include "MuxClient.h" bool MuxClient::muxReady = false; int MuxClient::adressPins[4] = {0,0,0,0}; int MuxClient::enablePin = 0; int MuxClient::dataPin = 0; bool MuxClient::state = false; MuxClient::MuxClient(){ clientAdress = 0; clientType = 2; // errorMessage = "NULL"; } MuxClient::MuxClient(int adress, int type){ clientAdress = adress; clientType = type; // errorMessage = "NULL"; } void MuxClient::setupMux(int adressPin1, int adressPin2, int adressPin3, int adressPin4, int data, int enable) { adressPins[0] = adressPin1; adressPins[1] = adressPin2; adressPins[2] = adressPin3; adressPins[3] = adressPin4; enablePin = enable; dataPin = data; pinMode(enablePin, OUTPUT); pinMode(dataPin, INPUT_PULLUP); for (int i=0; i<4; i++){ pinMode(adressPins[i], OUTPUT); } muxReady = true; powerMux(true); } void MuxClient::powerMux(bool setState){ if (muxReady) { if (setState){ digitalWrite(enablePin, LOW); state = true; } else { digitalWrite(enablePin, HIGH); state = false; } } else { // errorMessage = "run setupMux() after client declarations"; state = false; } } bool MuxClient::powerMux(){ if (muxReady) { return state; } else { // errorMessage = "run setupMux() after client declarations"; return false; } } void MuxClient::listen() { if (muxReady) { if (clientType == digital_read) { pinMode(dataPin, INPUT); setAdress(clientAdress); pinMode(dataPin, INPUT_PULLUP); delayMicroseconds(1); } else if (clientType == analog_read) { pinMode(dataPin, INPUT); setAdress(clientAdress); delayMicroseconds(1); } else if (clientType == digital_write || clientType == analog_write ) { pinMode(dataPin, INPUT); //short circuit protection setAdress(clientAdress); pinMode(dataPin, OUTPUT); delayMicroseconds(1); } } else { // errorMessage = "run setupMux() after client declarations"; } } int MuxClient::read() { if (muxReady) { if (clientType == digital_read) { pinMode(dataPin, INPUT); setAdress(clientAdress); pinMode(dataPin, INPUT_PULLUP); delayMicroseconds(1); outputDigital = digitalRead(dataPin); return outputDigital; } else if (clientType == analog_read) { pinMode(dataPin, INPUT); setAdress(clientAdress); delayMicroseconds(1); outputAnalog = analogRead(dataPin); delayMicroseconds(1); return outputAnalog; } else { // errorMessage = "client type mismatch"; } } else { // errorMessage = "run setupMux() after client declarations"; } } int MuxClient::bufferedRead(int bufferSize, int scale) { int returnValue = 0; bufferValue[counter++ % bufferSize] = read(); for (int i=0; i