# Alienware AW410K RGB Controller

This project provides control to the RGB lighting on an Alienware AW410K
keyboard based on ADC readings from an attached light dependent resistor using
a webpage served by a Raspberry Pi Pico 2. Each key on the keyboard is
individually configurable from the webpage and can be set to automatically
adjust brightness based off the ambient lighting.

![Keyboard lighting changing with light on a LDR](ambient.gif)

## Setup

### Hardware

You will need the following hardware to make the device:
- Raspberry Pi Pico 2
- USB extension cable
- light dependent resistor such as GL5528 (specific part number may vary)
- 10k ohm resistor (resistance value may vary)

You will need to cut the USB extension in half and connect the wires from the
female end to the Raspberry Pi Pico 2. The default configuration is to attach
the USB's green wire to pin 1/GP0 and USB's white wire to pin 2/GP1. You will
also need to connect the red to 5V VBUS/VSYS (since you'll be powering from the
USB host device, VBUS should be fine) and the black to any ground pin. Pin 38
is the closest and most convenient. While you can connect the Raspberry Pi Pico
to the host device using the micro USB port and a micro USB cable, since
you have already sacrificed half of the USB extension cable, you might as well
use the male half to create a standard USB-A connection.  If you wish to do so,
then simply connect the red and black wires of the male connector end to
VBUS (5V) and GND, respectively. For the data wires, you can solder them to the
two test points TP2 and TP3 on the back of the Raspberry Pi Pico 2. The white
cable goes to TP2 and the green cable to TP3.

![Back of Raspberry Pi Pico with USB connections to TP2 and TP3](back.jpg)

The LDR should then be connected to the ADC2 pin (pin 34/GP28) and 3.3V (pin 36)
or alternatively to the ADC_VREF (pin 35) and the regular resistor between ADC2
and any ground, including ADC_GND (pin 33). The results look something like
the following.

![Back of Raspberry Pi Pico with USB and LDR connected](front.jpg)

The individual wires on the USB can be fragile, so hot glue or other strain
relief is a good idea. There is also a [model for a 3D printed
enclosure](pico-usb-ldr.stl) that you can use to place the Pico inside with a
hole for the LDR to detect ambient light.

![3D printed enclosure with finished device](enclosure.jpg)

## Software

Flash the aw410k_rgb.uf2 file found from the latest
[release](https://git.kkozai.com/kenji/aw410k_rgb/releases) to the Raspberry Pi
Pico 2, and connect the keyboard to the female USB port and insert the male USB
connector of the device into your host device such as PC.

To load the UI for configuring the RGB lighting, open a browser to the page
at http://aw410k.usb, or if that doesn't load, to http://192.168.40.1. From the
webpage, you can click on any individual key that you want to configure and
change the color using the color selector or by manually inputting the RGB
color value into the text boxes. To finalize setting the color for the
selected key(s), click on the "Set Color" button.

![Interface for setting the color of individual keys](ui.jpg)

To save the lighting configuration to the Pico 2 so that it loads the next time
it is powered on, click on the "Save" button under the "Flash Memory" section.
If you make unsaved changes and want to reload the configuration from memory,
you can also click the "Load" button to reset to the last saved setting.

If the checkbox next to "Adaptive" is selected when setting the color, the key's
brightness will automatically adjust with the ambient lighting, as determined by
the ADC reading of the LDR. Leaving it unchecked will set the RGB color to be
constant regardless of the ambient light level.

The mute button has special behavior when set to adaptive mode. It will toggle
between the user-configured color and red each time that it is pressed as a
way to indicate whether system sounds are muted or not. The keyboard will
always boot in the "unmuted" setting, so if your system starts off muted,
the LED color on the keyboard and your system may not match.

## Licensing

This software is distributed under the [GNU General Public License version
3](LICENSE), with the exception of the libraries in the following section.

## Credits

The project uses code from the following sources:
- [OpenRGB](https://gitlab.com/CalcProgrammer1/OpenRGB/) for code related to
  Alienware's [RGB lighting protocol](https://gitlab.com/CalcProgrammer1/OpenRGB/-/tree/master/Controllers/AlienwareKeyboardController/AlienwareAW410KController)
  on the AW410K keyboard licensed under GNU-GPLv2
- [Pico-PIO-USB](https://github.com/sekigon-gonnoc/Pico-PIO-USB/) for templates
  used from the [host_hid_to_device_cdc](https://github.com/sekigon-gonnoc/Pico-PIO-USB/tree/control-keyboard-led/examples/host_hid_to_device_cdc)
  example released under the MIT license
- [TinyUSB](https://github.com/hathach/tinyusb) for templates used from the
  [net_lwip_webserver](https://github.com/hathach/tinyusb/tree/master/examples/device/net_lwip_webserver)
  example distributed under the MIT license
