diff --git a/README.md b/README.md new file mode 100644 index 0000000..b3b0e45 --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ +# DDC/CI VCP library +This is an Arduino library for controlling external computer monitors over the VESA DDC/CI standard. This standard is present on many different types of connectors but depends heavily on the actual manufacturer to implement it properly. Check if your setup is actually compatible with this. + +# Examples +Some simple examples can be found in the examples directory. + +# Functionality +- setting and reading brightness +- setting and reading power status +- setting and reading input source +- custom VCP commands + +More Functionality can easily be added. See [ddcutil](https://www.ddcutil.com/vcpinfo_output/) for some further reference on VPC codes. + +# Connector +The Arduino is to be connected to VGA, HDMI or DVI using the corresponding pins for DDC. This is essentially an I2C connection and uses the standard arduino Wire library so use the appropriate pins for I2C on your microcontroller. + +Disclaimer: I only tested this with a VGA connection, however DVI and HDMI should work as well. Pay attention to using the correct voltages though! + +## VGA +- Pin 15: DDC Clock (SCL) +- Pin 12: DDC Data (SDA) + +## DVI +- Pin 6: DDC clock (SCL) +- Pin 7: DDC Data (SDA) + +## HDMI +- Pin 15: DDC Clock (SCL) +- Pin 16: DDC Data (SDA) + diff --git a/ddcvcp/ddcvcp.ino b/ddcvcp/ddcvcp.ino deleted file mode 100644 index 4ba06ff..0000000 --- a/ddcvcp/ddcvcp.ino +++ /dev/null @@ -1,104 +0,0 @@ -#include -void setup() { - - Wire.begin(); // join i2c bus (address optional for master) - - Serial.begin(9600); // start serial communication at 9600bps - delay(2000); -} - -byte i2c_port = 0x37; //0x6E>>1 - - -// op: VCP opcode. Can be found with ddc capabilities -// value: 16bit value -void setVCP(byte op, uint16_t value) { - Wire.beginTransmission(i2c_port); - Wire.write(0x51); - Wire.write(0x84); - Wire.write(0x03); - Wire.write(op); - Wire.write(byte(value >> 8)); - Wire.write(byte(value)); - Wire.write((i2c_port << 1) ^ 0x51 ^ 0x84 ^ 0x03 ^ op ^ byte(value >> 8)^byte(value)); //XOR checksum. Include all bytes sent, including slave address! - Wire.endTransmission(); -} - -uint16_t getVCP(byte op) { - Wire.beginTransmission(i2c_port); - Wire.write(0x51); - Wire.write(0x82); - Wire.write(0x01); - Wire.write(op); - Wire.write((i2c_port << 1) ^ 0x51 ^ 0x82 ^ 0x01 ^ op); //XOR checksum. Include all bytes sent, including slave address! - Wire.endTransmission(); - delay(40); - Wire.requestFrom(i2c_port, 12); - byte response[12]; - int i = 0; - while (Wire.available()) // slave may send less than requested - { - byte c = Wire.read(); // receive a byte as character - response[i] = c; - i++; - //Serial.printf("%x ",c); // print the character - } - return (response[8] << 8) + response[9]; -} - -// 0-100 -void setBrightness(uint16_t value) { - if (value > 100) { - value = 100; - } - setVCP(0x10, value); -} - -uint16_t getBrightness() { - return getVCP(0x10); -} - - -// 0x01:VGA, 0x03:DVI, 0x0f:DP -void setSource(uint16_t value) { - setVCP(0x60, value); -} - -uint16_t getSource() { - return getVCP(0x60); -} - - -// true:on, false:suspend -void setPower(bool value) { - if (value) { - setVCP(0xD6, 0x01); - } - else { - setVCP(0xD6, 0x03); - } -} - -bool getPower(){ - byte power = getVCP(0xD6); - if (power==0x01){ - return true; - } - return false; -} - -void loop() { - Serial.println("\n\nsending command"); // print the reading - //setBrightness(66); - setPower(true); - delay(5000); - setSource(0x0f); - delay(5000); - setPower(true); - //delay(2000); - //setBrightness(68); - //delay(1000); - //Serial.println(getSource()); - delay(10000000000000); - -} diff --git a/examples/setBrightness/setBrightness.ino b/examples/setBrightness/setBrightness.ino index 8d1c8b6..d1b369c 100644 --- a/examples/setBrightness/setBrightness.ino +++ b/examples/setBrightness/setBrightness.ino @@ -1 +1,21 @@ - +#include "DDCVCP.h" + +DDCVCP ddc; + +void setup() { + Serial.begin(9600); + while (!ddc.begin()) { + Serial.print(F("- Unable to find DDC/CI. Trying again in 5 seconds.\n")); + delay(5000); + } + Serial.print(F("- Found DDC/CI successfully.\n")); +} + +void loop() { + ddc.setBrightness(10); + delay(3000); + ddc.setBrightness(50); + delay(3000); + ddc.setBrightness(100); + delay(3000); +} diff --git a/keywords.txt b/keywords.txt index 13e3d8a..02a8aba 100644 --- a/keywords.txt +++ b/keywords.txt @@ -1,12 +1,18 @@ # Syntax Coloring Map For ExampleLibrary # Datatypes (KEYWORD1) +DDCVCP KEYWORD1 # Methods and Functions (KEYWORD2) -setBrightness KEYWORD2 -getBrightness KEYWORD2 setPower KEYWORD2 getPower KEYWORD2 +begin KEYWORD2 +setBrightness KEYWORD2 +getBrightness KEYWORD2 +setVCP KEYWORD2 +getVCP KEYWORD2 +setSource KEYWORD2 +getSource KEYWORD2 # Instances (KEYWORD2) diff --git a/src/DDCVCP.cpp b/src/DDCVCP.cpp index 8d1c8b6..6948cec 100644 --- a/src/DDCVCP.cpp +++ b/src/DDCVCP.cpp @@ -1 +1,98 @@ - +/* + DDCVCP.cpp - Library for controlling monitors over DDC/CI. + Created by Toni Tanner, 2021. +*/ + +#include "Arduino.h" +#include "DDCVCP.h" + +DDCVCP::DDCVCP() +{ + +} + +bool DDCVCP::begin() { + Wire.begin(); + Wire.beginTransmission(_I2CAddress); + if (Wire.endTransmission() == 0) { + return true; + } + return false; +} + +// op: VCP opcode. Can be found with ddc capabilities +// value: 16bit value +void DDCVCP::setVCP(byte op, uint16_t value) { + Wire.beginTransmission(_I2CAddress); + Wire.write(0x51); + Wire.write(0x84); + Wire.write(0x03); + Wire.write(op); + Wire.write(byte(value >> 8)); + Wire.write(byte(value)); + Wire.write((_I2CAddress << 1) ^ 0x51 ^ 0x84 ^ 0x03 ^ op ^ byte(value >> 8)^byte(value)); //XOR checksum. Include all bytes sent, including slave address! + Wire.endTransmission(); +} + +uint16_t DDCVCP::getVCP(byte op) { + Wire.beginTransmission(_I2CAddress); + Wire.write(0x51); + Wire.write(0x82); + Wire.write(0x01); + Wire.write(op); + Wire.write((_I2CAddress << 1) ^ 0x51 ^ 0x82 ^ 0x01 ^ op); //XOR checksum. Include all bytes sent, including slave address! + Wire.endTransmission(); + delay(40); + Wire.requestFrom(_I2CAddress, 12); + byte response[12]; + int i = 0; + while (Wire.available()) // slave may send less than requested + { + byte c = Wire.read(); // receive a byte as character + response[i] = c; + i++; + //Serial.printf("%x ",c); // print the character + } + return (response[8] << 8) + response[9]; +} + +void DDCVCP::setBrightness(int value) +{ + if (value > 100) { + value = 100; + } + setVCP(0x10, value); +} + +uint16_t DDCVCP::getBrightness() +{ + return getVCP(0x10); +} + +// 0x01:VGA, 0x03:DVI, 0x0f:DP +void DDCVCP::setSource(uint16_t value) { + setVCP(0x60, value); +} + +uint16_t DDCVCP::getSource() { + return getVCP(0x60); +} + + +// true:on, false:suspend +void DDCVCP::setPower(bool value) { + if (value) { + setVCP(0xD6, 0x01); + } + else { + setVCP(0xD6, 0x03); + } +} + +bool DDCVCP::getPower(){ + byte power = getVCP(0xD6); + if (power==0x01){ + return true; + } + return false; +} diff --git a/src/DDCVCP.h b/src/DDCVCP.h index 8d1c8b6..6111acf 100644 --- a/src/DDCVCP.h +++ b/src/DDCVCP.h @@ -1 +1,32 @@ +/* + DDCVCP.h - Library for controlling monitors over DDC/CI. + Created by Toni Tanner, 2021. +*/ + +#ifndef DDCVCP_h +#define DDCVCP_h + +#include +#include "Arduino.h" + + +class DDCVCP +{ + public: + DDCVCP(); + bool begin(); + void setBrightness(int value); + uint16_t getBrightness(); + void setVCP(byte op, uint16_t value); + uint16_t getVCP(byte op); + void setSource(uint16_t value); + uint16_t getSource(); + void setPower(bool value); + bool getPower(); + private: + uint8_t _I2CAddress = 0x37; +}; + +#endif +