/*
* mood.c
*
* Control an LED mood light via USB using V-USB.
*
* Created 6 Apr 2014 by J. Palermo
* (C) Copyright 2014 J. Palermo, GNU GPLv3
*
* Based on code from the following project:
*
* Project: AVR ATtiny USB Tutorial at http://codeandlife.com/
* Author: Joonas Pihlajamaa, joonas.pihlajamaa@iki.fi
* Based on V-USB example code by Christian Starkjohann
* Copyright: (C) 2012 by Joonas Pihlajamaa
* License: GNU GPL v3 (see License.txt)
*
* 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
* (at your option) any later version.
*
* 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
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
#include
#include
#include
#include // for cmd line switches
#include // from libusb
#include "usbhelper.h"
// Same as in uC code
#define RED 0
#define GREEN 1
#define BLUE 2
#define FADE 3
// used for helper function usbOpenDevice
#define VENDOR 0x16C0
#define VENDOR_NAME "example.com"
#define PRODUCT 0x05DC
#define PRODUCT_NAME "Mood Light"
/*********************************** main *************************************/
int main(int argc, char **argv) {
// usage
if(argc < 2) {
printf("\n");
printf("Usage:\n");
printf("Control Mode: \t%s -r [0:255] -g [0:255] -b [0:255]\n", argv[0]);
printf("Fade Mode: \t%s -f\n", argv[0]);
printf("\n");
exit(1);
}
int nBytes = 0; // usb_control_msg returns 0 on success, <0 on error
char buffer[256];
int arg; // for parsing cmd line arguements
int r_val = 0; // values passed to MCU
int g_val = 0;
int b_val = 0;
int rflag = 1; // flags to know which parameters
int gflag = 1; // to send to the MCU: rgb or fade
int bflag = 1;
int fflag = 0;
/* parse cmd line arguments
*
* There are two modes: contol mode and fade mode.
* In control mode, the rgb values are specified, and
* the light will stay at that one color.
*
* In fade mode, no colors are specified, and the
* light will cycle through colors.
*/
while ((arg = getopt(argc, argv, "r:g:b:f")) != -1){
switch (arg){
case 'r':
r_val = atoi(optarg);
if (r_val > 255){
r_val = 255;
}
break;
case 'g':
g_val = atoi(optarg);
if (g_val > 255){
g_val = 255;
}
break;
case 'b':
b_val = atoi(optarg);
if (b_val > 255){
b_val = 255;
}
break;
case 'f':
// clear the rgb flags
rflag = 0;
gflag = 0;
bflag = 0;
fflag = 1;
break;
} // end switch
} // end while
// open usb device
usb_dev_handle *handle = NULL;
handle = usbOpenDevice(VENDOR, VENDOR_NAME, PRODUCT, PRODUCT_NAME);
if(handle == NULL) {
fprintf(stderr, "\nCould not find USB device!\n");
exit(1);
}
// send control messages, one for each color
// red
if (rflag){
nBytes = usb_control_msg(
handle, // dev_handle
USB_TYPE_VENDOR | // bmRequest
USB_RECIP_DEVICE |
USB_ENDPOINT_IN,
RED, // bRequest
r_val, // wValue
0, // wIndex
(char *)buffer, // data
sizeof(buffer), // wLength
5000); // timeout
if(nBytes < 0)
fprintf(stderr, "USB error: %s\n", usb_strerror());
}
// green
if (gflag){
nBytes = usb_control_msg(
handle, // dev_handle
USB_TYPE_VENDOR | // bmRequest
USB_RECIP_DEVICE |
USB_ENDPOINT_IN,
GREEN, // bRequest
g_val, // wValue
0, // wIndex
(char *)buffer, // data
sizeof(buffer), // wLength
5000); // timeout
if(nBytes < 0)
fprintf(stderr, "USB error: %s\n", usb_strerror());
}
// blue
if (bflag){
nBytes = usb_control_msg(
handle, // dev_handle
USB_TYPE_VENDOR | // bmRequest
USB_RECIP_DEVICE |
USB_ENDPOINT_IN,
BLUE, // bRequest
b_val, // wValue
0, // wIndex
(char *)buffer, // data
sizeof(buffer), // wLength
5000); // timeout
if(nBytes < 0)
fprintf(stderr, "USB error: %s\n", usb_strerror());
}
// fade
if (fflag){
nBytes = usb_control_msg(
handle, // dev_handle
USB_TYPE_VENDOR | // bmRequest
USB_RECIP_DEVICE |
USB_ENDPOINT_IN,
FADE, // bRequest
0, // wValue
0, // wIndex
(char *)buffer, // data
sizeof(buffer), // wLength
5000); // timeout
if(nBytes < 0)
fprintf(stderr, "USB error: %s\n", usb_strerror());
}
// close handle and exit
usb_close(handle);
return 0;
} // end main