+// ----- Capabilities -----
+
+// Basic LED Control Capability
+typedef enum LedControlMode {
+ // Single LED Modes
+ LedControlMode_brightness_decrease,
+ LedControlMode_brightness_increase,
+ LedControlMode_brightness_set,
+ // Set all LEDs (index argument not required)
+ LedControlMode_brightness_decrease_all,
+ LedControlMode_brightness_increase_all,
+ LedControlMode_brightness_set_all,
+} LedControlMode;
+
+typedef struct LedControl {
+ LedControlMode mode; // XXX Make sure to adjust the .kll capability if this variable is larger than 8 bits
+ uint8_t amount;
+ uint16_t index;
+} LedControl;
+
+uint8_t LED_control_timer = 0;
+void LED_control( LedControl *control )
+{
+ // Only send if we've completed all other transactions
+ if ( I2C_TxBuffer.sequencePos > 0 )
+ return;
+
+ // XXX
+ // ISSI Chip locks up if we spam updates too quickly (might be an I2C bug on this side too -HaaTa)
+ // Make sure we only send an update every 30 milliseconds at most
+ // It may be possible to optimize speed even further, but will likely require serious time with a logic analyzer
+
+ uint8_t currentTime = (uint8_t)systick_millis_count;
+ int8_t compare = (int8_t)(currentTime - LED_control_timer) & 0x7F;
+ if ( compare < 30 )
+ {
+ return;
+ }
+ LED_control_timer = currentTime;
+
+ // Configure based upon the given mode
+ // TODO Handle multiple issi chips per node
+ // TODO Perhaps do gamma adjustment?
+ switch ( control->mode )
+ {
+ case LedControlMode_brightness_decrease:
+ // Don't worry about rolling over, the cycle is quick
+ LED_pageBuffer.buffer[ control->index ] -= control->amount;
+ break;
+
+ case LedControlMode_brightness_increase:
+ // Don't worry about rolling over, the cycle is quick
+ LED_pageBuffer.buffer[ control->index ] += control->amount;
+ break;
+
+ case LedControlMode_brightness_set:
+ LED_pageBuffer.buffer[ control->index ] = control->amount;
+ break;
+
+ case LedControlMode_brightness_decrease_all:
+ for ( uint8_t channel = 0; channel < LED_BufferLength; channel++ )
+ {
+ // Don't worry about rolling over, the cycle is quick
+ LED_pageBuffer.buffer[ channel ] -= control->amount;
+ }
+ break;
+
+ case LedControlMode_brightness_increase_all:
+ for ( uint8_t channel = 0; channel < LED_BufferLength; channel++ )
+ {
+ // Don't worry about rolling over, the cycle is quick
+ LED_pageBuffer.buffer[ channel ] += control->amount;
+ }
+ break;
+
+ case LedControlMode_brightness_set_all:
+ for ( uint8_t channel = 0; channel < LED_BufferLength; channel++ )
+ {
+ LED_pageBuffer.buffer[ channel ] = control->amount;
+ }
+ break;
+ }
+
+ // Sync LED buffer with ISSI chip buffer
+ // TODO Support multiple frames
+ LED_pageBuffer.i2c_addr = 0xE8; // Chip 1
+ LED_pageBuffer.reg_addr = 0x24; // Brightness section
+ LED_sendPage( (uint8_t*)&LED_pageBuffer, sizeof( LED_Buffer ), 0 );
+}
+
+void LED_control_capability( uint8_t state, uint8_t stateType, uint8_t *args )
+{
+ // Display capability name
+ if ( stateType == 0xFF && state == 0xFF )
+ {
+ print("LED_control_capability(mode,amount,index)");
+ return;
+ }
+
+ // Only use capability on press
+ // TODO Analog
+ if ( stateType == 0x00 && state == 0x03 ) // Not on release
+ return;
+
+ // Set the input structure
+ LedControl *control = (LedControl*)args;
+
+ // TODO broadcast to rest of interconnect nodes if necessary
+ LED_control( control );
+}
+
+
+