Add first
This commit is contained in:
commit
b8238d78d1
4 changed files with 167 additions and 0 deletions
11
simpleddc/setup.py
Normal file
11
simpleddc/setup.py
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
from setuptools import setup, Extension
|
||||
|
||||
module = Extension('simpleddc',
|
||||
sources=['simpleddc.c', 'simpleddc-python.c'],
|
||||
libraries=['ddcutil'],
|
||||
library_dirs=['/usr/lib/aarch64-linux-gnu/libddcutil.so.4'],
|
||||
include_dirs=['/usr/include'])
|
||||
|
||||
setup(name='example',
|
||||
version='1.0',
|
||||
ext_modules=[module])
|
||||
14
simpleddc/simple-ddc.h
Normal file
14
simpleddc/simple-ddc.h
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <ddcutil_c_api.h>
|
||||
#include <ddcutil_status_codes.h>
|
||||
|
||||
DDCA_Display_Handle * open_first_display_by_dlist();
|
||||
DDCA_Status switch_input(DDCA_Display_Handle* handle, uint8_t input);
|
||||
uint8_t show_any_value(
|
||||
DDCA_Display_Handle dh,
|
||||
DDCA_Vcp_Value_Type value_type,
|
||||
DDCA_Vcp_Feature_Code feature_code);
|
||||
39
simpleddc/simpleddc-python.c
Normal file
39
simpleddc/simpleddc-python.c
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
#include <Python.h>
|
||||
#include "simple-ddc.h"
|
||||
|
||||
static PyObject* switch_to_input(PyObject* self, PyObject* args) {
|
||||
uint8_t display, input;
|
||||
if (!PyArg_ParseTuple(args, "ii", &display, &input))
|
||||
return NULL;
|
||||
|
||||
DDCA_Display_Handle* handle = open_first_display_by_dlist();
|
||||
switch_input(handle,input);
|
||||
ddca_close_display(handle);
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static PyObject* show_input(PyObject* self, PyObject* args) {
|
||||
DDCA_Display_Handle* handle = open_first_display_by_dlist();
|
||||
int result = show_any_value(handle,DDCA_NON_TABLE_VCP_VALUE, 0x60);
|
||||
ddca_close_display(handle);
|
||||
return PyLong_FromLong(result);
|
||||
}
|
||||
|
||||
static PyMethodDef Methods[] = {
|
||||
{"switch_to_input", switch_to_input, METH_VARARGS, "Switch to input"},
|
||||
{"show_input", show_input, METH_VARARGS, "Show input"},
|
||||
{NULL, NULL, 0, NULL} /* Sentinel */
|
||||
};
|
||||
|
||||
static struct PyModuleDef module = {
|
||||
PyModuleDef_HEAD_INIT,
|
||||
"simpleddc",
|
||||
NULL, /* module documentation, may be NULL */
|
||||
-1, /* size of per-interpreter state of the module,
|
||||
or -1 if the module keeps state in global variables. */
|
||||
Methods
|
||||
};
|
||||
|
||||
PyMODINIT_FUNC PyInit_simpleddc(void) {
|
||||
return PyModule_Create(&module);
|
||||
}
|
||||
103
simpleddc/simpleddc.c
Normal file
103
simpleddc/simpleddc.c
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
#include "simple-ddc.h"
|
||||
|
||||
#define DDC_ERRMSG(function_name,status_code) \
|
||||
do { \
|
||||
printf("(%s) %s() returned %d (%s): %s\n", \
|
||||
__func__, function_name, status_code, \
|
||||
ddca_rc_name(status_code), \
|
||||
ddca_rc_desc(status_code)); \
|
||||
} while(0)
|
||||
|
||||
DDCA_Display_Handle * open_first_display_by_dlist() {
|
||||
printf("Check for monitors using ddca_get_displays()...\n");
|
||||
DDCA_Display_Handle dh = NULL;
|
||||
|
||||
// Inquire about detected monitors.
|
||||
DDCA_Display_Info_List* dlist = NULL;
|
||||
ddca_get_display_info_list2(
|
||||
false, // don't include invalid displays
|
||||
&dlist);
|
||||
|
||||
if (dlist->ct == 0) {
|
||||
printf(" No DDC capable displays found\n");
|
||||
}
|
||||
else {
|
||||
DDCA_Display_Info * dinf = &dlist->info[0];
|
||||
DDCA_Display_Ref * dref = dinf->dref;
|
||||
printf("Opening display %s\n", dinf->model_name);
|
||||
printf("Model: %s\n", dinf->model_name);
|
||||
//printf("Model: %s\n", dinf->mmid.model_name);
|
||||
DDCA_Status rc = ddca_open_display2(dref, false, &dh);
|
||||
if (rc != 0) {
|
||||
DDC_ERRMSG("ddca_open_display2", rc);
|
||||
}
|
||||
}
|
||||
ddca_free_display_info_list(dlist);
|
||||
return dh;
|
||||
}
|
||||
|
||||
uint8_t show_any_value(
|
||||
DDCA_Display_Handle dh,
|
||||
DDCA_Vcp_Value_Type value_type,
|
||||
DDCA_Vcp_Feature_Code feature_code)
|
||||
{
|
||||
DDCA_Status ddcrc;
|
||||
DDCA_Any_Vcp_Value * valrec;
|
||||
|
||||
ddcrc = ddca_get_any_vcp_value_using_explicit_type(
|
||||
dh,
|
||||
feature_code,
|
||||
value_type,
|
||||
&valrec);
|
||||
if (ddcrc != 0) {
|
||||
DDC_ERRMSG("ddca_get_any_vcp_value_using_explicit_type", ddcrc);
|
||||
goto bye;
|
||||
}
|
||||
|
||||
if (valrec->value_type == DDCA_NON_TABLE_VCP_VALUE) {
|
||||
/*printf("Non-Table value: mh=0x%02x, ml=0x%02x, sh=0x%02x, ml=0x%02x\n",
|
||||
valrec->val.c_nc.mh,
|
||||
valrec->val.c_nc.ml,
|
||||
valrec->val.c_nc.sh,
|
||||
valrec->val.c_nc.sl);
|
||||
printf("As continuous value (if applicable): max value = %d, cur value = %d\n",
|
||||
valrec->val.c_nc.mh << 8 | valrec->val.c_nc.ml, // or use macro VALREC_MAX_VAL()
|
||||
valrec->val.c_nc.sh << 8 | valrec->val.c_nc.sl); // or use macro VALREC_CUR_VAL()*/
|
||||
|
||||
uint8_t ret_value = valrec->val.c_nc.sl;
|
||||
free(valrec);
|
||||
return ret_value;
|
||||
}
|
||||
else {
|
||||
assert(valrec->value_type == DDCA_TABLE_VCP_VALUE);
|
||||
printf("Table value: 0x");
|
||||
for (int ndx=0; ndx<valrec->val.t.bytect; ndx++)
|
||||
printf("%02x", valrec->val.t.bytes[ndx]);
|
||||
puts("");
|
||||
}
|
||||
|
||||
bye:
|
||||
if (valrec != NULL)
|
||||
free(valrec);
|
||||
return 0;
|
||||
}
|
||||
|
||||
DDCA_Status switch_input(DDCA_Display_Handle* handle, uint8_t input) {
|
||||
bool saved_enable_verify = ddca_enable_verify(true);
|
||||
|
||||
DDCA_Status ddcrc = ddca_set_non_table_vcp_value(handle,0x60, 0, input);
|
||||
|
||||
if (ddcrc == DDCRC_VERIFY) {
|
||||
printf("Value verification failed. Current value is now:\n");
|
||||
show_any_value(handle, DDCA_NON_TABLE_VCP_VALUE, 0x60);
|
||||
}
|
||||
else if (ddcrc != 0) {
|
||||
DDC_ERRMSG("ddca_set_non_table_vcp_value", ddcrc);
|
||||
}
|
||||
else {
|
||||
printf("Setting new value succeeded.\n");
|
||||
}
|
||||
|
||||
ddca_enable_verify(saved_enable_verify);
|
||||
return ddcrc;
|
||||
}
|
||||
Loading…
Reference in a new issue