ledcontroller-3bit/main.c

158 lines
4.7 KiB
C
Raw Normal View History

2021-11-30 10:01:28 +00:00
/**
2021-11-30 10:31:00 +00:00
* @file main.c
* ledcontroller-3bit
*
* This file is a fork of the LedController code. You will find
2021-11-30 10:49:26 +00:00
* the original copyright notices below.
2021-11-30 10:31:00 +00:00
*
2021-11-30 10:53:13 +00:00
* @copyright Copyright (C) 2013 andmarios - http://www.andmarios.com \n
2021-11-30 10:31:00 +00:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
2021-11-30 10:49:26 +00:00
* (at your option) any later version. \n
2021-11-30 10:31:00 +00:00
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2021-11-30 10:49:26 +00:00
* GNU General Public License for more details. \n
2021-11-30 10:31:00 +00:00
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
2021-11-30 10:53:13 +00:00
* @copyright Original Copyright Notice: \n
2021-11-30 10:31:00 +00:00
*
2021-11-30 10:53:13 +00:00
* LedController \n
2021-11-30 10:49:26 +00:00
* Copyright (C) 2011 jmrobles - http://robleshermoso.wordpress.com \n
2021-11-30 10:31:00 +00:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
2021-11-30 10:53:13 +00:00
* (at your option) any later version. \n
2021-11-30 10:31:00 +00:00
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2021-11-30 10:53:13 +00:00
* GNU General Public License for more details. \n
2021-11-30 10:31:00 +00:00
*
* You should have received a copy of the GNU General Public License
2021-11-30 10:53:13 +00:00
* along with this program. If not, see <http://www.gnu.org/licenses/>. \n
*/
#include <stdio.h>
#include <libusb.h>
#include <errno.h>
2019-04-01 08:10:35 +00:00
#include <string.h>
2021-11-30 10:49:26 +00:00
#define USB_VID 0x1294 /**< USB Vendor ID */
#define USB_PID 0x1320 /**< USB Product ID */
#define USB_EP 0x2 /**< USB EndPoint */
static struct libusb_device_handle *devh = NULL;
2021-11-30 10:01:28 +00:00
/**
2021-11-30 10:31:00 +00:00
* The device has a RGB LED, which has two states for each "sub-LED",
* on and off.
* The "sub-LEDs" are red, green and blue.
*
* Obviously there are 8 possible combinations for their state.
* It seems there aren't bits mapped to each sub-LED, but instead
* 3-bit values mapped to the 8 combinations.
*
* | R | G | B | Color | Value | Comment |
* | - | - | - | ------ | ----- | ------------------------------- |
* | 0 | 0 | 0 | off | 0x0 | |
* | 0 | 0 | 1 | blue | 0x1 | |
* | 1 | 0 | 0 | red | 0x2 | |
* | 0 | 1 | 0 | green | 0x3 | |
* | 0 | 1 | 1 | aqua | 0x4 | |
* | 1 | 0 | 1 | purple | 0x5 | |
* | 1 | 1 | 0 | yellow | 0x6 | more of a mustard - green color |
* | 1 | 1 | 1 | white | 0x7 | a very blue-ish white |
*
* @param argc POSIX number of parameters
* @param argv Array of parameters
2021-11-30 10:53:13 +00:00
* @return Error code (0 on success)
2021-11-30 10:31:00 +00:00
*/
2021-11-30 10:01:28 +00:00
int main(int argc, char **argv) {
if (argc != 2)
{
2021-11-30 10:49:26 +00:00
printf("Syntax: %s blue | red | green | aqua | purple | yellow | white | off\n", argv[0]);
2021-11-30 10:01:28 +00:00
return -1;
}
2021-11-30 10:49:26 +00:00
unsigned char color_code = 0;
if (strcmp(argv[1], "off") == 0)
2021-11-30 10:01:28 +00:00
{
2021-11-30 10:49:26 +00:00
color_code = 0;
}
else if (strcmp(argv[1], "blue") == 0)
{
color_code = 1;
2021-11-30 10:01:28 +00:00
}
else if (strcmp(argv[1], "red") == 0)
{
2021-11-30 10:49:26 +00:00
color_code = 2;
2021-11-30 10:01:28 +00:00
}
else if (strcmp(argv[1], "green") == 0)
{
2021-11-30 10:49:26 +00:00
color_code = 3;
2021-11-30 10:01:28 +00:00
}
else if (strcmp(argv[1], "aqua") == 0)
{
2021-11-30 10:49:26 +00:00
color_code = 4;
2021-11-30 10:01:28 +00:00
}
else if (strcmp(argv[1], "purple") == 0)
{
2021-11-30 10:49:26 +00:00
color_code = 5;
2021-11-30 10:01:28 +00:00
}
else if (strcmp(argv[1], "yellow") == 0)
{
2021-11-30 10:49:26 +00:00
color_code = 6;
2021-11-30 10:01:28 +00:00
}
else if (strcmp(argv[1], "white") == 0)
{
2021-11-30 10:49:26 +00:00
color_code = 7;
2021-11-30 10:01:28 +00:00
}
else
{
puts("Invalid color");
return -1;
2019-04-01 08:37:42 +00:00
}
2019-04-01 08:40:05 +00:00
libusb_init(NULL);
2021-11-30 10:49:26 +00:00
devh = libusb_open_device_with_vid_pid(NULL, USB_VID, USB_PID);
2021-11-30 10:01:28 +00:00
if (devh == NULL)
{
puts("Device not found");
return -1;
2019-04-01 08:37:42 +00:00
}
2021-11-30 10:01:28 +00:00
if (libusb_kernel_driver_active(devh, 0))
{
puts("Detach driver from kernel");
2021-11-30 10:49:26 +00:00
int ret = libusb_detach_kernel_driver(devh, 0);
2021-11-30 10:01:28 +00:00
if (ret < 0)
{
puts("Could not detach driver");
return -1;
}
2019-04-01 08:37:42 +00:00
}
2019-04-01 08:40:05 +00:00
char data[5];
2021-11-30 10:49:26 +00:00
data[0] = color_code;
2019-04-01 08:40:05 +00:00
data[1] = 0x4;
data[2] = 0x4;
data[3] = 0x4;
data[4] = 0x4;
libusb_claim_interface(devh, 0);
2021-11-30 10:49:26 +00:00
int ret = libusb_interrupt_transfer(devh, USB_EP, data, 5, NULL, 0);
2021-11-30 10:01:28 +00:00
if (ret < 0)
{
perror("libusb error");
return -1;
2019-04-01 08:37:42 +00:00
}
2019-04-01 08:40:05 +00:00
return 0;
2021-11-30 10:01:28 +00:00
}