annotate Scan/ISSILed/led_scan.c @ 384:82ce1988fefe

Adding basic ISSI led brightness control capabilities - 6 modes * Single led: decrease, increase, set * All leds: decrease, increase, set - Currently update speed limited to once every 30 ms * Likely an I2C driver or ISSI limitation preventing even faster updates
author Jacob Alexander <haata@kiibohd.com>
date Fri, 09 Oct 2015 22:28:31 -0700
parents 86b937945313
children a3825c7fc651
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
1 /* Copyright (C) 2014-2015 by Jacob Alexander
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
2 *
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
3 * This file is free software: you can redistribute it and/or modify
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
4 * it under the terms of the GNU General Public License as published by
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
5 * the Free Software Foundation, either version 3 of the License, or
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
6 * (at your option) any later version.
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
7 *
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
8 * This file is distributed in the hope that it will be useful,
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
11 * GNU General Public License for more details.
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
12 *
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
13 * You should have received a copy of the GNU General Public License
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
14 * along with this file. If not, see <http://www.gnu.org/licenses/>.
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
15 */
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
16
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
17 // ----- Includes -----
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
18
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
19 // Compiler Includes
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
20 #include <Lib/ScanLib.h>
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
21
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
22 // Project Includes
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
23 #include <cli.h>
357
86b937945313 Fixing CMake dependency checking for kll_defs.h
Jacob Alexander <haata@kiibohd.com>
parents: 356
diff changeset
24 #include <kll_defs.h>
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
25 #include <led.h>
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
26 #include <print.h>
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
27
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
28 // Local Includes
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
29 #include "led_scan.h"
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
30
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
31
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
32
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
33 // ----- Defines -----
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
34
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
35 #define I2C_TxBufferLength 300
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
36 #define I2C_RxBufferLength 8
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
37
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
38 #define LED_BufferLength 144
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
39
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
40
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
41 // ----- Structs -----
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
42
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
43 typedef struct I2C_Buffer {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
44 uint16_t head;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
45 uint16_t tail;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
46 uint8_t sequencePos;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
47 uint16_t size;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
48 uint8_t *buffer;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
49 } I2C_Buffer;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
50
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
51 typedef struct LED_Buffer {
384
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
52 uint8_t i2c_addr;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
53 uint8_t reg_addr;
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
54 uint8_t buffer[LED_BufferLength];
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
55 } LED_Buffer;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
56
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
57
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
58
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
59 // ----- Function Declarations -----
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
60
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
61 // CLI Functions
356
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
62 void cliFunc_i2cRecv ( char* args );
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
63 void cliFunc_i2cSend ( char* args );
384
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
64 void cliFunc_ledCtrl ( char* args );
356
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
65 void cliFunc_ledRPage( char* args );
331
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
66 void cliFunc_ledStart( char* args );
356
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
67 void cliFunc_ledTest ( char* args );
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
68 void cliFunc_ledWPage( char* args );
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
69 void cliFunc_ledZero ( char* args );
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
70
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
71 uint8_t I2C_TxBufferPop();
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
72 void I2C_BufferPush( uint8_t byte, I2C_Buffer *buffer );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
73 uint16_t I2C_BufferLen( I2C_Buffer *buffer );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
74 uint8_t I2C_Send( uint8_t *data, uint8_t sendLen, uint8_t recvLen );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
75
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
76
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
77
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
78 // ----- Variables -----
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
79
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
80 // Scan Module command dictionary
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
81 CLIDict_Entry( i2cRecv, "Send I2C sequence of bytes and expect a reply of 1 byte on the last sequence." NL "\t\tUse |'s to split sequences with a stop." );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
82 CLIDict_Entry( i2cSend, "Send I2C sequence of bytes. Use |'s to split sequences with a stop." );
384
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
83 CLIDict_Entry( ledCtrl, "Basic LED control. Args: <mode> <amount> [<index>]" );
356
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
84 CLIDict_Entry( ledRPage, "Read the given register page." );
331
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
85 CLIDict_Entry( ledStart, "Disable software shutdown." );
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
86 CLIDict_Entry( ledTest, "Test out the led pages." );
356
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
87 CLIDict_Entry( ledWPage, "Write to given register page starting at address. i.e. 0x2 0x24 0xF0 0x12" );
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
88 CLIDict_Entry( ledZero, "Zero out LED register pages (non-configuration)." );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
89
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
90 CLIDict_Def( ledCLIDict, "ISSI LED Module Commands" ) = {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
91 CLIDict_Item( i2cRecv ),
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
92 CLIDict_Item( i2cSend ),
384
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
93 CLIDict_Item( ledCtrl ),
356
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
94 CLIDict_Item( ledRPage ),
331
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
95 CLIDict_Item( ledStart ),
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
96 CLIDict_Item( ledTest ),
356
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
97 CLIDict_Item( ledWPage ),
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
98 CLIDict_Item( ledZero ),
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
99 { 0, 0, 0 } // Null entry for dictionary end
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
100 };
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
101
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
102
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
103
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
104 // Before sending the sequence, I2C_TxBuffer_CurLen is assigned and as each byte is sent, it is decremented
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
105 // Once I2C_TxBuffer_CurLen reaches zero, a STOP on the I2C bus is sent
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
106 volatile uint8_t I2C_TxBufferPtr[ I2C_TxBufferLength ];
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
107 volatile uint8_t I2C_RxBufferPtr[ I2C_TxBufferLength ];
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
108
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
109 volatile I2C_Buffer I2C_TxBuffer = { 0, 0, 0, I2C_TxBufferLength, (uint8_t*)I2C_TxBufferPtr };
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
110 volatile I2C_Buffer I2C_RxBuffer = { 0, 0, 0, I2C_RxBufferLength, (uint8_t*)I2C_RxBufferPtr };
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
111
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
112 LED_Buffer LED_pageBuffer;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
113
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
114 // A bit mask determining which LEDs are enabled in the ISSI chip
356
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
115 const uint8_t LED_ledEnableMask1[] = {
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
116 0xE8, // I2C address
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
117 0x00, // Starting register address
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
118 ISSILedMask1_define
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
119 };
341
66eccdd9ced5 Code cleanup
Jacob Alexander <haata@kiibohd.com>
parents: 331
diff changeset
120
356
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
121 // Default LED brightness
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
122 const uint8_t LED_defaultBrightness1[] = {
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
123 0xE8, // I2C address
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
124 0x24, // Starting register address
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
125 ISSILedBrightness1_define
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
126 };
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
127
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
128
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
129
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
130 // ----- Interrupt Functions -----
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
131
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
132 void i2c0_isr()
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
133 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
134 cli(); // Disable Interrupts
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
135
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
136 uint8_t status = I2C0_S; // Read I2C Bus status
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
137
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
138 // Master Mode Transmit
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
139 if ( I2C0_C1 & I2C_C1_TX )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
140 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
141 // Check current use of the I2C bus
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
142 // Currently sending data
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
143 if ( I2C_TxBuffer.sequencePos > 0 )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
144 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
145 // Make sure slave sent an ACK
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
146 if ( status & I2C_S_RXAK )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
147 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
148 // NACK Detected, disable interrupt
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
149 erro_print("I2C NAK detected...");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
150 I2C0_C1 = I2C_C1_IICEN;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
151
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
152 // Abort Tx Buffer
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
153 I2C_TxBuffer.head = 0;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
154 I2C_TxBuffer.tail = 0;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
155 I2C_TxBuffer.sequencePos = 0;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
156 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
157 else
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
158 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
159 // Transmit byte
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
160 I2C0_D = I2C_TxBufferPop();
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
161 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
162 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
163 // Receiving data
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
164 else if ( I2C_RxBuffer.sequencePos > 0 )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
165 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
166 // Master Receive, addr sent
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
167 if ( status & I2C_S_ARBL )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
168 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
169 // Arbitration Lost
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
170 erro_print("Arbitration lost...");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
171 // TODO Abort Rx
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
172
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
173 I2C0_C1 = I2C_C1_IICEN;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
174 I2C0_S = I2C_S_ARBL | I2C_S_IICIF; // Clear ARBL flag and interrupt
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
175 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
176 if ( status & I2C_S_RXAK )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
177 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
178 // Slave Address NACK Detected, disable interrupt
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
179 erro_print("Slave Address I2C NAK detected...");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
180 // TODO Abort Rx
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
181
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
182 I2C0_C1 = I2C_C1_IICEN;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
183 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
184 else
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
185 {
384
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
186 dbug_msg("Attempting to read byte - ");
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
187 printHex( I2C_RxBuffer.sequencePos );
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
188 print( NL );
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
189 I2C0_C1 = I2C_RxBuffer.sequencePos == 1
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
190 ? I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST | I2C_C1_TXAK // Single byte read
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
191 : I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST; // Multi-byte read
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
192 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
193 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
194 else
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
195 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
196 /*
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
197 dbug_msg("STOP - ");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
198 printHex( I2C_BufferLen( (I2C_Buffer*)&I2C_TxBuffer ) );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
199 print(NL);
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
200 */
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
201
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
202 // Delay around STOP to make sure it actually happens...
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
203 delayMicroseconds( 1 );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
204 I2C0_C1 = I2C_C1_IICEN; // Send STOP
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
205 delayMicroseconds( 7 );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
206
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
207 // If there is another sequence, start sending
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
208 if ( I2C_BufferLen( (I2C_Buffer*)&I2C_TxBuffer ) < I2C_TxBuffer.size )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
209 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
210 // Clear status flags
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
211 I2C0_S = I2C_S_IICIF | I2C_S_ARBL;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
212
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
213 // Wait...till the master dies
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
214 while ( I2C0_S & I2C_S_BUSY );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
215
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
216 // Enable I2C interrupt
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
217 I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST | I2C_C1_TX;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
218
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
219 // Transmit byte
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
220 I2C0_D = I2C_TxBufferPop();
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
221 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
222 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
223 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
224 // Master Mode Receive
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
225 else
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
226 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
227 // XXX Do we need to handle 2nd last byte?
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
228 //I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST | I2C_C1_TXAK; // No STOP, Rx, NAK on recv
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
229
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
230 // Last byte
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
231 if ( I2C_TxBuffer.sequencePos <= 1 )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
232 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
233 // Change to Tx mode
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
234 I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
235
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
236 // Grab last byte
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
237 I2C_BufferPush( I2C0_D, (I2C_Buffer*)&I2C_RxBuffer );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
238
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
239 delayMicroseconds( 1 ); // Should be enough time before issuing the stop
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
240 I2C0_C1 = I2C_C1_IICEN; // Send STOP
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
241 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
242 else
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
243 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
244 // Retrieve data
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
245 I2C_BufferPush( I2C0_D, (I2C_Buffer*)&I2C_RxBuffer );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
246 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
247 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
248
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
249 I2C0_S = I2C_S_IICIF; // Clear interrupt
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
250
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
251 sei(); // Re-enable Interrupts
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
252 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
253
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
254
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
255
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
256 // ----- Functions -----
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
257
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
258 inline void I2C_setup()
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
259 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
260 // Enable I2C internal clock
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
261 SIM_SCGC4 |= SIM_SCGC4_I2C0; // Bus 0
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
262
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
263 // External pull-up resistor
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
264 PORTB_PCR0 = PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(2);
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
265 PORTB_PCR1 = PORT_PCR_ODE | PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(2);
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
266
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
267 // SCL Frequency Divider
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
268 // 400kHz -> 120 (0x85) @ 48 MHz F_BUS
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
269 I2C0_F = 0x85;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
270 I2C0_FLT = 4;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
271 I2C0_C1 = I2C_C1_IICEN;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
272 I2C0_C2 = I2C_C2_HDRS; // High drive select
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
273
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
274 // Enable I2C Interrupt
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
275 NVIC_ENABLE_IRQ( IRQ_I2C0 );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
276 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
277
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
278 void LED_zeroPages( uint8_t startPage, uint8_t numPages, uint8_t startReg, uint8_t endReg )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
279 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
280 // Page Setup
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
281 uint8_t pageSetup[] = { 0xE8, 0xFD, 0x00 };
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
282
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
283 // Max length of a page + chip id + reg start
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
284 uint8_t fullPage[ 0xB4 + 2 ] = { 0 }; // Max size of page
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
285 fullPage[0] = 0xE8; // Set chip id
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
286 fullPage[1] = startReg; // Set start reg
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
287
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
288 // Iterate through given pages, zero'ing out the given register regions
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
289 for ( uint8_t page = startPage; page < startPage + numPages; page++ )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
290 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
291 // Set page
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
292 pageSetup[2] = page;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
293
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
294 // Setup page
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
295 while ( I2C_Send( pageSetup, sizeof( pageSetup ), 0 ) == 0 )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
296 delay(1);
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
297
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
298 // Zero out page
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
299 while ( I2C_Send( fullPage, endReg - startReg + 2, 0 ) == 0 )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
300 delay(1);
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
301 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
302 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
303
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
304 void LED_sendPage( uint8_t *buffer, uint8_t len, uint8_t page )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
305 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
306 // Page Setup
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
307 uint8_t pageSetup[] = { 0xE8, 0xFD, page };
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
308
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
309 // Setup page
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
310 while ( I2C_Send( pageSetup, sizeof( pageSetup ), 0 ) == 0 )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
311 delay(1);
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
312
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
313 // Write page to I2C Tx Buffer
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
314 while ( I2C_Send( buffer, len, 0 ) == 0 )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
315 delay(1);
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
316
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
317 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
318
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
319 void LED_writeReg( uint8_t reg, uint8_t val, uint8_t page )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
320 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
321 // Page Setup
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
322 uint8_t pageSetup[] = { 0xE8, 0xFD, page };
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
323
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
324 // Reg Write Setup
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
325 uint8_t writeData[] = { 0xE8, reg, val };
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
326
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
327 // Setup page
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
328 while ( I2C_Send( pageSetup, sizeof( pageSetup ), 0 ) == 0 )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
329 delay(1);
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
330
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
331 while ( I2C_Send( writeData, sizeof( writeData ), 0 ) == 0 )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
332 delay(1);
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
333 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
334
384
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
335 void LED_readPage( uint8_t len, uint8_t page )
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
336 {
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
337 // Software shutdown must be enabled to read registers
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
338 LED_writeReg( 0x0A, 0x00, 0x0B );
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
339
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
340 // Page Setup
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
341 uint8_t pageSetup[] = { 0xE8, 0xFD, page };
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
342
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
343 // Setup page
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
344 while ( I2C_Send( pageSetup, sizeof( pageSetup ), 0 ) == 0 )
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
345 delay(1);
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
346
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
347 // Register Setup
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
348 uint8_t regSetup[] = { 0xE8, 0x00 };
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
349
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
350 // Read each register in the page
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
351 for ( uint8_t reg = 0; reg < len; reg++ )
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
352 {
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
353 // Update register to read
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
354 regSetup[1] = reg;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
355
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
356 // Configure register
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
357 while ( I2C_Send( regSetup, sizeof( regSetup ), 0 ) == 0 )
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
358 delay(1);
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
359
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
360 // Register Read Command
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
361 uint8_t regReadCmd[] = { 0xE9 };
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
362
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
363 // Request single register byte
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
364 while ( I2C_Send( regReadCmd, sizeof( regReadCmd ), 1 ) == 0 )
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
365 delay(1);
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
366 dbug_print("NEXT");
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
367 }
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
368
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
369 // Disable software shutdown
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
370 LED_writeReg( 0x0A, 0x01, 0x0B );
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
371 }
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
372
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
373 // Setup
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
374 inline void LED_setup()
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
375 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
376 // Register Scan CLI dictionary
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
377 CLI_registerDictionary( ledCLIDict, ledCLIDictName );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
378
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
379 // Initialize I2C
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
380 I2C_setup();
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
381
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
382 // Zero out Frame Registers
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
383 // This needs to be done before disabling the hardware shutdown (or the leds will do undefined things)
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
384 LED_zeroPages( 0x0B, 1, 0x00, 0x0C ); // Control Registers
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
385
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
386 // Disable Hardware shutdown of ISSI chip (pull high)
341
66eccdd9ced5 Code cleanup
Jacob Alexander <haata@kiibohd.com>
parents: 331
diff changeset
387 GPIOB_PDDR |= (1<<16);
66eccdd9ced5 Code cleanup
Jacob Alexander <haata@kiibohd.com>
parents: 331
diff changeset
388 PORTB_PCR16 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
66eccdd9ced5 Code cleanup
Jacob Alexander <haata@kiibohd.com>
parents: 331
diff changeset
389 GPIOB_PSOR |= (1<<16);
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
390
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
391 // Clear LED Pages
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
392 LED_zeroPages( 0x00, 8, 0x00, 0xB4 ); // LED Registers
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
393
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
394 // Enable LEDs based upon mask
356
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
395 LED_sendPage( (uint8_t*)LED_ledEnableMask1, sizeof( LED_ledEnableMask1 ), 0 );
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
396
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
397 // Set default brightness
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
398 LED_sendPage( (uint8_t*)LED_defaultBrightness1, sizeof( LED_defaultBrightness1 ), 0 );
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
399
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
400 // Disable Software shutdown of ISSI chip
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
401 LED_writeReg( 0x0A, 0x01, 0x0B );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
402 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
403
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
404
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
405 inline uint8_t I2C_BufferCopy( uint8_t *data, uint8_t sendLen, uint8_t recvLen, I2C_Buffer *buffer )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
406 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
407 uint8_t reTurn = 0;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
408
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
409 // If sendLen is greater than buffer fail right away
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
410 if ( sendLen > buffer->size )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
411 return 0;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
412
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
413 // Calculate new tail to determine if buffer has enough space
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
414 // The first element specifies the expected number of bytes from the slave (+1)
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
415 // The second element in the new buffer is the length of the buffer sequence (+1)
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
416 uint16_t newTail = buffer->tail + sendLen + 2;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
417 if ( newTail >= buffer->size )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
418 newTail -= buffer->size;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
419
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
420 if ( I2C_BufferLen( buffer ) < sendLen + 2 )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
421 return 0;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
422
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
423 /*
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
424 print("|");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
425 printHex( sendLen + 2 );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
426 print("|");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
427 printHex( *tail );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
428 print("@");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
429 printHex( newTail );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
430 print("@");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
431 */
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
432
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
433 // If buffer is clean, return 1, otherwise 2
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
434 reTurn = buffer->head == buffer->tail ? 1 : 2;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
435
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
436 // Add to buffer, already know there is enough room (simplifies adding logic)
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
437 uint8_t bufferHeaderPos = 0;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
438 for ( uint16_t c = 0; c < sendLen; c++ )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
439 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
440 // Add data to buffer
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
441 switch ( bufferHeaderPos )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
442 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
443 case 0:
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
444 buffer->buffer[ buffer->tail ] = recvLen;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
445 bufferHeaderPos++;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
446 c--;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
447 break;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
448
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
449 case 1:
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
450 buffer->buffer[ buffer->tail ] = sendLen;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
451 bufferHeaderPos++;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
452 c--;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
453 break;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
454
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
455 default:
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
456 buffer->buffer[ buffer->tail ] = data[ c ];
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
457 break;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
458 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
459
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
460 // Check for wrap-around case
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
461 if ( buffer->tail + 1 >= buffer->size )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
462 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
463 buffer->tail = 0;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
464 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
465 // Normal case
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
466 else
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
467 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
468 buffer->tail++;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
469 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
470 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
471
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
472 return reTurn;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
473 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
474
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
475
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
476 inline uint16_t I2C_BufferLen( I2C_Buffer *buffer )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
477 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
478 // Tail >= Head
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
479 if ( buffer->tail >= buffer->head )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
480 return buffer->head + buffer->size - buffer->tail;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
481
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
482 // Head > Tail
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
483 return buffer->head - buffer->tail;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
484 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
485
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
486
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
487 void I2C_BufferPush( uint8_t byte, I2C_Buffer *buffer )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
488 {
331
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
489 dbug_msg("DATA: ");
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
490 printHex( byte );
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
491
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
492 // Make sure buffer isn't full
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
493 if ( buffer->tail + 1 == buffer->head || ( buffer->head > buffer->tail && buffer->tail + 1 - buffer->size == buffer->head ) )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
494 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
495 warn_msg("I2C_BufferPush failed, buffer full: ");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
496 printHex( byte );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
497 print( NL );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
498 return;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
499 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
500
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
501 // Check for wrap-around case
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
502 if ( buffer->tail + 1 >= buffer->size )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
503 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
504 buffer->tail = 0;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
505 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
506 // Normal case
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
507 else
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
508 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
509 buffer->tail++;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
510 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
511
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
512 // Add byte to buffer
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
513 buffer->buffer[ buffer->tail ] = byte;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
514 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
515
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
516
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
517 uint8_t I2C_TxBufferPop()
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
518 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
519 // Return 0xFF if no buffer left (do not rely on this)
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
520 if ( I2C_BufferLen( (I2C_Buffer*)&I2C_TxBuffer ) >= I2C_TxBuffer.size )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
521 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
522 erro_msg("No buffer to pop an entry from... ");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
523 printHex( I2C_TxBuffer.head );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
524 print(" ");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
525 printHex( I2C_TxBuffer.tail );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
526 print(" ");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
527 printHex( I2C_TxBuffer.sequencePos );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
528 print(NL);
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
529 return 0xFF;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
530 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
531
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
532 // If there is currently no sequence being sent, the first entry in the RingBuffer is the length
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
533 if ( I2C_TxBuffer.sequencePos == 0 )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
534 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
535 I2C_TxBuffer.sequencePos = 0xFF; // So this doesn't become an infinite loop
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
536 I2C_RxBuffer.sequencePos = I2C_TxBufferPop();
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
537 I2C_TxBuffer.sequencePos = I2C_TxBufferPop();
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
538 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
539
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
540 uint8_t data = I2C_TxBuffer.buffer[ I2C_TxBuffer.head ];
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
541
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
542 // Prune head
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
543 I2C_TxBuffer.head++;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
544
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
545 // Wrap-around case
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
546 if ( I2C_TxBuffer.head >= I2C_TxBuffer.size )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
547 I2C_TxBuffer.head = 0;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
548
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
549 // Decrement buffer sequence (until next stop will be sent)
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
550 I2C_TxBuffer.sequencePos--;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
551
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
552 /*
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
553 dbug_msg("Popping: ");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
554 printHex( data );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
555 print(" ");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
556 printHex( I2C_TxBuffer.head );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
557 print(" ");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
558 printHex( I2C_TxBuffer.tail );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
559 print(" ");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
560 printHex( I2C_TxBuffer.sequencePos );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
561 print(NL);
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
562 */
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
563 return data;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
564 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
565
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
566
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
567 uint8_t I2C_Send( uint8_t *data, uint8_t sendLen, uint8_t recvLen )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
568 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
569 // Check head and tail pointers
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
570 // If full, return 0
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
571 // If empty, start up I2C Master Tx
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
572 // If buffer is non-empty and non-full, just append to the buffer
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
573 switch ( I2C_BufferCopy( data, sendLen, recvLen, (I2C_Buffer*)&I2C_TxBuffer ) )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
574 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
575 // Not enough buffer space...
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
576 case 0:
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
577 /*
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
578 erro_msg("Not enough Tx buffer space... ");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
579 printHex( I2C_TxBuffer.head );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
580 print(":");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
581 printHex( I2C_TxBuffer.tail );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
582 print("+");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
583 printHex( sendLen );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
584 print("|");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
585 printHex( I2C_TxBuffer.size );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
586 print( NL );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
587 */
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
588 return 0;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
589
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
590 // Empty buffer, initialize I2C
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
591 case 1:
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
592 // Clear status flags
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
593 I2C0_S = I2C_S_IICIF | I2C_S_ARBL;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
594
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
595 // Check to see if we already have control of the bus
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
596 if ( I2C0_C1 & I2C_C1_MST )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
597 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
598 // Already the master (ah yeah), send a repeated start
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
599 I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_RSTA | I2C_C1_TX;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
600 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
601 // Otherwise, seize control
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
602 else
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
603 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
604 // Wait...till the master dies
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
605 while ( I2C0_S & I2C_S_BUSY );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
606
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
607 // Now we're the master (ah yisss), get ready to send stuffs
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
608 I2C0_C1 = I2C_C1_IICEN | I2C_C1_MST | I2C_C1_TX;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
609 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
610
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
611 // Enable I2C interrupt
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
612 I2C0_C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_MST | I2C_C1_TX;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
613
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
614 // Depending on what type of transfer, the first byte is configured for R or W
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
615 I2C0_D = I2C_TxBufferPop();
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
616
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
617 return 1;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
618 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
619
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
620 // Dirty buffer, I2C already initialized
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
621 return 2;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
622 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
623
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
624
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
625
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
626 // LED State processing loop
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
627 inline uint8_t LED_scan()
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
628 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
629
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
630 // I2C Busy
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
631 // S & I2C_S_BUSY
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
632 //I2C_S_BUSY
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
633
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
634 return 0;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
635 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
636
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
637
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
638
384
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
639 // ----- Capabilities -----
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
640
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
641 // Basic LED Control Capability
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
642 typedef enum LedControlMode {
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
643 // Single LED Modes
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
644 LedControlMode_brightness_decrease,
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
645 LedControlMode_brightness_increase,
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
646 LedControlMode_brightness_set,
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
647 // Set all LEDs (index argument not required)
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
648 LedControlMode_brightness_decrease_all,
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
649 LedControlMode_brightness_increase_all,
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
650 LedControlMode_brightness_set_all,
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
651 } LedControlMode;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
652
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
653 typedef struct LedControl {
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
654 LedControlMode mode; // XXX Make sure to adjust the .kll capability if this variable is larger than 8 bits
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
655 uint8_t amount;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
656 uint16_t index;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
657 } LedControl;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
658
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
659 uint8_t LED_control_timer = 0;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
660 void LED_control( LedControl *control )
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
661 {
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
662 // Only send if we've completed all other transactions
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
663 if ( I2C_TxBuffer.sequencePos > 0 )
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
664 return;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
665
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
666 // XXX
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
667 // ISSI Chip locks up if we spam updates too quickly (might be an I2C bug on this side too -HaaTa)
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
668 // Make sure we only send an update every 30 milliseconds at most
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
669 // It may be possible to optimize speed even further, but will likely require serious time with a logic analyzer
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
670
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
671 uint8_t currentTime = (uint8_t)systick_millis_count;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
672 int8_t compare = (int8_t)(currentTime - LED_control_timer) & 0x7F;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
673 if ( compare < 30 )
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
674 {
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
675 return;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
676 }
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
677 LED_control_timer = currentTime;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
678
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
679 // Configure based upon the given mode
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
680 // TODO Handle multiple issi chips per node
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
681 // TODO Perhaps do gamma adjustment?
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
682 switch ( control->mode )
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
683 {
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
684 case LedControlMode_brightness_decrease:
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
685 // Don't worry about rolling over, the cycle is quick
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
686 LED_pageBuffer.buffer[ control->index ] -= control->amount;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
687 break;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
688
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
689 case LedControlMode_brightness_increase:
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
690 // Don't worry about rolling over, the cycle is quick
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
691 LED_pageBuffer.buffer[ control->index ] += control->amount;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
692 break;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
693
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
694 case LedControlMode_brightness_set:
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
695 LED_pageBuffer.buffer[ control->index ] = control->amount;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
696 break;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
697
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
698 case LedControlMode_brightness_decrease_all:
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
699 for ( uint8_t channel = 0; channel < LED_BufferLength; channel++ )
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
700 {
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
701 // Don't worry about rolling over, the cycle is quick
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
702 LED_pageBuffer.buffer[ channel ] -= control->amount;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
703 }
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
704 break;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
705
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
706 case LedControlMode_brightness_increase_all:
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
707 for ( uint8_t channel = 0; channel < LED_BufferLength; channel++ )
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
708 {
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
709 // Don't worry about rolling over, the cycle is quick
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
710 LED_pageBuffer.buffer[ channel ] += control->amount;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
711 }
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
712 break;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
713
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
714 case LedControlMode_brightness_set_all:
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
715 for ( uint8_t channel = 0; channel < LED_BufferLength; channel++ )
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
716 {
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
717 LED_pageBuffer.buffer[ channel ] = control->amount;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
718 }
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
719 break;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
720 }
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
721
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
722 // Sync LED buffer with ISSI chip buffer
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
723 // TODO Support multiple frames
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
724 LED_pageBuffer.i2c_addr = 0xE8; // Chip 1
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
725 LED_pageBuffer.reg_addr = 0x24; // Brightness section
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
726 LED_sendPage( (uint8_t*)&LED_pageBuffer, sizeof( LED_Buffer ), 0 );
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
727 }
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
728
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
729 void LED_control_capability( uint8_t state, uint8_t stateType, uint8_t *args )
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
730 {
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
731 // Display capability name
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
732 if ( stateType == 0xFF && state == 0xFF )
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
733 {
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
734 print("LED_control_capability(mode,amount,index)");
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
735 return;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
736 }
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
737
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
738 // Only use capability on press
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
739 // TODO Analog
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
740 if ( stateType == 0x00 && state == 0x03 ) // Not on release
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
741 return;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
742
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
743 // Set the input structure
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
744 LedControl *control = (LedControl*)args;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
745
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
746 // TODO broadcast to rest of interconnect nodes if necessary
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
747 LED_control( control );
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
748 }
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
749
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
750
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
751
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
752 // ----- CLI Command Functions -----
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
753
384
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
754 // TODO Currently not working correctly
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
755 void cliFunc_i2cSend( char* args )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
756 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
757 char* curArgs;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
758 char* arg1Ptr;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
759 char* arg2Ptr = args;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
760
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
761 // Buffer used after interpretting the args, will be sent to I2C functions
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
762 // NOTE: Limited to 8 bytes currently (can be increased if necessary
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
763 #define i2cSend_BuffLenMax 8
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
764 uint8_t buffer[ i2cSend_BuffLenMax ];
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
765 uint8_t bufferLen = 0;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
766
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
767 // No \r\n by default after the command is entered
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
768 print( NL );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
769 info_msg("Sending: ");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
770
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
771 // Parse args until a \0 is found
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
772 while ( bufferLen < i2cSend_BuffLenMax )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
773 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
774 curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
775 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
776
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
777 // Stop processing args if no more are found
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
778 if ( *arg1Ptr == '\0' )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
779 break;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
780
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
781 // If | is found, end sequence and start new one
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
782 if ( *arg1Ptr == '|' )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
783 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
784 print("| ");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
785 I2C_Send( buffer, bufferLen, 0 );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
786 bufferLen = 0;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
787 continue;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
788 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
789
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
790 // Interpret the argument
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
791 buffer[ bufferLen++ ] = (uint8_t)numToInt( arg1Ptr );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
792
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
793 // Print out the arg
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
794 dPrint( arg1Ptr );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
795 print(" ");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
796 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
797
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
798 print( NL );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
799
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
800 I2C_Send( buffer, bufferLen, 0 );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
801 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
802
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
803 void cliFunc_i2cRecv( char* args )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
804 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
805 char* curArgs;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
806 char* arg1Ptr;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
807 char* arg2Ptr = args;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
808
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
809 // Buffer used after interpretting the args, will be sent to I2C functions
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
810 // NOTE: Limited to 8 bytes currently (can be increased if necessary
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
811 #define i2cSend_BuffLenMax 8
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
812 uint8_t buffer[ i2cSend_BuffLenMax ];
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
813 uint8_t bufferLen = 0;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
814
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
815 // No \r\n by default after the command is entered
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
816 print( NL );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
817 info_msg("Sending: ");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
818
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
819 // Parse args until a \0 is found
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
820 while ( bufferLen < i2cSend_BuffLenMax )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
821 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
822 curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
823 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
824
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
825 // Stop processing args if no more are found
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
826 if ( *arg1Ptr == '\0' )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
827 break;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
828
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
829 // If | is found, end sequence and start new one
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
830 if ( *arg1Ptr == '|' )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
831 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
832 print("| ");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
833 I2C_Send( buffer, bufferLen, 0 );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
834 bufferLen = 0;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
835 continue;
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
836 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
837
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
838 // Interpret the argument
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
839 buffer[ bufferLen++ ] = (uint8_t)numToInt( arg1Ptr );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
840
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
841 // Print out the arg
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
842 dPrint( arg1Ptr );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
843 print(" ");
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
844 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
845
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
846 print( NL );
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
847
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
848 I2C_Send( buffer, bufferLen, 1 ); // Only 1 byte is ever read at a time with the ISSI chip
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
849 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
850
384
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
851 // TODO Currently not working correctly
356
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
852 void cliFunc_ledRPage( char* args )
331
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
853 {
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
854 // Parse number from argument
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
855 // NOTE: Only first argument is used
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
856 char* arg1Ptr;
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
857 char* arg2Ptr;
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
858 CLI_argumentIsolation( args, &arg1Ptr, &arg2Ptr );
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
859
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
860 // Default to 0 if no argument is given
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
861 uint8_t page = 0;
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
862
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
863 if ( arg1Ptr[0] != '\0' )
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
864 {
384
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
865 page = (uint8_t)numToInt( arg1Ptr );
331
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
866 }
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
867
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
868 // No \r\n by default after the command is entered
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
869 print( NL );
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
870
384
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
871 LED_readPage( 0x1, page );
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
872 //LED_readPage( 0xB4, page );
331
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
873 }
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
874
356
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
875 void cliFunc_ledWPage( char* args )
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
876 {
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
877 char* curArgs;
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
878 char* arg1Ptr;
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
879 char* arg2Ptr = args;
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
880
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
881 // First process page and starting address
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
882 curArgs = arg2Ptr;
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
883 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
884
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
885 // Stop processing args if no more are found
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
886 if ( *arg1Ptr == '\0' )
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
887 return;
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
888 uint8_t page[] = { 0xE8, 0xFD, numToInt( arg1Ptr ) };
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
889
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
890 curArgs = arg2Ptr;
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
891 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
892
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
893 // Stop processing args if no more are found
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
894 if ( *arg1Ptr == '\0' )
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
895 return;
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
896 uint8_t data[] = { 0xE8, numToInt( arg1Ptr ), 0 };
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
897
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
898 // Set the register page
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
899 while ( I2C_Send( page, sizeof( page ), 0 ) == 0 )
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
900 delay(1);
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
901
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
902 // Process all args
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
903 for ( ;; )
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
904 {
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
905 curArgs = arg2Ptr;
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
906 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
907
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
908 // Stop processing args if no more are found
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
909 if ( *arg1Ptr == '\0' )
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
910 break;
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
911
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
912 data[2] = numToInt( arg1Ptr );
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
913
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
914 // Write register location and data to I2C
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
915 while ( I2C_Send( data, sizeof( data ), 0 ) == 0 )
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
916 delay(1);
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
917
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
918 // Increment address
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
919 data[1]++;
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
920 }
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
921 }
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
922
331
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
923 void cliFunc_ledStart( char* args )
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
924 {
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
925 print( NL ); // No \r\n by default after the command is entered
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
926 LED_zeroPages( 0x0B, 1, 0x00, 0x0C ); // Control Registers
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
927 //LED_zeroPages( 0x00, 8, 0x00, 0xB4 ); // LED Registers
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
928 LED_writeReg( 0x0A, 0x01, 0x0B );
356
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
929 LED_sendPage( (uint8_t*)LED_ledEnableMask1, sizeof( LED_ledEnableMask1 ), 0 );
331
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
930
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
931 }
9e31d92caf12 Initial STLcd code.
Jacob Alexander <haata@kiibohd.com>
parents: 330
diff changeset
932
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
933 void cliFunc_ledTest( char* args )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
934 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
935 print( NL ); // No \r\n by default after the command is entered
356
58d2c5bf95eb Adding ISSILed API calls and default brightness
Jacob Alexander <haata@kiibohd.com>
parents: 341
diff changeset
936 LED_sendPage( (uint8_t*)LED_defaultBrightness1, sizeof( LED_defaultBrightness1 ), 0 );
315
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
937 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
938
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
939 void cliFunc_ledZero( char* args )
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
940 {
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
941 print( NL ); // No \r\n by default after the command is entered
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
942 LED_zeroPages( 0x00, 8, 0x24, 0xB4 ); // Only PWMs
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
943 }
bcdc04cb8e2e Initial LED support for ISSI IS31FL3731C
Jacob Alexander <haata@kiibohd.com>
parents:
diff changeset
944
384
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
945 void cliFunc_ledCtrl( char* args )
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
946 {
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
947 char* curArgs;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
948 char* arg1Ptr;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
949 char* arg2Ptr = args;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
950 LedControl control;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
951
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
952 // First process mode
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
953 curArgs = arg2Ptr;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
954 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
955
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
956 // Stop processing args if no more are found
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
957 if ( *arg1Ptr == '\0' )
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
958 return;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
959 control.mode = numToInt( arg1Ptr );
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
960
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
961
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
962 // Next process amount
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
963 curArgs = arg2Ptr;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
964 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
965
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
966 // Stop processing args if no more are found
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
967 if ( *arg1Ptr == '\0' )
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
968 return;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
969 control.amount = numToInt( arg1Ptr );
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
970
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
971
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
972 // Finally process led index, if it exists
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
973 // Default to 0
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
974 curArgs = arg2Ptr;
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
975 CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
976 control.index = *arg1Ptr == '\0' ? 0 : numToInt( arg1Ptr );
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
977
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
978 // Process request
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
979 LED_control( &control );
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
980 }
82ce1988fefe Adding basic ISSI led brightness control capabilities
Jacob Alexander <haata@kiibohd.com>
parents: 357
diff changeset
981