├── README.md ├── arduino_port_expander.h └── arduino_port_expander_sketch.ino /README.md: -------------------------------------------------------------------------------- 1 | # ESPHome-Arduino-Port-Expander 2 | An Arduino Port Expander for ESPHome with added Arduino Mega 2560 support 3 | See the Esphome page for documentation. 4 | -------------------------------------------------------------------------------- /arduino_port_expander.h: -------------------------------------------------------------------------------- 1 | // Must disable logging if using logging in main.cpp or in other custom components for the 2 | // __c causes a section type conflict with __c thingy 3 | // you can enable logging and use it if you enable this in logger: 4 | /* 5 | logger: 6 | level: DEBUG 7 | esp8266_store_log_strings_in_flash: False 8 | */ 9 | 10 | //#define APE_LOGGING 11 | 12 | // take advantage of LOG_ defines to decide which code to include 13 | #ifdef LOG_BINARY_OUTPUT 14 | #define APE_BINARY_OUTPUT 15 | #endif 16 | #ifdef LOG_BINARY_SENSOR 17 | #define APE_BINARY_SENSOR 18 | #endif 19 | #ifdef LOG_SENSOR 20 | #define APE_SENSOR 21 | #endif 22 | 23 | static const char *TAGape = "ape"; 24 | 25 | #define APE_CMD_DIGITAL_READ 0 26 | #define APE_CMD_WRITE_ANALOG 2 27 | #define APE_CMD_WRITE_DIGITAL_HIGH 3 28 | #define APE_CMD_WRITE_DIGITAL_LOW 4 29 | #define APE_CMD_SETUP_PIN_OUTPUT 5 30 | #define APE_CMD_SETUP_PIN_INPUT_PULLUP 6 31 | #define APE_CMD_SETUP_PIN_INPUT 7 32 | // 16 analog registers.. A0 to A15 33 | // A4 and A5 on Arduino Uno not supported due to I2C 34 | #define CMD_ANALOG_READ_A0 0b1000 // 0x8 = A0 35 | // .... 36 | #define CMD_ANALOG_READ_A15 10111 // 17 = A15 37 | 38 | #define CMD_SETUP_ANALOG_INTERNAL 0x10 39 | #define CMD_SETUP_ANALOG_DEFAULT 0x11 40 | 41 | #define get_ape(constructor) static_cast(constructor.get_component(0)) 42 | 43 | #define ape_binary_output(ape, pin) get_ape(ape)->get_binary_output(pin) 44 | #define ape_binary_sensor(ape, pin) get_ape(ape)->get_binary_sensor(pin) 45 | #define ape_analog_input(ape, pin) get_ape(ape)->get_analog_input(pin) 46 | 47 | class ArduinoPortExpander; 48 | 49 | using namespace esphome; 50 | 51 | #ifdef APE_BINARY_OUTPUT 52 | class ApeBinaryOutput : public output::BinaryOutput 53 | { 54 | public: 55 | ApeBinaryOutput(ArduinoPortExpander *parent, uint8_t pin) 56 | { 57 | this->parent_ = parent; 58 | this->pin_ = pin; 59 | } 60 | void write_state(bool state) override; 61 | uint8_t get_pin() { return this->pin_; } 62 | 63 | protected: 64 | ArduinoPortExpander *parent_; 65 | uint8_t pin_; 66 | // Pins are setup as output after the state is written, Arduino has no open drain outputs, after setting an output it will either sink or source thus activating outputs writen to false during a flick. 67 | bool setup_{true}; 68 | bool state_{false}; 69 | 70 | friend class ArduinoPortExpander; 71 | }; 72 | #endif 73 | 74 | #ifdef APE_BINARY_SENSOR 75 | class ApeBinarySensor : public binary_sensor::BinarySensor 76 | { 77 | public: 78 | ApeBinarySensor(ArduinoPortExpander *parent, uint8_t pin) 79 | { 80 | this->pin_ = pin; 81 | } 82 | uint8_t get_pin() { return this->pin_; } 83 | 84 | protected: 85 | uint8_t pin_; 86 | }; 87 | #endif 88 | 89 | #ifdef APE_SENSOR 90 | class ApeAnalogInput : public sensor::Sensor 91 | { 92 | public: 93 | ApeAnalogInput(ArduinoPortExpander *parent, uint8_t pin) 94 | { 95 | this->pin_ = pin; 96 | } 97 | uint8_t get_pin() { return this->pin_; } 98 | 99 | protected: 100 | uint8_t pin_; 101 | }; 102 | #endif 103 | 104 | class ArduinoPortExpander : public Component, public I2CDevice 105 | { 106 | public: 107 | ArduinoPortExpander(I2CBus *bus, uint8_t address, bool vref_default = false) 108 | { 109 | set_i2c_address(address); 110 | set_i2c_bus(bus); 111 | this->vref_default_ = vref_default; 112 | } 113 | void setup() override 114 | { 115 | #ifdef APE_LOGGING 116 | ESP_LOGCONFIG(TAGape, "Setting up ArduinoPortExpander at %#02x ...", address_); 117 | #endif 118 | 119 | /* We cannot setup as usual as arduino boots later than esp8266 120 | 121 | Poll i2c bus for our Arduino for a n seconds instead of failing fast, 122 | also this is important as pin setup (INPUT_PULLUP, OUTPUT it's done once) 123 | */ 124 | this->configure_timeout_ = millis() + 5000; 125 | } 126 | void loop() override 127 | { 128 | if (millis() < this->configure_timeout_) 129 | { 130 | bool try_configure = millis() % 100 > 50; 131 | if (try_configure == this->configure_) 132 | return; 133 | this->configure_ = try_configure; 134 | 135 | if (ERROR_OK == this->read_register(APE_CMD_DIGITAL_READ, const_cast(this->read_buffer_), 9)) //changed 3 to 9 136 | { 137 | #ifdef APE_LOGGING 138 | ESP_LOGCONFIG(TAGape, "ArduinoPortExpander found at %#02x", address_); 139 | #endif 140 | delay(10); 141 | if (this->vref_default_) 142 | { 143 | this->write_register(CMD_SETUP_ANALOG_DEFAULT, nullptr, 0); // 0: unused 144 | } 145 | 146 | // Config success 147 | this->configure_timeout_ = 0; 148 | this->status_clear_error(); 149 | #ifdef APE_BINARY_SENSOR 150 | for (ApeBinarySensor *pin : this->input_pins_) 151 | { 152 | App.feed_wdt(); 153 | uint8_t pinNo = pin->get_pin(); 154 | #ifdef APE_LOGGING 155 | ESP_LOGCONFIG(TAGape, "Setup input pin %d", pinNo); 156 | #endif 157 | this->write_register(APE_CMD_SETUP_PIN_INPUT_PULLUP, &pinNo, 1); 158 | delay(20); 159 | } 160 | #endif 161 | #ifdef APE_BINARY_OUTPUT 162 | for (ApeBinaryOutput *output : this->output_pins_) 163 | { 164 | if (!output->setup_) 165 | { // this output has a valid value already 166 | this->write_state(output->pin_, output->state_, true); 167 | App.feed_wdt(); 168 | delay(20); 169 | } 170 | } 171 | #endif 172 | #ifdef APE_SENSOR 173 | for (ApeAnalogInput *sensor : this->analog_pins_) 174 | { 175 | App.feed_wdt(); 176 | uint8_t pinNo = sensor->get_pin(); 177 | #ifdef APE_LOGGING 178 | ESP_LOGCONFIG(TAGape, "Setup analog input pin %d", pinNo); 179 | #endif 180 | this->write_register(APE_CMD_SETUP_PIN_INPUT, &pinNo, 1); 181 | delay(20); 182 | } 183 | #endif 184 | return; 185 | } 186 | // Still not answering 187 | return; 188 | } 189 | if (this->configure_timeout_ != 0 && millis() > this->configure_timeout_) 190 | { 191 | #ifdef APE_LOGGING 192 | ESP_LOGE(TAGape, "ArduinoPortExpander NOT found at %#02x", address_); 193 | #endif 194 | this->mark_failed(); 195 | return; 196 | } 197 | 198 | #ifdef APE_BINARY_SENSOR 199 | if (ERROR_OK != this->read_register(APE_CMD_DIGITAL_READ, const_cast(this->read_buffer_), 9)) //Changed this from 3 to 9 200 | { 201 | #ifdef APE_LOGGING 202 | ESP_LOGE(TAGape, "Error reading. Reconfiguring pending."); 203 | #endif 204 | this->status_set_error(); 205 | this->configure_timeout_ = millis() + 5000; 206 | return; 207 | } 208 | for (ApeBinarySensor *pin : this->input_pins_) 209 | { 210 | uint8_t pinNo = pin->get_pin(); 211 | 212 | uint8_t bit = pinNo % 8; 213 | 214 | uint8_t value = 0; 215 | if(pinNo < 8){ 216 | value = this->read_buffer_[0]; 217 | }else if(pinNo < 16){ 218 | value = this->read_buffer_[1]; 219 | }else if(pinNo < 24){ 220 | value = this->read_buffer_[2]; 221 | }else if(pinNo < 32){ 222 | value = this->read_buffer_[3]; 223 | }else if(pinNo < 40){ 224 | value = this->read_buffer_[4]; 225 | }else if(pinNo < 48){ 226 | value = this->read_buffer_[5]; 227 | }else if(pinNo < 56){ 228 | value = this->read_buffer_[6]; 229 | }else if(pinNo < 64){ 230 | value = this->read_buffer_[7]; 231 | }else{ 232 | value = this->read_buffer_[8]; 233 | } 234 | 235 | bool ret = value & (1 << bit); 236 | if (this->initial_state_) 237 | pin->publish_initial_state(ret); 238 | else 239 | pin->publish_state(ret); 240 | } 241 | #endif 242 | #ifdef APE_SENSOR 243 | for (ApeAnalogInput *pin : this->analog_pins_) 244 | { 245 | uint8_t pinNo = pin->get_pin(); 246 | pin->publish_state(analogRead(pinNo)); 247 | } 248 | #endif 249 | this->initial_state_ = false; 250 | } 251 | 252 | #ifdef APE_SENSOR 253 | uint16_t analogRead(uint8_t pin) 254 | { 255 | bool ok = (ERROR_OK == this->read_register((uint8_t)(CMD_ANALOG_READ_A0 + pin), const_cast(this->read_buffer_), 2)); 256 | #ifdef APE_LOGGING 257 | ESP_LOGVV(TAGape, "analog read pin: %d ok: %d byte0: %d byte1: %d", pin, ok, this->read_buffer_[0], this->read_buffer_[1]); 258 | #endif 259 | uint16_t value = this->read_buffer_[0] | ((uint16_t)this->read_buffer_[1] << 8); 260 | return value; 261 | } 262 | #endif 263 | 264 | #ifdef APE_BINARY_OUTPUT 265 | output::BinaryOutput *get_binary_output(uint8_t pin) 266 | { 267 | ApeBinaryOutput *output = new ApeBinaryOutput(this, pin); 268 | output_pins_.push_back(output); 269 | return output; 270 | } 271 | #endif 272 | #ifdef APE_BINARY_SENSOR 273 | binary_sensor::BinarySensor *get_binary_sensor(uint8_t pin) 274 | { 275 | ApeBinarySensor *binarySensor = new ApeBinarySensor(this, pin); 276 | input_pins_.push_back(binarySensor); 277 | return binarySensor; 278 | } 279 | #endif 280 | #ifdef APE_SENSOR 281 | sensor::Sensor *get_analog_input(uint8_t pin) 282 | { 283 | ApeAnalogInput *input = new ApeAnalogInput(this, pin); 284 | analog_pins_.push_back(input); 285 | return input; 286 | } 287 | #endif 288 | void write_state(uint8_t pin, bool state, bool setup = false) 289 | { 290 | if (this->configure_timeout_ != 0) 291 | return; 292 | #ifdef APE_LOGGING 293 | ESP_LOGD(TAGape, "Writing %d to pin %d", state, pin); 294 | #endif 295 | this->write_register(state ? APE_CMD_WRITE_DIGITAL_HIGH : APE_CMD_WRITE_DIGITAL_LOW, &pin, 1); 296 | if (setup) 297 | { 298 | App.feed_wdt(); 299 | delay(20); 300 | #ifdef APE_LOGGING 301 | ESP_LOGI(TAGape, "Setup output pin %d", pin); 302 | #endif 303 | this->write_register(APE_CMD_SETUP_PIN_OUTPUT, &pin, 1); 304 | } 305 | } 306 | 307 | protected: 308 | bool configure_{true}; 309 | bool initial_state_{true}; 310 | uint8_t read_buffer_[9]{0, 0, 0, 0, 0, 0, 0, 0, 0}; //changed from [3]{0, 0, 0} 311 | unsigned long configure_timeout_{5000}; 312 | bool vref_default_{false}; 313 | 314 | #ifdef APE_BINARY_OUTPUT 315 | std::vector output_pins_; 316 | #endif 317 | #ifdef APE_BINARY_SENSOR 318 | std::vector input_pins_; 319 | #endif 320 | #ifdef APE_SENSOR 321 | std::vector analog_pins_; 322 | #endif 323 | }; 324 | 325 | #ifdef APE_BINARY_OUTPUT 326 | void ApeBinaryOutput::write_state(bool state) 327 | { 328 | this->state_ = state; 329 | this->parent_->write_state(this->pin_, state, this->setup_); 330 | this->setup_ = false; 331 | } 332 | #endif -------------------------------------------------------------------------------- /arduino_port_expander_sketch.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Ports: 3 | 0 0 .. 13 13 .. 53 53 4 | Uno: A0: 14, A1: 15, A2: 16, A3: 17: A4: 18: A5: 19: A6: 20, A7: 21 5 | Mega: A0: 54, A1: 55, A2: 56, A3: 57: A4: 58: A5: 59: A6: 60, A7: 61... 6 | port bits: 5 ... 0..32 7 | 0: 0: 00000 8 | 1: 1: 00001 9 | A7: 21: 10101 10 | */ 11 | 12 | #include 13 | #include 14 | 15 | #define DEBUG // remove debug so pin 0 and 1 can be used for IO 16 | //#define DEBUG_READ // more advanced debuging 17 | 18 | #define I2C_ADDRESS 8 //i2c starts at pin 8 19 | 20 | void onRequest(); 21 | void onReceive(int); 22 | 23 | void setup() 24 | { 25 | #ifdef DEBUG 26 | Serial.begin(115200); 27 | Serial.println(F("Init ")); 28 | #endif 29 | 30 | #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) 31 | analogReference(INTERNAL1V1); 32 | #endif 33 | #if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) 34 | analogReference(INTERNAL); 35 | #endif 36 | 37 | Wire.begin(I2C_ADDRESS); 38 | Wire.onRequest(onRequest); 39 | Wire.onReceive(onReceive); 40 | 41 | #ifdef DEBUG 42 | Serial.println(F("Wire ok")); 43 | #endif 44 | } 45 | 46 | void loop() 47 | { 48 | //int temp = analogRead(A1); 49 | //Serial.println(temp); 50 | } 51 | 52 | volatile byte buffer[9]; 53 | volatile byte len = 1; 54 | 55 | #define DIGITAL_READ(b, pin, mask) \ 56 | if (digitalRead(pin)) \ 57 | buffer[b] |= mask; 58 | 59 | void readDigital() 60 | { 61 | len = 9; 62 | buffer[0] = 0; 63 | buffer[1] = 0; 64 | buffer[2] = 0; 65 | buffer[3] = 0; 66 | buffer[4] = 0; 67 | buffer[5] = 0; 68 | buffer[6] = 0; 69 | buffer[7] = 0; 70 | buffer[8] = 0; 71 | 72 | DIGITAL_READ(0, 0, 1); //0 73 | DIGITAL_READ(0, 1, 2); //1 74 | DIGITAL_READ(0, 2, 4); //2 75 | DIGITAL_READ(0, 3, 8); //3 76 | DIGITAL_READ(0, 4, 16); //4 77 | DIGITAL_READ(0, 5, 32); //5 78 | DIGITAL_READ(0, 6, 64); //6 79 | DIGITAL_READ(0, 7, 128); //7 80 | 81 | DIGITAL_READ(1, 8, 1); //8 82 | DIGITAL_READ(1, 9, 2); //9 83 | DIGITAL_READ(1, 10, 4); //10 84 | DIGITAL_READ(1, 11, 8); //11 85 | DIGITAL_READ(1, 12, 16); //12 86 | DIGITAL_READ(1, 13, 32); //13 87 | 88 | //Arduino Mega Boards 89 | #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) 90 | DIGITAL_READ(1, 14, 64); //14 91 | DIGITAL_READ(1, 15, 128); //15 92 | 93 | DIGITAL_READ(2, 16, 1); //16 94 | DIGITAL_READ(2, 17, 2); //17 95 | DIGITAL_READ(2, 18, 4); //18 96 | DIGITAL_READ(2, 19, 8); //19 97 | DIGITAL_READ(2, 19, 16); //20 Not avaliable on Arduino Mega, i2c pin. 98 | DIGITAL_READ(2, 19, 32); //21 Not avaliable on Arduino Mega, i2c pin. 99 | DIGITAL_READ(2, 22, 64); //22 100 | DIGITAL_READ(2, 23, 128); //23 101 | 102 | DIGITAL_READ(3, 24, 1); //24 103 | DIGITAL_READ(3, 25, 2); //25 104 | DIGITAL_READ(3, 26, 4); //26 105 | DIGITAL_READ(3, 27, 8); //27 106 | DIGITAL_READ(3, 28, 16); //28 107 | DIGITAL_READ(3, 29, 32); //29 108 | DIGITAL_READ(3, 30, 64); //30 109 | DIGITAL_READ(3, 31, 128); //31 110 | 111 | DIGITAL_READ(4, 32, 1); //32 112 | DIGITAL_READ(4, 33, 2); //33 113 | DIGITAL_READ(4, 34, 4); //34 114 | DIGITAL_READ(4, 35, 8); //35 115 | DIGITAL_READ(4, 36, 16); //36 116 | DIGITAL_READ(4, 37, 32); //37 117 | DIGITAL_READ(4, 38, 64); //38 118 | DIGITAL_READ(4, 39, 128); //39 119 | 120 | DIGITAL_READ(5, 40, 1); //40 121 | DIGITAL_READ(5, 41, 2); //41 122 | DIGITAL_READ(5, 42, 4); //42 123 | DIGITAL_READ(5, 43, 8); //43 124 | DIGITAL_READ(5, 44, 16); //44 125 | DIGITAL_READ(5, 45, 32); //45 126 | DIGITAL_READ(5, 46, 64); //46 127 | DIGITAL_READ(5, 47, 128); //47 128 | 129 | DIGITAL_READ(6, 48, 1); //48 130 | DIGITAL_READ(6, 49, 2); //49 131 | DIGITAL_READ(6, 50, 4); //50 132 | DIGITAL_READ(6, 51, 8); //51 133 | DIGITAL_READ(6, 52, 16); //52 134 | DIGITAL_READ(6, 53, 32); //53 135 | DIGITAL_READ(6, A0, 64); //54 136 | DIGITAL_READ(6, A1, 128); //55 137 | 138 | DIGITAL_READ(7, A2, 1); //56 139 | DIGITAL_READ(7, A3, 2); //57 140 | DIGITAL_READ(7, A4, 4); //58 141 | DIGITAL_READ(7, A5, 8); //59 142 | DIGITAL_READ(7, A6, 16); //60 143 | DIGITAL_READ(7, A7, 32); //61 144 | DIGITAL_READ(7, A8, 64); //62 145 | DIGITAL_READ(7, A9, 128); //63 146 | 147 | DIGITAL_READ(8, A10, 1); //64 148 | DIGITAL_READ(8, A11, 2); //65 149 | DIGITAL_READ(8, A12, 4); //66 150 | DIGITAL_READ(8, A13, 8); //67 151 | DIGITAL_READ(8, A14, 16); //68 152 | DIGITAL_READ(8, A15, 32); //69 153 | #endif 154 | 155 | // Arduino Uno Boards 156 | #if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) 157 | DIGITAL_READ(1, A0, 64); //14 158 | DIGITAL_READ(1, A1, 128); //15 159 | 160 | DIGITAL_READ(2, A2, 1); //16 161 | DIGITAL_READ(2, A3, 2); //17 162 | //DIGITAL_READ(2, A4, 4); //Not avaliable on Arduino Uno, i2c pin. 163 | //DIGITAL_READ(2, A5, 8); //Not avaliable on Arduino Uno, i2c pin. 164 | DIGITAL_READ(2, A6, 16); //20 165 | DIGITAL_READ(2, A7, 32); //21 166 | #endif 167 | 168 | // DIGITAL READ not supports on A3 .. A7 on Arduino Uno 169 | #ifdef DEBUG_READ 170 | Serial.print(F("Read 9 bytes: ")); 171 | Serial.print(buffer[0]); 172 | Serial.print(' '); 173 | Serial.print(buffer[1]); 174 | Serial.print(' '); 175 | Serial.println(buffer[2]); 176 | Serial.print(' '); 177 | Serial.println(buffer[3]); 178 | Serial.print(' '); 179 | Serial.println(buffer[4]); 180 | Serial.print(' '); 181 | Serial.println(buffer[5]); 182 | Serial.print(' '); 183 | Serial.println(buffer[6]); 184 | Serial.print(' '); 185 | Serial.println(buffer[7]); 186 | Serial.print(' '); 187 | Serial.println(buffer[8]); 188 | #endif 189 | } 190 | void readAnalog(int pin) 191 | { 192 | int val = analogRead(A0 + pin); 193 | len = 2; 194 | buffer[0] = val & 0xFF; 195 | buffer[1] = (val >> 8) & 0b11; 196 | #ifdef DEBUG_READ 197 | Serial.print(F("Read analog pin ")); 198 | Serial.println(pin); 199 | #endif 200 | } 201 | 202 | void onRequest() 203 | { 204 | Wire.write(const_cast(buffer), len); 205 | } 206 | 207 | #define CMD_DIGITAL_READ 0x0 208 | 209 | #define CMD_WRITE_ANALOG 0x2 210 | #define CMD_WRITE_DIGITAL_HIGH 0x3 211 | #define CMD_WRITE_DIGITAL_LOW 0x4 212 | 213 | #define CMD_SETUP_PIN_OUTPUT 0x5 214 | #define CMD_SETUP_PIN_INPUT_PULLUP 0x6 215 | #define CMD_SETUP_PIN_INPUT 0x7 216 | 217 | // 8 analog registers.. A0 to A14 218 | // A4 and A5 on Arduino Uno are not supported due to I2C 219 | #define CMD_ANALOG_READ_A0 0b1000 // 0x8 220 | // .... 221 | #define CMD_ANALOG_READ_A14 0b10000 //16 222 | 223 | #define CMD_SETUP_ANALOG_INTERNAL 0x10 224 | #define CMD_SETUP_ANALOG_DEFAULT 0x12 225 | 226 | void onReceive(int numBytes) 227 | { 228 | #ifdef DEBUG_READ 229 | Serial.print("Received bytes: "); 230 | Serial.println(numBytes); 231 | #endif 232 | int cmd = Wire.read(); 233 | 234 | switch (cmd) 235 | { 236 | case CMD_DIGITAL_READ: 237 | readDigital(); 238 | break; 239 | } 240 | 241 | if (cmd >= CMD_ANALOG_READ_A0 && cmd <= CMD_ANALOG_READ_A14) 242 | { 243 | readAnalog(cmd & 0b1110); 244 | return; 245 | } 246 | 247 | int pin = Wire.read(); 248 | 249 | switch (cmd) 250 | { 251 | case CMD_WRITE_DIGITAL_HIGH: 252 | case CMD_WRITE_DIGITAL_LOW: 253 | { 254 | bool output = cmd == CMD_WRITE_DIGITAL_HIGH; 255 | digitalWrite(pin, output); 256 | #ifdef DEBUG 257 | Serial.print(F("Pin ")); 258 | Serial.print(pin); 259 | Serial.println(output ? F(" HIGH") : F(" LOW")); 260 | #endif 261 | break; 262 | } 263 | case CMD_WRITE_ANALOG: 264 | { 265 | int val = Wire.read() & (Wire.read() << 8); 266 | analogWrite(pin, val); 267 | #ifdef DEBUG 268 | Serial.print(F("Pin ")); 269 | Serial.print(pin); 270 | Serial.print(F(" Analog write ")); 271 | Serial.println(val); 272 | #endif 273 | break; 274 | } 275 | case CMD_SETUP_PIN_OUTPUT: 276 | pinMode(pin, OUTPUT); 277 | #ifdef DEBUG 278 | Serial.print(F("Pin ")); 279 | Serial.print(pin); 280 | Serial.println(F(" OUTPUT")); 281 | #endif 282 | break; 283 | case CMD_SETUP_PIN_INPUT: 284 | pinMode(pin, INPUT); 285 | #ifdef DEBUG 286 | Serial.print(F("Pin ")); 287 | Serial.print(pin); 288 | Serial.println(F("INPUT")); 289 | #endif 290 | break; 291 | case CMD_SETUP_PIN_INPUT_PULLUP: 292 | pinMode(pin, INPUT_PULLUP); 293 | #ifdef DEBUG 294 | Serial.print(F("Pin ")); 295 | Serial.print(pin); 296 | Serial.println(F("INPUT PULLUP")); 297 | #endif 298 | break; 299 | case CMD_SETUP_ANALOG_INTERNAL: 300 | 301 | #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) 302 | analogReference(INTERNAL1V1); 303 | #endif 304 | #if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) 305 | analogReference(INTERNAL); 306 | #endif 307 | #ifdef DEBUG 308 | Serial.println(F("Analog reference INTERNAL")); 309 | #endif 310 | break; 311 | case CMD_SETUP_ANALOG_DEFAULT: 312 | analogReference(DEFAULT); 313 | #ifdef DEBUG 314 | Serial.println(F("Analog reference DEFAULT")); 315 | #endif 316 | break; 317 | } 318 | } 319 | --------------------------------------------------------------------------------