Skip to content

Commit

Permalink
cleaned up
Browse files Browse the repository at this point in the history
  • Loading branch information
tttttx2 committed Apr 5, 2021
1 parent 0d3c60e commit ce62e6d
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 108 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)

104 changes: 0 additions & 104 deletions ddcvcp/ddcvcp.ino

This file was deleted.

22 changes: 21 additions & 1 deletion examples/setBrightness/setBrightness.ino
Original file line number Diff line number Diff line change
@@ -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);
}
10 changes: 8 additions & 2 deletions keywords.txt
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
99 changes: 98 additions & 1 deletion src/DDCVCP.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
31 changes: 31 additions & 0 deletions src/DDCVCP.h
Original file line number Diff line number Diff line change
@@ -1 +1,32 @@
/*
DDCVCP.h - Library for controlling monitors over DDC/CI.
Created by Toni Tanner, 2021.
*/

#ifndef DDCVCP_h
#define DDCVCP_h

#include <Wire.h>
#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


0 comments on commit ce62e6d

Please sign in to comment.