Merge remote-tracking branch 'upstream/develop' into 22927-turn-off-osl-on-nesting

This commit is contained in:
Krister Svanlund 2024-02-29 20:23:32 +01:00
commit 7349e41c95
3351 changed files with 53062 additions and 168792 deletions

View File

@ -337,24 +337,23 @@ define BUILD_TEST
endif
endef
define LIST_TEST
include $(BUILDDEFS_PATH)/testlist.mk
FOUND_TESTS := $$(patsubst ./tests/%,%,$$(TEST_LIST))
$$(info $$(FOUND_TESTS))
endef
define PARSE_TEST
TESTS :=
# list of possible targets, colon-delimited, to reassign to MAKE_TARGET and remove
TARGETS := :clean:
ifneq (,$$(findstring :$$(lastword $$(subst :, ,$$(RULE))):, $$(TARGETS)))
MAKE_TARGET := $$(lastword $$(subst :, ,$$(RULE)))
TEST_SUBPATH := $$(subst $$(eval) ,/,$$(wordlist 2, $$(words $$(subst :, ,$$(RULE))), _ $$(subst :, ,$$(RULE))))
else
MAKE_TARGET :=
TEST_SUBPATH := $$(subst :,/,$$(RULE))
endif
TEST_NAME := $$(firstword $$(subst :, ,$$(RULE)))
TEST_TARGET := $$(subst $$(TEST_NAME),,$$(subst $$(TEST_NAME):,,$$(RULE)))
include $(BUILDDEFS_PATH)/testlist.mk
ifeq ($$(RULE),all)
ifeq ($$(TEST_NAME),all)
MATCHED_TESTS := $$(TEST_LIST)
else
MATCHED_TESTS := $$(foreach TEST, $$(TEST_LIST),$$(if $$(findstring /$$(TEST_SUBPATH)/, $$(patsubst %,%/,$$(TEST))), $$(TEST),))
MATCHED_TESTS := $$(foreach TEST, $$(TEST_LIST),$$(if $$(findstring x$$(TEST_NAME)x, x$$(patsubst ./tests/%,%,$$(TEST)x)), $$(TEST),))
endif
$$(foreach TEST,$$(MATCHED_TESTS),$$(eval $$(call BUILD_TEST,$$(TEST),$$(MAKE_TARGET))))
$$(foreach TEST,$$(MATCHED_TESTS),$$(eval $$(call BUILD_TEST,$$(TEST),$$(TEST_TARGET))))
endef
@ -437,6 +436,10 @@ git-submodules: git-submodule
list-keyboards:
$(QMK_BIN) list-keyboards --no-resolve-defaults | tr '\n' ' '
.PHONY: list-tests
list-tests:
$(eval $(call LIST_TEST))
.PHONY: generate-keyboards-file
generate-keyboards-file:
$(QMK_BIN) list-keyboards --no-resolve-defaults

View File

@ -340,7 +340,7 @@ LED_MATRIX_DRIVER := snled27351
endif
LED_MATRIX_ENABLE ?= no
VALID_LED_MATRIX_TYPES := is31fl3218 is31fl3731 is31fl3733 is31fl3736 is31fl3737 is31fl3741 is31fl3742a is31fl3743a is31fl3745 is31fl3746a snled27351 custom
VALID_LED_MATRIX_TYPES := is31fl3218 is31fl3729 is31fl3731 is31fl3733 is31fl3736 is31fl3737 is31fl3741 is31fl3742a is31fl3743a is31fl3745 is31fl3746a snled27351 custom
ifeq ($(strip $(LED_MATRIX_ENABLE)), yes)
ifeq ($(filter $(LED_MATRIX_DRIVER),$(VALID_LED_MATRIX_TYPES)),)
@ -365,6 +365,12 @@ ifeq ($(strip $(LED_MATRIX_ENABLE)), yes)
SRC += is31fl3218-mono.c
endif
ifeq ($(strip $(LED_MATRIX_DRIVER)), is31fl3729)
I2C_DRIVER_REQUIRED = yes
COMMON_VPATH += $(DRIVER_PATH)/led/issi
SRC += is31fl3729-mono.c
endif
ifeq ($(strip $(LED_MATRIX_DRIVER)), is31fl3731)
I2C_DRIVER_REQUIRED = yes
COMMON_VPATH += $(DRIVER_PATH)/led/issi
@ -437,7 +443,7 @@ endif
RGB_MATRIX_ENABLE ?= no
VALID_RGB_MATRIX_TYPES := aw20216s is31fl3218 is31fl3731 is31fl3733 is31fl3736 is31fl3737 is31fl3741 is31fl3742a is31fl3743a is31fl3745 is31fl3746a snled27351 ws2812 custom
VALID_RGB_MATRIX_TYPES := aw20216s is31fl3218 is31fl3729 is31fl3731 is31fl3733 is31fl3736 is31fl3737 is31fl3741 is31fl3742a is31fl3743a is31fl3745 is31fl3746a snled27351 ws2812 custom
ifeq ($(strip $(RGB_MATRIX_ENABLE)), yes)
ifeq ($(filter $(RGB_MATRIX_DRIVER),$(VALID_RGB_MATRIX_TYPES)),)
$(call CATASTROPHIC_ERROR,Invalid RGB_MATRIX_DRIVER,RGB_MATRIX_DRIVER="$(RGB_MATRIX_DRIVER)" is not a valid matrix type)
@ -468,6 +474,12 @@ ifeq ($(strip $(RGB_MATRIX_ENABLE)), yes)
SRC += is31fl3218.c
endif
ifeq ($(strip $(RGB_MATRIX_DRIVER)), is31fl3729)
I2C_DRIVER_REQUIRED = yes
COMMON_VPATH += $(DRIVER_PATH)/led/issi
SRC += is31fl3729.c
endif
ifeq ($(strip $(RGB_MATRIX_DRIVER)), is31fl3731)
I2C_DRIVER_REQUIRED = yes
COMMON_VPATH += $(DRIVER_PATH)/led/issi
@ -815,15 +827,12 @@ ifeq ($(strip $(JOYSTICK_ENABLE)), yes)
$(call CATASTROPHIC_ERROR,Invalid JOYSTICK_DRIVER,JOYSTICK_DRIVER="$(JOYSTICK_DRIVER)" is not a valid joystick driver)
endif
OPT_DEFS += -DJOYSTICK_ENABLE
OPT_DEFS += -DJOYSTICK_$(strip $(shell echo $(JOYSTICK_DRIVER) | tr '[:lower:]' '[:upper:]'))
SRC += $(QUANTUM_DIR)/process_keycode/process_joystick.c
SRC += $(QUANTUM_DIR)/joystick.c
ifeq ($(strip $(JOYSTICK_DRIVER)), analog)
ANALOG_DRIVER_REQUIRED = yes
OPT_DEFS += -DANALOG_JOYSTICK_ENABLE
endif
ifeq ($(strip $(JOYSTICK_DRIVER)), digital)
OPT_DEFS += -DDIGITAL_JOYSTICK_ENABLE
endif
endif
@ -877,9 +886,24 @@ ifeq ($(strip $(BLUETOOTH_ENABLE)), yes)
endif
endif
ENCODER_ENABLE ?= no
ENCODER_DRIVER ?= quadrature
VALID_ENCODER_DRIVER_TYPES := quadrature custom
ifeq ($(strip $(ENCODER_ENABLE)), yes)
ifeq ($(filter $(ENCODER_DRIVER),$(VALID_ENCODER_DRIVER_TYPES)),)
$(call CATASTROPHIC_ERROR,Invalid ENCODER_DRIVER,ENCODER_DRIVER="$(ENCODER_DRIVER)" is not a valid encoder driver)
endif
SRC += $(QUANTUM_DIR)/encoder.c
OPT_DEFS += -DENCODER_ENABLE
OPT_DEFS += -DENCODER_DRIVER_$(strip $(shell echo $(ENCODER_DRIVER) | tr '[:lower:]' '[:upper:]'))
COMMON_VPATH += $(PLATFORM_PATH)/$(PLATFORM_KEY)/$(DRIVER_DIR)/encoder
COMMON_VPATH += $(DRIVER_PATH)/encoder
ifneq ($(strip $(ENCODER_DRIVER)), custom)
SRC += encoder_$(strip $(ENCODER_DRIVER)).c
endif
ifeq ($(strip $(ENCODER_MAP_ENABLE)), yes)
OPT_DEFS += -DENCODER_MAP_ENABLE
endif
@ -936,6 +960,15 @@ ifeq ($(strip $(SPI_DRIVER_REQUIRED)), yes)
endif
ifeq ($(strip $(UART_DRIVER_REQUIRED)), yes)
OPT_DEFS += -DHAL_USE_SERIAL=TRUE
QUANTUM_LIB_SRC += uart.c
ifeq ($(strip $(PLATFORM)), CHIBIOS)
ifneq ($(filter $(MCU_SERIES),RP2040),)
OPT_DEFS += -DHAL_USE_SIO=TRUE
QUANTUM_LIB_SRC += uart_sio.c
else
OPT_DEFS += -DHAL_USE_SERIAL=TRUE
QUANTUM_LIB_SRC += uart_serial.c
endif
else
QUANTUM_LIB_SRC += uart.c
endif
endif

View File

@ -36,10 +36,10 @@
"BACKLIGHT_DEFAULT_LEVEL": {"info_key": "backlight.default.brightness", "value_type": "int"},
// Bootmagic
"BOOTMAGIC_LITE_COLUMN": {"info_key": "bootmagic.matrix.1", "value_type": "int"},
"BOOTMAGIC_LITE_COLUMN_RIGHT": {"info_key": "split.bootmagic.matrix.1", "value_type": "int"},
"BOOTMAGIC_LITE_ROW": {"info_key": "bootmagic.matrix.0", "value_type": "int"},
"BOOTMAGIC_LITE_ROW_RIGHT": {"info_key": "split.bootmagic.matrix.0", "value_type": "int"},
"BOOTMAGIC_COLUMN": {"info_key": "bootmagic.matrix.1", "value_type": "int"},
"BOOTMAGIC_COLUMN_RIGHT": {"info_key": "split.bootmagic.matrix.1", "value_type": "int"},
"BOOTMAGIC_ROW": {"info_key": "bootmagic.matrix.0", "value_type": "int"},
"BOOTMAGIC_ROW_RIGHT": {"info_key": "split.bootmagic.matrix.0", "value_type": "int"},
// Caps Word
"BOTH_SHIFTS_TURNS_ON_CAPS_WORD": {"info_key": "caps_word.both_shifts_turns_on", "value_type": "flag"},

View File

@ -21,6 +21,7 @@
"DEBOUNCE_TYPE": {"info_key": "build.debounce_type"},
"EEPROM_DRIVER": {"info_key": "eeprom.driver"},
"ENCODER_ENABLE": {"info_key": "encoder.enabled", "value_type": "bool"},
"ENCODER_DRIVER": {"info_key": "encoder.driver"},
"FIRMWARE_FORMAT": {"info_key": "build.firmware_format"},
"KEYBOARD_SHARED_EP": {"info_key": "usb.shared_endpoint.keyboard", "value_type": "bool"},
"LAYOUTS": {"info_key": "community_layouts", "value_type": "list"},

View File

@ -185,6 +185,9 @@
"eek": {
"target": "eek/silk_down"
},
"epoch80": {
"target": "kbdfans/epoch80"
},
"era/klein": {
"target": "era/sirind/klein_sd"
},
@ -311,6 +314,9 @@
"jj50": {
"target": "kprepublic/jj50"
},
"jm60": {
"target": "kbdfans/jm60"
},
"jones": {
"target": "jones/v03_1"
},
@ -894,6 +900,9 @@
"espectro": {
"target": "mechkeys/espectro"
},
"eu_isolation": {
"target": "p3d/eu_isolation"
},
"felix": {
"target": "unikeyboard/felix"
},
@ -1224,6 +1233,9 @@
"pw88": {
"target": "smoll/pw88"
},
"q4z": {
"target": "p3d/q4z"
},
"qaz": {
"target": "tominabox1/qaz"
},
@ -1359,6 +1371,9 @@
"space_space/rev2": {
"target": "qpockets/space_space/rev2"
},
"spacey": {
"target": "p3d/spacey"
},
"spiderisland/winry25tc": {
"target": "winry/winry25tc"
},
@ -1389,6 +1404,9 @@
"suihankey/split": {
"target": "kakunpc/suihankey/split"
},
"synapse": {
"target": "p3d/synapse"
},
"the_ruler": {
"target": "maple_computing/the_ruler"
},
@ -1410,6 +1428,9 @@
"tronguylabs/m122_3270": {
"target": "ibm/model_m_122/m122_3270/teensy"
},
"tw40": {
"target": "p3d/tw40"
},
"ua62": {
"target": "nacly/ua62"
},

View File

@ -6,6 +6,10 @@
"encoder_config": {
"type": "object",
"properties": {
"driver": {
"type": "string",
"enum": ["custom", "quadrature"]
},
"rotary": {
"type": "array",
"items": {
@ -24,14 +28,12 @@
"dip_switch_config": {
"type": "object",
"properties": {
"pins": {
"$ref": "qmk.definitions.v1#/mcu_pin_array"
}
"pins": {"$ref": "qmk.definitions.v1#/mcu_pin_array"}
}
},
}
},
"type": "object",
"not": { "required": [ "vendorId", "productId" ] }, // reject via keys...
"not": {"required": ["vendorId", "productId"]}, // reject via keys...
"properties": {
"keyboard_name": {"$ref": "qmk.definitions.v1#/text_identifier"},
"keyboard_folder": {"$ref": "qmk.definitions.v1#/keyboard"},
@ -142,7 +144,7 @@
"properties": {
"driver": {
"type": "string",
"enum": ["pwm", "software", "timer", "custom"]
"enum": ["custom", "pwm", "software", "timer"]
},
"default": {
"type": "object",
@ -314,8 +316,8 @@
},
"features": {
"$ref": "qmk.definitions.v1#/boolean_array",
"propertyNames": { "$ref": "qmk.definitions.v1#/snake_case" }
"propertyNames": {"$ref": "qmk.definitions.v1#/snake_case"},
"not": {"required": ["lto"]}
},
"indicators": {
"type": "object",
@ -340,15 +342,9 @@
"type": "object",
"additionalProperties": false,
"properties": {
"filename": {
"type": "string"
},
"c_macro": {
"type": "boolean"
},
"json_layout": {
"type": "boolean"
},
"filename": {"type": "string"},
"c_macro": {"type": "boolean"},
"json_layout": {"type": "boolean"},
"layout": {
"type": "array",
"items": {
@ -431,10 +427,8 @@
"properties": {
"animations": {
"type": "object",
"propertyNames": { "$ref": "qmk.definitions.v1#/snake_case" }
"additionalProperties": {
"type": "boolean"
}
"propertyNames": {"$ref": "qmk.definitions.v1#/snake_case"},
"additionalProperties": {"type": "boolean"}
},
"default": {
"type": "object",
@ -446,7 +440,24 @@
"speed": {"$ref": "qmk.definitions.v1#/unsigned_int_8"}
}
},
"driver": {"type": "string"},
"driver": {
"type": "string",
"enum": [
"custom",
"is31fl3218",
"is31fl3729",
"is31fl3731",
"is31fl3733",
"is31fl3736",
"is31fl3737",
"is31fl3741",
"is31fl3742a",
"is31fl3743a",
"is31fl3745",
"is31fl3746a",
"snled27351"
]
},
"center_point": {
"type": "array",
"minItems": 2,
@ -495,10 +506,8 @@
"properties": {
"animations": {
"type": "object",
"propertyNames": { "$ref": "qmk.definitions.v1#/snake_case" }
"additionalProperties": {
"type": "boolean"
}
"propertyNames": {"$ref": "qmk.definitions.v1#/snake_case"},
"additionalProperties": {"type": "boolean"}
},
"default": {
"type": "object",
@ -512,7 +521,26 @@
"speed": {"$ref": "qmk.definitions.v1#/unsigned_int_8"}
}
},
"driver": {"type": "string"},
"driver": {
"type": "string",
"enum": [
"aw20216s",
"custom",
"is31fl3218",
"is31fl3729",
"is31fl3731",
"is31fl3733",
"is31fl3736",
"is31fl3737",
"is31fl3741",
"is31fl3742a",
"is31fl3743a",
"is31fl3745",
"is31fl3746a",
"snled27351",
"ws2812"
]
},
"center_point": {
"type": "array",
"minItems": 2,
@ -564,10 +592,8 @@
"properties": {
"animations": {
"type": "object",
"propertyNames": { "$ref": "qmk.definitions.v1#/snake_case" }
"additionalProperties": {
"type": "boolean"
}
"propertyNames": {"$ref": "qmk.definitions.v1#/snake_case"},
"additionalProperties": {"type": "boolean"}
},
"brightness_steps": {"$ref": "qmk.definitions.v1#/unsigned_int"},
"default": {
@ -770,7 +796,7 @@
"st7565": {"type": "boolean"},
"wpm": {"type": "boolean"}
}
}
},
"watchdog": {"type": "boolean"},
"watchdog_timeout": {"$ref": "qmk.definitions.v1#/unsigned_int"},
"sync_matrix_state": {

367
docs/ChangeLog/20240225.md Normal file
View File

@ -0,0 +1,367 @@
# QMK Breaking Changes - 2024 February 25 Changelog
## Notable Features :id=notable-features
_0.24.0_ is mainly a maintenance release of QMK Firmware -- as per last few breaking changes cycles, there have been a lot of behind-the-scenes changes, mainly:
* continued purge of user keymaps
* migration of RGB matrix configuration into `info.json` files
* standardisation of `LAYOUT` naming
* keyboard relocations
* addressing technical debt
## Changes Requiring User Action :id=changes-requiring-user-action
### Windows Driver Changes ([QMK Toolbox 0.3.0 Release](https://github.com/qmk/qmk_toolbox/releases/tag/0.3.0))
Flashing keyboards that target `atmel-dfu` or `qmk-dfu` on Windows using `qmk flash` or QMK Toolbox have traditionally used _libusb_ for access to the DFU USB device. Since QMK Toolbox 0.3.0, this has changed to WinUSB.
If you update QMK Toolbox or update QMK MSYS, you may find that flashing Atmel DFU keyboards no longer functions as intended. If you strike such issues when flashing new firmware, you will need to replace the _libusb_ driver with _WinUSB_ using Zadig. You can follow the [Recovering from Installation to Wrong Device](driver_installation_zadig.md#recovering-from-installation-to-wrong-device) instructions to replace the driver associated with the Atmel DFU bootloader, skipping the section about removal as Zadig will safely replace the driver instead. Please ensure your keyboard is in bootloader mode and has _libusb_ as the existing driver before attempting to use Zadig to replace the driver. If instead you see _HidUsb_ you're not in bootloader mode and should not continue with driver replacement.
### Updated Keyboard Codebases :id=updated-keyboard-codebases
One note with updated keyboard names -- historical keyboard names are still considered valid when using [External Userspace](newbs_external_userspace.md) for builds. If you're already using External Userspace, you do not need to move your keymap inside your repository.
| Old Keyboard Name | New Keyboard Name |
|-------------------------|---------------------------------|
| enter67 | kezewa/enter67 |
| enter80 | kezewa/enter80 |
| epoch80 | kbdfans/epoch80 |
| eu_isolation | p3d/eu_isolation |
| flygone60/rev3 | shandoncodes/flygone60/rev3 |
| hub16 | joshajohnson/hub16 |
| hub20 | joshajohnson/hub20 |
| jm60 | kbdfans/jm60 |
| kira75 | kira/kira75 |
| kira80 | kira/kira80 |
| kmac | kbdmania/kmac |
| kmac_pad | kbdmania/kmac_pad |
| kudox/columner | kumaokobo/kudox/columner |
| kudox/rev1 | kumaokobo/kudox/rev1 |
| kudox/rev2 | kumaokobo/kudox/rev2 |
| kudox/rev3 | kumaokobo/kudox/rev3 |
| kudox_full/rev1 | kumaokobo/kudox_full/rev1 |
| kudox_game | kumaokobo/kudox_game |
| kudox_game/rev1 | kumaokobo/kudox_game/rev1 |
| kudox_game/rev2 | kumaokobo/kudox_game/rev2 |
| laser_ninja/pumpkin_pad | laser_ninja/pumpkinpad |
| late9/rev1 | rookiebwoy/late9/rev1 |
| lefty | smoll/lefty |
| lefty/rev1 | smoll/lefty/rev1 |
| lefty/rev2 | smoll/lefty/rev2 |
| lpad | laneware/lpad |
| lw67 | laneware/lw67 |
| lw75 | laneware/lw75 |
| macro1 | laneware/macro1 |
| macro3 | handwired/macro3 |
| miniaxe | kagizaraya/miniaxe |
| mino/hotswap | shandoncodes/mino/hotswap |
| mino_plus/hotswap | shandoncodes/mino_plus/hotswap |
| mino_plus/soldered | shandoncodes/mino_plus/soldered |
| mnk1800s | monokei/mnk1800s |
| mnk50 | monokei/mnk50 |
| mnk75 | monokei/mnk75 |
| moonlander | zsa/moonlander |
| neopad/rev1 | rookiebwoy/neopad/rev1 |
| pico/65keys | kumaokobo/pico/65keys |
| pico/70keys | kumaokobo/pico/70keys |
| pw88 | smoll/pw88 |
| q4z | p3d/q4z |
| raindrop | laneware/raindrop |
| redox_w | redox/wireless |
| riot_pad | shandoncodes/riot_pad |
| spacey | p3d/spacey |
| synapse | p3d/synapse |
| tw40 | p3d/tw40 |
| w1_at | geonworks/w1_at |
| z12 | zigotica/z12 |
| z34 | zigotica/z34 |
## Notable core changes :id=notable-core
### Renaming Arduino-style GPIO pin functions ([#23085](https://github.com/qmk/qmk_firmware/pull/23085), [#23093](https://github.com/qmk/qmk_firmware/pull/23093)) :id=gpio-rename
QMK has long used Arduino-style GPIO naming conventions. This has been confusing for users, as over time they've had new variations added, as well as users mistakenly thinking that QMK supports the rest of the Arduino ecosystem.
The decision was made to rename the GPIO manipulation functions with ones matching QMK Firmware's code styling.
| Old | New |
|------------------------------|---------------------------------------|
| `setPinInput(pin)` | `gpio_set_pin_input(pin)` |
| `setPinInputHigh(pin)` | `gpio_set_pin_input_high(pin)` |
| `setPinInputLow(pin)` | `gpio_set_pin_input_low(pin)` |
| `setPinOutput(pin)` | `gpio_set_pin_output(pin)` |
| `setPinOutputPushPull(pin)` | `gpio_set_pin_output_push_pull(pin)` |
| `setPinOutputOpenDrain(pin)` | `gpio_set_pin_output_open_drain(pin)` |
| `writePinHigh(pin)` | `gpio_write_pin_high(pin)` |
| `writePinLow(pin)` | `gpio_write_pin_low(pin)` |
| `writePin(pin, level)` | `gpio_write_pin(pin, level)` |
| `readPin(pin)` | `gpio_read_pin(pin)` |
| `togglePin(pin)` | `gpio_toggle_pin(pin)` |
### I2C driver API Changes ([#22905](https://github.com/qmk/qmk_firmware/pull/22905))
Much like the GPIO refactoring, I2C APIs were also updated to conform to QMK naming standards. This is largely irrelevant to people using subsystem abstractions such as touchpads or RGB lighting, and only affects people manually communicating with other peripherals.
| Old API | New API |
|--------------------|--------------------------|
| `i2c_readReg()` | `i2c_read_register()` |
| `i2c_readReg16()` | `i2c_read_register16()` |
| `i2c_writeReg()` | `i2c_write_register()` |
| `i2c_writeReg16()` | `i2c_write_register16()` |
### Renaming _Bootmagic Lite_ => _Bootmagic_ ([#22970](https://github.com/qmk/qmk_firmware/pull/22970), [#22979](https://github.com/qmk/qmk_firmware/pull/22979)) :id=bootmagic-rename
Bootmagic "Lite" had no real meaning once the historical Bootmagic "Full" was deprecated and removed. Any references to _Bootmagic Lite_ should now just refer to _Bootmagic_. We hope we got the majority of the code and the documentation, so if you find any more, let us know!
### Threshold for automatic mouse layer activation ([#21398](https://github.com/qmk/qmk_firmware/pull/21398)) :id=auto-mouse-layer
In some cases, accidental automatic activation of the mouse layer made it difficult to continue typing, such as when brushing across a trackball. `AUTO_MOUSE_THRESHOLD` is now a configurable option in `config.h` which allows for specifying what the movement threshold is before automatically activating the mouse layer.
### DIP Switch Mapping ([#22543](https://github.com/qmk/qmk_firmware/pull/22543)) :id=dip-switch-map
Much like Encoder Mapping, DIP Switch Mapping allows for specifying a table of actions to execute when a DIP switch state changes. See the [DIP Switch Documentation](feature_dip_switch.md#dip-switch-map) for more information.
```c
#if defined(DIP_SWITCH_MAP_ENABLE)
const uint16_t PROGMEM dip_switch_map[NUM_DIP_SWITCHES][NUM_DIP_STATES] = {
DIP_SWITCH_OFF_ON(DF(0), DF(1)),
DIP_SWITCH_OFF_ON(EC_NORM, EC_SWAP)
};
#endif
```
### Quantum Painter updates ([#18521](https://github.com/qmk/qmk_firmware/pull/18521), [#20645](https://github.com/qmk/qmk_firmware/pull/20645), [#22358](https://github.com/qmk/qmk_firmware/pull/22358)) :id=qp-updates
Quantum Painter picked up support for the following:
* ILI9486 displays
* SSD1306 displays, including smaller OLEDs
* Native panel pixel format support for fonts
Quantum Painter now supports the majority of common OLED panels supported by the basic OLED driver, so if you're using an ARM-based board you may find Quantum Painter a much more feature-rich API in comparison.
## Full changelist :id=full-changelist
Core:
* [Driver] ILI9486 on Quantum Painter ([#18521](https://github.com/qmk/qmk_firmware/pull/18521))
* Insert delay between shifted chars in send_string_with_delay ([#19280](https://github.com/qmk/qmk_firmware/pull/19280))
* [QP] Native palette support for fonts ([#20645](https://github.com/qmk/qmk_firmware/pull/20645))
* I2C driver cleanup ([#21273](https://github.com/qmk/qmk_firmware/pull/21273))
* Add option for auto mouse movement threshold ([#21398](https://github.com/qmk/qmk_firmware/pull/21398))
* Add Canadian French input locale ([#21456](https://github.com/qmk/qmk_firmware/pull/21456))
* Add encoder abstraction. ([#21548](https://github.com/qmk/qmk_firmware/pull/21548))
* Converted RGB matrix to use last_input_activity_elapsed(). ([#21687](https://github.com/qmk/qmk_firmware/pull/21687))
* Ignore space cadet key release when caps word is active ([#21721](https://github.com/qmk/qmk_firmware/pull/21721))
* Add OS detection callbacks ([#21777](https://github.com/qmk/qmk_firmware/pull/21777))
* joystick weights ([#21883](https://github.com/qmk/qmk_firmware/pull/21883))
* Add RGB matrix & LED Matrix support for IS31FL3729 ([#21944](https://github.com/qmk/qmk_firmware/pull/21944))
* dac_additive: Decouple the buffer length from the waveform length ([#22276](https://github.com/qmk/qmk_firmware/pull/22276))
* Add missing rgb matrix default parameters ([#22281](https://github.com/qmk/qmk_firmware/pull/22281))
* Remove console out endpoint ([#22304](https://github.com/qmk/qmk_firmware/pull/22304))
* Add ADC support STM32L4xx and STM32G4xx series MCUs ([#22341](https://github.com/qmk/qmk_firmware/pull/22341))
* Add QP support for smaller OLED displays and SSD1306 ([#22358](https://github.com/qmk/qmk_firmware/pull/22358))
* Add Imera converter ([#22419](https://github.com/qmk/qmk_firmware/pull/22419))
* LED drivers: refactor page selection ([#22518](https://github.com/qmk/qmk_firmware/pull/22518))
* Rework RGBLight driver system ([#22529](https://github.com/qmk/qmk_firmware/pull/22529))
* Add `APA102_LED_COUNT` define ([#22530](https://github.com/qmk/qmk_firmware/pull/22530))
* Add latam spanish headers ([#22542](https://github.com/qmk/qmk_firmware/pull/22542))
* Keymap introspection for Dip Switches ([#22543](https://github.com/qmk/qmk_firmware/pull/22543))
* Add basic presence check for cirque trackpad. ([#22546](https://github.com/qmk/qmk_firmware/pull/22546))
* Rename `RGBLED_NUM` -> `RGBLIGHT_LED_COUNT` ([#22570](https://github.com/qmk/qmk_firmware/pull/22570))
* LED drivers: change "TWI" to "I2C" ([#22617](https://github.com/qmk/qmk_firmware/pull/22617))
* LED drivers: extract IS31FL3742A from IS31COMMON ([#22620](https://github.com/qmk/qmk_firmware/pull/22620))
* Align Dip Switch feature ([#22625](https://github.com/qmk/qmk_firmware/pull/22625))
* LED/RGB Matrix: add header for drivers ([#22628](https://github.com/qmk/qmk_firmware/pull/22628))
* LED drivers: extract IS31FL3743A from IS31COMMON ([#22635](https://github.com/qmk/qmk_firmware/pull/22635))
* LED drivers: extract IS31FL3745 from IS31COMMON ([#22636](https://github.com/qmk/qmk_firmware/pull/22636))
* LED drivers: extract IS31FL3746A from IS31COMMON ([#22637](https://github.com/qmk/qmk_firmware/pull/22637))
* Update keyboard LED driver configs ([#22638](https://github.com/qmk/qmk_firmware/pull/22638))
* Solid reactive: improve fading effect ([#22656](https://github.com/qmk/qmk_firmware/pull/22656))
* Remove redundant RGB/LED matrix eeconfig init ([#22673](https://github.com/qmk/qmk_firmware/pull/22673))
* Remove redundant rgblight eeconfig init ([#22674](https://github.com/qmk/qmk_firmware/pull/22674))
* Remove redundant steno eeconfig init ([#22680](https://github.com/qmk/qmk_firmware/pull/22680))
* Rename `LED_DISABLE_WHEN_USB_SUSPENDED` -> `LED_MATRIX_SLEEP` ([#22681](https://github.com/qmk/qmk_firmware/pull/22681))
* Rename `RGB_DISABLE_WHEN_USB_SUSPENDED` -> `RGB_MATRIX_SLEEP` ([#22682](https://github.com/qmk/qmk_firmware/pull/22682))
* Align VUSB suspend protocol logic ([#22688](https://github.com/qmk/qmk_firmware/pull/22688))
* [Audio] Enable Complementary output for PWM Hardware driver ([#22726](https://github.com/qmk/qmk_firmware/pull/22726))
* Remove redundant audio eeconfig init ([#22736](https://github.com/qmk/qmk_firmware/pull/22736))
* Align location of tap dance keycode ([#22742](https://github.com/qmk/qmk_firmware/pull/22742))
* Align `SPLIT_HAND_MATRIX_GRID` left/right logic with `SPLIT_HAND_PIN` ([#22775](https://github.com/qmk/qmk_firmware/pull/22775))
* [CI] Regenerate Files ([#22795](https://github.com/qmk/qmk_firmware/pull/22795))
* Remove IS31FLCOMMON code ([#22800](https://github.com/qmk/qmk_firmware/pull/22800))
* Cirque reachable calibration aide ([#22803](https://github.com/qmk/qmk_firmware/pull/22803))
* LED drivers: rename "simple" to "mono" ([#22814](https://github.com/qmk/qmk_firmware/pull/22814))
* is31fl3733: change `write_register()` return type to `void` ([#22824](https://github.com/qmk/qmk_firmware/pull/22824))
* snled27351: change `write_register()` return type to `void` ([#22825](https://github.com/qmk/qmk_firmware/pull/22825))
* apa102: cleanups ([#22826](https://github.com/qmk/qmk_firmware/pull/22826))
* Remove PWM advanced check for WS2812 driver ([#22830](https://github.com/qmk/qmk_firmware/pull/22830))
* Allow ChibiOS `SIO` driver for `UART` driver ([#22839](https://github.com/qmk/qmk_firmware/pull/22839))
* LED drivers: more formatting ([#22865](https://github.com/qmk/qmk_firmware/pull/22865))
* LED drivers: change `write_pwm_buffer()` return type to `void` ([#22869](https://github.com/qmk/qmk_firmware/pull/22869))
* [CI] Regenerate Files ([#22872](https://github.com/qmk/qmk_firmware/pull/22872))
* LED drivers: switch to i2c_writeReg() ([#22878](https://github.com/qmk/qmk_firmware/pull/22878))
* LED drivers: remove `write_pwm_buffer()` from public API ([#22884](https://github.com/qmk/qmk_firmware/pull/22884))
* i2c: rename read/write register functions ([#22905](https://github.com/qmk/qmk_firmware/pull/22905))
* LED drivers: update I2C API usage ([#22951](https://github.com/qmk/qmk_firmware/pull/22951))
* LED drivers: create structs to hold PWM/scaling buffers ([#22955](https://github.com/qmk/qmk_firmware/pull/22955))
* Migrate and remove deprecated debug utils ([#22961](https://github.com/qmk/qmk_firmware/pull/22961))
* Remove call to removed i2c function in azoteq driver ([#22966](https://github.com/qmk/qmk_firmware/pull/22966))
* Tidy up print/debug logging headers ([#22969](https://github.com/qmk/qmk_firmware/pull/22969))
* Begin removal of bootmagic lite terminology ([#22970](https://github.com/qmk/qmk_firmware/pull/22970))
* LED drivers: place I2C addresses into an array ([#22975](https://github.com/qmk/qmk_firmware/pull/22975))
* Removal of bootmagic lite terminology ([#22979](https://github.com/qmk/qmk_firmware/pull/22979))
* Init pins for Analog Joystick sensor ([#22985](https://github.com/qmk/qmk_firmware/pull/22985))
* Workaround for G431 eeprom emulation ([#23002](https://github.com/qmk/qmk_firmware/pull/23002))
* is31fl3741: split PWM and scaling buffers ([#23049](https://github.com/qmk/qmk_firmware/pull/23049))
* LED drivers: update PWM register defines for `g_<driver>_leds` ([#23052](https://github.com/qmk/qmk_firmware/pull/23052))
* LED drivers: add support for shutdown pin ([#23058](https://github.com/qmk/qmk_firmware/pull/23058))
* AW20216S: combine EN pin defines ([#23067](https://github.com/qmk/qmk_firmware/pull/23067))
* Update naming convention for GPIO control macros ([#23085](https://github.com/qmk/qmk_firmware/pull/23085))
* Update GPIO macro usages in core ([#23093](https://github.com/qmk/qmk_firmware/pull/23093))
* OS Detection - Entire file should not be wrapped with ifdef ([#23108](https://github.com/qmk/qmk_firmware/pull/23108))
* IS31FL3729 updates ([#23109](https://github.com/qmk/qmk_firmware/pull/23109))
* Nix shell updates (Nixpkgs 2024-02-23, QMK CLI 1.1.5) ([#23143](https://github.com/qmk/qmk_firmware/pull/23143))
CLI:
* [Refactor] `qmk find` ([#21096](https://github.com/qmk/qmk_firmware/pull/21096))
* [Refactor] Break `QGFImageFile`'s `_save` function into smaller pieces ([#21124](https://github.com/qmk/qmk_firmware/pull/21124))
* [Enhancement] Prepare for `SyntaxWarning` ([#22562](https://github.com/qmk/qmk_firmware/pull/22562))
* Flag invalid keyboard features during lint ([#22832](https://github.com/qmk/qmk_firmware/pull/22832))
Submodule updates:
* chore(chibios-contrib): sync with chibios-21.11.x ([#22560](https://github.com/qmk/qmk_firmware/pull/22560))
Keyboards:
* Move `redox_w` into `redox` ([#21448](https://github.com/qmk/qmk_firmware/pull/21448))
* null ST110R2.1 (SaikouType) basic support with layouts ([#21623](https://github.com/qmk/qmk_firmware/pull/21623))
* New keyboard addition: Orthograph ([#21770](https://github.com/qmk/qmk_firmware/pull/21770))
* Add Olly JF Rev.2 ([#21775](https://github.com/qmk/qmk_firmware/pull/21775))
* Cleanup Satisfaction75 Firmware and add new revisions ([#22082](https://github.com/qmk/qmk_firmware/pull/22082))
* Migrate dynamic_keymap.layer_count < 4 where requried ([#22091](https://github.com/qmk/qmk_firmware/pull/22091))
* Bastard Keyboards: Add support for Dilemma v2 (3x5+3) ([#22185](https://github.com/qmk/qmk_firmware/pull/22185))
* Karn: correct layout data ([#22201](https://github.com/qmk/qmk_firmware/pull/22201))
* zk3mod : added OLED ([#22303](https://github.com/qmk/qmk_firmware/pull/22303))
* Adds support for the Iron180 V2 PCBs ([#22314](https://github.com/qmk/qmk_firmware/pull/22314))
* Add 5x13 and 6x13 ortho community layouts ([#22315](https://github.com/qmk/qmk_firmware/pull/22315))
* Cipulot refactoring ([#22368](https://github.com/qmk/qmk_firmware/pull/22368))
* Remove era/klein ([#22384](https://github.com/qmk/qmk_firmware/pull/22384))
* consolidate firmware folder in smoll parent folder ([#22401](https://github.com/qmk/qmk_firmware/pull/22401))
* `keycapsss/plaid_pad`: switch to encoder map ([#22474](https://github.com/qmk/qmk_firmware/pull/22474))
* Add EE-AT and move W1-AT under geonworks ([#22526](https://github.com/qmk/qmk_firmware/pull/22526))
* refactor: projectcain/vault35 ([#22558](https://github.com/qmk/qmk_firmware/pull/22558))
* Update Q5 ([#22575](https://github.com/qmk/qmk_firmware/pull/22575))
* Update Q7 ([#22577](https://github.com/qmk/qmk_firmware/pull/22577))
* Update Q8 ([#22578](https://github.com/qmk/qmk_firmware/pull/22578))
* Update Q9 ([#22579](https://github.com/qmk/qmk_firmware/pull/22579))
* Remove "empty" files ([#22603](https://github.com/qmk/qmk_firmware/pull/22603))
* Rename Pumpkin Pad to Pumkinpad ([#22651](https://github.com/qmk/qmk_firmware/pull/22651))
* Noodlepad Additions and Updates ([#22701](https://github.com/qmk/qmk_firmware/pull/22701))
* Refactor: move miniaxe into kagizaraya ([#22708](https://github.com/qmk/qmk_firmware/pull/22708))
* Refactor: move keyboards into zigotica folder ([#22709](https://github.com/qmk/qmk_firmware/pull/22709))
* Refactor: move keyboards into laneware folder ([#22710](https://github.com/qmk/qmk_firmware/pull/22710))
* Refactor: move keyboards into kezewa ([#22712](https://github.com/qmk/qmk_firmware/pull/22712))
* Refactor: move keyboards into kbdmania folder ([#22714](https://github.com/qmk/qmk_firmware/pull/22714))
* Refactor: move keyboards into monokei folder ([#22715](https://github.com/qmk/qmk_firmware/pull/22715))
* Refactor: move keyboards into kumaokobo ([#22719](https://github.com/qmk/qmk_firmware/pull/22719))
* Updating NCC1701KB and adding via support ([#22721](https://github.com/qmk/qmk_firmware/pull/22721))
* Move Moonlander to ZSA folder ([#22740](https://github.com/qmk/qmk_firmware/pull/22740))
* Refactor: group shandoncodes keyboards ([#22743](https://github.com/qmk/qmk_firmware/pull/22743))
* Refactor: group rookiebwoy keyboards ([#22745](https://github.com/qmk/qmk_firmware/pull/22745))
* Default folder correction for kumaokobo ([#22750](https://github.com/qmk/qmk_firmware/pull/22750))
* Default folder correction for rookiebwoy ([#22753](https://github.com/qmk/qmk_firmware/pull/22753))
* Refactor: move macro3 into handwired folder ([#22759](https://github.com/qmk/qmk_firmware/pull/22759))
* Refactor: group kira keyboards ([#22760](https://github.com/qmk/qmk_firmware/pull/22760))
* Refactor: group hub keyboards ([#22762](https://github.com/qmk/qmk_firmware/pull/22762))
* Refactor: move p3d keyboards ([#22763](https://github.com/qmk/qmk_firmware/pull/22763))
* Refactor: group kbdfans keyboards ([#22764](https://github.com/qmk/qmk_firmware/pull/22764))
* Remove incorrect use of WS2812_PIO_USE_PIO1 ([#22771](https://github.com/qmk/qmk_firmware/pull/22771))
* Migrate LED Matrix config to info.json ([#22792](https://github.com/qmk/qmk_firmware/pull/22792))
* Migrate RGB Matrix config to info.json - [0-9] ([#22797](https://github.com/qmk/qmk_firmware/pull/22797))
* Migrate RGB Matrix config to info.json - A ([#22798](https://github.com/qmk/qmk_firmware/pull/22798))
* Late9 keymaps update, added VIA support ([#22801](https://github.com/qmk/qmk_firmware/pull/22801))
* Migrate RGB Matrix config to info.json - B ([#22806](https://github.com/qmk/qmk_firmware/pull/22806))
* Migrate RGB Matrix config to info.json - C ([#22807](https://github.com/qmk/qmk_firmware/pull/22807))
* Migrate RGB Matrix config to info.json - EF ([#22808](https://github.com/qmk/qmk_firmware/pull/22808))
* Migrate RGB Matrix config to info.json - D ([#22811](https://github.com/qmk/qmk_firmware/pull/22811))
* H87g2 updates ([#22819](https://github.com/qmk/qmk_firmware/pull/22819))
* WT boards: extract `g_is31fl3736_leds` from wt_mono_backlight ([#22823](https://github.com/qmk/qmk_firmware/pull/22823))
* Migrate RGB Matrix config to info.json - G ([#22859](https://github.com/qmk/qmk_firmware/pull/22859))
* Use existing columns for 3x5 layout ([#22860](https://github.com/qmk/qmk_firmware/pull/22860))
* Migrate RGB Matrix config to info.json - H ([#22861](https://github.com/qmk/qmk_firmware/pull/22861))
* Migrate RGB Matrix config to info.json - J ([#22862](https://github.com/qmk/qmk_firmware/pull/22862))
* Migrate RGB Matrix config to info.json - I ([#22863](https://github.com/qmk/qmk_firmware/pull/22863))
* Migrate RGB Matrix config to info.json - L ([#22864](https://github.com/qmk/qmk_firmware/pull/22864))
* Migrate RGB Matrix config to info.json - NOPQ ([#22866](https://github.com/qmk/qmk_firmware/pull/22866))
* Migrate RGB Matrix config to info.json - XZY ([#22879](https://github.com/qmk/qmk_firmware/pull/22879))
* Zed65/no_backlight/cor65 correct data layout ([#22898](https://github.com/qmk/qmk_firmware/pull/22898))
* Migrate RGB Matrix config to info.json - M ([#22908](https://github.com/qmk/qmk_firmware/pull/22908))
* Migrate RGB Matrix config to info.json - RS ([#22909](https://github.com/qmk/qmk_firmware/pull/22909))
* Migrate RGB Matrix config to info.json - TUVW ([#22910](https://github.com/qmk/qmk_firmware/pull/22910))
* Migrate RGB Matrix config to info.json - K ([#22911](https://github.com/qmk/qmk_firmware/pull/22911))
* Remove `LAYOUTS_HAS_RGB` ([#22917](https://github.com/qmk/qmk_firmware/pull/22917))
* Migrate lighting defaults to info.json ([#22920](https://github.com/qmk/qmk_firmware/pull/22920))
* Ensure LTO is enabled as a `info.json` build config option ([#22932](https://github.com/qmk/qmk_firmware/pull/22932))
* refactor(keyboard): quokka ([#22942](https://github.com/qmk/qmk_firmware/pull/22942))
* Sango Keyboard ([#22971](https://github.com/qmk/qmk_firmware/pull/22971))
* Add FS streampad ([#22991](https://github.com/qmk/qmk_firmware/pull/22991))
* Remove always enabled effects from lighting animation list ([#22992](https://github.com/qmk/qmk_firmware/pull/22992))
* Migrate RGB Matrix config to info.json - keychron ([#22998](https://github.com/qmk/qmk_firmware/pull/22998))
* Migrate RGB Matrix config to info.json - Misc ([#23000](https://github.com/qmk/qmk_firmware/pull/23000))
* Remove ee_hands config from ferris/sweep firmware ([#23029](https://github.com/qmk/qmk_firmware/pull/23029))
* Migrate dip switch config to info.json - keychron ([#23037](https://github.com/qmk/qmk_firmware/pull/23037))
* [unicorne] Add a layout alias ([#23056](https://github.com/qmk/qmk_firmware/pull/23056))
* nacly/sodium62: Update vid, pid, and add via keymap ([#23063](https://github.com/qmk/qmk_firmware/pull/23063))
* LED drivers: update keyboard LED configs ([#23073](https://github.com/qmk/qmk_firmware/pull/23073))
* Remove invalid keyboard level features ([#23074](https://github.com/qmk/qmk_firmware/pull/23074))
* Migrate WEAR_LEVELING_* to info.json ([#23077](https://github.com/qmk/qmk_firmware/pull/23077))
* [Keymap Removal] keyboard with most keymaps ([#23081](https://github.com/qmk/qmk_firmware/pull/23081))
* Remove obvious user keymaps, keyboards/{v,x,y,z}* edition. ([#23083](https://github.com/qmk/qmk_firmware/pull/23083))
* Remove obvious user keymaps, keyboards/{s,t}* edition. ([#23084](https://github.com/qmk/qmk_firmware/pull/23084))
* [Keymap Removal] keyboard with most keymaps ([#23092](https://github.com/qmk/qmk_firmware/pull/23092))
* Fiuxup takashicompany/heavy_left ([#23094](https://github.com/qmk/qmk_firmware/pull/23094))
* Remove obvious user keymaps, keyboards/{i,j,k}* edition ([#23102](https://github.com/qmk/qmk_firmware/pull/23102))
* Manual user keymap removal ([#23104](https://github.com/qmk/qmk_firmware/pull/23104))
* Manual user keymap removal ([#23119](https://github.com/qmk/qmk_firmware/pull/23119))
* Migrate `RGBLED_NUM` -> `RGBLIGHT_LED_COUNT` in remaining non-user keymaps ([#23128](https://github.com/qmk/qmk_firmware/pull/23128))
Keyboard fixes:
* Fix VID and PID for AnnePro2 ([#22263](https://github.com/qmk/qmk_firmware/pull/22263))
* fix(kikoslab/kl90): Fix firmware to support encoder knobs properly ([#22649](https://github.com/qmk/qmk_firmware/pull/22649))
* fix: improper usage of keyboard/user-level functions ([#22652](https://github.com/qmk/qmk_firmware/pull/22652))
* Temporary fix for mechlovin/olly/octagon ([#22796](https://github.com/qmk/qmk_firmware/pull/22796))
* Keychron Q11 usb poweron fix ([#22799](https://github.com/qmk/qmk_firmware/pull/22799))
* capsunlocked/cu80/v2: Fix invalid RGB matrix config ([#22873](https://github.com/qmk/qmk_firmware/pull/22873))
* Fix typo in Redox config ([#22899](https://github.com/qmk/qmk_firmware/pull/22899))
* Fixup doio/kb16 ([#22921](https://github.com/qmk/qmk_firmware/pull/22921))
* Fixup takashicompany/minizone ([#22922](https://github.com/qmk/qmk_firmware/pull/22922))
* Fixup sofle ([#22934](https://github.com/qmk/qmk_firmware/pull/22934))
* Fix Issue with RGB Matrix not understanding the split keyboard ([#22997](https://github.com/qmk/qmk_firmware/pull/22997))
* Fixup sawnsprojects/krush60 ([#23095](https://github.com/qmk/qmk_firmware/pull/23095))
* Fixup kbd67/rev1 ([#23096](https://github.com/qmk/qmk_firmware/pull/23096))
* Fixup boardsource/equals ([#23106](https://github.com/qmk/qmk_firmware/pull/23106))
* Fixup inett_studio/sq80 ([#23121](https://github.com/qmk/qmk_firmware/pull/23121))
* Add LED/RGB Matrix drivers to info.json schema ([#23127](https://github.com/qmk/qmk_firmware/pull/23127))
* Fix for multiple AMUX usage ([#23155](https://github.com/qmk/qmk_firmware/pull/23155))
Bugs:
* MIDI sustain effect fix on qmk 0.22.2 ([#22114](https://github.com/qmk/qmk_firmware/pull/22114))
* Prevent `qmk migrate` processing unparsed info.json values ([#22374](https://github.com/qmk/qmk_firmware/pull/22374))
* Remove redundant backlight eeconfig init ([#22675](https://github.com/qmk/qmk_firmware/pull/22675))
* pointing_device ifdef indentation fix ([#22802](https://github.com/qmk/qmk_firmware/pull/22802))
* Ensure LED config is extracted when feature is disabled ([#22809](https://github.com/qmk/qmk_firmware/pull/22809))
* Generate true/false for _DEFAULT_ON options ([#22829](https://github.com/qmk/qmk_firmware/pull/22829))
* is31fl3733: fix driver sync backwards compatibility defines ([#22851](https://github.com/qmk/qmk_firmware/pull/22851))
* LED drivers: misc formatting and typos ([#22857](https://github.com/qmk/qmk_firmware/pull/22857))
* Allow generation of both LED and RGB Matrix config ([#22896](https://github.com/qmk/qmk_firmware/pull/22896))
* LED drivers: remove PWM register offsets ([#22897](https://github.com/qmk/qmk_firmware/pull/22897))
* `qmk format-json`: Force Unix line endings and ensure LF at EOF ([#22901](https://github.com/qmk/qmk_firmware/pull/22901))
* Fix cirque connected check ([#22948](https://github.com/qmk/qmk_firmware/pull/22948))
* Fix joystick initialization ([#22953](https://github.com/qmk/qmk_firmware/pull/22953))
* Workaround for `make test:all DEBUG=1` ([#23047](https://github.com/qmk/qmk_firmware/pull/23047))
* Fix unit test execution ([#23048](https://github.com/qmk/qmk_firmware/pull/23048))
* Fix git-submodule running in wrong location ([#23059](https://github.com/qmk/qmk_firmware/pull/23059))
* WS2812 bitbang: prefix for `NOP_FUDGE` define ([#23110](https://github.com/qmk/qmk_firmware/pull/23110))
* Fix make clean test:os_detection ([#23112](https://github.com/qmk/qmk_firmware/pull/23112))
* Fix pmw33xx sensor corruption on get-cpi call ([#23116](https://github.com/qmk/qmk_firmware/pull/23116))
* Ensure `qmk generate-compilation-database` copies to userspace as well. ([#23129](https://github.com/qmk/qmk_firmware/pull/23129))

View File

@ -138,7 +138,7 @@
* Breaking Changes
* [Overview](breaking_changes.md)
* [My Pull Request Was Flagged](breaking_changes_instructions.md)
* [Most Recent ChangeLog](ChangeLog/20231126.md "QMK v0.23.0 - 2023 Nov 26")
* [Most Recent ChangeLog](ChangeLog/20240225.md "QMK v0.24.0 - 2024 Feb 25")
* [Past Breaking Changes](breaking_changes_history.md)
* C Development

View File

@ -10,25 +10,25 @@ Practically, this means QMK merges the `develop` branch into the `master` branch
## What has been included in past Breaking Changes?
* [2024 Feb 25](ChangeLog/20240225.md)
* [2023 Nov 26](ChangeLog/20231126.md)
* [2023 Aug 27](ChangeLog/20230827.md)
* [2023 May 28](ChangeLog/20230528.md)
* [Older Breaking Changes](breaking_changes_history.md)
## When is the next Breaking Change?
The next Breaking Change is scheduled for November 26, 2023.
The next Breaking Change is scheduled for May 26, 2024.
### Important Dates
* 2023 Nov 26 - `develop` is tagged with a new release version. Each push to `master` is subsequently merged to `develop` by GitHub actions.
* 2024 Jan 28 - `develop` closed to new PRs.
* 2024 Jan 28 - Call for testers.
* 2024 Feb 4 - Last day for merges -- after this point `develop` is locked for testing and accepts only bugfixes
* 2024 Feb 18 - `develop` is locked, only critical bugfix PRs merged.
* 2024 Feb 22 - `master` is locked, no PRs merged.
* 2024 Feb 25 - Merge `develop` to `master`.
* 2024 Feb 25 - `master` is unlocked. PRs can be merged again.
* 2024 Feb 25 - `develop` is tagged with a new release version. Each push to `master` is subsequently merged to `develop` by GitHub actions.
* 2024 Apr 28 - `develop` closed to new PRs.
* 2024 Apr 28 - Call for testers.
* 2024 May 5 - Last day for merges -- after this point `develop` is locked for testing and accepts only bugfixes
* 2024 May 19 - `develop` is locked, only critical bugfix PRs merged.
* 2024 May 23 - `master` is locked, no PRs merged.
* 2024 May 26 - Merge `develop` to `master`.
* 2024 May 26 - `master` is unlocked. PRs can be merged again.
## What changes will be included?
@ -48,7 +48,7 @@ Criteria for acceptance:
Strongly suggested:
* The PR has a ChangeLog file describing the changes under `<qmk_firmware>/docs/Changelog/20240225`.
* The PR has a ChangeLog file describing the changes under `<qmk_firmware>/docs/Changelog/20240526`.
* This should be in Markdown format, with a name in the format `PR12345.md`, substituting the digits for your PRs ID.
* One strong recommendation that the ChangeLog document matches the PR description on GitHub, so as to ensure traceability.

View File

@ -2,6 +2,7 @@
This page links to all previous changelogs from the QMK Breaking Changes process.
* [2024 Feb 25](ChangeLog/20240225.md) - version 0.24.0
* [2023 Nov 26](ChangeLog/20231126.md) - version 0.23.0
* [2023 Aug 27](ChangeLog/20230827.md) - version 0.22.0
* [2023 May 28](ChangeLog/20230528.md) - version 0.21.0

View File

@ -119,11 +119,11 @@ void keyboard_pre_init_user(void) {
// Call the keyboard pre init code.
// Set our LED pins as output
setPinOutput(B0);
setPinOutput(B1);
setPinOutput(B2);
setPinOutput(B3);
setPinOutput(B4);
gpio_set_pin_output(B0);
gpio_set_pin_output(B1);
gpio_set_pin_output(B2);
gpio_set_pin_output(B3);
gpio_set_pin_output(B4);
}
```

View File

@ -70,13 +70,13 @@ The device name here is the name that appears in Zadig, and may not be what the
|Bootloader |Device Name |VID/PID |Driver |
|--------------|------------------------------|--------------|-------|
|`atmel-dfu` |ATmega16u2 DFU |`03EB:2FEF` |libusb0|
|`atmel-dfu` |ATmega32U2 DFU |`03EB:2FF0` |libusb0|
|`atmel-dfu` |ATm16U4 DFU V1.0.2 |`03EB:2FF3` |libusb0|
|`atmel-dfu` |ATm32U4DFU |`03EB:2FF4` |libusb0|
|`atmel-dfu` |*none* (AT90USB64) |`03EB:2FF9` |libusb0|
|`atmel-dfu` |AT90USB128 DFU |`03EB:2FFB` |libusb0|
|`qmk-dfu` |(keyboard name) Bootloader |As `atmel-dfu`|libusb0|
|`atmel-dfu` |ATmega16u2 DFU |`03EB:2FEF` |WinUSB |
|`atmel-dfu` |ATmega32U2 DFU |`03EB:2FF0` |WinUSB |
|`atmel-dfu` |ATm16U4 DFU V1.0.2 |`03EB:2FF3` |WinUSB |
|`atmel-dfu` |ATm32U4DFU |`03EB:2FF4` |WinUSB |
|`atmel-dfu` |*none* (AT90USB64) |`03EB:2FF9` |WinUSB |
|`atmel-dfu` |AT90USB128 DFU |`03EB:2FFB` |WinUSB |
|`qmk-dfu` |(keyboard name) Bootloader |As `atmel-dfu`|WinUSB |
|`halfkay` |*none* |`16C0:0478` |HidUsb |
|`caterina` |Pro Micro 3.3V |`1B4F:9203` |usbser |
|`caterina` |Pro Micro 5V |`1B4F:9205` |usbser |

View File

@ -1,8 +1,8 @@
# Bootmagic Lite :id=bootmagic-lite
# Bootmagic :id=bootmagic
The Bootmagic Lite feature that only handles jumping into the bootloader. This is great for boards that don't have a physical reset button, giving you a way to jump into the bootloader
The Bootmagic feature that only handles jumping into the bootloader. This is great for boards that don't have a physical reset button, giving you a way to jump into the bootloader
On some keyboards Bootmagic Lite is disabled by default. If this is the case, it must be explicitly enabled in your `rules.mk` with:
On some keyboards Bootmagic is disabled by default. If this is the case, it must be explicitly enabled in your `rules.mk` with:
```make
BOOTMAGIC_ENABLE = yes
@ -11,15 +11,15 @@ BOOTMAGIC_ENABLE = yes
Additionally, you may want to specify which key to use. This is especially useful for keyboards that have unusual matrices. To do so, you need to specify the row and column of the key that you want to use. Add these entries to your `config.h` file:
```c
#define BOOTMAGIC_LITE_ROW 0
#define BOOTMAGIC_LITE_COLUMN 1
#define BOOTMAGIC_ROW 0
#define BOOTMAGIC_COLUMN 1
```
By default, these are set to 0 and 0, which is usually the "ESC" key on a majority of keyboards.
And to trigger the bootloader, you hold this key down when plugging the keyboard in. Just the single key.
!> Using Bootmagic Lite will **always reset** the EEPROM, so you will lose any settings that have been saved.
!> Using Bootmagic will **always reset** the EEPROM, so you will lose any settings that have been saved.
## Split Keyboards
@ -44,35 +44,35 @@ When [handedness](feature_split_keyboard.md#setting-handedness) is predetermined
}
```
If you pick the top right key for the right half, it is `R05` on the top layout. Within the key matrix below, `R05` is located on row 4 columnn 4. To use that key as the right half's Bootmagic Lite trigger, add these entries to your `config.h` file:
If you pick the top right key for the right half, it is `R05` on the top layout. Within the key matrix below, `R05` is located on row 4 columnn 4. To use that key as the right half's Bootmagic trigger, add these entries to your `config.h` file:
```c
#define BOOTMAGIC_LITE_ROW_RIGHT 4
#define BOOTMAGIC_LITE_COLUMN_RIGHT 4
#define BOOTMAGIC_ROW_RIGHT 4
#define BOOTMAGIC_COLUMN_RIGHT 4
```
?> These values are not set by default.
## Advanced Bootmagic Lite
## Advanced Bootmagic
The `bootmagic_lite` function is defined weakly, so that you can replace this in your code, if you need. A great example of this is the Zeal60 boards that have some additional handling needed.
The `bootmagic_scan` function is defined weakly, so that you can replace this in your code, if you need. A great example of this is the Zeal60 boards that have some additional handling needed.
To replace the function, all you need to do is add something like this to your code:
```c
void bootmagic_lite(void) {
void bootmagic_scan(void) {
matrix_scan();
wait_ms(DEBOUNCE * 2);
matrix_scan();
if (matrix_get_row(BOOTMAGIC_LITE_ROW) & (1 << BOOTMAGIC_LITE_COLUMN)) {
if (matrix_get_row(BOOTMAGIC_ROW) & (1 << BOOTMAGIC_COLUMN)) {
// Jump to bootloader.
bootloader_jump();
}
}
```
You can define additional logic here. For instance, resetting the EEPROM or requiring additional keys to be pressed to trigger Bootmagic Lite. Keep in mind that `bootmagic_lite` is called before a majority of features are initialized in the firmware.
You can define additional logic here. For instance, resetting the EEPROM or requiring additional keys to be pressed to trigger Bootmagic. Keep in mind that `bootmagic_scan` is called before a majority of features are initialized in the firmware.
## Addenda

View File

@ -333,27 +333,26 @@ will give the _NAV layer as a reference to it's self. All other layers
will have the default for their combo reference layer. If the default
is not set, all other layers will reference themselves.
```c
#define COMBO_REF_DEFAULT _MY_COMBO_LAYER
...
```c
#define COMBO_REF_DEFAULT _MY_COMBO_LAYER
uint8_t combo_ref_from_layer(uint8_t layer){
switch (get_highest_layer(layer_state)){
case _DVORAK: return _QWERTY;
case _NAV: return _NAV;
default: return _MY_COMBO_LAYER;
}
return layer; // important if default is not in case.
uint8_t combo_ref_from_layer(uint8_t layer){
switch (get_highest_layer(layer_state)){
case _DVORAK: return _QWERTY;
case _NAV: return _NAV;
default: return _MY_COMBO_LAYER;
}
```
return layer; // important if default is not in case.
}
```
The equivalent definition using the combo macros is this:
```c
COMBO_REF_LAYER(_DVORAK, _QWERTY)
COMBO_REF_LAYER(_NAV, _NAV)
DEFAULT_REF_LAYER(_MY_COMBO_LAYER).
```
```c
COMBO_REF_LAYER(_DVORAK, _QWERTY)
COMBO_REF_LAYER(_NAV, _NAV)
DEFAULT_REF_LAYER(_MY_COMBO_LAYER).
```
## User callbacks

View File

@ -82,10 +82,10 @@ Your `keymap.c` will then need an encoder mapping defined (for four layers and t
```c
#if defined(ENCODER_MAP_ENABLE)
const uint16_t PROGMEM encoder_map[][NUM_ENCODERS][NUM_DIRECTIONS] = {
[_BASE] = { ENCODER_CCW_CW(KC_MS_WH_UP, KC_MS_WH_DOWN), ENCODER_CCW_CW(KC_VOLD, KC_VOLU) },
[_LOWER] = { ENCODER_CCW_CW(RGB_HUD, RGB_HUI), ENCODER_CCW_CW(RGB_SAD, RGB_SAI) },
[_RAISE] = { ENCODER_CCW_CW(RGB_VAD, RGB_VAI), ENCODER_CCW_CW(RGB_SPD, RGB_SPI) },
[_ADJUST] = { ENCODER_CCW_CW(RGB_RMOD, RGB_MOD), ENCODER_CCW_CW(KC_RIGHT, KC_LEFT) },
[0] = { ENCODER_CCW_CW(KC_MS_WH_UP, KC_MS_WH_DOWN), ENCODER_CCW_CW(KC_VOLD, KC_VOLU) },
[1] = { ENCODER_CCW_CW(RGB_HUD, RGB_HUI), ENCODER_CCW_CW(RGB_SAD, RGB_SAI) },
[2] = { ENCODER_CCW_CW(RGB_VAD, RGB_VAI), ENCODER_CCW_CW(RGB_SPD, RGB_SPI) },
[3] = { ENCODER_CCW_CW(RGB_RMOD, RGB_MOD), ENCODER_CCW_CW(KC_RIGHT, KC_LEFT) },
};
#endif
```

View File

@ -50,10 +50,6 @@ Axes can be configured using one of the following macros:
* `JOYSTICK_AXIS_IN(input_pin, low, rest, high)`
The ADC samples the provided pin. `low`, `high` and `rest` correspond to the minimum, maximum, and resting (or centered) analog values of the axis, respectively.
* `JOYSTICK_AXIS_IN_OUT(input_pin, output_pin, low, rest, high)`
Same as `JOYSTICK_AXIS_IN()`, but the provided `output_pin` will be pulled high before `input_pin` is read.
* `JOYSTICK_AXIS_IN_OUT_GROUND(input_pin, output_pin, ground_pin, low, rest, high)`
Same as `JOYSTICK_AXIS_IN_OUT()`, but the provided `ground_pin` will be pulled low before reading from `input_pin`.
* `JOYSTICK_AXIS_VIRTUAL`
No ADC reading is performed. The value should be provided by user code.
@ -160,12 +156,8 @@ Describes a single axis.
#### Members :id=api-joystick-config-t-members
- `pin_t output_pin`
A pin to set as output high when reading the analog value, or `JS_VIRTUAL_AXIS`.
- `pin_t input_pin`
The pin to read the analog value from, or `JS_VIRTUAL_AXIS`.
- `pin_t ground_pin`
A pin to set as output low when reading the analog value, or `JS_VIRTUAL_AXIS`.
- `uint16_t min_digit`
The minimum analog value.
- `uint16_t mid_digit`

View File

@ -56,16 +56,16 @@ This is a template indicator function that can be implemented on keyboard level
bool led_update_kb(led_t led_state) {
bool res = led_update_user(led_state);
if(res) {
// writePin sets the pin high for 1 and low for 0.
// gpio_write_pin sets the pin high for 1 and low for 0.
// In this example the pins are inverted, setting
// it low/0 turns it on, and high/1 turns the LED off.
// This behavior depends on whether the LED is between the pin
// and VCC or the pin and GND.
writePin(B0, !led_state.num_lock);
writePin(B1, !led_state.caps_lock);
writePin(B2, !led_state.scroll_lock);
writePin(B3, !led_state.compose);
writePin(B4, !led_state.kana);
gpio_write_pin(B0, !led_state.num_lock);
gpio_write_pin(B1, !led_state.caps_lock);
gpio_write_pin(B2, !led_state.scroll_lock);
gpio_write_pin(B3, !led_state.compose);
gpio_write_pin(B4, !led_state.kana);
}
return res;
}

View File

@ -142,8 +142,8 @@ const is31_led PROGMEM g_is31_leds[LED_MATRIX_LED_COUNT] = {
* driver
* | LED address
* | | */
{ 0, CS1_SW1 },
{ 0, CS2_SW1 },
{ 0, SW1_CS1 },
{ 0, SW1_CS2 },
// ...
}
```
@ -365,6 +365,7 @@ For inspiration and examples, check out the built-in effects under `quantum/led_
#define LED_MATRIX_DEFAULT_MODE LED_MATRIX_SOLID // Sets the default mode, if none has been set
#define LED_MATRIX_DEFAULT_VAL LED_MATRIX_MAXIMUM_BRIGHTNESS // Sets the default brightness value, if none has been set
#define LED_MATRIX_DEFAULT_SPD 127 // Sets the default animation speed, if none has been set
#define LED_MATRIX_DEFAULT_FLAGS LED_FLAG_ALL // Sets the default LED flags, if none has been set
#define LED_MATRIX_SPLIT { X, Y } // (Optional) For split keyboards, the number of LEDs connected on each half. X = left, Y = Right.
// If reactive effects are enabled, you also will want to enable SPLIT_TRANSPORT_MIRROR
```

View File

@ -14,7 +14,7 @@ In your `rules.mk` add:
OS_DETECTION_ENABLE = yes
```
Include `"os_detection.h"` in your `keymap.c`.
It will automatically include the required headers file.
It declares `os_variant_t detected_host_os(void);` which you can call to get detected OS.
It returns one of the following values:
@ -32,6 +32,54 @@ enum {
?> Note that it takes some time after firmware is booted to detect the OS.
This time is quite short, probably hundreds of milliseconds, but this data may be not ready in keyboard and layout setup functions which run very early during firmware startup.
## Callbacks :id=callbacks
If you want to perform custom actions when the OS is detected, then you can use the `process_detected_host_os_kb` function on the keyboard level source file, or `process_detected_host_os_user` function in the user `keymap.c`.
```c
bool process_detected_host_os_kb(os_variant_t detected_os) {
if (!process_detected_host_os_user(detected_os)) {
return false;
}
switch (detected_os) {
case OS_MACOS:
case OS_IOS:
rgb_matrix_set_color_all(RGB_WHITE);
break;
case OS_WINDOWS:
rgb_matrix_set_color_all(RGB_BLUE);
break;
case OS_LINUX:
rgb_matrix_set_color_all(RGB_ORANGE);
break;
case OS_UNSURE:
rgb_matrix_set_color_all(RGB_RED);
break;
}
return true;
}
```
## OS detection stability
The OS detection is currently handled while the USB device descriptor is being assembled.
The process is done in steps, generating a number of intermediate results until it stabilizes.
We therefore resort to debouncing the result until it has been stable for a given amount of milliseconds.
This amount can be configured, in case your board is not stable within the default debouncing time of 200ms.
## KVM and USB switches
Some KVM and USB switches may not trigger the USB controller on the keyboard to fully reset upon switching machines.
If your keyboard does not redetect the OS in this situation, you can force the keyboard to reset when the USB initialization event is detected, forcing the USB controller to be reconfigured.
## Configuration Options
* `#define OS_DETECTION_DEBOUNCE 200`
* defined the debounce time for OS detection, in milliseconds
* `#define OS_DETECTION_KEYBOARD_RESET`
* enables the keyboard reset upon a USB device reinitilization, such as switching devices on some KVMs
## Debug
If OS is guessed incorrectly, you may want to collect data about USB setup packets to refine the detection logic.

View File

@ -143,15 +143,15 @@ const is31fl3733_led_t PROGMEM g_is31fl3733_leds[IS31FL3733_LED_COUNT] = {
/* Refer to IS31 manual for these locations
* driver
* | R location
* | | G location
* | | | B location
* | | | | */
{0, B_1, A_1, C_1},
* | | G location
* | | | B location
* | | | | */
{0, SW1_CS1, SW1_CS2, SW1_CS3},
....
}
```
Where `X_Y` is the location of the LED in the matrix defined by [the datasheet](https://www.issi.com/WW/pdf/31FL3733.pdf) and the header file `drivers/led/issi/is31fl3733.h`. The `driver` is the index of the driver you defined in your `config.h` (`0`, `1`, `2`, or `3` for now).
Where `SWx_CSy` is the location of the LED in the matrix defined by [the datasheet](https://www.issi.com/WW/pdf/31FL3733.pdf) and the header file `drivers/led/issi/is31fl3733.h`. The `driver` is the index of the driver you defined in your `config.h` (`0`, `1`, `2`, or `3` for now).
---
### IS31FL3736 :id=is31fl3736
@ -222,10 +222,10 @@ const is31fl3736_led_t PROGMEM g_is31fl3736_leds[IS31FL3736_LED_COUNT] = {
/* Refer to IS31 manual for these locations
* driver
* | R location
* | | G location
* | | | B location
* | | | | */
{0, B_1, A_1, C_1},
* | | G location
* | | | B location
* | | | | */
{0, SW1_CS1, SW1_CS2, SW1_CS3},
....
}
```
@ -296,15 +296,15 @@ const is31fl3737_led_t PROGMEM g_is31fl3737_leds[IS31FL3737_LED_COUNT] = {
/* Refer to IS31 manual for these locations
* driver
* | R location
* | | G location
* | | | B location
* | | | | */
{0, B_1, A_1, C_1},
* | | G location
* | | | B location
* | | | | */
{0, SW1_CS1, SW1_CS2, SW1_CS3},
....
}
```
Where `X_Y` is the location of the LED in the matrix defined by [the datasheet](https://www.issi.com/WW/pdf/31FL3737.pdf) and the header file `drivers/led/issi/is31fl3737.h`. The `driver` is the index of the driver you defined in your `config.h` (Only `0`, `1`, `2`, or `3` for now).
Where `SWx_CSy` is the location of the LED in the matrix defined by [the datasheet](https://www.issi.com/WW/pdf/31FL3737.pdf) and the header file `drivers/led/issi/is31fl3737.h`. The `driver` is the index of the driver you defined in your `config.h` (Only `0`, `1`, `2`, or `3` for now).
---
### IS31FLCOMMON :id=is31flcommon
@ -386,7 +386,7 @@ const is31_led PROGMEM g_is31_leds[RGB_MATRIX_LED_COUNT] = {
* | | G location
* | | | B location
* | | | | */
{0, CS1_SW1, CS2_SW1, CS3_SW1},
{0, SW1_CS1, SW1_CS2, SW1_CS3},
....
}
```
@ -511,16 +511,16 @@ const aw20216s_led_t PROGMEM g_aw20216s_leds[AW20216S_LED_COUNT] = {
* | | G location
* | | | B location
* | | | | */
{ 0, CS1_SW1, CS2_SW1, CS3_SW1 },
{ 0, CS4_SW1, CS5_SW1, CS6_SW1 },
{ 0, CS7_SW1, CS8_SW1, CS9_SW1 },
{ 0, CS10_SW1, CS11_SW1, CS12_SW1 },
{ 0, CS13_SW1, CS14_SW1, CS15_SW1 },
{ 0, SW1_CS1, SW1_CS2, SW1_CS3 },
{ 0, SW1_CS4, SW1_CS5, SW1_CS6 },
{ 0, SW1_CS7, SW1_CS8, SW1_CS9 },
{ 0, SW1_CS10, SW1_CS11, SW1_CS12 },
{ 0, SW1_CS13, SW1_CS14, SW1_CS15 },
...
{ 1, CS1_SW1, CS2_SW1, CS3_SW1 },
{ 1, CS13_SW1, CS14_SW1, CS15_SW1 },
{ 1, CS16_SW1, CS17_SW1, CS18_SW1 },
{ 1, CS4_SW2, CS5_SW2, CS6_SW2 },
{ 1, SW1_CS1, SW1_CS2, SW1_CS3 },
{ 1, SW1_CS13, SW1_CS14, SW1_CS15 },
{ 1, SW1_CS16, SW1_CS17, SW1_CS18 },
{ 1, SW2_CS4, SW2_CS5, SW2_CS6 },
...
};
```
@ -873,12 +873,13 @@ These are defined in [`color.h`](https://github.com/qmk/qmk_firmware/blob/master
#define RGB_MATRIX_LED_PROCESS_LIMIT (RGB_MATRIX_LED_COUNT + 4) / 5 // limits the number of LEDs to process in an animation per task run (increases keyboard responsiveness)
#define RGB_MATRIX_LED_FLUSH_LIMIT 16 // limits in milliseconds how frequently an animation will update the LEDs. 16 (16ms) is equivalent to limiting to 60fps (increases keyboard responsiveness)
#define RGB_MATRIX_MAXIMUM_BRIGHTNESS 200 // limits maximum brightness of LEDs to 200 out of 255. If not defined maximum brightness is set to 255
#define RGB_MATRIX_DEFAULT_ON true // Sets the default enabled state, if none has been set
#define RGB_MATRIX_DEFAULT_MODE RGB_MATRIX_CYCLE_LEFT_RIGHT // Sets the default mode, if none has been set
#define RGB_MATRIX_DEFAULT_HUE 0 // Sets the default hue value, if none has been set
#define RGB_MATRIX_DEFAULT_SAT 255 // Sets the default saturation value, if none has been set
#define RGB_MATRIX_DEFAULT_ON true // Sets the default enabled state, if none has been set
#define RGB_MATRIX_DEFAULT_VAL RGB_MATRIX_MAXIMUM_BRIGHTNESS // Sets the default brightness value, if none has been set
#define RGB_MATRIX_DEFAULT_SPD 127 // Sets the default animation speed, if none has been set
#define RGB_MATRIX_DEFAULT_FLAGS LED_FLAG_ALL // Sets the default LED flags, if none has been set
#define RGB_MATRIX_DISABLE_KEYCODES // disables control of rgb matrix by keycodes (must use code functions to control the feature)
#define RGB_MATRIX_SPLIT { X, Y } // (Optional) For split keyboards, the number of LEDs connected on each half. X = left, Y = Right.
// If reactive effects are enabled, you also will want to enable SPLIT_TRANSPORT_MIRROR

View File

@ -2,29 +2,29 @@
QMK has a GPIO control abstraction layer which is microcontroller agnostic. This is done to allow easy access to pin control across different platforms.
## Functions :id=functions
## Macros :id=macros
The following functions provide basic control of GPIOs and are found in `platforms/<platform>/gpio.h`.
The following macros provide basic control of GPIOs and are found in `platforms/<platform>/gpio.h`.
| Function | Description | Old AVR Examples | Old ChibiOS/ARM Examples |
|------------------------------|-----------------------------------------------------|-------------------------------------------------|--------------------------------------------------|
| `setPinInput(pin)` | Set pin as input with high impedance (High-Z) | `DDRB &= ~(1<<2)` | `palSetLineMode(pin, PAL_MODE_INPUT)` |
| `setPinInputHigh(pin)` | Set pin as input with builtin pull-up resistor | `DDRB &= ~(1<<2); PORTB \|= (1<<2)` | `palSetLineMode(pin, PAL_MODE_INPUT_PULLUP)` |
| `setPinInputLow(pin)` | Set pin as input with builtin pull-down resistor | N/A (Not supported on AVR) | `palSetLineMode(pin, PAL_MODE_INPUT_PULLDOWN)` |
| `setPinOutput(pin)` | Set pin as output (alias of `setPinOutputPushPull`) | `DDRB \|= (1<<2)` | `palSetLineMode(pin, PAL_MODE_OUTPUT_PUSHPULL)` |
| `setPinOutputPushPull(pin)` | Set pin as output, push/pull mode | `DDRB \|= (1<<2)` | `palSetLineMode(pin, PAL_MODE_OUTPUT_PUSHPULL)` |
| `setPinOutputOpenDrain(pin)` | Set pin as output, open-drain mode | N/A (Not implemented on AVR) | `palSetLineMode(pin, PAL_MODE_OUTPUT_OPENDRAIN)` |
| `writePinHigh(pin)` | Set pin level as high, assuming it is an output | `PORTB \|= (1<<2)` | `palSetLine(pin)` |
| `writePinLow(pin)` | Set pin level as low, assuming it is an output | `PORTB &= ~(1<<2)` | `palClearLine(pin)` |
| `writePin(pin, level)` | Set pin level, assuming it is an output | `(level) ? PORTB \|= (1<<2) : PORTB &= ~(1<<2)` | `(level) ? palSetLine(pin) : palClearLine(pin)` |
| `readPin(pin)` | Returns the level of the pin | `_SFR_IO8(pin >> 4) & _BV(pin & 0xF)` | `palReadLine(pin)` |
| `togglePin(pin)` | Invert pin level, assuming it is an output | `PORTB ^= (1<<2)` | `palToggleLine(pin)` |
|Macro |Description |
|-------------------------------------|---------------------------------------------------------------------|
|`gpio_set_pin_input(pin)` |Set pin as input with high impedance (High-Z) |
|`gpio_set_pin_input_high(pin)` |Set pin as input with builtin pull-up resistor |
|`gpio_set_pin_input_low(pin)` |Set pin as input with builtin pull-down resistor (unavailable on AVR)|
|`gpio_set_pin_output(pin)` |Set pin as output (alias of `gpio_set_pin_output_push_pull`) |
|`gpio_set_pin_output_push_pull(pin)` |Set pin as output, push/pull mode |
|`gpio_set_pin_output_open_drain(pin)`|Set pin as output, open-drain mode (unavailable on AVR and ATSAM) |
|`gpio_write_pin_high(pin)` |Set pin level as high, assuming it is an output |
|`gpio_write_pin_low(pin)` |Set pin level as low, assuming it is an output |
|`gpio_write_pin(pin, level)` |Set pin level, assuming it is an output |
|`gpio_read_pin(pin)` |Returns the level of the pin |
|`gpio_toggle_pin(pin)` |Invert pin level, assuming it is an output |
## Advanced Settings :id=advanced-settings
Each microcontroller can have multiple advanced settings regarding its GPIO. This abstraction layer does not limit the use of architecture-specific functions. Advanced users should consult the datasheet of their desired device and include any needed libraries. For AVR, the standard avr/io.h library is used; for STM32, the ChibiOS [PAL library](https://chibios.sourceforge.net/docs3/hal/group___p_a_l.html) is used.
Each microcontroller can have multiple advanced settings regarding its GPIO. This abstraction layer does not limit the use of architecture-specific functions. Advanced users should consult the datasheet of their desired device. For AVR, the standard `avr/io.h` library is used; for STM32, the ChibiOS [PAL library](https://chibios.sourceforge.net/docs3/hal/group___p_a_l.html) is used.
## Atomic Operation
## Atomic Operation :id=atomic-operation
The above functions are not always guaranteed to work atomically. Therefore, if you want to prevent interruptions in the middle of operations when using multiple combinations of the above functions, use the following `ATOMIC_BLOCK_FORCEON` macro.

View File

@ -127,8 +127,8 @@ This function is weakly defined, meaning it can be overridden if necessary for y
```c
void i2c_init(void) {
setPinInput(B6); // Try releasing special pins for a short time
setPinInput(B7);
gpio_set_pin_input(B6); // Try releasing special pins for a short time
gpio_set_pin_input(B7);
wait_ms(10); // Wait for the release to happen
palSetPadMode(GPIOB, 6, PAL_MODE_ALTERNATE(4) | PAL_STM32_OTYPE_OPENDRAIN | PAL_STM32_PUPDR_PULLUP); // Set B6 to I2C function

View File

@ -139,8 +139,8 @@ BOOTMAGIC_ENABLE = lite
さらに、どのキーを使うかを指定したほうが良いかもしれません。これは普通ではないマトリックスを持つキーボードで特に便利です。そのためには、使いたいキーの行と列を指定する必要があります。`config.h` ファイルにこれらのエントリを追加します:
```c
#define BOOTMAGIC_LITE_ROW 0
#define BOOTMAGIC_LITE_COLUMN 1
#define BOOTMAGIC_ROW 0
#define BOOTMAGIC_COLUMN 1
```
デフォルトでは、これらは 0 と 0 に設定されます。これは通常はほとんどのキーボードで "ESC" キーです。
@ -154,8 +154,8 @@ BOOTMAGIC_ENABLE = lite
`SPLIT_HAND_PIN` のようなオプションで、左右の設定があらかじめ決められている場合は、キーボードの左右で別のキーを設定する必要があるかもしれません。これを行うには、`config.h` ファイルに以下のエントリを追加します。
```c
#define BOOTMAGIC_LITE_ROW_RIGHT 4
#define BOOTMAGIC_LITE_COLUMN_RIGHT 1
#define BOOTMAGIC_ROW_RIGHT 4
#define BOOTMAGIC_COLUMN_RIGHT 1
```
デフォルトでは、これらの値は設定されていません。
@ -172,7 +172,7 @@ void bootmagic_lite(void) {
wait_ms(DEBOUNCE * 2);
matrix_scan();
if (matrix_get_row(BOOTMAGIC_LITE_ROW) & (1 << BOOTMAGIC_LITE_COLUMN)) {
if (matrix_get_row(BOOTMAGIC_ROW) & (1 << BOOTMAGIC_COLUMN)) {
// ブートローダにジャンプする。
bootloader_jump();
}

View File

@ -2,7 +2,7 @@
The following table shows the current driver status for peripherals on RP2040 MCUs:
| System | Support |
| System | Support |
| ---------------------------------------------------------------- | ---------------------------------------------- |
| [ADC driver](adc_driver.md) | :heavy_check_mark: |
| [Audio](audio_driver.md#pwm-hardware) | :heavy_check_mark: |
@ -13,7 +13,7 @@ The following table shows the current driver status for peripherals on RP2040 MC
| [External EEPROMs](eeprom_driver.md) | :heavy_check_mark: using `I2C` or `SPI` driver |
| [EEPROM emulation](eeprom_driver.md#wear_leveling-configuration) | :heavy_check_mark: |
| [serial driver](serial_driver.md) | :heavy_check_mark: using `SIO` or `PIO` driver |
| [UART driver](uart_driver.md) | Support planned (no ETA) |
| [UART driver](uart_driver.md) | :heavy_check_mark: using `SIO` driver |
## GPIO
@ -52,6 +52,13 @@ To configure the I2C driver please read the [ChibiOS/ARM](i2c_driver.md#arm-conf
To configure the SPI driver please read the [ChibiOS/ARM](spi_driver.md#chibiosarm-configuration) section.
### UART Driver
| RP2040 Peripheral | `mcuconf.h` values | `UART_DRIVER` |
| ----------------- | ------------------ | ------------- |
| `UART0` | `RP_SIO_USE_UART0` | `SIOD0` |
| `UART1` | `RP_SIO_USE_UART1` | `SIOD1` |
## Double-tap reset boot-loader entry :id=double-tap
The double-tap reset mechanism is an alternate way in QMK to enter the embedded mass storage UF2 boot-loader of the RP2040. It enables bootloader entry by a fast double-tap of the reset pin on start up, which is similar to the behavior of AVR Pro Micros. This feature activated by default for the Pro Micro RP2040 board, but has to be configured for other boards. To activate it, add the following options to your keyboards `config.h` file:
@ -87,6 +94,10 @@ This is the default board that is chosen, unless any other RP2040 board is selec
| `SOFT_SERIAL_PIN` | undefined, use `SERIAL_USART_TX_PIN` |
| `SERIAL_USART_TX_PIN` | `GP0` |
| `SERIAL_USART_RX_PIN` | `GP1` |
| **UART driver** | |
| `UART_DRIVER` | `SIOD0` |
| `UART_TX_PIN` | `GP0` |
| `UART_RX_PIN` | `GP1` |
?> The pin-outs of Adafruit's KB2040 and Boardsource's Blok both deviate from the Sparkfun Pro Micro RP2040. Lookup the pin-out of these boards and adjust your keyboards pin definition accordingly if you want to use these boards.

View File

@ -24,6 +24,7 @@ Supported devices:
| GC9A01 | RGB LCD (circular) | 240x240 | SPI + D/C + RST | `QUANTUM_PAINTER_DRIVERS += gc9a01_spi` |
| ILI9163 | RGB LCD | 128x128 | SPI + D/C + RST | `QUANTUM_PAINTER_DRIVERS += ili9163_spi` |
| ILI9341 | RGB LCD | 240x320 | SPI + D/C + RST | `QUANTUM_PAINTER_DRIVERS += ili9341_spi` |
| ILI9486 | RGB LCD | 320x480 | SPI + D/C + RST | `QUANTUM_PAINTER_DRIVERS += ili9486_spi` |
| ILI9488 | RGB LCD | 320x480 | SPI + D/C + RST | `QUANTUM_PAINTER_DRIVERS += ili9488_spi` |
| SSD1351 | RGB OLED | 128x128 | SPI + D/C + RST | `QUANTUM_PAINTER_DRIVERS += ssd1351_spi` |
| ST7735 | RGB LCD | 132x162, 80x160 | SPI + D/C + RST | `QUANTUM_PAINTER_DRIVERS += st7735_spi` |
@ -281,6 +282,39 @@ The maximum number of displays can be configured by changing the following in yo
Native color format rgb565 is compatible with ILI9341
#### ** ILI9486 **
Enabling support for the ILI9486 in Quantum Painter is done by adding the following to `rules.mk`:
```make
QUANTUM_PAINTER_ENABLE = yes
QUANTUM_PAINTER_DRIVERS += ili9486_spi
```
Creating a ILI9486 device in firmware can then be done with the following API:
```c
painter_device_t qp_ili9486_make_spi_device(uint16_t panel_width, uint16_t panel_height, pin_t chip_select_pin, pin_t dc_pin, pin_t reset_pin, uint16_t spi_divisor, int spi_mode);
```
There's another variant for this [Waveshare module](https://www.waveshare.com/wiki/3.5inch_TFT_Touch_Shield), because it has a quirky SPI->Parallel converter. You can create it with:
```c
painter_device_t qp_ili9486_make_spi_waveshare_device(uint16_t panel_width, uint16_t panel_height, pin_t chip_select_pin, pin_t dc_pin, pin_t reset_pin, uint16_t spi_divisor, int spi_mode);
```
The device handle returned from these functions can be used to perform all other drawing operations.
The maximum number of displays can be configured by changing the following in your `config.h` (default is 1):
```c
// 3 displays:
#define ILI9486_NUM_DEVICES 3
```
Native color format rgb888 is compatible with ILI9486
Native color format rgb565 is compatible with ILI9486 Waveshare
#### ** ILI9488 **
Enabling support for the ILI9488 in Quantum Painter is done by adding the following to `rules.mk`:

View File

@ -633,7 +633,7 @@ Configures the [RGB Matrix](feature_rgb_matrix.md) feature.
* The default animation speed.
* Default: `128`
* `driver` (Required)
* The driver to use. Must be one of `aw20216s`, `custom`, `is31fl3218`, `is31fl3731`, `is31fl3733`, `is31fl3736`, `is31fl3737`, `is31fl3741`, `is31fl3742a`, `is31fl3743a`, `is31fl3745`, `is31fl3746a`, `snled27351`, `ws2812`.
* The driver to use. Must be one of `aw20216s`, `custom`, `is31fl3218`, `is31fl3729`, `is31fl3731`, `is31fl3733`, `is31fl3736`, `is31fl3737`, `is31fl3741`, `is31fl3742a`, `is31fl3743a`, `is31fl3745`, `is31fl3746a`, `snled27351`, `ws2812`.
* `hue_steps`
* The number of hue adjustment steps.
* Default: `8`

View File

@ -32,13 +32,7 @@ No special setup is required - just connect the `RX` and `TX` pins of your UART
You'll need to determine which pins can be used for UART -- as an example, STM32 parts generally have multiple UART peripherals, labeled USART1, USART2, USART3 etc.
To enable UART, modify your board's `halconf.h` to enable the serial driver:
```c
#define HAL_USE_SERIAL TRUE
```
Then, modify your board's `mcuconf.h` to enable the peripheral you've chosen, for example:
To enable UART, modify your board's `mcuconf.h` to enable the peripheral you've chosen, for example:
```c
#undef STM32_SERIAL_USE_USART2
@ -47,17 +41,17 @@ Then, modify your board's `mcuconf.h` to enable the peripheral you've chosen, fo
Configuration-wise, you'll need to set up the peripheral as per your MCU's datasheet -- the defaults match the pins for a Proton-C, i.e. STM32F303.
|`config.h` override |Description |Default Value|
|--------------------------|---------------------------------------------------------------|-------------|
|`#define SERIAL_DRIVER` |USART peripheral to use - USART1 -> `SD1`, USART2 -> `SD2` etc.|`SD1` |
|`#define SD1_TX_PIN` |The pin to use for TX |`A9` |
|`#define SD1_TX_PAL_MODE` |The alternate function mode for TX |`7` |
|`#define SD1_RX_PIN` |The pin to use for RX |`A10` |
|`#define SD1_RX_PAL_MODE` |The alternate function mode for RX |`7` |
|`#define SD1_CTS_PIN` |The pin to use for CTS |`A11` |
|`#define SD1_CTS_PAL_MODE`|The alternate function mode for CTS |`7` |
|`#define SD1_RTS_PIN` |The pin to use for RTS |`A12` |
|`#define SD1_RTS_PAL_MODE`|The alternate function mode for RTS |`7` |
| `config.h` override | Description | Default Value |
| --------------------------- | --------------------------------------------------------------- | ------------- |
| `#define UART_DRIVER` | USART peripheral to use - USART1 -> `SD1`, USART2 -> `SD2` etc. | `SD1` |
| `#define UART_TX_PIN` | The pin to use for TX | `A9` |
| `#define UART_TX_PAL_MODE` | The alternate function mode for TX | `7` |
| `#define UART_RX_PIN` | The pin to use for RX | `A10` |
| `#define UART_RX_PAL_MODE` | The alternate function mode for RX | `7` |
| `#define UART_CTS_PIN` | The pin to use for CTS | `A11` |
| `#define UART_CTS_PAL_MODE` | The alternate function mode for CTS | `7` |
| `#define UART_RTS_PIN` | The pin to use for RTS | `A12` |
| `#define UART_RTS_PAL_MODE` | The alternate function mode for RTS | `7` |
## API :id=api

View File

@ -188,7 +188,7 @@ static bool sdep_recv_pkt(struct sdep_msg *msg, uint16_t timeout) {
bool ready = false;
do {
ready = readPin(BLUEFRUIT_LE_IRQ_PIN);
ready = gpio_read_pin(BLUEFRUIT_LE_IRQ_PIN);
if (ready) {
break;
}
@ -231,7 +231,7 @@ static void resp_buf_read_one(bool greedy) {
return;
}
if (readPin(BLUEFRUIT_LE_IRQ_PIN)) {
if (gpio_read_pin(BLUEFRUIT_LE_IRQ_PIN)) {
struct sdep_msg msg;
again:
@ -242,7 +242,7 @@ static void resp_buf_read_one(bool greedy) {
dprintf("recv latency %dms\n", TIMER_DIFF_16(timer_read(), last_send));
}
if (greedy && resp_buf.peek(last_send) && readPin(BLUEFRUIT_LE_IRQ_PIN)) {
if (greedy && resp_buf.peek(last_send) && gpio_read_pin(BLUEFRUIT_LE_IRQ_PIN)) {
goto again;
}
}
@ -293,16 +293,16 @@ void bluefruit_le_init(void) {
state.configured = false;
state.is_connected = false;
setPinInput(BLUEFRUIT_LE_IRQ_PIN);
gpio_set_pin_input(BLUEFRUIT_LE_IRQ_PIN);
spi_init();
// Perform a hardware reset
setPinOutput(BLUEFRUIT_LE_RST_PIN);
writePinHigh(BLUEFRUIT_LE_RST_PIN);
writePinLow(BLUEFRUIT_LE_RST_PIN);
gpio_set_pin_output(BLUEFRUIT_LE_RST_PIN);
gpio_write_pin_high(BLUEFRUIT_LE_RST_PIN);
gpio_write_pin_low(BLUEFRUIT_LE_RST_PIN);
wait_ms(10);
writePinHigh(BLUEFRUIT_LE_RST_PIN);
gpio_write_pin_high(BLUEFRUIT_LE_RST_PIN);
wait_ms(1000); // Give it a second to initialize
@ -508,7 +508,7 @@ void bluefruit_le_task(void) {
resp_buf_read_one(true);
send_buf_send_one(SdepShortTimeout);
if (resp_buf.empty() && (state.event_flags & UsingEvents) && readPin(BLUEFRUIT_LE_IRQ_PIN)) {
if (resp_buf.empty() && (state.event_flags & UsingEvents) && gpio_read_pin(BLUEFRUIT_LE_IRQ_PIN)) {
// Must be an event update
if (at_command_P(PSTR("AT+EVENTSTATUS"), resbuf, sizeof(resbuf))) {
uint32_t mask = strtoul(resbuf, NULL, 16);

View File

@ -57,8 +57,8 @@ void eeprom_driver_init(void) {
i2c_init();
#if defined(EXTERNAL_EEPROM_WP_PIN)
/* We are setting the WP pin to high in a way that requires at least two bit-flips to change back to 0 */
writePin(EXTERNAL_EEPROM_WP_PIN, 1);
setPinInputHigh(EXTERNAL_EEPROM_WP_PIN);
gpio_write_pin(EXTERNAL_EEPROM_WP_PIN, 1);
gpio_set_pin_input_high(EXTERNAL_EEPROM_WP_PIN);
#endif
}
@ -100,8 +100,8 @@ void eeprom_write_block(const void *buf, void *addr, size_t len) {
uintptr_t target_addr = (uintptr_t)addr;
#if defined(EXTERNAL_EEPROM_WP_PIN)
setPinOutput(EXTERNAL_EEPROM_WP_PIN);
writePin(EXTERNAL_EEPROM_WP_PIN, 0);
gpio_set_pin_output(EXTERNAL_EEPROM_WP_PIN);
gpio_write_pin(EXTERNAL_EEPROM_WP_PIN, 0);
#endif
while (len > 0) {
@ -134,7 +134,7 @@ void eeprom_write_block(const void *buf, void *addr, size_t len) {
#if defined(EXTERNAL_EEPROM_WP_PIN)
/* We are setting the WP pin to high in a way that requires at least two bit-flips to change back to 0 */
writePin(EXTERNAL_EEPROM_WP_PIN, 1);
setPinInputHigh(EXTERNAL_EEPROM_WP_PIN);
gpio_write_pin(EXTERNAL_EEPROM_WP_PIN, 1);
gpio_set_pin_input_high(EXTERNAL_EEPROM_WP_PIN);
#endif
}

View File

@ -0,0 +1,213 @@
// Copyright 2018 Jack Humbert <jack.humb@gmail.com>
// Copyright 2018-2023 Nick Brassel (@tzarc)
// SPDX-License-Identifier: GPL-2.0-or-later
#include <stdint.h>
#include "encoder.h"
#include "gpio.h"
#include "keyboard.h"
#include "action.h"
#include "keycodes.h"
#include "wait.h"
#ifdef SPLIT_KEYBOARD
# include "split_util.h"
#endif
// for memcpy
#include <string.h>
#if !defined(ENCODER_RESOLUTIONS) && !defined(ENCODER_RESOLUTION)
# define ENCODER_RESOLUTION 4
#endif
#undef ENCODER_DEFAULT_PIN_API_IMPL
#if defined(ENCODERS_PAD_A) && defined(ENCODERS_PAD_B)
// Inform the quadrature driver that it needs to implement pin init/read functions
# define ENCODER_DEFAULT_PIN_API_IMPL
#endif
extern volatile bool isLeftHand;
__attribute__((weak)) void encoder_quadrature_init_pin(uint8_t index, bool pad_b);
__attribute__((weak)) uint8_t encoder_quadrature_read_pin(uint8_t index, bool pad_b);
#ifdef ENCODER_DEFAULT_PIN_API_IMPL
static pin_t encoders_pad_a[NUM_ENCODERS_MAX_PER_SIDE] = ENCODERS_PAD_A;
static pin_t encoders_pad_b[NUM_ENCODERS_MAX_PER_SIDE] = ENCODERS_PAD_B;
__attribute__((weak)) void encoder_wait_pullup_charge(void) {
wait_us(100);
}
__attribute__((weak)) void encoder_quadrature_init_pin(uint8_t index, bool pad_b) {
pin_t pin = pad_b ? encoders_pad_b[index] : encoders_pad_a[index];
if (pin != NO_PIN) {
gpio_set_pin_input_high(pin);
}
}
__attribute__((weak)) uint8_t encoder_quadrature_read_pin(uint8_t index, bool pad_b) {
pin_t pin = pad_b ? encoders_pad_b[index] : encoders_pad_a[index];
if (pin != NO_PIN) {
return gpio_read_pin(pin) ? 1 : 0;
}
return 0;
}
#endif // ENCODER_DEFAULT_PIN_API_IMPL
#ifdef ENCODER_RESOLUTIONS
static uint8_t encoder_resolutions[NUM_ENCODERS] = ENCODER_RESOLUTIONS;
#endif
#ifndef ENCODER_DIRECTION_FLIP
# define ENCODER_CLOCKWISE true
# define ENCODER_COUNTER_CLOCKWISE false
#else
# define ENCODER_CLOCKWISE false
# define ENCODER_COUNTER_CLOCKWISE true
#endif
static int8_t encoder_LUT[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
static uint8_t encoder_state[NUM_ENCODERS] = {0};
static int8_t encoder_pulses[NUM_ENCODERS] = {0};
// encoder counts
static uint8_t thisCount;
#ifdef SPLIT_KEYBOARD
// encoder offsets for each hand
static uint8_t thisHand, thatHand;
// encoder counts for each hand
static uint8_t thatCount;
#endif
__attribute__((weak)) void encoder_quadrature_post_init_kb(void) {
extern void encoder_quadrature_handle_read(uint8_t index, uint8_t pin_a_state, uint8_t pin_b_state);
// Unused normally, but can be used for things like setting up pin-change interrupts in keyboard code.
// During the interrupt, read the pins then call `encoder_handle_read()` with the pin states and it'll queue up an encoder event if needed.
}
void encoder_quadrature_post_init(void) {
#ifdef ENCODER_DEFAULT_PIN_API_IMPL
for (uint8_t i = 0; i < thisCount; i++) {
encoder_quadrature_init_pin(i, false);
encoder_quadrature_init_pin(i, true);
}
encoder_wait_pullup_charge();
for (uint8_t i = 0; i < thisCount; i++) {
encoder_state[i] = (encoder_quadrature_read_pin(i, false) << 0) | (encoder_quadrature_read_pin(i, true) << 1);
}
#else
memset(encoder_state, 0, sizeof(encoder_state));
#endif
encoder_quadrature_post_init_kb();
}
void encoder_driver_init(void) {
#ifdef SPLIT_KEYBOARD
thisHand = isLeftHand ? 0 : NUM_ENCODERS_LEFT;
thatHand = NUM_ENCODERS_LEFT - thisHand;
thisCount = isLeftHand ? NUM_ENCODERS_LEFT : NUM_ENCODERS_RIGHT;
thatCount = isLeftHand ? NUM_ENCODERS_RIGHT : NUM_ENCODERS_LEFT;
#else // SPLIT_KEYBOARD
thisCount = NUM_ENCODERS;
#endif
#ifdef ENCODER_TESTS
// Annoying that we have to clear out values during initialisation here, but
// because all the arrays are static locals, rerunning tests in the same
// executable doesn't reset any of these. Kinda crappy having test-only code
// here, but it's the simplest solution.
memset(encoder_state, 0, sizeof(encoder_state));
memset(encoder_pulses, 0, sizeof(encoder_pulses));
const pin_t encoders_pad_a_left[] = ENCODERS_PAD_A;
const pin_t encoders_pad_b_left[] = ENCODERS_PAD_B;
for (uint8_t i = 0; i < thisCount; i++) {
encoders_pad_a[i] = encoders_pad_a_left[i];
encoders_pad_b[i] = encoders_pad_b_left[i];
}
#endif
#if defined(SPLIT_KEYBOARD) && defined(ENCODERS_PAD_A_RIGHT) && defined(ENCODERS_PAD_B_RIGHT)
// Re-initialise the pads if it's the right-hand side
if (!isLeftHand) {
const pin_t encoders_pad_a_right[] = ENCODERS_PAD_A_RIGHT;
const pin_t encoders_pad_b_right[] = ENCODERS_PAD_B_RIGHT;
for (uint8_t i = 0; i < thisCount; i++) {
encoders_pad_a[i] = encoders_pad_a_right[i];
encoders_pad_b[i] = encoders_pad_b_right[i];
}
}
#endif // defined(SPLIT_KEYBOARD) && defined(ENCODERS_PAD_A_RIGHT) && defined(ENCODERS_PAD_B_RIGHT)
// Encoder resolutions is defined differently in config.h, so concatenate
#if defined(SPLIT_KEYBOARD) && defined(ENCODER_RESOLUTIONS)
# if defined(ENCODER_RESOLUTIONS_RIGHT)
static const uint8_t encoder_resolutions_right[NUM_ENCODERS_RIGHT] = ENCODER_RESOLUTIONS_RIGHT;
# else // defined(ENCODER_RESOLUTIONS_RIGHT)
static const uint8_t encoder_resolutions_right[NUM_ENCODERS_RIGHT] = ENCODER_RESOLUTIONS;
# endif // defined(ENCODER_RESOLUTIONS_RIGHT)
for (uint8_t i = 0; i < NUM_ENCODERS_RIGHT; i++) {
encoder_resolutions[NUM_ENCODERS_LEFT + i] = encoder_resolutions_right[i];
}
#endif // defined(SPLIT_KEYBOARD) && defined(ENCODER_RESOLUTIONS)
encoder_quadrature_post_init();
}
static void encoder_handle_state_change(uint8_t index, uint8_t state) {
uint8_t i = index;
#ifdef SPLIT_KEYBOARD
index += thisHand;
#endif
#ifdef ENCODER_RESOLUTIONS
const uint8_t resolution = encoder_resolutions[index];
#else
const uint8_t resolution = ENCODER_RESOLUTION;
#endif
encoder_pulses[i] += encoder_LUT[state & 0xF];
#ifdef ENCODER_DEFAULT_POS
if ((encoder_pulses[i] >= resolution) || (encoder_pulses[i] <= -resolution) || ((state & 0x3) == ENCODER_DEFAULT_POS)) {
if (encoder_pulses[i] >= 1) {
#else
if (encoder_pulses[i] >= resolution) {
#endif
encoder_queue_event(index, ENCODER_COUNTER_CLOCKWISE);
}
#ifdef ENCODER_DEFAULT_POS
if (encoder_pulses[i] <= -1) {
#else
if (encoder_pulses[i] <= -resolution) { // direction is arbitrary here, but this clockwise
#endif
encoder_queue_event(index, ENCODER_CLOCKWISE);
}
encoder_pulses[i] %= resolution;
#ifdef ENCODER_DEFAULT_POS
encoder_pulses[i] = 0;
}
#endif
}
void encoder_quadrature_handle_read(uint8_t index, uint8_t pin_a_state, uint8_t pin_b_state) {
uint8_t state = pin_a_state | (pin_b_state << 1);
if ((encoder_state[index] & 0x3) != state) {
encoder_state[index] <<= 2;
encoder_state[index] |= state;
encoder_handle_state_change(index, encoder_state[index]);
}
}
__attribute__((weak)) void encoder_driver_task(void) {
for (uint8_t i = 0; i < thisCount; i++) {
encoder_quadrature_handle_read(i, encoder_quadrature_read_pin(i, false), encoder_quadrature_read_pin(i, true));
}
}

View File

@ -27,39 +27,39 @@ static const pin_t address_pins[ADDRESS_PIN_COUNT] = SN74X138_ADDRESS_PINS;
void sn74x138_init(void) {
for (int i = 0; i < ADDRESS_PIN_COUNT; i++) {
setPinOutput(address_pins[i]);
writePinLow(address_pins[i]);
gpio_set_pin_output(address_pins[i]);
gpio_write_pin_low(address_pins[i]);
}
#if defined(SN74X138_E1_PIN)
setPinOutput(SN74X138_E1_PIN);
writePinHigh(SN74X138_E1_PIN);
gpio_set_pin_output(SN74X138_E1_PIN);
gpio_write_pin_high(SN74X138_E1_PIN);
#endif
#if defined(SN74X138_E2_PIN)
setPinOutput(SN74X138_E2_PIN);
writePinHigh(SN74X138_E2_PIN);
gpio_set_pin_output(SN74X138_E2_PIN);
gpio_write_pin_high(SN74X138_E2_PIN);
#endif
#if defined(SN74X138_E3_PIN)
setPinOutput(SN74X138_E3_PIN);
writePinLow(SN74X138_E3_PIN);
gpio_set_pin_output(SN74X138_E3_PIN);
gpio_write_pin_low(SN74X138_E3_PIN);
#endif
}
void sn74x138_set_enabled(bool enabled) {
#if defined(SN74X138_E1_PIN)
writePin(SN74X138_E1_PIN, !enabled);
gpio_write_pin(SN74X138_E1_PIN, !enabled);
#endif
#if defined(SN74X138_E2_PIN)
writePin(SN74X138_E2_PIN, !enabled);
gpio_write_pin(SN74X138_E2_PIN, !enabled);
#endif
#if defined(SN74X138_E3_PIN)
writePin(SN74X138_E3_PIN, enabled);
gpio_write_pin(SN74X138_E3_PIN, enabled);
#endif
}
void sn74x138_set_addr(uint8_t address) {
for (int i = 0; i < ADDRESS_PIN_COUNT; i++) {
writePin(address_pins[i], address & (1 << i));
gpio_write_pin(address_pins[i], address & (1 << i));
}
}

View File

@ -27,32 +27,32 @@ static const pin_t address_pins[ADDRESS_PIN_COUNT] = SN74X154_ADDRESS_PINS;
void sn74x154_init(void) {
for (int i = 0; i < ADDRESS_PIN_COUNT; i++) {
setPinOutput(address_pins[i]);
writePinLow(address_pins[i]);
gpio_set_pin_output(address_pins[i]);
gpio_write_pin_low(address_pins[i]);
}
#if defined(SN74X154_E0_PIN)
setPinOutput(SN74X154_E0_PIN);
writePinHigh(SN74X154_E0_PIN);
gpio_set_pin_output(SN74X154_E0_PIN);
gpio_write_pin_high(SN74X154_E0_PIN);
#endif
#if defined(SN74X154_E1_PIN)
setPinOutput(SN74X154_E1_PIN);
writePinHigh(SN74X154_E1_PIN);
gpio_set_pin_output(SN74X154_E1_PIN);
gpio_write_pin_high(SN74X154_E1_PIN);
#endif
}
void sn74x154_set_enabled(bool enabled) {
#if defined(SN74X154_E0_PIN)
writePin(SN74X154_E0_PIN, !enabled);
gpio_write_pin(SN74X154_E0_PIN, !enabled);
#endif
#if defined(SN74X154_E1_PIN)
writePin(SN74X154_E1_PIN, !enabled);
gpio_write_pin(SN74X154_E1_PIN, !enabled);
#endif
}
void sn74x154_set_addr(uint8_t address) {
for (int i = 0; i < ADDRESS_PIN_COUNT; i++) {
writePin(address_pins[i], address & (1 << i));
gpio_write_pin(address_pins[i], address & (1 << i));
}
}

View File

@ -61,7 +61,7 @@ void solenoid_set_dwell(uint8_t dwell) {
* @param index select which solenoid to check/stop
*/
void solenoid_stop(uint8_t index) {
writePin(solenoid_pads[index], !solenoid_active_state[index]);
gpio_write_pin(solenoid_pads[index], !solenoid_active_state[index]);
solenoid_on[index] = false;
solenoid_buzzing[index] = false;
}
@ -78,7 +78,7 @@ void solenoid_fire(uint8_t index) {
solenoid_on[index] = true;
solenoid_buzzing[index] = true;
solenoid_start[index] = timer_read();
writePin(solenoid_pads[index], solenoid_active_state[index]);
gpio_write_pin(solenoid_pads[index], solenoid_active_state[index]);
}
/**
@ -128,12 +128,12 @@ void solenoid_check(void) {
if ((elapsed[i] % (SOLENOID_BUZZ_ACTUATED + SOLENOID_BUZZ_NONACTUATED)) < SOLENOID_BUZZ_ACTUATED) {
if (!solenoid_buzzing[i]) {
solenoid_buzzing[i] = true;
writePin(solenoid_pads[i], solenoid_active_state[i]);
gpio_write_pin(solenoid_pads[i], solenoid_active_state[i]);
}
} else {
if (solenoid_buzzing[i]) {
solenoid_buzzing[i] = false;
writePin(solenoid_pads[i], !solenoid_active_state[i]);
gpio_write_pin(solenoid_pads[i], !solenoid_active_state[i]);
}
}
}
@ -156,8 +156,8 @@ void solenoid_setup(void) {
#else
solenoid_active_state[i] = high;
#endif
writePin(solenoid_pads[i], !solenoid_active_state[i]);
setPinOutput(solenoid_pads[i]);
gpio_write_pin(solenoid_pads[i], !solenoid_active_state[i]);
gpio_set_pin_output(solenoid_pads[i]);
if ((!HAPTIC_OFF_IN_LOW_POWER) || (usb_device_state == USB_DEVICE_STATE_CONFIGURED)) {
solenoid_fire(i);
}
@ -170,6 +170,6 @@ void solenoid_setup(void) {
*/
void solenoid_shutdown(void) {
for (uint8_t i = 0; i < NUMBER_OF_SOLENOIDS; i++) {
writePin(solenoid_pads[i], !solenoid_active_state[i]);
gpio_write_pin(solenoid_pads[i], !solenoid_active_state[i]);
}
}

View File

@ -57,67 +57,67 @@ static const pin_t data_pins[4] = HD44780_DATA_PINS;
#define HD44780_ENABLE_DELAY_US 1
static void hd44780_latch(void) {
writePinHigh(HD44780_E_PIN);
gpio_write_pin_high(HD44780_E_PIN);
wait_us(HD44780_ENABLE_DELAY_US);
writePinLow(HD44780_E_PIN);
gpio_write_pin_low(HD44780_E_PIN);
}
void hd44780_write(uint8_t data, bool isData) {
writePin(HD44780_RS_PIN, isData);
writePinLow(HD44780_RW_PIN);
gpio_write_pin(HD44780_RS_PIN, isData);
gpio_write_pin_low(HD44780_RW_PIN);
for (int i = 0; i < 4; i++) {
setPinOutput(data_pins[i]);
gpio_set_pin_output(data_pins[i]);
}
// Write high nibble
for (int i = 0; i < 4; i++) {
writePin(data_pins[i], (data >> 4) & (1 << i));
gpio_write_pin(data_pins[i], (data >> 4) & (1 << i));
}
hd44780_latch();
// Write low nibble
for (int i = 0; i < 4; i++) {
writePin(data_pins[i], data & (1 << i));
gpio_write_pin(data_pins[i], data & (1 << i));
}
hd44780_latch();
for (int i = 0; i < 4; i++) {
writePinHigh(data_pins[i]);
gpio_write_pin_high(data_pins[i]);
}
}
uint8_t hd44780_read(bool isData) {
uint8_t data = 0;
writePin(HD44780_RS_PIN, isData);
writePinHigh(HD44780_RW_PIN);
gpio_write_pin(HD44780_RS_PIN, isData);
gpio_write_pin_high(HD44780_RW_PIN);
for (int i = 0; i < 4; i++) {
setPinInput(data_pins[i]);
gpio_set_pin_input(data_pins[i]);
}
writePinHigh(HD44780_E_PIN);
gpio_write_pin_high(HD44780_E_PIN);
wait_us(HD44780_ENABLE_DELAY_US);
// Read high nibble
for (int i = 0; i < 4; i++) {
data |= (readPin(data_pins[i]) << i);
data |= (gpio_read_pin(data_pins[i]) << i);
}
data <<= 4;
writePinLow(HD44780_E_PIN);
gpio_write_pin_low(HD44780_E_PIN);
wait_us(HD44780_ENABLE_DELAY_US);
writePinHigh(HD44780_E_PIN);
gpio_write_pin_high(HD44780_E_PIN);
wait_us(HD44780_ENABLE_DELAY_US);
// Read low nibble
for (int i = 0; i < 4; i++) {
data |= (readPin(data_pins[i]) << i);
data |= (gpio_read_pin(data_pins[i]) << i);
}
writePinLow(HD44780_E_PIN);
gpio_write_pin_low(HD44780_E_PIN);
return data;
}
@ -171,20 +171,20 @@ void hd44780_set_ddram_address(uint8_t address) {
}
void hd44780_init(bool cursor, bool blink) {
setPinOutput(HD44780_RS_PIN);
setPinOutput(HD44780_RW_PIN);
setPinOutput(HD44780_E_PIN);
gpio_set_pin_output(HD44780_RS_PIN);
gpio_set_pin_output(HD44780_RW_PIN);
gpio_set_pin_output(HD44780_E_PIN);
for (int i = 0; i < 4; i++) {
setPinOutput(data_pins[i]);
gpio_set_pin_output(data_pins[i]);
}
wait_ms(HD44780_INIT_DELAY_MS);
// Manually configure for 4-bit mode - can't use hd44780_command() yet
// HD44780U datasheet, Fig. 24 (p46)
writePinHigh(data_pins[0]); // Function set
writePinHigh(data_pins[1]); // DL = 1
gpio_write_pin_high(data_pins[0]); // Function set
gpio_write_pin_high(data_pins[1]); // DL = 1
hd44780_latch();
wait_ms(5);
// Send again
@ -194,7 +194,7 @@ void hd44780_init(bool cursor, bool blink) {
hd44780_latch();
wait_us(64);
writePinLow(data_pins[0]); // DL = 0
gpio_write_pin_low(data_pins[0]); // DL = 0
hd44780_latch();
wait_us(64);

View File

@ -92,10 +92,10 @@ static void InvertCharacter(uint8_t *cursor) {
}
bool st7565_init(display_rotation_t rotation) {
setPinOutput(ST7565_A0_PIN);
writePinHigh(ST7565_A0_PIN);
setPinOutput(ST7565_RST_PIN);
writePinHigh(ST7565_RST_PIN);
gpio_set_pin_output(ST7565_A0_PIN);
gpio_write_pin_high(ST7565_A0_PIN);
gpio_set_pin_output(ST7565_RST_PIN);
gpio_write_pin_high(ST7565_RST_PIN);
st7565_rotation = st7565_init_user(rotation);
@ -488,18 +488,18 @@ void st7565_task(void) {
__attribute__((weak)) void st7565_task_user(void) {}
void st7565_reset(void) {
writePinLow(ST7565_RST_PIN);
gpio_write_pin_low(ST7565_RST_PIN);
wait_ms(20);
writePinHigh(ST7565_RST_PIN);
gpio_write_pin_high(ST7565_RST_PIN);
wait_ms(20);
}
spi_status_t st7565_send_cmd(uint8_t cmd) {
writePinLow(ST7565_A0_PIN);
gpio_write_pin_low(ST7565_A0_PIN);
return spi_write(cmd);
}
spi_status_t st7565_send_data(uint8_t *data, uint16_t length) {
writePinHigh(ST7565_A0_PIN);
gpio_write_pin_high(ST7565_A0_PIN);
return spi_transmit(data, length);
}

View File

@ -43,14 +43,14 @@
} \
} while (0)
#define APA102_SEND_BIT(byte, bit) \
do { \
writePin(APA102_DI_PIN, (byte >> bit) & 1); \
io_wait; \
writePinHigh(APA102_CI_PIN); \
io_wait; \
writePinLow(APA102_CI_PIN); \
io_wait; \
#define APA102_SEND_BIT(byte, bit) \
do { \
gpio_write_pin(APA102_DI_PIN, (byte >> bit) & 1); \
io_wait; \
gpio_write_pin_high(APA102_CI_PIN); \
io_wait; \
gpio_write_pin_low(APA102_CI_PIN); \
io_wait; \
} while (0)
uint8_t apa102_led_brightness = APA102_DEFAULT_BRIGHTNESS;
@ -114,11 +114,11 @@ static void apa102_send_frame(uint8_t red, uint8_t green, uint8_t blue, uint8_t
}
void apa102_init(void) {
setPinOutput(APA102_DI_PIN);
setPinOutput(APA102_CI_PIN);
gpio_set_pin_output(APA102_DI_PIN);
gpio_set_pin_output(APA102_CI_PIN);
writePinLow(APA102_DI_PIN);
writePinLow(APA102_CI_PIN);
gpio_write_pin_low(APA102_DI_PIN);
gpio_write_pin_low(APA102_CI_PIN);
}
void apa102_setleds(rgb_led_t *start_led, uint16_t num_leds) {

View File

@ -45,8 +45,15 @@
# define AW20216S_SPI_DIVISOR 4
#endif
uint8_t g_pwm_buffer[AW20216S_DRIVER_COUNT][AW20216S_PWM_REGISTER_COUNT];
bool g_pwm_buffer_update_required[AW20216S_DRIVER_COUNT] = {false};
typedef struct aw20216s_driver_t {
uint8_t pwm_buffer[AW20216S_PWM_REGISTER_COUNT];
bool pwm_buffer_dirty;
} PACKED aw20216s_driver_t;
aw20216s_driver_t driver_buffers[AW20216S_DRIVER_COUNT] = {{
.pwm_buffer = {0},
.pwm_buffer_dirty = false,
}};
bool aw20216s_write(pin_t cs_pin, uint8_t page, uint8_t reg, uint8_t* data, uint8_t len) {
static uint8_t s_spi_transfer_buffer[2] = {0};
@ -106,16 +113,18 @@ static inline void aw20216s_auto_lowpower(pin_t cs_pin) {
void aw20216s_init_drivers(void) {
spi_init();
aw20216s_init(AW20216S_CS_PIN_1, AW20216S_EN_PIN_1);
#if defined(AW20216S_EN_PIN)
gpio_set_pin_output(AW20216S_EN_PIN);
gpio_write_pin_high(AW20216S_EN_PIN);
#endif
aw20216s_init(AW20216S_CS_PIN_1);
#if defined(AW20216S_CS_PIN_2)
aw20216s_init(AW20216S_CS_PIN_2, AW20216S_EN_PIN_2);
aw20216s_init(AW20216S_CS_PIN_2);
#endif
}
void aw20216s_init(pin_t cs_pin, pin_t en_pin) {
setPinOutput(en_pin);
writePinHigh(en_pin);
void aw20216s_init(pin_t cs_pin) {
aw20216s_soft_reset(cs_pin);
wait_ms(2);
@ -131,14 +140,14 @@ void aw20216s_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
aw20216s_led_t led;
memcpy_P(&led, (&g_aw20216s_leds[index]), sizeof(led));
if (g_pwm_buffer[led.driver][led.r] == red && g_pwm_buffer[led.driver][led.g] == green && g_pwm_buffer[led.driver][led.b] == blue) {
if (driver_buffers[led.driver].pwm_buffer[led.r] == red && driver_buffers[led.driver].pwm_buffer[led.g] == green && driver_buffers[led.driver].pwm_buffer[led.b] == blue) {
return;
}
g_pwm_buffer[led.driver][led.r] = red;
g_pwm_buffer[led.driver][led.g] = green;
g_pwm_buffer[led.driver][led.b] = blue;
g_pwm_buffer_update_required[led.driver] = true;
driver_buffers[led.driver].pwm_buffer[led.r] = red;
driver_buffers[led.driver].pwm_buffer[led.g] = green;
driver_buffers[led.driver].pwm_buffer[led.b] = blue;
driver_buffers[led.driver].pwm_buffer_dirty = true;
}
void aw20216s_set_color_all(uint8_t red, uint8_t green, uint8_t blue) {
@ -148,9 +157,9 @@ void aw20216s_set_color_all(uint8_t red, uint8_t green, uint8_t blue) {
}
void aw20216s_update_pwm_buffers(pin_t cs_pin, uint8_t index) {
if (g_pwm_buffer_update_required[index]) {
aw20216s_write(cs_pin, AW20216S_PAGE_PWM, 0, g_pwm_buffer[index], AW20216S_PWM_REGISTER_COUNT);
g_pwm_buffer_update_required[index] = false;
if (driver_buffers[index].pwm_buffer_dirty) {
aw20216s_write(cs_pin, AW20216S_PAGE_PWM, 0, driver_buffers[index].pwm_buffer, AW20216S_PWM_REGISTER_COUNT);
driver_buffers[index].pwm_buffer_dirty = false;
}
}

View File

@ -42,10 +42,10 @@
# define AW20216S_CS_PIN_2 DRIVER_2_CS
#endif
#ifdef DRIVER_1_EN
# define AW20216S_EN_PIN_1 DRIVER_1_EN
# define AW20216S_EN_PIN DRIVER_1_EN
#endif
#ifdef DRIVER_2_EN
# define AW20216S_EN_PIN_2 DRIVER_2_EN
#ifdef AW20216S_EN_PIN_1
# define AW20216S_EN_PIN AW20216S_EN_PIN_1
#endif
#define aw_led aw20216s_led_t
@ -94,237 +94,467 @@ typedef struct aw20216s_led_t {
extern const aw20216s_led_t PROGMEM g_aw20216s_leds[AW20216S_LED_COUNT];
void aw20216s_init_drivers(void);
void aw20216s_init(pin_t cs_pin, pin_t en_pin);
void aw20216s_init(pin_t cs_pin);
void aw20216s_set_color(int index, uint8_t red, uint8_t green, uint8_t blue);
void aw20216s_set_color_all(uint8_t red, uint8_t green, uint8_t blue);
void aw20216s_update_pwm_buffers(pin_t cs_pin, uint8_t index);
void aw20216s_flush(void);
#define CS1_SW1 0x00
#define CS2_SW1 0x01
#define CS3_SW1 0x02
#define CS4_SW1 0x03
#define CS5_SW1 0x04
#define CS6_SW1 0x05
#define CS7_SW1 0x06
#define CS8_SW1 0x07
#define CS9_SW1 0x08
#define CS10_SW1 0x09
#define CS11_SW1 0x0A
#define CS12_SW1 0x0B
#define CS13_SW1 0x0C
#define CS14_SW1 0x0D
#define CS15_SW1 0x0E
#define CS16_SW1 0x0F
#define CS17_SW1 0x10
#define CS18_SW1 0x11
#define SW1_CS1 0x00
#define SW1_CS2 0x01
#define SW1_CS3 0x02
#define SW1_CS4 0x03
#define SW1_CS5 0x04
#define SW1_CS6 0x05
#define SW1_CS7 0x06
#define SW1_CS8 0x07
#define SW1_CS9 0x08
#define SW1_CS10 0x09
#define SW1_CS11 0x0A
#define SW1_CS12 0x0B
#define SW1_CS13 0x0C
#define SW1_CS14 0x0D
#define SW1_CS15 0x0E
#define SW1_CS16 0x0F
#define SW1_CS17 0x10
#define SW1_CS18 0x11
#define CS1_SW2 0x12
#define CS2_SW2 0x13
#define CS3_SW2 0x14
#define CS4_SW2 0x15
#define CS5_SW2 0x16
#define CS6_SW2 0x17
#define CS7_SW2 0x18
#define CS8_SW2 0x19
#define CS9_SW2 0x1A
#define CS10_SW2 0x1B
#define CS11_SW2 0x1C
#define CS12_SW2 0x1D
#define CS13_SW2 0x1E
#define CS14_SW2 0x1F
#define CS15_SW2 0x20
#define CS16_SW2 0x21
#define CS17_SW2 0x22
#define CS18_SW2 0x23
#define SW2_CS1 0x12
#define SW2_CS2 0x13
#define SW2_CS3 0x14
#define SW2_CS4 0x15
#define SW2_CS5 0x16
#define SW2_CS6 0x17
#define SW2_CS7 0x18
#define SW2_CS8 0x19
#define SW2_CS9 0x1A
#define SW2_CS10 0x1B
#define SW2_CS11 0x1C
#define SW2_CS12 0x1D
#define SW2_CS13 0x1E
#define SW2_CS14 0x1F
#define SW2_CS15 0x20
#define SW2_CS16 0x21
#define SW2_CS17 0x22
#define SW2_CS18 0x23
#define CS1_SW3 0x24
#define CS2_SW3 0x25
#define CS3_SW3 0x26
#define CS4_SW3 0x27
#define CS5_SW3 0x28
#define CS6_SW3 0x29
#define CS7_SW3 0x2A
#define CS8_SW3 0x2B
#define CS9_SW3 0x2C
#define CS10_SW3 0x2D
#define CS11_SW3 0x2E
#define CS12_SW3 0x2F
#define CS13_SW3 0x30
#define CS14_SW3 0x31
#define CS15_SW3 0x32
#define CS16_SW3 0x33
#define CS17_SW3 0x34
#define CS18_SW3 0x35
#define SW3_CS1 0x24
#define SW3_CS2 0x25
#define SW3_CS3 0x26
#define SW3_CS4 0x27
#define SW3_CS5 0x28
#define SW3_CS6 0x29
#define SW3_CS7 0x2A
#define SW3_CS8 0x2B
#define SW3_CS9 0x2C
#define SW3_CS10 0x2D
#define SW3_CS11 0x2E
#define SW3_CS12 0x2F
#define SW3_CS13 0x30
#define SW3_CS14 0x31
#define SW3_CS15 0x32
#define SW3_CS16 0x33
#define SW3_CS17 0x34
#define SW3_CS18 0x35
#define CS1_SW4 0x36
#define CS2_SW4 0x37
#define CS3_SW4 0x38
#define CS4_SW4 0x39
#define CS5_SW4 0x3A
#define CS6_SW4 0x3B
#define CS7_SW4 0x3C
#define CS8_SW4 0x3D
#define CS9_SW4 0x3E
#define CS10_SW4 0x3F
#define CS11_SW4 0x40
#define CS12_SW4 0x41
#define CS13_SW4 0x42
#define CS14_SW4 0x43
#define CS15_SW4 0x44
#define CS16_SW4 0x45
#define CS17_SW4 0x46
#define CS18_SW4 0x47
#define SW4_CS1 0x36
#define SW4_CS2 0x37
#define SW4_CS3 0x38
#define SW4_CS4 0x39
#define SW4_CS5 0x3A
#define SW4_CS6 0x3B
#define SW4_CS7 0x3C
#define SW4_CS8 0x3D
#define SW4_CS9 0x3E
#define SW4_CS10 0x3F
#define SW4_CS11 0x40
#define SW4_CS12 0x41
#define SW4_CS13 0x42
#define SW4_CS14 0x43
#define SW4_CS15 0x44
#define SW4_CS16 0x45
#define SW4_CS17 0x46
#define SW4_CS18 0x47
#define CS1_SW5 0x48
#define CS2_SW5 0x49
#define CS3_SW5 0x4A
#define CS4_SW5 0x4B
#define CS5_SW5 0x4C
#define CS6_SW5 0x4D
#define CS7_SW5 0x4E
#define CS8_SW5 0x4F
#define CS9_SW5 0x50
#define CS10_SW5 0x51
#define CS11_SW5 0x52
#define CS12_SW5 0x53
#define CS13_SW5 0x54
#define CS14_SW5 0x55
#define CS15_SW5 0x56
#define CS16_SW5 0x57
#define CS17_SW5 0x58
#define CS18_SW5 0x59
#define SW5_CS1 0x48
#define SW5_CS2 0x49
#define SW5_CS3 0x4A
#define SW5_CS4 0x4B
#define SW5_CS5 0x4C
#define SW5_CS6 0x4D
#define SW5_CS7 0x4E
#define SW5_CS8 0x4F
#define SW5_CS9 0x50
#define SW5_CS10 0x51
#define SW5_CS11 0x52
#define SW5_CS12 0x53
#define SW5_CS13 0x54
#define SW5_CS14 0x55
#define SW5_CS15 0x56
#define SW5_CS16 0x57
#define SW5_CS17 0x58
#define SW5_CS18 0x59
#define CS1_SW6 0x5A
#define CS2_SW6 0x5B
#define CS3_SW6 0x5C
#define CS4_SW6 0x5D
#define CS5_SW6 0x5E
#define CS6_SW6 0x5F
#define CS7_SW6 0x60
#define CS8_SW6 0x61
#define CS9_SW6 0x62
#define CS10_SW6 0x63
#define CS11_SW6 0x64
#define CS12_SW6 0x65
#define CS13_SW6 0x66
#define CS14_SW6 0x67
#define CS15_SW6 0x68
#define CS16_SW6 0x69
#define CS17_SW6 0x6A
#define CS18_SW6 0x6B
#define SW6_CS1 0x5A
#define SW6_CS2 0x5B
#define SW6_CS3 0x5C
#define SW6_CS4 0x5D
#define SW6_CS5 0x5E
#define SW6_CS6 0x5F
#define SW6_CS7 0x60
#define SW6_CS8 0x61
#define SW6_CS9 0x62
#define SW6_CS10 0x63
#define SW6_CS11 0x64
#define SW6_CS12 0x65
#define SW6_CS13 0x66
#define SW6_CS14 0x67
#define SW6_CS15 0x68
#define SW6_CS16 0x69
#define SW6_CS17 0x6A
#define SW6_CS18 0x6B
#define CS1_SW7 0x6C
#define CS2_SW7 0x6D
#define CS3_SW7 0x6E
#define CS4_SW7 0x6F
#define CS5_SW7 0x70
#define CS6_SW7 0x71
#define CS7_SW7 0x72
#define CS8_SW7 0x73
#define CS9_SW7 0x74
#define CS10_SW7 0x75
#define CS11_SW7 0x76
#define CS12_SW7 0x77
#define CS13_SW7 0x78
#define CS14_SW7 0x79
#define CS15_SW7 0x7A
#define CS16_SW7 0x7B
#define CS17_SW7 0x7C
#define CS18_SW7 0x7D
#define SW7_CS1 0x6C
#define SW7_CS2 0x6D
#define SW7_CS3 0x6E
#define SW7_CS4 0x6F
#define SW7_CS5 0x70
#define SW7_CS6 0x71
#define SW7_CS7 0x72
#define SW7_CS8 0x73
#define SW7_CS9 0x74
#define SW7_CS10 0x75
#define SW7_CS11 0x76
#define SW7_CS12 0x77
#define SW7_CS13 0x78
#define SW7_CS14 0x79
#define SW7_CS15 0x7A
#define SW7_CS16 0x7B
#define SW7_CS17 0x7C
#define SW7_CS18 0x7D
#define CS1_SW8 0x7E
#define CS2_SW8 0x7F
#define CS3_SW8 0x80
#define CS4_SW8 0x81
#define CS5_SW8 0x82
#define CS6_SW8 0x83
#define CS7_SW8 0x84
#define CS8_SW8 0x85
#define CS9_SW8 0x86
#define CS10_SW8 0x87
#define CS11_SW8 0x88
#define CS12_SW8 0x89
#define CS13_SW8 0x8A
#define CS14_SW8 0x8B
#define CS15_SW8 0x8C
#define CS16_SW8 0x8D
#define CS17_SW8 0x8E
#define CS18_SW8 0x8F
#define SW8_CS1 0x7E
#define SW8_CS2 0x7F
#define SW8_CS3 0x80
#define SW8_CS4 0x81
#define SW8_CS5 0x82
#define SW8_CS6 0x83
#define SW8_CS7 0x84
#define SW8_CS8 0x85
#define SW8_CS9 0x86
#define SW8_CS10 0x87
#define SW8_CS11 0x88
#define SW8_CS12 0x89
#define SW8_CS13 0x8A
#define SW8_CS14 0x8B
#define SW8_CS15 0x8C
#define SW8_CS16 0x8D
#define SW8_CS17 0x8E
#define SW8_CS18 0x8F
#define CS1_SW9 0x90
#define CS2_SW9 0x91
#define CS3_SW9 0x92
#define CS4_SW9 0x93
#define CS5_SW9 0x94
#define CS6_SW9 0x95
#define CS7_SW9 0x96
#define CS8_SW9 0x97
#define CS9_SW9 0x98
#define CS10_SW9 0x99
#define CS11_SW9 0x9A
#define CS12_SW9 0x9B
#define CS13_SW9 0x9C
#define CS14_SW9 0x9D
#define CS15_SW9 0x9E
#define CS16_SW9 0x9F
#define CS17_SW9 0xA0
#define CS18_SW9 0xA1
#define SW9_CS1 0x90
#define SW9_CS2 0x91
#define SW9_CS3 0x92
#define SW9_CS4 0x93
#define SW9_CS5 0x94
#define SW9_CS6 0x95
#define SW9_CS7 0x96
#define SW9_CS8 0x97
#define SW9_CS9 0x98
#define SW9_CS10 0x99
#define SW9_CS11 0x9A
#define SW9_CS12 0x9B
#define SW9_CS13 0x9C
#define SW9_CS14 0x9D
#define SW9_CS15 0x9E
#define SW9_CS16 0x9F
#define SW9_CS17 0xA0
#define SW9_CS18 0xA1
#define CS1_SW10 0xA2
#define CS2_SW10 0xA3
#define CS3_SW10 0xA4
#define CS4_SW10 0xA5
#define CS5_SW10 0xA6
#define CS6_SW10 0xA7
#define CS7_SW10 0xA8
#define CS8_SW10 0xA9
#define CS9_SW10 0xAA
#define CS10_SW10 0xAB
#define CS11_SW10 0xAC
#define CS12_SW10 0xAD
#define CS13_SW10 0xAE
#define CS14_SW10 0xAF
#define CS15_SW10 0xB0
#define CS16_SW10 0xB1
#define CS17_SW10 0xB2
#define CS18_SW10 0xB3
#define SW10_CS1 0xA2
#define SW10_CS2 0xA3
#define SW10_CS3 0xA4
#define SW10_CS4 0xA5
#define SW10_CS5 0xA6
#define SW10_CS6 0xA7
#define SW10_CS7 0xA8
#define SW10_CS8 0xA9
#define SW10_CS9 0xAA
#define SW10_CS10 0xAB
#define SW10_CS11 0xAC
#define SW10_CS12 0xAD
#define SW10_CS13 0xAE
#define SW10_CS14 0xAF
#define SW10_CS15 0xB0
#define SW10_CS16 0xB1
#define SW10_CS17 0xB2
#define SW10_CS18 0xB3
#define CS1_SW11 0xB4
#define CS2_SW11 0xB5
#define CS3_SW11 0xB6
#define CS4_SW11 0xB7
#define CS5_SW11 0xB8
#define CS6_SW11 0xB9
#define CS7_SW11 0xBA
#define CS8_SW11 0xBB
#define CS9_SW11 0xBC
#define CS10_SW11 0xBD
#define CS11_SW11 0xBE
#define CS12_SW11 0xBF
#define CS13_SW11 0xC0
#define CS14_SW11 0xC1
#define CS15_SW11 0xC2
#define CS16_SW11 0xC3
#define CS17_SW11 0xC4
#define CS18_SW11 0xC5
#define SW11_CS1 0xB4
#define SW11_CS2 0xB5
#define SW11_CS3 0xB6
#define SW11_CS4 0xB7
#define SW11_CS5 0xB8
#define SW11_CS6 0xB9
#define SW11_CS7 0xBA
#define SW11_CS8 0xBB
#define SW11_CS9 0xBC
#define SW11_CS10 0xBD
#define SW11_CS11 0xBE
#define SW11_CS12 0xBF
#define SW11_CS13 0xC0
#define SW11_CS14 0xC1
#define SW11_CS15 0xC2
#define SW11_CS16 0xC3
#define SW11_CS17 0xC4
#define SW11_CS18 0xC5
#define CS1_SW12 0xC6
#define CS2_SW12 0xC7
#define CS3_SW12 0xC8
#define CS4_SW12 0xC9
#define CS5_SW12 0xCA
#define CS6_SW12 0xCB
#define CS7_SW12 0xCC
#define CS8_SW12 0xCD
#define CS9_SW12 0xCE
#define CS10_SW12 0xCF
#define CS11_SW12 0xD0
#define CS12_SW12 0xD1
#define CS13_SW12 0xD2
#define CS14_SW12 0xD3
#define CS15_SW12 0xD4
#define CS16_SW12 0xD5
#define CS17_SW12 0xD6
#define CS18_SW12 0xD7
#define SW12_CS1 0xC6
#define SW12_CS2 0xC7
#define SW12_CS3 0xC8
#define SW12_CS4 0xC9
#define SW12_CS5 0xCA
#define SW12_CS6 0xCB
#define SW12_CS7 0xCC
#define SW12_CS8 0xCD
#define SW12_CS9 0xCE
#define SW12_CS10 0xCF
#define SW12_CS11 0xD0
#define SW12_CS12 0xD1
#define SW12_CS13 0xD2
#define SW12_CS14 0xD3
#define SW12_CS15 0xD4
#define SW12_CS16 0xD5
#define SW12_CS17 0xD6
#define SW12_CS18 0xD7
// DEPRECATED - DO NOT USE
#define CS1_SW1 SW1_CS1
#define CS2_SW1 SW1_CS2
#define CS3_SW1 SW1_CS3
#define CS4_SW1 SW1_CS4
#define CS5_SW1 SW1_CS5
#define CS6_SW1 SW1_CS6
#define CS7_SW1 SW1_CS7
#define CS8_SW1 SW1_CS8
#define CS9_SW1 SW1_CS9
#define CS10_SW1 SW1_CS10
#define CS11_SW1 SW1_CS11
#define CS12_SW1 SW1_CS12
#define CS13_SW1 SW1_CS13
#define CS14_SW1 SW1_CS14
#define CS15_SW1 SW1_CS15
#define CS16_SW1 SW1_CS16
#define CS17_SW1 SW1_CS17
#define CS18_SW1 SW1_CS18
#define CS1_SW2 SW2_CS1
#define CS2_SW2 SW2_CS2
#define CS3_SW2 SW2_CS3
#define CS4_SW2 SW2_CS4
#define CS5_SW2 SW2_CS5
#define CS6_SW2 SW2_CS6
#define CS7_SW2 SW2_CS7
#define CS8_SW2 SW2_CS8
#define CS9_SW2 SW2_CS9
#define CS10_SW2 SW2_CS10
#define CS11_SW2 SW2_CS11
#define CS12_SW2 SW2_CS12
#define CS13_SW2 SW2_CS13
#define CS14_SW2 SW2_CS14
#define CS15_SW2 SW2_CS15
#define CS16_SW2 SW2_CS16
#define CS17_SW2 SW2_CS17
#define CS18_SW2 SW2_CS18
#define CS1_SW3 SW3_CS1
#define CS2_SW3 SW3_CS2
#define CS3_SW3 SW3_CS3
#define CS4_SW3 SW3_CS4
#define CS5_SW3 SW3_CS5
#define CS6_SW3 SW3_CS6
#define CS7_SW3 SW3_CS7
#define CS8_SW3 SW3_CS8
#define CS9_SW3 SW3_CS9
#define CS10_SW3 SW3_CS10
#define CS11_SW3 SW3_CS11
#define CS12_SW3 SW3_CS12
#define CS13_SW3 SW3_CS13
#define CS14_SW3 SW3_CS14
#define CS15_SW3 SW3_CS15
#define CS16_SW3 SW3_CS16
#define CS17_SW3 SW3_CS17
#define CS18_SW3 SW3_CS18
#define CS1_SW4 SW4_CS1
#define CS2_SW4 SW4_CS2
#define CS3_SW4 SW4_CS3
#define CS4_SW4 SW4_CS4
#define CS5_SW4 SW4_CS5
#define CS6_SW4 SW4_CS6
#define CS7_SW4 SW4_CS7
#define CS8_SW4 SW4_CS8
#define CS9_SW4 SW4_CS9
#define CS10_SW4 SW4_CS10
#define CS11_SW4 SW4_CS11
#define CS12_SW4 SW4_CS12
#define CS13_SW4 SW4_CS13
#define CS14_SW4 SW4_CS14
#define CS15_SW4 SW4_CS15
#define CS16_SW4 SW4_CS16
#define CS17_SW4 SW4_CS17
#define CS18_SW4 SW4_CS18
#define CS1_SW5 SW5_CS1
#define CS2_SW5 SW5_CS2
#define CS3_SW5 SW5_CS3
#define CS4_SW5 SW5_CS4
#define CS5_SW5 SW5_CS5
#define CS6_SW5 SW5_CS6
#define CS7_SW5 SW5_CS7
#define CS8_SW5 SW5_CS8
#define CS9_SW5 SW5_CS9
#define CS10_SW5 SW5_CS10
#define CS11_SW5 SW5_CS11
#define CS12_SW5 SW5_CS12
#define CS13_SW5 SW5_CS13
#define CS14_SW5 SW5_CS14
#define CS15_SW5 SW5_CS15
#define CS16_SW5 SW5_CS16
#define CS17_SW5 SW5_CS17
#define CS18_SW5 SW5_CS18
#define CS1_SW6 SW6_CS1
#define CS2_SW6 SW6_CS2
#define CS3_SW6 SW6_CS3
#define CS4_SW6 SW6_CS4
#define CS5_SW6 SW6_CS5
#define CS6_SW6 SW6_CS6
#define CS7_SW6 SW6_CS7
#define CS8_SW6 SW6_CS8
#define CS9_SW6 SW6_CS9
#define CS10_SW6 SW6_CS10
#define CS11_SW6 SW6_CS11
#define CS12_SW6 SW6_CS12
#define CS13_SW6 SW6_CS13
#define CS14_SW6 SW6_CS14
#define CS15_SW6 SW6_CS15
#define CS16_SW6 SW6_CS16
#define CS17_SW6 SW6_CS17
#define CS18_SW6 SW6_CS18
#define CS1_SW7 SW7_CS1
#define CS2_SW7 SW7_CS2
#define CS3_SW7 SW7_CS3
#define CS4_SW7 SW7_CS4
#define CS5_SW7 SW7_CS5
#define CS6_SW7 SW7_CS6
#define CS7_SW7 SW7_CS7
#define CS8_SW7 SW7_CS8
#define CS9_SW7 SW7_CS9
#define CS10_SW7 SW7_CS10
#define CS11_SW7 SW7_CS11
#define CS12_SW7 SW7_CS12
#define CS13_SW7 SW7_CS13
#define CS14_SW7 SW7_CS14
#define CS15_SW7 SW7_CS15
#define CS16_SW7 SW7_CS16
#define CS17_SW7 SW7_CS17
#define CS18_SW7 SW7_CS18
#define CS1_SW8 SW8_CS1
#define CS2_SW8 SW8_CS2
#define CS3_SW8 SW8_CS3
#define CS4_SW8 SW8_CS4
#define CS5_SW8 SW8_CS5
#define CS6_SW8 SW8_CS6
#define CS7_SW8 SW8_CS7
#define CS8_SW8 SW8_CS8
#define CS9_SW8 SW8_CS9
#define CS10_SW8 SW8_CS10
#define CS11_SW8 SW8_CS11
#define CS12_SW8 SW8_CS12
#define CS13_SW8 SW8_CS13
#define CS14_SW8 SW8_CS14
#define CS15_SW8 SW8_CS15
#define CS16_SW8 SW8_CS16
#define CS17_SW8 SW8_CS17
#define CS18_SW8 SW8_CS18
#define CS1_SW9 SW9_CS1
#define CS2_SW9 SW9_CS2
#define CS3_SW9 SW9_CS3
#define CS4_SW9 SW9_CS4
#define CS5_SW9 SW9_CS5
#define CS6_SW9 SW9_CS6
#define CS7_SW9 SW9_CS7
#define CS8_SW9 SW9_CS8
#define CS9_SW9 SW9_CS9
#define CS10_SW9 SW9_CS10
#define CS11_SW9 SW9_CS11
#define CS12_SW9 SW9_CS12
#define CS13_SW9 SW9_CS13
#define CS14_SW9 SW9_CS14
#define CS15_SW9 SW9_CS15
#define CS16_SW9 SW9_CS16
#define CS17_SW9 SW9_CS17
#define CS18_SW9 SW9_CS18
#define CS1_SW10 SW10_CS1
#define CS2_SW10 SW10_CS2
#define CS3_SW10 SW10_CS3
#define CS4_SW10 SW10_CS4
#define CS5_SW10 SW10_CS5
#define CS6_SW10 SW10_CS6
#define CS7_SW10 SW10_CS7
#define CS8_SW10 SW10_CS8
#define CS9_SW10 SW10_CS9
#define CS10_SW10 SW10_CS10
#define CS11_SW10 SW10_CS11
#define CS12_SW10 SW10_CS12
#define CS13_SW10 SW10_CS13
#define CS14_SW10 SW10_CS14
#define CS15_SW10 SW10_CS15
#define CS16_SW10 SW10_CS16
#define CS17_SW10 SW10_CS17
#define CS18_SW10 SW10_CS18
#define CS1_SW11 SW11_CS1
#define CS2_SW11 SW11_CS2
#define CS3_SW11 SW11_CS3
#define CS4_SW11 SW11_CS4
#define CS5_SW11 SW11_CS5
#define CS6_SW11 SW11_CS6
#define CS7_SW11 SW11_CS7
#define CS8_SW11 SW11_CS8
#define CS9_SW11 SW11_CS9
#define CS10_SW11 SW11_CS10
#define CS11_SW11 SW11_CS11
#define CS12_SW11 SW11_CS12
#define CS13_SW11 SW11_CS13
#define CS14_SW11 SW11_CS14
#define CS15_SW11 SW11_CS15
#define CS16_SW11 SW11_CS16
#define CS17_SW11 SW11_CS17
#define CS18_SW11 SW11_CS18
#define CS1_SW12 SW12_CS1
#define CS2_SW12 SW12_CS2
#define CS3_SW12 SW12_CS3
#define CS4_SW12 SW12_CS4
#define CS5_SW12 SW12_CS5
#define CS6_SW12 SW12_CS6
#define CS7_SW12 SW12_CS7
#define CS8_SW12 SW12_CS8
#define CS9_SW12 SW12_CS9
#define CS10_SW12 SW12_CS10
#define CS11_SW12 SW12_CS11
#define CS12_SW12 SW12_CS12
#define CS13_SW12 SW12_CS13
#define CS14_SW12 SW12_CS14
#define CS15_SW12 SW12_CS15
#define CS16_SW12 SW12_CS16
#define CS17_SW12 SW12_CS17
#define CS18_SW12 SW12_CS18

View File

@ -16,6 +16,7 @@
#include "is31fl3218-mono.h"
#include "i2c_master.h"
#include "gpio.h"
#define IS31FL3218_PWM_REGISTER_COUNT 18
#define IS31FL3218_LED_CONTROL_REGISTER_COUNT 3
@ -28,12 +29,20 @@
# define IS31FL3218_I2C_PERSISTENCE 0
#endif
// IS31FL3218 has 18 PWM outputs and a fixed I2C address, so no chaining.
uint8_t g_pwm_buffer[IS31FL3218_PWM_REGISTER_COUNT];
bool g_pwm_buffer_update_required = false;
typedef struct is31fl3218_driver_t {
uint8_t pwm_buffer[IS31FL3218_PWM_REGISTER_COUNT];
bool pwm_buffer_dirty;
uint8_t led_control_buffer[IS31FL3218_LED_CONTROL_REGISTER_COUNT];
bool led_control_buffer_dirty;
} PACKED is31fl3218_driver_t;
uint8_t g_led_control_registers[IS31FL3218_LED_CONTROL_REGISTER_COUNT] = {0};
bool g_led_control_registers_update_required = false;
// IS31FL3218 has 18 PWM outputs and a fixed I2C address, so no chaining.
is31fl3218_driver_t driver_buffers = {
.pwm_buffer = {0},
.pwm_buffer_dirty = false,
.led_control_buffer = {0},
.led_control_buffer_dirty = false,
};
void is31fl3218_write_register(uint8_t reg, uint8_t data) {
#if IS31FL3218_I2C_PERSISTENCE > 0
@ -48,16 +57,21 @@ void is31fl3218_write_register(uint8_t reg, uint8_t data) {
void is31fl3218_write_pwm_buffer(void) {
#if IS31FL3218_I2C_PERSISTENCE > 0
for (uint8_t i = 0; i < IS31FL3218_I2C_PERSISTENCE; i++) {
if (i2c_write_register(IS31FL3218_I2C_ADDRESS << 1, IS31FL3218_REG_PWM, g_pwm_buffer, 18, IS31FL3218_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(IS31FL3218_I2C_ADDRESS << 1, IS31FL3218_REG_PWM, driver_buffers.pwm_buffer, 18, IS31FL3218_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(IS31FL3218_I2C_ADDRESS << 1, IS31FL3218_REG_PWM, g_pwm_buffer, 18, IS31FL3218_I2C_TIMEOUT);
i2c_write_register(IS31FL3218_I2C_ADDRESS << 1, IS31FL3218_REG_PWM, driver_buffers.pwm_buffer, 18, IS31FL3218_I2C_TIMEOUT);
#endif
}
void is31fl3218_init(void) {
i2c_init();
#if defined(IS31FL3218_SDB_PIN)
gpio_set_pin_output(IS31FL3218_SDB_PIN);
gpio_write_pin_high(IS31FL3218_SDB_PIN);
#endif
// In case we ever want to reinitialize (?)
is31fl3218_write_register(IS31FL3218_REG_RESET, 0x00);
@ -90,12 +104,12 @@ void is31fl3218_set_value(int index, uint8_t value) {
if (index >= 0 && index < IS31FL3218_LED_COUNT) {
memcpy_P(&led, (&g_is31fl3218_leds[index]), sizeof(led));
if (g_pwm_buffer[led.v] == value) {
if (driver_buffers.pwm_buffer[led.v] == value) {
return;
}
g_pwm_buffer[led.v] = value;
g_pwm_buffer_update_required = true;
driver_buffers.pwm_buffer[led.v] = value;
driver_buffers.pwm_buffer_dirty = true;
}
}
@ -113,30 +127,30 @@ void is31fl3218_set_led_control_register(uint8_t index, bool value) {
uint8_t bit_value = led.v % 6;
if (value) {
g_led_control_registers[control_register] |= (1 << bit_value);
driver_buffers.led_control_buffer[control_register] |= (1 << bit_value);
} else {
g_led_control_registers[control_register] &= ~(1 << bit_value);
driver_buffers.led_control_buffer[control_register] &= ~(1 << bit_value);
}
g_led_control_registers_update_required = true;
driver_buffers.led_control_buffer_dirty = true;
}
void is31fl3218_update_pwm_buffers(void) {
if (g_pwm_buffer_update_required) {
if (driver_buffers.pwm_buffer_dirty) {
is31fl3218_write_pwm_buffer();
// Load PWM registers and LED Control register data
is31fl3218_write_register(IS31FL3218_REG_UPDATE, 0x01);
g_pwm_buffer_update_required = false;
driver_buffers.pwm_buffer_dirty = false;
}
}
void is31fl3218_update_led_control_registers(void) {
if (g_led_control_registers_update_required) {
if (driver_buffers.led_control_buffer_dirty) {
for (uint8_t i = 0; i < IS31FL3218_LED_CONTROL_REGISTER_COUNT; i++) {
is31fl3218_write_register(IS31FL3218_REG_LED_CONTROL_1 + i, g_led_control_registers[i]);
is31fl3218_write_register(IS31FL3218_REG_LED_CONTROL_1 + i, driver_buffers.led_control_buffer[i]);
}
g_led_control_registers_update_required = false;
driver_buffers.led_control_buffer_dirty = false;
}
}

View File

@ -16,6 +16,7 @@
#include "is31fl3218.h"
#include "i2c_master.h"
#include "gpio.h"
#define IS31FL3218_PWM_REGISTER_COUNT 18
#define IS31FL3218_LED_CONTROL_REGISTER_COUNT 3
@ -28,12 +29,20 @@
# define IS31FL3218_I2C_PERSISTENCE 0
#endif
// IS31FL3218 has 18 PWM outputs and a fixed I2C address, so no chaining.
uint8_t g_pwm_buffer[IS31FL3218_PWM_REGISTER_COUNT];
bool g_pwm_buffer_update_required = false;
typedef struct is31fl3218_driver_t {
uint8_t pwm_buffer[IS31FL3218_PWM_REGISTER_COUNT];
bool pwm_buffer_dirty;
uint8_t led_control_buffer[IS31FL3218_LED_CONTROL_REGISTER_COUNT];
bool led_control_buffer_dirty;
} PACKED is31fl3218_driver_t;
uint8_t g_led_control_registers[IS31FL3218_LED_CONTROL_REGISTER_COUNT] = {0};
bool g_led_control_registers_update_required = false;
// IS31FL3218 has 18 PWM outputs and a fixed I2C address, so no chaining.
is31fl3218_driver_t driver_buffers = {
.pwm_buffer = {0},
.pwm_buffer_dirty = false,
.led_control_buffer = {0},
.led_control_buffer_dirty = false,
};
void is31fl3218_write_register(uint8_t reg, uint8_t data) {
#if IS31FL3218_I2C_PERSISTENCE > 0
@ -48,16 +57,21 @@ void is31fl3218_write_register(uint8_t reg, uint8_t data) {
void is31fl3218_write_pwm_buffer(void) {
#if IS31FL3218_I2C_PERSISTENCE > 0
for (uint8_t i = 0; i < IS31FL3218_I2C_PERSISTENCE; i++) {
if (i2c_write_register(IS31FL3218_I2C_ADDRESS << 1, IS31FL3218_REG_PWM, g_pwm_buffer, 18, IS31FL3218_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(IS31FL3218_I2C_ADDRESS << 1, IS31FL3218_REG_PWM, driver_buffers.pwm_buffer, 18, IS31FL3218_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(IS31FL3218_I2C_ADDRESS << 1, IS31FL3218_REG_PWM, g_pwm_buffer, 18, IS31FL3218_I2C_TIMEOUT);
i2c_write_register(IS31FL3218_I2C_ADDRESS << 1, IS31FL3218_REG_PWM, driver_buffers.pwm_buffer, 18, IS31FL3218_I2C_TIMEOUT);
#endif
}
void is31fl3218_init(void) {
i2c_init();
#if defined(IS31FL3218_SDB_PIN)
gpio_set_pin_output(IS31FL3218_SDB_PIN);
gpio_write_pin_high(IS31FL3218_SDB_PIN);
#endif
// In case we ever want to reinitialize (?)
is31fl3218_write_register(IS31FL3218_REG_RESET, 0x00);
@ -90,14 +104,14 @@ void is31fl3218_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
if (index >= 0 && index < IS31FL3218_LED_COUNT) {
memcpy_P(&led, (&g_is31fl3218_leds[index]), sizeof(led));
if (g_pwm_buffer[led.r] == red && g_pwm_buffer[led.g] == green && g_pwm_buffer[led.b] == blue) {
if (driver_buffers.pwm_buffer[led.r] == red && driver_buffers.pwm_buffer[led.g] == green && driver_buffers.pwm_buffer[led.b] == blue) {
return;
}
g_pwm_buffer[led.r] = red;
g_pwm_buffer[led.g] = green;
g_pwm_buffer[led.b] = blue;
g_pwm_buffer_update_required = true;
driver_buffers.pwm_buffer[led.r] = red;
driver_buffers.pwm_buffer[led.g] = green;
driver_buffers.pwm_buffer[led.b] = blue;
driver_buffers.pwm_buffer_dirty = true;
}
}
@ -119,40 +133,40 @@ void is31fl3218_set_led_control_register(uint8_t index, bool red, bool green, bo
uint8_t bit_b = led.b % 6;
if (red) {
g_led_control_registers[control_register_r] |= (1 << bit_r);
driver_buffers.led_control_buffer[control_register_r] |= (1 << bit_r);
} else {
g_led_control_registers[control_register_r] &= ~(1 << bit_r);
driver_buffers.led_control_buffer[control_register_r] &= ~(1 << bit_r);
}
if (green) {
g_led_control_registers[control_register_g] |= (1 << bit_g);
driver_buffers.led_control_buffer[control_register_g] |= (1 << bit_g);
} else {
g_led_control_registers[control_register_g] &= ~(1 << bit_g);
driver_buffers.led_control_buffer[control_register_g] &= ~(1 << bit_g);
}
if (blue) {
g_led_control_registers[control_register_b] |= (1 << bit_b);
driver_buffers.led_control_buffer[control_register_b] |= (1 << bit_b);
} else {
g_led_control_registers[control_register_b] &= ~(1 << bit_b);
driver_buffers.led_control_buffer[control_register_b] &= ~(1 << bit_b);
}
g_led_control_registers_update_required = true;
driver_buffers.led_control_buffer_dirty = true;
}
void is31fl3218_update_pwm_buffers(void) {
if (g_pwm_buffer_update_required) {
if (driver_buffers.pwm_buffer_dirty) {
is31fl3218_write_pwm_buffer();
// Load PWM registers and LED Control register data
is31fl3218_write_register(IS31FL3218_REG_UPDATE, 0x01);
g_pwm_buffer_update_required = false;
driver_buffers.pwm_buffer_dirty = false;
}
}
void is31fl3218_update_led_control_registers(void) {
if (g_led_control_registers_update_required) {
if (driver_buffers.led_control_buffer_dirty) {
for (uint8_t i = 0; i < IS31FL3218_LED_CONTROL_REGISTER_COUNT; i++) {
is31fl3218_write_register(IS31FL3218_REG_LED_CONTROL_1 + i, g_led_control_registers[i]);
is31fl3218_write_register(IS31FL3218_REG_LED_CONTROL_1 + i, driver_buffers.led_control_buffer[i]);
}
g_led_control_registers_update_required = false;
driver_buffers.led_control_buffer_dirty = false;
}
}

View File

@ -0,0 +1,213 @@
/* Copyright 2024 HorrorTroll <https://github.com/HorrorTroll>
* Copyright 2024 Harrison Chan (Xelus)
* Copyright 2024 Dimitris Mantzouranis <d3xter93@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "is31fl3729-mono.h"
#include "i2c_master.h"
#include "gpio.h"
#include "wait.h"
#define IS31FL3729_PWM_REGISTER_COUNT 143
#define IS31FL3729_SCALING_REGISTER_COUNT 16
#ifndef IS31FL3729_I2C_TIMEOUT
# define IS31FL3729_I2C_TIMEOUT 100
#endif
#ifndef IS31FL3729_I2C_PERSISTENCE
# define IS31FL3729_I2C_PERSISTENCE 0
#endif
#ifndef IS31FL3729_CONFIGURATION
# define IS31FL3729_CONFIGURATION IS31FL3729_CONFIG_SWS_15_9
#endif
#ifndef IS31FL3729_GLOBAL_CURRENT
# define IS31FL3729_GLOBAL_CURRENT 0x40
#endif
#ifndef IS31FL3729_SW_PULLDOWN
# define IS31FL3729_SW_PULLDOWN IS31FL3729_SW_PULLDOWN_2K_OHM_SW_OFF
#endif
#ifndef IS31FL3729_CS_PULLUP
# define IS31FL3729_CS_PULLUP IS31FL3729_CS_PULLUP_2K_OHM_CS_OFF
#endif
#ifndef IS31FL3729_SPREAD_SPECTRUM
# define IS31FL3729_SPREAD_SPECTRUM IS31FL3729_SPREAD_SPECTRUM_DISABLE
#endif
#ifndef IS31FL3729_SPREAD_SPECTRUM_RANGE
# define IS31FL3729_SPREAD_SPECTRUM_RANGE IS31FL3729_SPREAD_SPECTRUM_RANGE_5_PERCENT
#endif
#ifndef IS31FL3729_SPREAD_SPECTRUM_CYCLE_TIME
# define IS31FL3729_SPREAD_SPECTRUM_CYCLE_TIME IS31FL3729_SPREAD_SPECTRUM_CYCLE_TIME_1980_US
#endif
#ifndef IS31FL3729_PWM_FREQUENCY
# define IS31FL3729_PWM_FREQUENCY IS31FL3729_PWM_FREQUENCY_32K_HZ
#endif
const uint8_t i2c_addresses[IS31FL3729_DRIVER_COUNT] = {
IS31FL3729_I2C_ADDRESS_1,
#ifdef IS31FL3729_I2C_ADDRESS_2
IS31FL3729_I2C_ADDRESS_2,
# ifdef IS31FL3729_I2C_ADDRESS_3
IS31FL3729_I2C_ADDRESS_3,
# ifdef IS31FL3729_I2C_ADDRESS_4
IS31FL3729_I2C_ADDRESS_4,
# endif
# endif
#endif
};
// These buffers match the PWM & scaling registers.
// Storing them like this is optimal for I2C transfers to the registers.
typedef struct is31fl3729_driver_t {
uint8_t pwm_buffer[IS31FL3729_PWM_REGISTER_COUNT];
bool pwm_buffer_dirty;
uint8_t scaling_buffer[IS31FL3729_SCALING_REGISTER_COUNT];
bool scaling_buffer_dirty;
} PACKED is31fl3729_driver_t;
is31fl3729_driver_t driver_buffers[IS31FL3729_DRIVER_COUNT] = {{
.pwm_buffer = {0},
.pwm_buffer_dirty = false,
.scaling_buffer = {0},
.scaling_buffer_dirty = false,
}};
void is31fl3729_write_register(uint8_t index, uint8_t reg, uint8_t data) {
#if IS31FL3729_I2C_PERSISTENCE > 0
for (uint8_t i = 0; i < IS31FL3729_I2C_PERSISTENCE; i++) {
if (i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3729_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3729_I2C_TIMEOUT);
#endif
}
void is31fl3729_write_pwm_buffer(uint8_t index) {
// Transmit PWM registers in 11 transfers of 13 bytes.
// Iterate over the pwm_buffer contents at 13 byte intervals.
for (uint8_t i = 0; i <= IS31FL3729_PWM_REGISTER_COUNT; i += 13) {
#if IS31FL3729_I2C_PERSISTENCE > 0
for (uint8_t j = 0; j < IS31FL3729_I2C_PERSISTENCE; j++) {
if (i2c_write_register(i2c_addresses[index] << 1, IS31FL3729_REG_PWM + i, driver_buffers[index].pwm_buffer + i, 13, IS31FL3729_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(i2c_addresses[index] << 1, IS31FL3729_REG_PWM + i, driver_buffers[index].pwm_buffer + i, 13, IS31FL3729_I2C_TIMEOUT);
#endif
}
}
void is31fl3729_init_drivers(void) {
i2c_init();
#if defined(IS31FL3729_SDB_PIN)
gpio_set_pin_output(IS31FL3729_SDB_PIN);
gpio_write_pin_high(IS31FL3729_SDB_PIN);
#endif
for (uint8_t i = 0; i < IS31FL3729_DRIVER_COUNT; i++) {
is31fl3729_init(i);
}
for (int i = 0; i < IS31FL3729_LED_COUNT; i++) {
is31fl3729_set_scaling_register(i, 0xFF);
}
for (uint8_t i = 0; i < IS31FL3729_DRIVER_COUNT; i++) {
is31fl3729_update_scaling_registers(i);
}
}
void is31fl3729_init(uint8_t index) {
// In order to avoid the LEDs being driven with garbage data
// in the LED driver's PWM registers, shutdown is enabled last.
// Set up the mode and other settings, clear the PWM registers,
// then disable software shutdown.
is31fl3729_write_register(index, IS31FL3729_REG_PULLDOWNUP, ((IS31FL3729_SW_PULLDOWN & 0b111) << 4) | (IS31FL3729_CS_PULLUP & 0b111));
is31fl3729_write_register(index, IS31FL3729_REG_SPREAD_SPECTRUM, ((IS31FL3729_SPREAD_SPECTRUM & 0b1) << 4) | ((IS31FL3729_SPREAD_SPECTRUM_RANGE & 0b11) << 2) | (IS31FL3729_SPREAD_SPECTRUM_CYCLE_TIME & 0b11));
is31fl3729_write_register(index, IS31FL3729_REG_PWM_FREQUENCY, IS31FL3729_PWM_FREQUENCY);
is31fl3729_write_register(index, IS31FL3729_REG_GLOBAL_CURRENT, IS31FL3729_GLOBAL_CURRENT);
is31fl3729_write_register(index, IS31FL3729_REG_CONFIGURATION, IS31FL3729_CONFIGURATION);
// Wait 10ms to ensure the device has woken up.
wait_ms(10);
}
void is31fl3729_set_value(int index, uint8_t value) {
is31fl3729_led_t led;
if (index >= 0 && index < IS31FL3729_LED_COUNT) {
memcpy_P(&led, (&g_is31fl3729_leds[index]), sizeof(led));
if (driver_buffers[led.driver].pwm_buffer[led.v] == value) {
return;
}
driver_buffers[led.driver].pwm_buffer[led.v] = value;
driver_buffers[led.driver].pwm_buffer_dirty = true;
}
}
void is31fl3729_set_value_all(uint8_t value) {
for (int i = 0; i < IS31FL3729_LED_COUNT; i++) {
is31fl3729_set_value(i, value);
}
}
void is31fl3729_set_scaling_register(uint8_t index, uint8_t value) {
is31fl3729_led_t led;
memcpy_P(&led, (&g_is31fl3729_leds[index]), sizeof(led));
// need to do a bit of checking here since 3729 scaling is per CS pin.
// not the usual per single LED key as per other ISSI drivers
// only enable them, since they should be default disabled
int cs_value = (led.v & 0x0F) - 1;
driver_buffers[led.driver].scaling_buffer[cs_value] = value;
driver_buffers[led.driver].scaling_buffer_dirty = true;
}
void is31fl3729_update_pwm_buffers(uint8_t index) {
if (driver_buffers[index].pwm_buffer_dirty) {
is31fl3729_write_pwm_buffer(index);
driver_buffers[index].pwm_buffer_dirty = false;
}
}
void is31fl3729_update_scaling_registers(uint8_t index) {
if (driver_buffers[index].scaling_buffer_dirty) {
for (uint8_t i = 0; i < IS31FL3729_SCALING_REGISTER_COUNT; i++) {
is31fl3729_write_register(index, IS31FL3729_REG_SCALING + i, driver_buffers[index].scaling_buffer[i]);
}
driver_buffers[index].scaling_buffer_dirty = false;
}
}
void is31fl3729_flush(void) {
for (uint8_t i = 0; i < IS31FL3729_DRIVER_COUNT; i++) {
is31fl3729_update_pwm_buffers(i);
}
}

View File

@ -0,0 +1,278 @@
/* Copyright 2024 HorrorTroll <https://github.com/HorrorTroll>
* Copyright 2024 Harrison Chan (Xelus)
* Copyright 2024 Dimitris Mantzouranis <d3xter93@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include "progmem.h"
#include "util.h"
#define IS31FL3729_REG_PWM 0x01
#define IS31FL3729_REG_SCALING 0x90
#define IS31FL3729_REG_CONFIGURATION 0xA0
#define IS31FL3729_REG_GLOBAL_CURRENT 0xA1
#define IS31FL3729_REG_PULLDOWNUP 0xB0
#define IS31FL3729_REG_SPREAD_SPECTRUM 0xB1
#define IS31FL3729_REG_PWM_FREQUENCY 0xB2
#define IS31FL3729_REG_RESET 0xCF
#define IS31FL3729_I2C_ADDRESS_GND 0x34
#define IS31FL3729_I2C_ADDRESS_SCL 0x35
#define IS31FL3729_I2C_ADDRESS_SDA 0x36
#define IS31FL3729_I2C_ADDRESS_VCC 0x37
#if defined(LED_MATRIX_IS31FL3729)
# define IS31FL3729_LED_COUNT LED_MATRIX_LED_COUNT
#endif
#if defined(IS31FL3729_I2C_ADDRESS_4)
# define IS31FL3729_DRIVER_COUNT 4
#elif defined(IS31FL3729_I2C_ADDRESS_3)
# define IS31FL3729_DRIVER_COUNT 3
#elif defined(IS31FL3729_I2C_ADDRESS_2)
# define IS31FL3729_DRIVER_COUNT 2
#elif defined(IS31FL3729_I2C_ADDRESS_1)
# define IS31FL3729_DRIVER_COUNT 1
#endif
typedef struct is31fl3729_led_t {
uint8_t driver : 2;
uint8_t v;
} PACKED is31fl3729_led_t;
extern const is31fl3729_led_t PROGMEM g_is31fl3729_leds[IS31FL3729_LED_COUNT];
void is31fl3729_init_drivers(void);
void is31fl3729_init(uint8_t index);
void is31fl3729_write_register(uint8_t index, uint8_t reg, uint8_t data);
void is31fl3729_set_value(int index, uint8_t value);
void is31fl3729_set_value_all(uint8_t value);
void is31fl3729_set_scaling_register(uint8_t index, uint8_t value);
// This should not be called from an interrupt
// (eg. from a timer interrupt).
// Call this while idle (in between matrix scans).
// If the buffer is dirty, it will update the driver with the buffer.
void is31fl3729_update_pwm_buffers(uint8_t index);
void is31fl3729_update_scaling_registers(uint8_t index);
void is31fl3729_flush(void);
#define IS31FL3729_SW_PULLDOWN_0_OHM 0b000
#define IS31FL3729_SW_PULLDOWN_0K5_OHM_SW_OFF 0b001
#define IS31FL3729_SW_PULLDOWN_1K_OHM_SW_OFF 0b010
#define IS31FL3729_SW_PULLDOWN_2K_OHM_SW_OFF 0b011
#define IS31FL3729_SW_PULLDOWN_1K_OHM 0b100
#define IS31FL3729_SW_PULLDOWN_2K_OHM 0b101
#define IS31FL3729_SW_PULLDOWN_4K_OHM 0b110
#define IS31FL3729_SW_PULLDOWN_8K_OHM 0b111
#define IS31FL3729_CS_PULLUP_0_OHM 0b000
#define IS31FL3729_CS_PULLUP_0K5_OHM_CS_OFF 0b001
#define IS31FL3729_CS_PULLUP_1K_OHM_CS_OFF 0b010
#define IS31FL3729_CS_PULLUP_2K_OHM_CS_OFF 0b011
#define IS31FL3729_CS_PULLUP_1K_OHM 0b100
#define IS31FL3729_CS_PULLUP_2K_OHM 0b101
#define IS31FL3729_CS_PULLUP_4K_OHM 0b110
#define IS31FL3729_CS_PULLUP_8K_OHM 0b111
#define IS31FL3729_SPREAD_SPECTRUM_DISABLE 0b0
#define IS31FL3729_SPREAD_SPECTRUM_ENABLE 0b1
#define IS31FL3729_SPREAD_SPECTRUM_RANGE_5_PERCENT 0b00
#define IS31FL3729_SPREAD_SPECTRUM_RANGE_15_PERCENT 0b01
#define IS31FL3729_SPREAD_SPECTRUM_RANGE_24_PERCENT 0b10
#define IS31FL3729_SPREAD_SPECTRUM_RANGE_34_PERCENT 0b11
#define IS31FL3729_SPREAD_SPECTRUM_CYCLE_TIME_1980_US 0b00
#define IS31FL3729_SPREAD_SPECTRUM_CYCLE_TIME_1200_US 0b01
#define IS31FL3729_SPREAD_SPECTRUM_CYCLE_TIME_820_US 0b10
#define IS31FL3729_SPREAD_SPECTRUM_CYCLE_TIME_660_US 0b11
#define IS31FL3729_PWM_FREQUENCY_55K_HZ 0b000
#define IS31FL3729_PWM_FREQUENCY_32K_HZ 0b001
#define IS31FL3729_PWM_FREQUENCY_4K_HZ 0b010
#define IS31FL3729_PWM_FREQUENCY_2K_HZ 0b011
#define IS31FL3729_PWM_FREQUENCY_1K_HZ 0b100
#define IS31FL3729_PWM_FREQUENCY_500_HZ 0b101
#define IS31FL3729_PWM_FREQUENCY_250_HZ 0b110
#define IS31FL3729_PWM_FREQUENCY_80K_HZ 0b111
#define IS31FL3729_CONFIG_SWS_15_9 0x01 // 15 CS x 9 SW matrix
#define IS31FL3729_CONFIG_SWS_16_8 0x11 // 16 CS x 8 SW matrix
#define IS31FL3729_CONFIG_SWS_16_7 0x21 // 16 CS x 7 SW matrix
#define IS31FL3729_CONFIG_SWS_16_6 0x31 // 16 CS x 6 SW matrix
#define IS31FL3729_CONFIG_SWS_16_5 0x41 // 16 CS x 5 SW matrix
#define IS31FL3729_CONFIG_SWS_16_4 0x51 // 16 CS x 4 SW matrix
#define IS31FL3729_CONFIG_SWS_16_3 0x61 // 16 CS x 3 SW matrix
#define IS31FL3729_CONFIG_SWS_16_2 0x71 // 16 CS x 2 SW matrix
#define SW1_CS1 0x00
#define SW1_CS2 0x01
#define SW1_CS3 0x02
#define SW1_CS4 0x03
#define SW1_CS5 0x04
#define SW1_CS6 0x05
#define SW1_CS7 0x06
#define SW1_CS8 0x07
#define SW1_CS9 0x08
#define SW1_CS10 0x09
#define SW1_CS11 0x0A
#define SW1_CS12 0x0B
#define SW1_CS13 0x0C
#define SW1_CS14 0x0D
#define SW1_CS15 0x0E
#define SW1_CS16 0x0F
#define SW2_CS1 0x10
#define SW2_CS2 0x11
#define SW2_CS3 0x12
#define SW2_CS4 0x13
#define SW2_CS5 0x14
#define SW2_CS6 0x15
#define SW2_CS7 0x16
#define SW2_CS8 0x17
#define SW2_CS9 0x18
#define SW2_CS10 0x19
#define SW2_CS11 0x1A
#define SW2_CS12 0x1B
#define SW2_CS13 0x1C
#define SW2_CS14 0x1D
#define SW2_CS15 0x1E
#define SW2_CS16 0x1F
#define SW3_CS1 0x20
#define SW3_CS2 0x21
#define SW3_CS3 0x22
#define SW3_CS4 0x23
#define SW3_CS5 0x24
#define SW3_CS6 0x25
#define SW3_CS7 0x26
#define SW3_CS8 0x27
#define SW3_CS9 0x28
#define SW3_CS10 0x29
#define SW3_CS11 0x2A
#define SW3_CS12 0x2B
#define SW3_CS13 0x2C
#define SW3_CS14 0x2D
#define SW3_CS15 0x2E
#define SW3_CS16 0x2F
#define SW4_CS1 0x30
#define SW4_CS2 0x31
#define SW4_CS3 0x32
#define SW4_CS4 0x33
#define SW4_CS5 0x34
#define SW4_CS6 0x35
#define SW4_CS7 0x36
#define SW4_CS8 0x37
#define SW4_CS9 0x38
#define SW4_CS10 0x39
#define SW4_CS11 0x3A
#define SW4_CS12 0x3B
#define SW4_CS13 0x3C
#define SW4_CS14 0x3D
#define SW4_CS15 0x3E
#define SW4_CS16 0x3F
#define SW5_CS1 0x40
#define SW5_CS2 0x41
#define SW5_CS3 0x42
#define SW5_CS4 0x43
#define SW5_CS5 0x44
#define SW5_CS6 0x45
#define SW5_CS7 0x46
#define SW5_CS8 0x47
#define SW5_CS9 0x48
#define SW5_CS10 0x49
#define SW5_CS11 0x4A
#define SW5_CS12 0x4B
#define SW5_CS13 0x4C
#define SW5_CS14 0x4D
#define SW5_CS15 0x4E
#define SW5_CS16 0x4F
#define SW6_CS1 0x50
#define SW6_CS2 0x51
#define SW6_CS3 0x52
#define SW6_CS4 0x53
#define SW6_CS5 0x54
#define SW6_CS6 0x55
#define SW6_CS7 0x56
#define SW6_CS8 0x57
#define SW6_CS9 0x58
#define SW6_CS10 0x59
#define SW6_CS11 0x5A
#define SW6_CS12 0x5B
#define SW6_CS13 0x5C
#define SW6_CS14 0x5D
#define SW6_CS15 0x5E
#define SW6_CS16 0x5F
#define SW7_CS1 0x60
#define SW7_CS2 0x61
#define SW7_CS3 0x62
#define SW7_CS4 0x63
#define SW7_CS5 0x64
#define SW7_CS6 0x65
#define SW7_CS7 0x66
#define SW7_CS8 0x67
#define SW7_CS9 0x68
#define SW7_CS10 0x69
#define SW7_CS11 0x6A
#define SW7_CS12 0x6B
#define SW7_CS13 0x6C
#define SW7_CS14 0x6D
#define SW7_CS15 0x6E
#define SW7_CS16 0x6F
#define SW8_CS1 0x70
#define SW8_CS2 0x71
#define SW8_CS3 0x72
#define SW8_CS4 0x73
#define SW8_CS5 0x74
#define SW8_CS6 0x75
#define SW8_CS7 0x76
#define SW8_CS8 0x77
#define SW8_CS9 0x78
#define SW8_CS10 0x79
#define SW8_CS11 0x7A
#define SW8_CS12 0x7B
#define SW8_CS13 0x7C
#define SW8_CS14 0x7D
#define SW8_CS15 0x7E
#define SW8_CS16 0x7F
#define SW9_CS1 0x80
#define SW9_CS2 0x81
#define SW9_CS3 0x82
#define SW9_CS4 0x83
#define SW9_CS5 0x84
#define SW9_CS6 0x85
#define SW9_CS7 0x86
#define SW9_CS8 0x87
#define SW9_CS9 0x88
#define SW9_CS10 0x89
#define SW9_CS11 0x8A
#define SW9_CS12 0x8B
#define SW9_CS13 0x8C
#define SW9_CS14 0x8D
#define SW9_CS15 0x8E

View File

@ -0,0 +1,219 @@
/* Copyright 2024 HorrorTroll <https://github.com/HorrorTroll>
* Copyright 2024 Harrison Chan (Xelus)
* Copyright 2024 Dimitris Mantzouranis <d3xter93@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "is31fl3729.h"
#include "i2c_master.h"
#include "gpio.h"
#include "wait.h"
#define IS31FL3729_PWM_REGISTER_COUNT 143
#define IS31FL3729_SCALING_REGISTER_COUNT 16
#ifndef IS31FL3729_I2C_TIMEOUT
# define IS31FL3729_I2C_TIMEOUT 100
#endif
#ifndef IS31FL3729_I2C_PERSISTENCE
# define IS31FL3729_I2C_PERSISTENCE 0
#endif
#ifndef IS31FL3729_CONFIGURATION
# define IS31FL3729_CONFIGURATION IS31FL3729_CONFIG_SWS_15_9
#endif
#ifndef IS31FL3729_GLOBAL_CURRENT
# define IS31FL3729_GLOBAL_CURRENT 0x40
#endif
#ifndef IS31FL3729_SW_PULLDOWN
# define IS31FL3729_SW_PULLDOWN IS31FL3729_SW_PULLDOWN_2K_OHM_SW_OFF
#endif
#ifndef IS31FL3729_CS_PULLUP
# define IS31FL3729_CS_PULLUP IS31FL3729_CS_PULLUP_2K_OHM_CS_OFF
#endif
#ifndef IS31FL3729_SPREAD_SPECTRUM
# define IS31FL3729_SPREAD_SPECTRUM IS31FL3729_SPREAD_SPECTRUM_DISABLE
#endif
#ifndef IS31FL3729_SPREAD_SPECTRUM_RANGE
# define IS31FL3729_SPREAD_SPECTRUM_RANGE IS31FL3729_SPREAD_SPECTRUM_RANGE_5_PERCENT
#endif
#ifndef IS31FL3729_SPREAD_SPECTRUM_CYCLE_TIME
# define IS31FL3729_SPREAD_SPECTRUM_CYCLE_TIME IS31FL3729_SPREAD_SPECTRUM_CYCLE_TIME_1980_US
#endif
#ifndef IS31FL3729_PWM_FREQUENCY
# define IS31FL3729_PWM_FREQUENCY IS31FL3729_PWM_FREQUENCY_32K_HZ
#endif
const uint8_t i2c_addresses[IS31FL3729_DRIVER_COUNT] = {
IS31FL3729_I2C_ADDRESS_1,
#ifdef IS31FL3729_I2C_ADDRESS_2
IS31FL3729_I2C_ADDRESS_2,
# ifdef IS31FL3729_I2C_ADDRESS_3
IS31FL3729_I2C_ADDRESS_3,
# ifdef IS31FL3729_I2C_ADDRESS_4
IS31FL3729_I2C_ADDRESS_4,
# endif
# endif
#endif
};
// These buffers match the PWM & scaling registers.
// Storing them like this is optimal for I2C transfers to the registers.
typedef struct is31fl3729_driver_t {
uint8_t pwm_buffer[IS31FL3729_PWM_REGISTER_COUNT];
bool pwm_buffer_dirty;
uint8_t scaling_buffer[IS31FL3729_SCALING_REGISTER_COUNT];
bool scaling_buffer_dirty;
} PACKED is31fl3729_driver_t;
is31fl3729_driver_t driver_buffers[IS31FL3729_DRIVER_COUNT] = {{
.pwm_buffer = {0},
.pwm_buffer_dirty = false,
.scaling_buffer = {0},
.scaling_buffer_dirty = false,
}};
void is31fl3729_write_register(uint8_t index, uint8_t reg, uint8_t data) {
#if IS31FL3729_I2C_PERSISTENCE > 0
for (uint8_t i = 0; i < IS31FL3729_I2C_PERSISTENCE; i++) {
if (i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3729_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3729_I2C_TIMEOUT);
#endif
}
void is31fl3729_write_pwm_buffer(uint8_t index) {
// Transmit PWM registers in 11 transfers of 13 bytes.
// Iterate over the pwm_buffer contents at 13 byte intervals.
for (uint8_t i = 0; i <= IS31FL3729_PWM_REGISTER_COUNT; i += 13) {
#if IS31FL3729_I2C_PERSISTENCE > 0
for (uint8_t j = 0; j < IS31FL3729_I2C_PERSISTENCE; j++) {
if (i2c_write_register(i2c_addresses[index] << 1, IS31FL3729_REG_PWM + i, driver_buffers[index].pwm_buffer + i, 13, IS31FL3729_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(i2c_addresses[index] << 1, IS31FL3729_REG_PWM + i, driver_buffers[index].pwm_buffer + i, 13, IS31FL3729_I2C_TIMEOUT);
#endif
}
}
void is31fl3729_init_drivers(void) {
i2c_init();
#if defined(IS31FL3729_SDB_PIN)
gpio_set_pin_output(IS31FL3729_SDB_PIN);
gpio_write_pin_high(IS31FL3729_SDB_PIN);
#endif
for (uint8_t i = 0; i < IS31FL3729_DRIVER_COUNT; i++) {
is31fl3729_init(i);
}
for (int i = 0; i < IS31FL3729_LED_COUNT; i++) {
is31fl3729_set_scaling_register(i, 0xFF, 0xFF, 0xFF);
}
for (uint8_t i = 0; i < IS31FL3729_DRIVER_COUNT; i++) {
is31fl3729_update_scaling_registers(i);
}
}
void is31fl3729_init(uint8_t index) {
// In order to avoid the LEDs being driven with garbage data
// in the LED driver's PWM registers, shutdown is enabled last.
// Set up the mode and other settings, clear the PWM registers,
// then disable software shutdown.
is31fl3729_write_register(index, IS31FL3729_REG_PULLDOWNUP, ((IS31FL3729_SW_PULLDOWN & 0b111) << 4) | (IS31FL3729_CS_PULLUP & 0b111));
is31fl3729_write_register(index, IS31FL3729_REG_SPREAD_SPECTRUM, ((IS31FL3729_SPREAD_SPECTRUM & 0b1) << 4) | ((IS31FL3729_SPREAD_SPECTRUM_RANGE & 0b11) << 2) | (IS31FL3729_SPREAD_SPECTRUM_CYCLE_TIME & 0b11));
is31fl3729_write_register(index, IS31FL3729_REG_PWM_FREQUENCY, IS31FL3729_PWM_FREQUENCY);
is31fl3729_write_register(index, IS31FL3729_REG_GLOBAL_CURRENT, IS31FL3729_GLOBAL_CURRENT);
is31fl3729_write_register(index, IS31FL3729_REG_CONFIGURATION, IS31FL3729_CONFIGURATION);
// Wait 10ms to ensure the device has woken up.
wait_ms(10);
}
void is31fl3729_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
is31fl3729_led_t led;
if (index >= 0 && index < IS31FL3729_LED_COUNT) {
memcpy_P(&led, (&g_is31fl3729_leds[index]), sizeof(led));
if (driver_buffers[led.driver].pwm_buffer[led.r] == red && driver_buffers[led.driver].pwm_buffer[led.g] == green && driver_buffers[led.driver].pwm_buffer[led.b] == blue) {
return;
}
driver_buffers[led.driver].pwm_buffer[led.r] = red;
driver_buffers[led.driver].pwm_buffer[led.g] = green;
driver_buffers[led.driver].pwm_buffer[led.b] = blue;
driver_buffers[led.driver].pwm_buffer_dirty = true;
}
}
void is31fl3729_set_color_all(uint8_t red, uint8_t green, uint8_t blue) {
for (int i = 0; i < IS31FL3729_LED_COUNT; i++) {
is31fl3729_set_color(i, red, green, blue);
}
}
void is31fl3729_set_scaling_register(uint8_t index, uint8_t red, uint8_t green, uint8_t blue) {
is31fl3729_led_t led;
memcpy_P(&led, (&g_is31fl3729_leds[index]), sizeof(led));
// need to do a bit of checking here since 3729 scaling is per CS pin.
// not the usual per RGB key as per other ISSI drivers
// only enable them, since they should be default disabled
int cs_red = (led.r & 0x0F) - 1;
int cs_green = (led.g & 0x0F) - 1;
int cs_blue = (led.b & 0x0F) - 1;
driver_buffers[led.driver].scaling_buffer[cs_red] = red;
driver_buffers[led.driver].scaling_buffer[cs_green] = green;
driver_buffers[led.driver].scaling_buffer[cs_blue] = blue;
driver_buffers[led.driver].scaling_buffer_dirty = true;
}
void is31fl3729_update_pwm_buffers(uint8_t index) {
if (driver_buffers[index].pwm_buffer_dirty) {
is31fl3729_write_pwm_buffer(index);
driver_buffers[index].pwm_buffer_dirty = false;
}
}
void is31fl3729_update_scaling_registers(uint8_t index) {
if (driver_buffers[index].scaling_buffer_dirty) {
for (uint8_t i = 0; i < IS31FL3729_SCALING_REGISTER_COUNT; i++) {
is31fl3729_write_register(index, IS31FL3729_REG_SCALING + i, driver_buffers[index].scaling_buffer[i]);
}
driver_buffers[index].scaling_buffer_dirty = false;
}
}
void is31fl3729_flush(void) {
for (uint8_t i = 0; i < IS31FL3729_DRIVER_COUNT; i++) {
is31fl3729_update_pwm_buffers(i);
}
}

View File

@ -0,0 +1,280 @@
/* Copyright 2024 HorrorTroll <https://github.com/HorrorTroll>
* Copyright 2024 Harrison Chan (Xelus)
* Copyright 2024 Dimitris Mantzouranis <d3xter93@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include "progmem.h"
#include "util.h"
#define IS31FL3729_REG_PWM 0x01
#define IS31FL3729_REG_SCALING 0x90
#define IS31FL3729_REG_CONFIGURATION 0xA0
#define IS31FL3729_REG_GLOBAL_CURRENT 0xA1
#define IS31FL3729_REG_PULLDOWNUP 0xB0
#define IS31FL3729_REG_SPREAD_SPECTRUM 0xB1
#define IS31FL3729_REG_PWM_FREQUENCY 0xB2
#define IS31FL3729_REG_RESET 0xCF
#define IS31FL3729_I2C_ADDRESS_GND 0x34
#define IS31FL3729_I2C_ADDRESS_SCL 0x35
#define IS31FL3729_I2C_ADDRESS_SDA 0x36
#define IS31FL3729_I2C_ADDRESS_VCC 0x37
#if defined(RGB_MATRIX_IS31FL3729)
# define IS31FL3729_LED_COUNT RGB_MATRIX_LED_COUNT
#endif
#if defined(IS31FL3729_I2C_ADDRESS_4)
# define IS31FL3729_DRIVER_COUNT 4
#elif defined(IS31FL3729_I2C_ADDRESS_3)
# define IS31FL3729_DRIVER_COUNT 3
#elif defined(IS31FL3729_I2C_ADDRESS_2)
# define IS31FL3729_DRIVER_COUNT 2
#elif defined(IS31FL3729_I2C_ADDRESS_1)
# define IS31FL3729_DRIVER_COUNT 1
#endif
typedef struct is31fl3729_led_t {
uint8_t driver : 2;
uint8_t r;
uint8_t g;
uint8_t b;
} PACKED is31fl3729_led_t;
extern const is31fl3729_led_t PROGMEM g_is31fl3729_leds[IS31FL3729_LED_COUNT];
void is31fl3729_init_drivers(void);
void is31fl3729_init(uint8_t index);
void is31fl3729_write_register(uint8_t index, uint8_t reg, uint8_t data);
void is31fl3729_set_color(int index, uint8_t red, uint8_t green, uint8_t blue);
void is31fl3729_set_color_all(uint8_t red, uint8_t green, uint8_t blue);
void is31fl3729_set_scaling_register(uint8_t index, uint8_t red, uint8_t green, uint8_t blue);
// This should not be called from an interrupt
// (eg. from a timer interrupt).
// Call this while idle (in between matrix scans).
// If the buffer is dirty, it will update the driver with the buffer.
void is31fl3729_update_pwm_buffers(uint8_t index);
void is31fl3729_update_scaling_registers(uint8_t index);
void is31fl3729_flush(void);
#define IS31FL3729_SW_PULLDOWN_0_OHM 0b000
#define IS31FL3729_SW_PULLDOWN_0K5_OHM_SW_OFF 0b001
#define IS31FL3729_SW_PULLDOWN_1K_OHM_SW_OFF 0b010
#define IS31FL3729_SW_PULLDOWN_2K_OHM_SW_OFF 0b011
#define IS31FL3729_SW_PULLDOWN_1K_OHM 0b100
#define IS31FL3729_SW_PULLDOWN_2K_OHM 0b101
#define IS31FL3729_SW_PULLDOWN_4K_OHM 0b110
#define IS31FL3729_SW_PULLDOWN_8K_OHM 0b111
#define IS31FL3729_CS_PULLUP_0_OHM 0b000
#define IS31FL3729_CS_PULLUP_0K5_OHM_CS_OFF 0b001
#define IS31FL3729_CS_PULLUP_1K_OHM_CS_OFF 0b010
#define IS31FL3729_CS_PULLUP_2K_OHM_CS_OFF 0b011
#define IS31FL3729_CS_PULLUP_1K_OHM 0b100
#define IS31FL3729_CS_PULLUP_2K_OHM 0b101
#define IS31FL3729_CS_PULLUP_4K_OHM 0b110
#define IS31FL3729_CS_PULLUP_8K_OHM 0b111
#define IS31FL3729_SPREAD_SPECTRUM_DISABLE 0b0
#define IS31FL3729_SPREAD_SPECTRUM_ENABLE 0b1
#define IS31FL3729_SPREAD_SPECTRUM_RANGE_5_PERCENT 0b00
#define IS31FL3729_SPREAD_SPECTRUM_RANGE_15_PERCENT 0b01
#define IS31FL3729_SPREAD_SPECTRUM_RANGE_24_PERCENT 0b10
#define IS31FL3729_SPREAD_SPECTRUM_RANGE_34_PERCENT 0b11
#define IS31FL3729_SPREAD_SPECTRUM_CYCLE_TIME_1980_US 0b00
#define IS31FL3729_SPREAD_SPECTRUM_CYCLE_TIME_1200_US 0b01
#define IS31FL3729_SPREAD_SPECTRUM_CYCLE_TIME_820_US 0b10
#define IS31FL3729_SPREAD_SPECTRUM_CYCLE_TIME_660_US 0b11
#define IS31FL3729_PWM_FREQUENCY_55K_HZ 0b000
#define IS31FL3729_PWM_FREQUENCY_32K_HZ 0b001
#define IS31FL3729_PWM_FREQUENCY_4K_HZ 0b010
#define IS31FL3729_PWM_FREQUENCY_2K_HZ 0b011
#define IS31FL3729_PWM_FREQUENCY_1K_HZ 0b100
#define IS31FL3729_PWM_FREQUENCY_500_HZ 0b101
#define IS31FL3729_PWM_FREQUENCY_250_HZ 0b110
#define IS31FL3729_PWM_FREQUENCY_80K_HZ 0b111
#define IS31FL3729_CONFIG_SWS_15_9 0x01 // 15 CS x 9 SW matrix
#define IS31FL3729_CONFIG_SWS_16_8 0x11 // 16 CS x 8 SW matrix
#define IS31FL3729_CONFIG_SWS_16_7 0x21 // 16 CS x 7 SW matrix
#define IS31FL3729_CONFIG_SWS_16_6 0x31 // 16 CS x 6 SW matrix
#define IS31FL3729_CONFIG_SWS_16_5 0x41 // 16 CS x 5 SW matrix
#define IS31FL3729_CONFIG_SWS_16_4 0x51 // 16 CS x 4 SW matrix
#define IS31FL3729_CONFIG_SWS_16_3 0x61 // 16 CS x 3 SW matrix
#define IS31FL3729_CONFIG_SWS_16_2 0x71 // 16 CS x 2 SW matrix
#define SW1_CS1 0x00
#define SW1_CS2 0x01
#define SW1_CS3 0x02
#define SW1_CS4 0x03
#define SW1_CS5 0x04
#define SW1_CS6 0x05
#define SW1_CS7 0x06
#define SW1_CS8 0x07
#define SW1_CS9 0x08
#define SW1_CS10 0x09
#define SW1_CS11 0x0A
#define SW1_CS12 0x0B
#define SW1_CS13 0x0C
#define SW1_CS14 0x0D
#define SW1_CS15 0x0E
#define SW1_CS16 0x0F
#define SW2_CS1 0x10
#define SW2_CS2 0x11
#define SW2_CS3 0x12
#define SW2_CS4 0x13
#define SW2_CS5 0x14
#define SW2_CS6 0x15
#define SW2_CS7 0x16
#define SW2_CS8 0x17
#define SW2_CS9 0x18
#define SW2_CS10 0x19
#define SW2_CS11 0x1A
#define SW2_CS12 0x1B
#define SW2_CS13 0x1C
#define SW2_CS14 0x1D
#define SW2_CS15 0x1E
#define SW2_CS16 0x1F
#define SW3_CS1 0x20
#define SW3_CS2 0x21
#define SW3_CS3 0x22
#define SW3_CS4 0x23
#define SW3_CS5 0x24
#define SW3_CS6 0x25
#define SW3_CS7 0x26
#define SW3_CS8 0x27
#define SW3_CS9 0x28
#define SW3_CS10 0x29
#define SW3_CS11 0x2A
#define SW3_CS12 0x2B
#define SW3_CS13 0x2C
#define SW3_CS14 0x2D
#define SW3_CS15 0x2E
#define SW3_CS16 0x2F
#define SW4_CS1 0x30
#define SW4_CS2 0x31
#define SW4_CS3 0x32
#define SW4_CS4 0x33
#define SW4_CS5 0x34
#define SW4_CS6 0x35
#define SW4_CS7 0x36
#define SW4_CS8 0x37
#define SW4_CS9 0x38
#define SW4_CS10 0x39
#define SW4_CS11 0x3A
#define SW4_CS12 0x3B
#define SW4_CS13 0x3C
#define SW4_CS14 0x3D
#define SW4_CS15 0x3E
#define SW4_CS16 0x3F
#define SW5_CS1 0x40
#define SW5_CS2 0x41
#define SW5_CS3 0x42
#define SW5_CS4 0x43
#define SW5_CS5 0x44
#define SW5_CS6 0x45
#define SW5_CS7 0x46
#define SW5_CS8 0x47
#define SW5_CS9 0x48
#define SW5_CS10 0x49
#define SW5_CS11 0x4A
#define SW5_CS12 0x4B
#define SW5_CS13 0x4C
#define SW5_CS14 0x4D
#define SW5_CS15 0x4E
#define SW5_CS16 0x4F
#define SW6_CS1 0x50
#define SW6_CS2 0x51
#define SW6_CS3 0x52
#define SW6_CS4 0x53
#define SW6_CS5 0x54
#define SW6_CS6 0x55
#define SW6_CS7 0x56
#define SW6_CS8 0x57
#define SW6_CS9 0x58
#define SW6_CS10 0x59
#define SW6_CS11 0x5A
#define SW6_CS12 0x5B
#define SW6_CS13 0x5C
#define SW6_CS14 0x5D
#define SW6_CS15 0x5E
#define SW6_CS16 0x5F
#define SW7_CS1 0x60
#define SW7_CS2 0x61
#define SW7_CS3 0x62
#define SW7_CS4 0x63
#define SW7_CS5 0x64
#define SW7_CS6 0x65
#define SW7_CS7 0x66
#define SW7_CS8 0x67
#define SW7_CS9 0x68
#define SW7_CS10 0x69
#define SW7_CS11 0x6A
#define SW7_CS12 0x6B
#define SW7_CS13 0x6C
#define SW7_CS14 0x6D
#define SW7_CS15 0x6E
#define SW7_CS16 0x6F
#define SW8_CS1 0x70
#define SW8_CS2 0x71
#define SW8_CS3 0x72
#define SW8_CS4 0x73
#define SW8_CS5 0x74
#define SW8_CS6 0x75
#define SW8_CS7 0x76
#define SW8_CS8 0x77
#define SW8_CS9 0x78
#define SW8_CS10 0x79
#define SW8_CS11 0x7A
#define SW8_CS12 0x7B
#define SW8_CS13 0x7C
#define SW8_CS14 0x7D
#define SW8_CS15 0x7E
#define SW8_CS16 0x7F
#define SW9_CS1 0x80
#define SW9_CS2 0x81
#define SW9_CS3 0x82
#define SW9_CS4 0x83
#define SW9_CS5 0x84
#define SW9_CS6 0x85
#define SW9_CS7 0x86
#define SW9_CS8 0x87
#define SW9_CS9 0x88
#define SW9_CS10 0x89
#define SW9_CS11 0x8A
#define SW9_CS12 0x8B
#define SW9_CS13 0x8C
#define SW9_CS14 0x8D
#define SW9_CS15 0x8E

View File

@ -19,6 +19,7 @@
#include "is31fl3731-mono.h"
#include "i2c_master.h"
#include "gpio.h"
#include "wait.h"
#define IS31FL3731_PWM_REGISTER_COUNT 144
@ -32,32 +33,53 @@
# define IS31FL3731_I2C_PERSISTENCE 0
#endif
const uint8_t i2c_addresses[IS31FL3731_DRIVER_COUNT] = {
IS31FL3731_I2C_ADDRESS_1,
#ifdef IS31FL3731_I2C_ADDRESS_2
IS31FL3731_I2C_ADDRESS_2,
# ifdef IS31FL3731_I2C_ADDRESS_3
IS31FL3731_I2C_ADDRESS_3,
# ifdef IS31FL3731_I2C_ADDRESS_4
IS31FL3731_I2C_ADDRESS_4,
# endif
# endif
#endif
};
// These buffers match the IS31FL3731 PWM registers 0x24-0xB3.
// Storing them like this is optimal for I2C transfers to the registers.
// We could optimize this and take out the unused registers from these
// buffers and the transfers in is31fl3731_write_pwm_buffer() but it's
// probably not worth the extra complexity.
uint8_t g_pwm_buffer[IS31FL3731_DRIVER_COUNT][IS31FL3731_PWM_REGISTER_COUNT];
bool g_pwm_buffer_update_required[IS31FL3731_DRIVER_COUNT] = {false};
typedef struct is31fl3731_driver_t {
uint8_t pwm_buffer[IS31FL3731_PWM_REGISTER_COUNT];
bool pwm_buffer_dirty;
uint8_t led_control_buffer[IS31FL3731_LED_CONTROL_REGISTER_COUNT];
bool led_control_buffer_dirty;
} PACKED is31fl3731_driver_t;
uint8_t g_led_control_registers[IS31FL3731_DRIVER_COUNT][IS31FL3731_LED_CONTROL_REGISTER_COUNT] = {0};
bool g_led_control_registers_update_required[IS31FL3731_DRIVER_COUNT] = {false};
is31fl3731_driver_t driver_buffers[IS31FL3731_DRIVER_COUNT] = {{
.pwm_buffer = {0},
.pwm_buffer_dirty = false,
.led_control_buffer = {0},
.led_control_buffer_dirty = false,
}};
void is31fl3731_write_register(uint8_t addr, uint8_t reg, uint8_t data) {
void is31fl3731_write_register(uint8_t index, uint8_t reg, uint8_t data) {
#if IS31FL3731_I2C_PERSISTENCE > 0
for (uint8_t i = 0; i < IS31FL3731_I2C_PERSISTENCE; i++) {
if (i2c_write_register(addr << 1, reg, &data, 1, IS31FL3731_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3731_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, reg, &data, 1, IS31FL3731_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3731_I2C_TIMEOUT);
#endif
}
void is31fl3731_select_page(uint8_t addr, uint8_t page) {
is31fl3731_write_register(addr, IS31FL3731_REG_COMMAND, page);
void is31fl3731_select_page(uint8_t index, uint8_t page) {
is31fl3731_write_register(index, IS31FL3731_REG_COMMAND, page);
}
void is31fl3731_write_pwm_buffer(uint8_t addr, uint8_t index) {
void is31fl3731_write_pwm_buffer(uint8_t index) {
// Assumes page 0 is already selected.
// Transmit PWM registers in 9 transfers of 16 bytes.
@ -65,10 +87,10 @@ void is31fl3731_write_pwm_buffer(uint8_t addr, uint8_t index) {
for (uint8_t i = 0; i < IS31FL3731_PWM_REGISTER_COUNT; i += 16) {
#if IS31FL3731_I2C_PERSISTENCE > 0
for (uint8_t j = 0; j < IS31FL3731_I2C_PERSISTENCE; j++) {
if (i2c_write_register(addr << 1, IS31FL3731_FRAME_REG_PWM + i, g_pwm_buffer[index] + i, 16, IS31FL3731_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, IS31FL3731_FRAME_REG_PWM + i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3731_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, IS31FL3731_FRAME_REG_PWM + i, g_pwm_buffer[index] + i, 16, IS31FL3731_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, IS31FL3731_FRAME_REG_PWM + i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3731_I2C_TIMEOUT);
#endif
}
}
@ -76,83 +98,74 @@ void is31fl3731_write_pwm_buffer(uint8_t addr, uint8_t index) {
void is31fl3731_init_drivers(void) {
i2c_init();
is31fl3731_init(IS31FL3731_I2C_ADDRESS_1);
#if defined(IS31FL3731_I2C_ADDRESS_2)
is31fl3731_init(IS31FL3731_I2C_ADDRESS_2);
# if defined(IS31FL3731_I2C_ADDRESS_3)
is31fl3731_init(IS31FL3731_I2C_ADDRESS_3);
# if defined(IS31FL3731_I2C_ADDRESS_4)
is31fl3731_init(IS31FL3731_I2C_ADDRESS_4);
# endif
# endif
#if defined(IS31FL3731_SDB_PIN)
gpio_set_pin_output(IS31FL3731_SDB_PIN);
gpio_write_pin_high(IS31FL3731_SDB_PIN);
#endif
for (uint8_t i = 0; i < IS31FL3731_DRIVER_COUNT; i++) {
is31fl3731_init(i);
}
for (int i = 0; i < IS31FL3731_LED_COUNT; i++) {
is31fl3731_set_led_control_register(i, true);
}
is31fl3731_update_led_control_registers(IS31FL3731_I2C_ADDRESS_1, 0);
#if defined(IS31FL3731_I2C_ADDRESS_2)
is31fl3731_update_led_control_registers(IS31FL3731_I2C_ADDRESS_2, 1);
# if defined(IS31FL3731_I2C_ADDRESS_3)
is31fl3731_update_led_control_registers(IS31FL3731_I2C_ADDRESS_3, 2);
# if defined(IS31FL3731_I2C_ADDRESS_4)
is31fl3731_update_led_control_registers(IS31FL3731_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3731_DRIVER_COUNT; i++) {
is31fl3731_update_led_control_registers(i);
}
}
void is31fl3731_init(uint8_t addr) {
void is31fl3731_init(uint8_t index) {
// In order to avoid the LEDs being driven with garbage data
// in the LED driver's PWM registers, first enable software shutdown,
// then set up the mode and other settings, clear the PWM registers,
// then disable software shutdown.
is31fl3731_select_page(addr, IS31FL3731_COMMAND_FUNCTION);
is31fl3731_select_page(index, IS31FL3731_COMMAND_FUNCTION);
// enable software shutdown
is31fl3731_write_register(addr, IS31FL3731_FUNCTION_REG_SHUTDOWN, 0x00);
is31fl3731_write_register(index, IS31FL3731_FUNCTION_REG_SHUTDOWN, 0x00);
#ifdef IS31FL3731_DEGHOST // set to enable de-ghosting of the array
is31fl3731_write_register(addr, IS31FL3731_FUNCTION_REG_GHOST_IMAGE_PREVENTION, IS31FL3731_GHOST_IMAGE_PREVENTION_GEN);
is31fl3731_write_register(index, IS31FL3731_FUNCTION_REG_GHOST_IMAGE_PREVENTION, IS31FL3731_GHOST_IMAGE_PREVENTION_GEN);
#endif
// this delay was copied from other drivers, might not be needed
wait_ms(10);
// picture mode
is31fl3731_write_register(addr, IS31FL3731_FUNCTION_REG_CONFIG, IS31FL3731_CONFIG_MODE_PICTURE);
is31fl3731_write_register(index, IS31FL3731_FUNCTION_REG_CONFIG, IS31FL3731_CONFIG_MODE_PICTURE);
// display frame 0
is31fl3731_write_register(addr, IS31FL3731_FUNCTION_REG_PICTURE_DISPLAY, 0x00);
is31fl3731_write_register(index, IS31FL3731_FUNCTION_REG_PICTURE_DISPLAY, 0x00);
// audio sync off
is31fl3731_write_register(addr, IS31FL3731_FUNCTION_REG_AUDIO_SYNC, 0x00);
is31fl3731_write_register(index, IS31FL3731_FUNCTION_REG_AUDIO_SYNC, 0x00);
is31fl3731_select_page(addr, IS31FL3731_COMMAND_FRAME_1);
is31fl3731_select_page(index, IS31FL3731_COMMAND_FRAME_1);
// turn off all LEDs in the LED control register
for (uint8_t i = 0; i < IS31FL3731_LED_CONTROL_REGISTER_COUNT; i++) {
is31fl3731_write_register(addr, IS31FL3731_FRAME_REG_LED_CONTROL + i, 0x00);
is31fl3731_write_register(index, IS31FL3731_FRAME_REG_LED_CONTROL + i, 0x00);
}
// turn off all LEDs in the blink control register (not really needed)
for (uint8_t i = 0; i < IS31FL3731_LED_CONTROL_REGISTER_COUNT; i++) {
is31fl3731_write_register(addr, IS31FL3731_FRAME_REG_BLINK_CONTROL + i, 0x00);
is31fl3731_write_register(index, IS31FL3731_FRAME_REG_BLINK_CONTROL + i, 0x00);
}
// set PWM on all LEDs to 0
for (uint8_t i = 0; i < IS31FL3731_PWM_REGISTER_COUNT; i++) {
is31fl3731_write_register(addr, IS31FL3731_FRAME_REG_PWM + i, 0x00);
is31fl3731_write_register(index, IS31FL3731_FRAME_REG_PWM + i, 0x00);
}
is31fl3731_select_page(addr, IS31FL3731_COMMAND_FUNCTION);
is31fl3731_select_page(index, IS31FL3731_COMMAND_FUNCTION);
// disable software shutdown
is31fl3731_write_register(addr, IS31FL3731_FUNCTION_REG_SHUTDOWN, 0x01);
is31fl3731_write_register(index, IS31FL3731_FUNCTION_REG_SHUTDOWN, 0x01);
// select page 0 and leave it selected.
// most usage after initialization is just writing PWM buffers in page 0
// as there's not much point in double-buffering
is31fl3731_select_page(addr, IS31FL3731_COMMAND_FRAME_1);
is31fl3731_select_page(index, IS31FL3731_COMMAND_FRAME_1);
}
void is31fl3731_set_value(int index, uint8_t value) {
@ -161,12 +174,12 @@ void is31fl3731_set_value(int index, uint8_t value) {
if (index >= 0 && index < IS31FL3731_LED_COUNT) {
memcpy_P(&led, (&g_is31fl3731_leds[index]), sizeof(led));
if (g_pwm_buffer[led.driver][led.v] == value) {
if (driver_buffers[led.driver].pwm_buffer[led.v] == value) {
return;
}
g_pwm_buffer[led.driver][led.v] = value;
g_pwm_buffer_update_required[led.driver] = true;
driver_buffers[led.driver].pwm_buffer[led.v] = value;
driver_buffers[led.driver].pwm_buffer_dirty = true;
}
}
@ -184,41 +197,34 @@ void is31fl3731_set_led_control_register(uint8_t index, bool value) {
uint8_t bit_value = led.v % 8;
if (value) {
g_led_control_registers[led.driver][control_register] |= (1 << bit_value);
driver_buffers[led.driver].led_control_buffer[control_register] |= (1 << bit_value);
} else {
g_led_control_registers[led.driver][control_register] &= ~(1 << bit_value);
driver_buffers[led.driver].led_control_buffer[control_register] &= ~(1 << bit_value);
}
g_led_control_registers_update_required[led.driver] = true;
driver_buffers[led.driver].led_control_buffer_dirty = true;
}
void is31fl3731_update_pwm_buffers(uint8_t addr, uint8_t index) {
if (g_pwm_buffer_update_required[index]) {
is31fl3731_write_pwm_buffer(addr, index);
void is31fl3731_update_pwm_buffers(uint8_t index) {
if (driver_buffers[index].pwm_buffer_dirty) {
is31fl3731_write_pwm_buffer(index);
g_pwm_buffer_update_required[index] = false;
driver_buffers[index].pwm_buffer_dirty = false;
}
}
void is31fl3731_update_led_control_registers(uint8_t addr, uint8_t index) {
if (g_led_control_registers_update_required[index]) {
void is31fl3731_update_led_control_registers(uint8_t index) {
if (driver_buffers[index].led_control_buffer_dirty) {
for (uint8_t i = 0; i < IS31FL3731_LED_CONTROL_REGISTER_COUNT; i++) {
is31fl3731_write_register(addr, i, g_led_control_registers[index][i]);
is31fl3731_write_register(index, i, driver_buffers[index].led_control_buffer[i]);
}
g_led_control_registers_update_required[index] = false;
driver_buffers[index].led_control_buffer_dirty = false;
}
}
void is31fl3731_flush(void) {
is31fl3731_update_pwm_buffers(IS31FL3731_I2C_ADDRESS_1, 0);
#if defined(IS31FL3731_I2C_ADDRESS_2)
is31fl3731_update_pwm_buffers(IS31FL3731_I2C_ADDRESS_2, 1);
# if defined(IS31FL3731_I2C_ADDRESS_3)
is31fl3731_update_pwm_buffers(IS31FL3731_I2C_ADDRESS_3, 2);
# if defined(IS31FL3731_I2C_ADDRESS_4)
is31fl3731_update_pwm_buffers(IS31FL3731_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3731_DRIVER_COUNT; i++) {
is31fl3731_update_pwm_buffers(i);
}
}

View File

@ -105,9 +105,9 @@ typedef struct is31fl3731_led_t {
extern const is31fl3731_led_t PROGMEM g_is31fl3731_leds[IS31FL3731_LED_COUNT];
void is31fl3731_init_drivers(void);
void is31fl3731_init(uint8_t addr);
void is31fl3731_write_register(uint8_t addr, uint8_t reg, uint8_t data);
void is31fl3731_select_page(uint8_t addr, uint8_t page);
void is31fl3731_init(uint8_t index);
void is31fl3731_write_register(uint8_t index, uint8_t reg, uint8_t data);
void is31fl3731_select_page(uint8_t index, uint8_t page);
void is31fl3731_set_value(int index, uint8_t value);
void is31fl3731_set_value_all(uint8_t value);
@ -118,8 +118,8 @@ void is31fl3731_set_led_control_register(uint8_t index, bool value);
// (eg. from a timer interrupt).
// Call this while idle (in between matrix scans).
// If the buffer is dirty, it will update the driver with the buffer.
void is31fl3731_update_pwm_buffers(uint8_t addr, uint8_t index);
void is31fl3731_update_led_control_registers(uint8_t addr, uint8_t index);
void is31fl3731_update_pwm_buffers(uint8_t index);
void is31fl3731_update_led_control_registers(uint8_t index);
void is31fl3731_flush(void);

View File

@ -18,6 +18,7 @@
#include "is31fl3731.h"
#include "i2c_master.h"
#include "gpio.h"
#include "wait.h"
#define IS31FL3731_PWM_REGISTER_COUNT 144
@ -31,32 +32,53 @@
# define IS31FL3731_I2C_PERSISTENCE 0
#endif
const uint8_t i2c_addresses[IS31FL3731_DRIVER_COUNT] = {
IS31FL3731_I2C_ADDRESS_1,
#ifdef IS31FL3731_I2C_ADDRESS_2
IS31FL3731_I2C_ADDRESS_2,
# ifdef IS31FL3731_I2C_ADDRESS_3
IS31FL3731_I2C_ADDRESS_3,
# ifdef IS31FL3731_I2C_ADDRESS_4
IS31FL3731_I2C_ADDRESS_4,
# endif
# endif
#endif
};
// These buffers match the IS31FL3731 PWM registers 0x24-0xB3.
// Storing them like this is optimal for I2C transfers to the registers.
// We could optimize this and take out the unused registers from these
// buffers and the transfers in is31fl3731_write_pwm_buffer() but it's
// probably not worth the extra complexity.
uint8_t g_pwm_buffer[IS31FL3731_DRIVER_COUNT][IS31FL3731_PWM_REGISTER_COUNT];
bool g_pwm_buffer_update_required[IS31FL3731_DRIVER_COUNT] = {false};
typedef struct is31fl3731_driver_t {
uint8_t pwm_buffer[IS31FL3731_PWM_REGISTER_COUNT];
bool pwm_buffer_dirty;
uint8_t led_control_buffer[IS31FL3731_LED_CONTROL_REGISTER_COUNT];
bool led_control_buffer_dirty;
} PACKED is31fl3731_driver_t;
uint8_t g_led_control_registers[IS31FL3731_DRIVER_COUNT][IS31FL3731_LED_CONTROL_REGISTER_COUNT] = {0};
bool g_led_control_registers_update_required[IS31FL3731_DRIVER_COUNT] = {false};
is31fl3731_driver_t driver_buffers[IS31FL3731_DRIVER_COUNT] = {{
.pwm_buffer = {0},
.pwm_buffer_dirty = false,
.led_control_buffer = {0},
.led_control_buffer_dirty = false,
}};
void is31fl3731_write_register(uint8_t addr, uint8_t reg, uint8_t data) {
void is31fl3731_write_register(uint8_t index, uint8_t reg, uint8_t data) {
#if IS31FL3731_I2C_PERSISTENCE > 0
for (uint8_t i = 0; i < IS31FL3731_I2C_PERSISTENCE; i++) {
if (i2c_write_register(addr << 1, reg, &data, 1, IS31FL3731_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3731_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, reg, &data, 1, IS31FL3731_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3731_I2C_TIMEOUT);
#endif
}
void is31fl3731_select_page(uint8_t addr, uint8_t page) {
is31fl3731_write_register(addr, IS31FL3731_REG_COMMAND, page);
void is31fl3731_select_page(uint8_t index, uint8_t page) {
is31fl3731_write_register(index, IS31FL3731_REG_COMMAND, page);
}
void is31fl3731_write_pwm_buffer(uint8_t addr, uint8_t index) {
void is31fl3731_write_pwm_buffer(uint8_t index) {
// Assumes page 0 is already selected.
// Transmit PWM registers in 9 transfers of 16 bytes.
@ -64,10 +86,10 @@ void is31fl3731_write_pwm_buffer(uint8_t addr, uint8_t index) {
for (uint8_t i = 0; i < IS31FL3731_PWM_REGISTER_COUNT; i += 16) {
#if IS31FL3731_I2C_PERSISTENCE > 0
for (uint8_t j = 0; j < IS31FL3731_I2C_PERSISTENCE; j++) {
if (i2c_write_register(addr << 1, IS31FL3731_FRAME_REG_PWM + i, g_pwm_buffer[index] + i, 16, IS31FL3731_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, IS31FL3731_FRAME_REG_PWM + i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3731_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, IS31FL3731_FRAME_REG_PWM + i, g_pwm_buffer[index] + i, 16, IS31FL3731_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, IS31FL3731_FRAME_REG_PWM + i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3731_I2C_TIMEOUT);
#endif
}
}
@ -75,83 +97,74 @@ void is31fl3731_write_pwm_buffer(uint8_t addr, uint8_t index) {
void is31fl3731_init_drivers(void) {
i2c_init();
is31fl3731_init(IS31FL3731_I2C_ADDRESS_1);
#if defined(IS31FL3731_I2C_ADDRESS_2)
is31fl3731_init(IS31FL3731_I2C_ADDRESS_2);
# if defined(IS31FL3731_I2C_ADDRESS_3)
is31fl3731_init(IS31FL3731_I2C_ADDRESS_3);
# if defined(IS31FL3731_I2C_ADDRESS_4)
is31fl3731_init(IS31FL3731_I2C_ADDRESS_4);
# endif
# endif
#if defined(IS31FL3731_SDB_PIN)
gpio_set_pin_output(IS31FL3731_SDB_PIN);
gpio_write_pin_high(IS31FL3731_SDB_PIN);
#endif
for (uint8_t i = 0; i < IS31FL3731_DRIVER_COUNT; i++) {
is31fl3731_init(i);
}
for (int i = 0; i < IS31FL3731_LED_COUNT; i++) {
is31fl3731_set_led_control_register(i, true, true, true);
}
is31fl3731_update_led_control_registers(IS31FL3731_I2C_ADDRESS_1, 0);
#if defined(IS31FL3731_I2C_ADDRESS_2)
is31fl3731_update_led_control_registers(IS31FL3731_I2C_ADDRESS_2, 1);
# if defined(IS31FL3731_I2C_ADDRESS_3)
is31fl3731_update_led_control_registers(IS31FL3731_I2C_ADDRESS_3, 2);
# if defined(IS31FL3731_I2C_ADDRESS_4)
is31fl3731_update_led_control_registers(IS31FL3731_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3731_DRIVER_COUNT; i++) {
is31fl3731_update_led_control_registers(i);
}
}
void is31fl3731_init(uint8_t addr) {
void is31fl3731_init(uint8_t index) {
// In order to avoid the LEDs being driven with garbage data
// in the LED driver's PWM registers, first enable software shutdown,
// then set up the mode and other settings, clear the PWM registers,
// then disable software shutdown.
is31fl3731_select_page(addr, IS31FL3731_COMMAND_FUNCTION);
is31fl3731_select_page(index, IS31FL3731_COMMAND_FUNCTION);
// enable software shutdown
is31fl3731_write_register(addr, IS31FL3731_FUNCTION_REG_SHUTDOWN, 0x00);
is31fl3731_write_register(index, IS31FL3731_FUNCTION_REG_SHUTDOWN, 0x00);
#ifdef IS31FL3731_DEGHOST // set to enable de-ghosting of the array
is31fl3731_write_register(addr, IS31FL3731_FUNCTION_REG_GHOST_IMAGE_PREVENTION, IS31FL3731_GHOST_IMAGE_PREVENTION_GEN);
is31fl3731_write_register(index, IS31FL3731_FUNCTION_REG_GHOST_IMAGE_PREVENTION, IS31FL3731_GHOST_IMAGE_PREVENTION_GEN);
#endif
// this delay was copied from other drivers, might not be needed
wait_ms(10);
// picture mode
is31fl3731_write_register(addr, IS31FL3731_FUNCTION_REG_CONFIG, IS31FL3731_CONFIG_MODE_PICTURE);
is31fl3731_write_register(index, IS31FL3731_FUNCTION_REG_CONFIG, IS31FL3731_CONFIG_MODE_PICTURE);
// display frame 0
is31fl3731_write_register(addr, IS31FL3731_FUNCTION_REG_PICTURE_DISPLAY, 0x00);
is31fl3731_write_register(index, IS31FL3731_FUNCTION_REG_PICTURE_DISPLAY, 0x00);
// audio sync off
is31fl3731_write_register(addr, IS31FL3731_FUNCTION_REG_AUDIO_SYNC, 0x00);
is31fl3731_write_register(index, IS31FL3731_FUNCTION_REG_AUDIO_SYNC, 0x00);
is31fl3731_select_page(addr, IS31FL3731_COMMAND_FRAME_1);
is31fl3731_select_page(index, IS31FL3731_COMMAND_FRAME_1);
// turn off all LEDs in the LED control register
for (uint8_t i = 0; i < IS31FL3731_LED_CONTROL_REGISTER_COUNT; i++) {
is31fl3731_write_register(addr, IS31FL3731_FRAME_REG_LED_CONTROL + i, 0x00);
is31fl3731_write_register(index, IS31FL3731_FRAME_REG_LED_CONTROL + i, 0x00);
}
// turn off all LEDs in the blink control register (not really needed)
for (uint8_t i = 0; i < IS31FL3731_LED_CONTROL_REGISTER_COUNT; i++) {
is31fl3731_write_register(addr, IS31FL3731_FRAME_REG_BLINK_CONTROL + i, 0x00);
is31fl3731_write_register(index, IS31FL3731_FRAME_REG_BLINK_CONTROL + i, 0x00);
}
// set PWM on all LEDs to 0
for (uint8_t i = 0; i < IS31FL3731_PWM_REGISTER_COUNT; i++) {
is31fl3731_write_register(addr, IS31FL3731_FRAME_REG_PWM + i, 0x00);
is31fl3731_write_register(index, IS31FL3731_FRAME_REG_PWM + i, 0x00);
}
is31fl3731_select_page(addr, IS31FL3731_COMMAND_FUNCTION);
is31fl3731_select_page(index, IS31FL3731_COMMAND_FUNCTION);
// disable software shutdown
is31fl3731_write_register(addr, IS31FL3731_FUNCTION_REG_SHUTDOWN, 0x01);
is31fl3731_write_register(index, IS31FL3731_FUNCTION_REG_SHUTDOWN, 0x01);
// select page 0 and leave it selected.
// most usage after initialization is just writing PWM buffers in page 0
// as there's not much point in double-buffering
is31fl3731_select_page(addr, IS31FL3731_COMMAND_FRAME_1);
is31fl3731_select_page(index, IS31FL3731_COMMAND_FRAME_1);
}
void is31fl3731_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
@ -160,14 +173,14 @@ void is31fl3731_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
if (index >= 0 && index < IS31FL3731_LED_COUNT) {
memcpy_P(&led, (&g_is31fl3731_leds[index]), sizeof(led));
if (g_pwm_buffer[led.driver][led.r] == red && g_pwm_buffer[led.driver][led.g] == green && g_pwm_buffer[led.driver][led.b] == blue) {
if (driver_buffers[led.driver].pwm_buffer[led.r] == red && driver_buffers[led.driver].pwm_buffer[led.g] == green && driver_buffers[led.driver].pwm_buffer[led.b] == blue) {
return;
}
g_pwm_buffer[led.driver][led.r] = red;
g_pwm_buffer[led.driver][led.g] = green;
g_pwm_buffer[led.driver][led.b] = blue;
g_pwm_buffer_update_required[led.driver] = true;
driver_buffers[led.driver].pwm_buffer[led.r] = red;
driver_buffers[led.driver].pwm_buffer[led.g] = green;
driver_buffers[led.driver].pwm_buffer[led.b] = blue;
driver_buffers[led.driver].pwm_buffer_dirty = true;
}
}
@ -189,51 +202,44 @@ void is31fl3731_set_led_control_register(uint8_t index, bool red, bool green, bo
uint8_t bit_b = led.b % 8;
if (red) {
g_led_control_registers[led.driver][control_register_r] |= (1 << bit_r);
driver_buffers[led.driver].led_control_buffer[control_register_r] |= (1 << bit_r);
} else {
g_led_control_registers[led.driver][control_register_r] &= ~(1 << bit_r);
driver_buffers[led.driver].led_control_buffer[control_register_r] &= ~(1 << bit_r);
}
if (green) {
g_led_control_registers[led.driver][control_register_g] |= (1 << bit_g);
driver_buffers[led.driver].led_control_buffer[control_register_g] |= (1 << bit_g);
} else {
g_led_control_registers[led.driver][control_register_g] &= ~(1 << bit_g);
driver_buffers[led.driver].led_control_buffer[control_register_g] &= ~(1 << bit_g);
}
if (blue) {
g_led_control_registers[led.driver][control_register_b] |= (1 << bit_b);
driver_buffers[led.driver].led_control_buffer[control_register_b] |= (1 << bit_b);
} else {
g_led_control_registers[led.driver][control_register_b] &= ~(1 << bit_b);
driver_buffers[led.driver].led_control_buffer[control_register_b] &= ~(1 << bit_b);
}
g_led_control_registers_update_required[led.driver] = true;
driver_buffers[led.driver].led_control_buffer_dirty = true;
}
void is31fl3731_update_pwm_buffers(uint8_t addr, uint8_t index) {
if (g_pwm_buffer_update_required[index]) {
is31fl3731_write_pwm_buffer(addr, index);
void is31fl3731_update_pwm_buffers(uint8_t index) {
if (driver_buffers[index].pwm_buffer_dirty) {
is31fl3731_write_pwm_buffer(index);
g_pwm_buffer_update_required[index] = false;
driver_buffers[index].pwm_buffer_dirty = false;
}
}
void is31fl3731_update_led_control_registers(uint8_t addr, uint8_t index) {
if (g_led_control_registers_update_required[index]) {
void is31fl3731_update_led_control_registers(uint8_t index) {
if (driver_buffers[index].led_control_buffer_dirty) {
for (uint8_t i = 0; i < IS31FL3731_LED_CONTROL_REGISTER_COUNT; i++) {
is31fl3731_write_register(addr, i, g_led_control_registers[index][i]);
is31fl3731_write_register(index, i, driver_buffers[index].led_control_buffer[i]);
}
g_led_control_registers_update_required[index] = false;
driver_buffers[index].led_control_buffer_dirty = false;
}
}
void is31fl3731_flush(void) {
is31fl3731_update_pwm_buffers(IS31FL3731_I2C_ADDRESS_1, 0);
#if defined(IS31FL3731_I2C_ADDRESS_2)
is31fl3731_update_pwm_buffers(IS31FL3731_I2C_ADDRESS_2, 1);
# if defined(IS31FL3731_I2C_ADDRESS_3)
is31fl3731_update_pwm_buffers(IS31FL3731_I2C_ADDRESS_3, 2);
# if defined(IS31FL3731_I2C_ADDRESS_4)
is31fl3731_update_pwm_buffers(IS31FL3731_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3731_DRIVER_COUNT; i++) {
is31fl3731_update_pwm_buffers(i);
}
}

View File

@ -106,9 +106,9 @@ typedef struct is31fl3731_led_t {
extern const is31fl3731_led_t PROGMEM g_is31fl3731_leds[IS31FL3731_LED_COUNT];
void is31fl3731_init_drivers(void);
void is31fl3731_init(uint8_t addr);
void is31fl3731_write_register(uint8_t addr, uint8_t reg, uint8_t data);
void is31fl3731_select_page(uint8_t addr, uint8_t page);
void is31fl3731_init(uint8_t index);
void is31fl3731_write_register(uint8_t index, uint8_t reg, uint8_t data);
void is31fl3731_select_page(uint8_t index, uint8_t page);
void is31fl3731_set_color(int index, uint8_t red, uint8_t green, uint8_t blue);
void is31fl3731_set_color_all(uint8_t red, uint8_t green, uint8_t blue);
@ -119,8 +119,8 @@ void is31fl3731_set_led_control_register(uint8_t index, bool red, bool green, bo
// (eg. from a timer interrupt).
// Call this while idle (in between matrix scans).
// If the buffer is dirty, it will update the driver with the buffer.
void is31fl3731_update_pwm_buffers(uint8_t addr, uint8_t index);
void is31fl3731_update_led_control_registers(uint8_t addr, uint8_t index);
void is31fl3731_update_pwm_buffers(uint8_t index);
void is31fl3731_update_led_control_registers(uint8_t index);
void is31fl3731_flush(void);

View File

@ -20,6 +20,7 @@
#include "is31fl3733-mono.h"
#include "i2c_master.h"
#include "gpio.h"
#include "wait.h"
#define IS31FL3733_PWM_REGISTER_COUNT 192
@ -62,34 +63,68 @@
# define IS31FL3733_SYNC_4 IS31FL3733_SYNC_NONE
#endif
const uint8_t i2c_addresses[IS31FL3733_DRIVER_COUNT] = {
IS31FL3733_I2C_ADDRESS_1,
#ifdef IS31FL3733_I2C_ADDRESS_2
IS31FL3733_I2C_ADDRESS_2,
# ifdef IS31FL3733_I2C_ADDRESS_3
IS31FL3733_I2C_ADDRESS_3,
# ifdef IS31FL3733_I2C_ADDRESS_4
IS31FL3733_I2C_ADDRESS_4,
# endif
# endif
#endif
};
const uint8_t driver_sync[IS31FL3733_DRIVER_COUNT] = {
IS31FL3733_SYNC_1,
#ifdef IS31FL3733_I2C_ADDRESS_2
IS31FL3733_SYNC_2,
# ifdef IS31FL3733_I2C_ADDRESS_3
IS31FL3733_SYNC_3,
# ifdef IS31FL3733_I2C_ADDRESS_4
IS31FL3733_SYNC_4,
# endif
# endif
#endif
};
// These buffers match the IS31FL3733 PWM registers.
// The control buffers match the page 0 LED On/Off registers.
// Storing them like this is optimal for I2C transfers to the registers.
// We could optimize this and take out the unused registers from these
// buffers and the transfers in is31fl3733_write_pwm_buffer() but it's
// probably not worth the extra complexity.
uint8_t g_pwm_buffer[IS31FL3733_DRIVER_COUNT][IS31FL3733_PWM_REGISTER_COUNT];
bool g_pwm_buffer_update_required[IS31FL3733_DRIVER_COUNT] = {false};
typedef struct is31fl3733_driver_t {
uint8_t pwm_buffer[IS31FL3733_PWM_REGISTER_COUNT];
bool pwm_buffer_dirty;
uint8_t led_control_buffer[IS31FL3733_LED_CONTROL_REGISTER_COUNT];
bool led_control_buffer_dirty;
} PACKED is31fl3733_driver_t;
uint8_t g_led_control_registers[IS31FL3733_DRIVER_COUNT][IS31FL3733_LED_CONTROL_REGISTER_COUNT] = {0};
bool g_led_control_registers_update_required[IS31FL3733_DRIVER_COUNT] = {false};
is31fl3733_driver_t driver_buffers[IS31FL3733_DRIVER_COUNT] = {{
.pwm_buffer = {0},
.pwm_buffer_dirty = false,
.led_control_buffer = {0},
.led_control_buffer_dirty = false,
}};
void is31fl3733_write_register(uint8_t addr, uint8_t reg, uint8_t data) {
void is31fl3733_write_register(uint8_t index, uint8_t reg, uint8_t data) {
#if IS31FL3733_I2C_PERSISTENCE > 0
for (uint8_t i = 0; i < IS31FL3733_I2C_PERSISTENCE; i++) {
if (i2c_write_register(addr << 1, reg, &data, 1, IS31FL3733_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3733_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, reg, &data, 1, IS31FL3733_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3733_I2C_TIMEOUT);
#endif
}
void is31fl3733_select_page(uint8_t addr, uint8_t page) {
is31fl3733_write_register(addr, IS31FL3733_REG_COMMAND_WRITE_LOCK, IS31FL3733_COMMAND_WRITE_LOCK_MAGIC);
is31fl3733_write_register(addr, IS31FL3733_REG_COMMAND, page);
void is31fl3733_select_page(uint8_t index, uint8_t page) {
is31fl3733_write_register(index, IS31FL3733_REG_COMMAND_WRITE_LOCK, IS31FL3733_COMMAND_WRITE_LOCK_MAGIC);
is31fl3733_write_register(index, IS31FL3733_REG_COMMAND, page);
}
void is31fl3733_write_pwm_buffer(uint8_t addr, uint8_t index) {
void is31fl3733_write_pwm_buffer(uint8_t index) {
// Assumes page 1 is already selected.
// Transmit PWM registers in 12 transfers of 16 bytes.
@ -97,10 +132,10 @@ void is31fl3733_write_pwm_buffer(uint8_t addr, uint8_t index) {
for (uint8_t i = 0; i < IS31FL3733_PWM_REGISTER_COUNT; i += 16) {
#if IS31FL3733_I2C_PERSISTENCE > 0
for (uint8_t j = 0; j < IS31FL3733_I2C_PERSISTENCE; j++) {
if (i2c_write_register(addr << 1, i, g_pwm_buffer[index] + i, 16, IS31FL3733_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3733_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, i, g_pwm_buffer[index] + i, 16, IS31FL3733_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3733_I2C_TIMEOUT);
#endif
}
}
@ -108,65 +143,57 @@ void is31fl3733_write_pwm_buffer(uint8_t addr, uint8_t index) {
void is31fl3733_init_drivers(void) {
i2c_init();
is31fl3733_init(IS31FL3733_I2C_ADDRESS_1, IS31FL3733_SYNC_1);
#if defined(IS31FL3733_I2C_ADDRESS_2)
is31fl3733_init(IS31FL3733_I2C_ADDRESS_2, IS31FL3733_SYNC_2);
# if defined(IS31FL3733_I2C_ADDRESS_3)
is31fl3733_init(IS31FL3733_I2C_ADDRESS_3, IS31FL3733_SYNC_3);
# if defined(IS31FL3733_I2C_ADDRESS_4)
is31fl3733_init(IS31FL3733_I2C_ADDRESS_4, IS31FL3733_SYNC_4);
# endif
# endif
#if defined(IS31FL3733_SDB_PIN)
gpio_set_pin_output(IS31FL3733_SDB_PIN);
gpio_write_pin_high(IS31FL3733_SDB_PIN);
#endif
for (uint8_t i = 0; i < IS31FL3733_DRIVER_COUNT; i++) {
is31fl3733_init(i);
}
for (int i = 0; i < IS31FL3733_LED_COUNT; i++) {
is31fl3733_set_led_control_register(i, true);
}
is31fl3733_update_led_control_registers(IS31FL3733_I2C_ADDRESS_1, 0);
#if defined(IS31FL3733_I2C_ADDRESS_2)
is31fl3733_update_led_control_registers(IS31FL3733_I2C_ADDRESS_2, 1);
# if defined(IS31FL3733_I2C_ADDRESS_3)
is31fl3733_update_led_control_registers(IS31FL3733_I2C_ADDRESS_3, 2);
# if defined(IS31FL3733_I2C_ADDRESS_4)
is31fl3733_update_led_control_registers(IS31FL3733_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3733_DRIVER_COUNT; i++) {
is31fl3733_update_led_control_registers(i);
}
}
void is31fl3733_init(uint8_t addr, uint8_t sync) {
void is31fl3733_init(uint8_t index) {
// In order to avoid the LEDs being driven with garbage data
// in the LED driver's PWM registers, shutdown is enabled last.
// Set up the mode and other settings, clear the PWM registers,
// then disable software shutdown.
// Sync is passed so set it according to the datasheet.
is31fl3733_select_page(addr, IS31FL3733_COMMAND_LED_CONTROL);
is31fl3733_select_page(index, IS31FL3733_COMMAND_LED_CONTROL);
// Turn off all LEDs.
for (uint8_t i = 0; i < IS31FL3733_LED_CONTROL_REGISTER_COUNT; i++) {
is31fl3733_write_register(addr, i, 0x00);
is31fl3733_write_register(index, i, 0x00);
}
is31fl3733_select_page(addr, IS31FL3733_COMMAND_PWM);
is31fl3733_select_page(index, IS31FL3733_COMMAND_PWM);
// Set PWM on all LEDs to 0
// No need to setup Breath registers to PWM as that is the default.
for (uint8_t i = 0; i < IS31FL3733_PWM_REGISTER_COUNT; i++) {
is31fl3733_write_register(addr, i, 0x00);
is31fl3733_write_register(index, i, 0x00);
}
is31fl3733_select_page(addr, IS31FL3733_COMMAND_FUNCTION);
is31fl3733_select_page(index, IS31FL3733_COMMAND_FUNCTION);
uint8_t sync = driver_sync[index];
// Set de-ghost pull-up resistors (SWx)
is31fl3733_write_register(addr, IS31FL3733_FUNCTION_REG_SW_PULLUP, IS31FL3733_SW_PULLUP);
is31fl3733_write_register(index, IS31FL3733_FUNCTION_REG_SW_PULLUP, IS31FL3733_SW_PULLUP);
// Set de-ghost pull-down resistors (CSx)
is31fl3733_write_register(addr, IS31FL3733_FUNCTION_REG_CS_PULLDOWN, IS31FL3733_CS_PULLDOWN);
is31fl3733_write_register(index, IS31FL3733_FUNCTION_REG_CS_PULLDOWN, IS31FL3733_CS_PULLDOWN);
// Set global current to maximum.
is31fl3733_write_register(addr, IS31FL3733_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3733_GLOBAL_CURRENT);
is31fl3733_write_register(index, IS31FL3733_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3733_GLOBAL_CURRENT);
// Disable software shutdown.
is31fl3733_write_register(addr, IS31FL3733_FUNCTION_REG_CONFIGURATION, ((sync & 0b11) << 6) | ((IS31FL3733_PWM_FREQUENCY & 0b111) << 3) | 0x01);
is31fl3733_write_register(index, IS31FL3733_FUNCTION_REG_CONFIGURATION, ((sync & 0b11) << 6) | ((IS31FL3733_PWM_FREQUENCY & 0b111) << 3) | 0x01);
// Wait 10ms to ensure the device has woken up.
wait_ms(10);
@ -178,12 +205,12 @@ void is31fl3733_set_value(int index, uint8_t value) {
if (index >= 0 && index < IS31FL3733_LED_COUNT) {
memcpy_P(&led, (&g_is31fl3733_leds[index]), sizeof(led));
if (g_pwm_buffer[led.driver][led.v] == value) {
if (driver_buffers[led.driver].pwm_buffer[led.v] == value) {
return;
}
g_pwm_buffer[led.driver][led.v] = value;
g_pwm_buffer_update_required[led.driver] = true;
driver_buffers[led.driver].pwm_buffer[led.v] = value;
driver_buffers[led.driver].pwm_buffer_dirty = true;
}
}
@ -201,45 +228,38 @@ void is31fl3733_set_led_control_register(uint8_t index, bool value) {
uint8_t bit_value = led.v % 8;
if (value) {
g_led_control_registers[led.driver][control_register] |= (1 << bit_value);
driver_buffers[led.driver].led_control_buffer[control_register] |= (1 << bit_value);
} else {
g_led_control_registers[led.driver][control_register] &= ~(1 << bit_value);
driver_buffers[led.driver].led_control_buffer[control_register] &= ~(1 << bit_value);
}
g_led_control_registers_update_required[led.driver] = true;
driver_buffers[led.driver].led_control_buffer_dirty = true;
}
void is31fl3733_update_pwm_buffers(uint8_t addr, uint8_t index) {
if (g_pwm_buffer_update_required[index]) {
is31fl3733_select_page(addr, IS31FL3733_COMMAND_PWM);
void is31fl3733_update_pwm_buffers(uint8_t index) {
if (driver_buffers[index].pwm_buffer_dirty) {
is31fl3733_select_page(index, IS31FL3733_COMMAND_PWM);
is31fl3733_write_pwm_buffer(addr, index);
is31fl3733_write_pwm_buffer(index);
g_pwm_buffer_update_required[index] = false;
driver_buffers[index].pwm_buffer_dirty = false;
}
}
void is31fl3733_update_led_control_registers(uint8_t addr, uint8_t index) {
if (g_led_control_registers_update_required[index]) {
is31fl3733_select_page(addr, IS31FL3733_COMMAND_LED_CONTROL);
void is31fl3733_update_led_control_registers(uint8_t index) {
if (driver_buffers[index].led_control_buffer_dirty) {
is31fl3733_select_page(index, IS31FL3733_COMMAND_LED_CONTROL);
for (uint8_t i = 0; i < IS31FL3733_LED_CONTROL_REGISTER_COUNT; i++) {
is31fl3733_write_register(addr, i, g_led_control_registers[index][i]);
is31fl3733_write_register(index, i, driver_buffers[index].led_control_buffer[i]);
}
g_led_control_registers_update_required[index] = false;
driver_buffers[index].led_control_buffer_dirty = false;
}
}
void is31fl3733_flush(void) {
is31fl3733_update_pwm_buffers(IS31FL3733_I2C_ADDRESS_1, 0);
#if defined(IS31FL3733_I2C_ADDRESS_2)
is31fl3733_update_pwm_buffers(IS31FL3733_I2C_ADDRESS_2, 1);
# if defined(IS31FL3733_I2C_ADDRESS_3)
is31fl3733_update_pwm_buffers(IS31FL3733_I2C_ADDRESS_3, 2);
# if defined(IS31FL3733_I2C_ADDRESS_4)
is31fl3733_update_pwm_buffers(IS31FL3733_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3733_DRIVER_COUNT; i++) {
is31fl3733_update_pwm_buffers(i);
}
}

View File

@ -115,9 +115,9 @@ typedef struct is31fl3733_led_t {
extern const is31fl3733_led_t PROGMEM g_is31fl3733_leds[IS31FL3733_LED_COUNT];
void is31fl3733_init_drivers(void);
void is31fl3733_init(uint8_t addr, uint8_t sync);
void is31fl3733_write_register(uint8_t addr, uint8_t reg, uint8_t data);
void is31fl3733_select_page(uint8_t addr, uint8_t page);
void is31fl3733_init(uint8_t index);
void is31fl3733_write_register(uint8_t index, uint8_t reg, uint8_t data);
void is31fl3733_select_page(uint8_t index, uint8_t page);
void is31fl3733_set_value(int index, uint8_t value);
void is31fl3733_set_value_all(uint8_t value);
@ -128,8 +128,8 @@ void is31fl3733_set_led_control_register(uint8_t index, bool value);
// (eg. from a timer interrupt).
// Call this while idle (in between matrix scans).
// If the buffer is dirty, it will update the driver with the buffer.
void is31fl3733_update_pwm_buffers(uint8_t addr, uint8_t index);
void is31fl3733_update_led_control_registers(uint8_t addr, uint8_t index);
void is31fl3733_update_pwm_buffers(uint8_t index);
void is31fl3733_update_led_control_registers(uint8_t index);
void is31fl3733_flush(void);
@ -161,206 +161,206 @@ void is31fl3733_flush(void);
#define IS31FL3733_SYNC_MASTER 0b01
#define IS31FL3733_SYNC_SLAVE 0b10
#define A_1 0x00
#define A_2 0x01
#define A_3 0x02
#define A_4 0x03
#define A_5 0x04
#define A_6 0x05
#define A_7 0x06
#define A_8 0x07
#define A_9 0x08
#define A_10 0x09
#define A_11 0x0A
#define A_12 0x0B
#define A_13 0x0C
#define A_14 0x0D
#define A_15 0x0E
#define A_16 0x0F
#define SW1_CS1 0x00
#define SW1_CS2 0x01
#define SW1_CS3 0x02
#define SW1_CS4 0x03
#define SW1_CS5 0x04
#define SW1_CS6 0x05
#define SW1_CS7 0x06
#define SW1_CS8 0x07
#define SW1_CS9 0x08
#define SW1_CS10 0x09
#define SW1_CS11 0x0A
#define SW1_CS12 0x0B
#define SW1_CS13 0x0C
#define SW1_CS14 0x0D
#define SW1_CS15 0x0E
#define SW1_CS16 0x0F
#define B_1 0x10
#define B_2 0x11
#define B_3 0x12
#define B_4 0x13
#define B_5 0x14
#define B_6 0x15
#define B_7 0x16
#define B_8 0x17
#define B_9 0x18
#define B_10 0x19
#define B_11 0x1A
#define B_12 0x1B
#define B_13 0x1C
#define B_14 0x1D
#define B_15 0x1E
#define B_16 0x1F
#define SW2_CS1 0x10
#define SW2_CS2 0x11
#define SW2_CS3 0x12
#define SW2_CS4 0x13
#define SW2_CS5 0x14
#define SW2_CS6 0x15
#define SW2_CS7 0x16
#define SW2_CS8 0x17
#define SW2_CS9 0x18
#define SW2_CS10 0x19
#define SW2_CS11 0x1A
#define SW2_CS12 0x1B
#define SW2_CS13 0x1C
#define SW2_CS14 0x1D
#define SW2_CS15 0x1E
#define SW2_CS16 0x1F
#define C_1 0x20
#define C_2 0x21
#define C_3 0x22
#define C_4 0x23
#define C_5 0x24
#define C_6 0x25
#define C_7 0x26
#define C_8 0x27
#define C_9 0x28
#define C_10 0x29
#define C_11 0x2A
#define C_12 0x2B
#define C_13 0x2C
#define C_14 0x2D
#define C_15 0x2E
#define C_16 0x2F
#define SW3_CS1 0x20
#define SW3_CS2 0x21
#define SW3_CS3 0x22
#define SW3_CS4 0x23
#define SW3_CS5 0x24
#define SW3_CS6 0x25
#define SW3_CS7 0x26
#define SW3_CS8 0x27
#define SW3_CS9 0x28
#define SW3_CS10 0x29
#define SW3_CS11 0x2A
#define SW3_CS12 0x2B
#define SW3_CS13 0x2C
#define SW3_CS14 0x2D
#define SW3_CS15 0x2E
#define SW3_CS16 0x2F
#define D_1 0x30
#define D_2 0x31
#define D_3 0x32
#define D_4 0x33
#define D_5 0x34
#define D_6 0x35
#define D_7 0x36
#define D_8 0x37
#define D_9 0x38
#define D_10 0x39
#define D_11 0x3A
#define D_12 0x3B
#define D_13 0x3C
#define D_14 0x3D
#define D_15 0x3E
#define D_16 0x3F
#define SW4_CS1 0x30
#define SW4_CS2 0x31
#define SW4_CS3 0x32
#define SW4_CS4 0x33
#define SW4_CS5 0x34
#define SW4_CS6 0x35
#define SW4_CS7 0x36
#define SW4_CS8 0x37
#define SW4_CS9 0x38
#define SW4_CS10 0x39
#define SW4_CS11 0x3A
#define SW4_CS12 0x3B
#define SW4_CS13 0x3C
#define SW4_CS14 0x3D
#define SW4_CS15 0x3E
#define SW4_CS16 0x3F
#define E_1 0x40
#define E_2 0x41
#define E_3 0x42
#define E_4 0x43
#define E_5 0x44
#define E_6 0x45
#define E_7 0x46
#define E_8 0x47
#define E_9 0x48
#define E_10 0x49
#define E_11 0x4A
#define E_12 0x4B
#define E_13 0x4C
#define E_14 0x4D
#define E_15 0x4E
#define E_16 0x4F
#define SW5_CS1 0x40
#define SW5_CS2 0x41
#define SW5_CS3 0x42
#define SW5_CS4 0x43
#define SW5_CS5 0x44
#define SW5_CS6 0x45
#define SW5_CS7 0x46
#define SW5_CS8 0x47
#define SW5_CS9 0x48
#define SW5_CS10 0x49
#define SW5_CS11 0x4A
#define SW5_CS12 0x4B
#define SW5_CS13 0x4C
#define SW5_CS14 0x4D
#define SW5_CS15 0x4E
#define SW5_CS16 0x4F
#define F_1 0x50
#define F_2 0x51
#define F_3 0x52
#define F_4 0x53
#define F_5 0x54
#define F_6 0x55
#define F_7 0x56
#define F_8 0x57
#define F_9 0x58
#define F_10 0x59
#define F_11 0x5A
#define F_12 0x5B
#define F_13 0x5C
#define F_14 0x5D
#define F_15 0x5E
#define F_16 0x5F
#define SW6_CS1 0x50
#define SW6_CS2 0x51
#define SW6_CS3 0x52
#define SW6_CS4 0x53
#define SW6_CS5 0x54
#define SW6_CS6 0x55
#define SW6_CS7 0x56
#define SW6_CS8 0x57
#define SW6_CS9 0x58
#define SW6_CS10 0x59
#define SW6_CS11 0x5A
#define SW6_CS12 0x5B
#define SW6_CS13 0x5C
#define SW6_CS14 0x5D
#define SW6_CS15 0x5E
#define SW6_CS16 0x5F
#define G_1 0x60
#define G_2 0x61
#define G_3 0x62
#define G_4 0x63
#define G_5 0x64
#define G_6 0x65
#define G_7 0x66
#define G_8 0x67
#define G_9 0x68
#define G_10 0x69
#define G_11 0x6A
#define G_12 0x6B
#define G_13 0x6C
#define G_14 0x6D
#define G_15 0x6E
#define G_16 0x6F
#define SW7_CS1 0x60
#define SW7_CS2 0x61
#define SW7_CS3 0x62
#define SW7_CS4 0x63
#define SW7_CS5 0x64
#define SW7_CS6 0x65
#define SW7_CS7 0x66
#define SW7_CS8 0x67
#define SW7_CS9 0x68
#define SW7_CS10 0x69
#define SW7_CS11 0x6A
#define SW7_CS12 0x6B
#define SW7_CS13 0x6C
#define SW7_CS14 0x6D
#define SW7_CS15 0x6E
#define SW7_CS16 0x6F
#define H_1 0x70
#define H_2 0x71
#define H_3 0x72
#define H_4 0x73
#define H_5 0x74
#define H_6 0x75
#define H_7 0x76
#define H_8 0x77
#define H_9 0x78
#define H_10 0x79
#define H_11 0x7A
#define H_12 0x7B
#define H_13 0x7C
#define H_14 0x7D
#define H_15 0x7E
#define H_16 0x7F
#define SW8_CS1 0x70
#define SW8_CS2 0x71
#define SW8_CS3 0x72
#define SW8_CS4 0x73
#define SW8_CS5 0x74
#define SW8_CS6 0x75
#define SW8_CS7 0x76
#define SW8_CS8 0x77
#define SW8_CS9 0x78
#define SW8_CS10 0x79
#define SW8_CS11 0x7A
#define SW8_CS12 0x7B
#define SW8_CS13 0x7C
#define SW8_CS14 0x7D
#define SW8_CS15 0x7E
#define SW8_CS16 0x7F
#define I_1 0x80
#define I_2 0x81
#define I_3 0x82
#define I_4 0x83
#define I_5 0x84
#define I_6 0x85
#define I_7 0x86
#define I_8 0x87
#define I_9 0x88
#define I_10 0x89
#define I_11 0x8A
#define I_12 0x8B
#define I_13 0x8C
#define I_14 0x8D
#define I_15 0x8E
#define I_16 0x8F
#define SW9_CS1 0x80
#define SW9_CS2 0x81
#define SW9_CS3 0x82
#define SW9_CS4 0x83
#define SW9_CS5 0x84
#define SW9_CS6 0x85
#define SW9_CS7 0x86
#define SW9_CS8 0x87
#define SW9_CS9 0x88
#define SW9_CS10 0x89
#define SW9_CS11 0x8A
#define SW9_CS12 0x8B
#define SW9_CS13 0x8C
#define SW9_CS14 0x8D
#define SW9_CS15 0x8E
#define SW9_CS16 0x8F
#define J_1 0x90
#define J_2 0x91
#define J_3 0x92
#define J_4 0x93
#define J_5 0x94
#define J_6 0x95
#define J_7 0x96
#define J_8 0x97
#define J_9 0x98
#define J_10 0x99
#define J_11 0x9A
#define J_12 0x9B
#define J_13 0x9C
#define J_14 0x9D
#define J_15 0x9E
#define J_16 0x9F
#define SW10_CS1 0x90
#define SW10_CS2 0x91
#define SW10_CS3 0x92
#define SW10_CS4 0x93
#define SW10_CS5 0x94
#define SW10_CS6 0x95
#define SW10_CS7 0x96
#define SW10_CS8 0x97
#define SW10_CS9 0x98
#define SW10_CS10 0x99
#define SW10_CS11 0x9A
#define SW10_CS12 0x9B
#define SW10_CS13 0x9C
#define SW10_CS14 0x9D
#define SW10_CS15 0x9E
#define SW10_CS16 0x9F
#define K_1 0xA0
#define K_2 0xA1
#define K_3 0xA2
#define K_4 0xA3
#define K_5 0xA4
#define K_6 0xA5
#define K_7 0xA6
#define K_8 0xA7
#define K_9 0xA8
#define K_10 0xA9
#define K_11 0xAA
#define K_12 0xAB
#define K_13 0xAC
#define K_14 0xAD
#define K_15 0xAE
#define K_16 0xAF
#define SW11_CS1 0xA0
#define SW11_CS2 0xA1
#define SW11_CS3 0xA2
#define SW11_CS4 0xA3
#define SW11_CS5 0xA4
#define SW11_CS6 0xA5
#define SW11_CS7 0xA6
#define SW11_CS8 0xA7
#define SW11_CS9 0xA8
#define SW11_CS10 0xA9
#define SW11_CS11 0xAA
#define SW11_CS12 0xAB
#define SW11_CS13 0xAC
#define SW11_CS14 0xAD
#define SW11_CS15 0xAE
#define SW11_CS16 0xAF
#define L_1 0xB0
#define L_2 0xB1
#define L_3 0xB2
#define L_4 0xB3
#define L_5 0xB4
#define L_6 0xB5
#define L_7 0xB6
#define L_8 0xB7
#define L_9 0xB8
#define L_10 0xB9
#define L_11 0xBA
#define L_12 0xBB
#define L_13 0xBC
#define L_14 0xBD
#define L_15 0xBE
#define L_16 0xBF
#define SW12_CS1 0xB0
#define SW12_CS2 0xB1
#define SW12_CS3 0xB2
#define SW12_CS4 0xB3
#define SW12_CS5 0xB4
#define SW12_CS6 0xB5
#define SW12_CS7 0xB6
#define SW12_CS8 0xB7
#define SW12_CS9 0xB8
#define SW12_CS10 0xB9
#define SW12_CS11 0xBA
#define SW12_CS12 0xBB
#define SW12_CS13 0xBC
#define SW12_CS14 0xBD
#define SW12_CS15 0xBE
#define SW12_CS16 0xBF

View File

@ -19,6 +19,7 @@
#include "is31fl3733.h"
#include "i2c_master.h"
#include "gpio.h"
#include "wait.h"
#define IS31FL3733_PWM_REGISTER_COUNT 192
@ -61,34 +62,68 @@
# define IS31FL3733_SYNC_4 IS31FL3733_SYNC_NONE
#endif
const uint8_t i2c_addresses[IS31FL3733_DRIVER_COUNT] = {
IS31FL3733_I2C_ADDRESS_1,
#ifdef IS31FL3733_I2C_ADDRESS_2
IS31FL3733_I2C_ADDRESS_2,
# ifdef IS31FL3733_I2C_ADDRESS_3
IS31FL3733_I2C_ADDRESS_3,
# ifdef IS31FL3733_I2C_ADDRESS_4
IS31FL3733_I2C_ADDRESS_4,
# endif
# endif
#endif
};
const uint8_t driver_sync[IS31FL3733_DRIVER_COUNT] = {
IS31FL3733_SYNC_1,
#ifdef IS31FL3733_I2C_ADDRESS_2
IS31FL3733_SYNC_2,
# ifdef IS31FL3733_I2C_ADDRESS_3
IS31FL3733_SYNC_3,
# ifdef IS31FL3733_I2C_ADDRESS_4
IS31FL3733_SYNC_4,
# endif
# endif
#endif
};
// These buffers match the IS31FL3733 PWM registers.
// The control buffers match the page 0 LED On/Off registers.
// Storing them like this is optimal for I2C transfers to the registers.
// We could optimize this and take out the unused registers from these
// buffers and the transfers in is31fl3733_write_pwm_buffer() but it's
// probably not worth the extra complexity.
uint8_t g_pwm_buffer[IS31FL3733_DRIVER_COUNT][IS31FL3733_PWM_REGISTER_COUNT];
bool g_pwm_buffer_update_required[IS31FL3733_DRIVER_COUNT] = {false};
typedef struct is31fl3733_driver_t {
uint8_t pwm_buffer[IS31FL3733_PWM_REGISTER_COUNT];
bool pwm_buffer_dirty;
uint8_t led_control_buffer[IS31FL3733_LED_CONTROL_REGISTER_COUNT];
bool led_control_buffer_dirty;
} PACKED is31fl3733_driver_t;
uint8_t g_led_control_registers[IS31FL3733_DRIVER_COUNT][IS31FL3733_LED_CONTROL_REGISTER_COUNT] = {0};
bool g_led_control_registers_update_required[IS31FL3733_DRIVER_COUNT] = {false};
is31fl3733_driver_t driver_buffers[IS31FL3733_DRIVER_COUNT] = {{
.pwm_buffer = {0},
.pwm_buffer_dirty = false,
.led_control_buffer = {0},
.led_control_buffer_dirty = false,
}};
void is31fl3733_write_register(uint8_t addr, uint8_t reg, uint8_t data) {
void is31fl3733_write_register(uint8_t index, uint8_t reg, uint8_t data) {
#if IS31FL3733_I2C_PERSISTENCE > 0
for (uint8_t i = 0; i < IS31FL3733_I2C_PERSISTENCE; i++) {
if (i2c_write_register(addr << 1, reg, &data, 1, IS31FL3733_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3733_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, reg, &data, 1, IS31FL3733_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3733_I2C_TIMEOUT);
#endif
}
void is31fl3733_select_page(uint8_t addr, uint8_t page) {
is31fl3733_write_register(addr, IS31FL3733_REG_COMMAND_WRITE_LOCK, IS31FL3733_COMMAND_WRITE_LOCK_MAGIC);
is31fl3733_write_register(addr, IS31FL3733_REG_COMMAND, page);
void is31fl3733_select_page(uint8_t index, uint8_t page) {
is31fl3733_write_register(index, IS31FL3733_REG_COMMAND_WRITE_LOCK, IS31FL3733_COMMAND_WRITE_LOCK_MAGIC);
is31fl3733_write_register(index, IS31FL3733_REG_COMMAND, page);
}
void is31fl3733_write_pwm_buffer(uint8_t addr, uint8_t index) {
void is31fl3733_write_pwm_buffer(uint8_t index) {
// Assumes page 1 is already selected.
// Transmit PWM registers in 12 transfers of 16 bytes.
@ -96,10 +131,10 @@ void is31fl3733_write_pwm_buffer(uint8_t addr, uint8_t index) {
for (uint8_t i = 0; i < IS31FL3733_PWM_REGISTER_COUNT; i += 16) {
#if IS31FL3733_I2C_PERSISTENCE > 0
for (uint8_t j = 0; j < IS31FL3733_I2C_PERSISTENCE; j++) {
if (i2c_write_register(addr << 1, i, g_pwm_buffer[index] + i, 16, IS31FL3733_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3733_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, i, g_pwm_buffer[index] + i, 16, IS31FL3733_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3733_I2C_TIMEOUT);
#endif
}
}
@ -107,65 +142,57 @@ void is31fl3733_write_pwm_buffer(uint8_t addr, uint8_t index) {
void is31fl3733_init_drivers(void) {
i2c_init();
is31fl3733_init(IS31FL3733_I2C_ADDRESS_1, IS31FL3733_SYNC_1);
#if defined(IS31FL3733_I2C_ADDRESS_2)
is31fl3733_init(IS31FL3733_I2C_ADDRESS_2, IS31FL3733_SYNC_2);
# if defined(IS31FL3733_I2C_ADDRESS_3)
is31fl3733_init(IS31FL3733_I2C_ADDRESS_3, IS31FL3733_SYNC_3);
# if defined(IS31FL3733_I2C_ADDRESS_4)
is31fl3733_init(IS31FL3733_I2C_ADDRESS_4, IS31FL3733_SYNC_4);
# endif
# endif
#if defined(IS31FL3733_SDB_PIN)
gpio_set_pin_output(IS31FL3733_SDB_PIN);
gpio_write_pin_high(IS31FL3733_SDB_PIN);
#endif
for (uint8_t i = 0; i < IS31FL3733_DRIVER_COUNT; i++) {
is31fl3733_init(i);
}
for (int i = 0; i < IS31FL3733_LED_COUNT; i++) {
is31fl3733_set_led_control_register(i, true, true, true);
}
is31fl3733_update_led_control_registers(IS31FL3733_I2C_ADDRESS_1, 0);
#if defined(IS31FL3733_I2C_ADDRESS_2)
is31fl3733_update_led_control_registers(IS31FL3733_I2C_ADDRESS_2, 1);
# if defined(IS31FL3733_I2C_ADDRESS_3)
is31fl3733_update_led_control_registers(IS31FL3733_I2C_ADDRESS_3, 2);
# if defined(IS31FL3733_I2C_ADDRESS_4)
is31fl3733_update_led_control_registers(IS31FL3733_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3733_DRIVER_COUNT; i++) {
is31fl3733_update_led_control_registers(i);
}
}
void is31fl3733_init(uint8_t addr, uint8_t sync) {
void is31fl3733_init(uint8_t index) {
// In order to avoid the LEDs being driven with garbage data
// in the LED driver's PWM registers, shutdown is enabled last.
// Set up the mode and other settings, clear the PWM registers,
// then disable software shutdown.
// Sync is passed so set it according to the datasheet.
is31fl3733_select_page(addr, IS31FL3733_COMMAND_LED_CONTROL);
is31fl3733_select_page(index, IS31FL3733_COMMAND_LED_CONTROL);
// Turn off all LEDs.
for (uint8_t i = 0; i < IS31FL3733_LED_CONTROL_REGISTER_COUNT; i++) {
is31fl3733_write_register(addr, i, 0x00);
is31fl3733_write_register(index, i, 0x00);
}
is31fl3733_select_page(addr, IS31FL3733_COMMAND_PWM);
is31fl3733_select_page(index, IS31FL3733_COMMAND_PWM);
// Set PWM on all LEDs to 0
// No need to setup Breath registers to PWM as that is the default.
for (uint8_t i = 0; i < IS31FL3733_PWM_REGISTER_COUNT; i++) {
is31fl3733_write_register(addr, i, 0x00);
is31fl3733_write_register(index, i, 0x00);
}
is31fl3733_select_page(addr, IS31FL3733_COMMAND_FUNCTION);
is31fl3733_select_page(index, IS31FL3733_COMMAND_FUNCTION);
uint8_t sync = driver_sync[index];
// Set de-ghost pull-up resistors (SWx)
is31fl3733_write_register(addr, IS31FL3733_FUNCTION_REG_SW_PULLUP, IS31FL3733_SW_PULLUP);
is31fl3733_write_register(index, IS31FL3733_FUNCTION_REG_SW_PULLUP, IS31FL3733_SW_PULLUP);
// Set de-ghost pull-down resistors (CSx)
is31fl3733_write_register(addr, IS31FL3733_FUNCTION_REG_CS_PULLDOWN, IS31FL3733_CS_PULLDOWN);
is31fl3733_write_register(index, IS31FL3733_FUNCTION_REG_CS_PULLDOWN, IS31FL3733_CS_PULLDOWN);
// Set global current to maximum.
is31fl3733_write_register(addr, IS31FL3733_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3733_GLOBAL_CURRENT);
is31fl3733_write_register(index, IS31FL3733_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3733_GLOBAL_CURRENT);
// Disable software shutdown.
is31fl3733_write_register(addr, IS31FL3733_FUNCTION_REG_CONFIGURATION, ((sync & 0b11) << 6) | ((IS31FL3733_PWM_FREQUENCY & 0b111) << 3) | 0x01);
is31fl3733_write_register(index, IS31FL3733_FUNCTION_REG_CONFIGURATION, ((sync & 0b11) << 6) | ((IS31FL3733_PWM_FREQUENCY & 0b111) << 3) | 0x01);
// Wait 10ms to ensure the device has woken up.
wait_ms(10);
@ -177,14 +204,14 @@ void is31fl3733_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
if (index >= 0 && index < IS31FL3733_LED_COUNT) {
memcpy_P(&led, (&g_is31fl3733_leds[index]), sizeof(led));
if (g_pwm_buffer[led.driver][led.r] == red && g_pwm_buffer[led.driver][led.g] == green && g_pwm_buffer[led.driver][led.b] == blue) {
if (driver_buffers[led.driver].pwm_buffer[led.r] == red && driver_buffers[led.driver].pwm_buffer[led.g] == green && driver_buffers[led.driver].pwm_buffer[led.b] == blue) {
return;
}
g_pwm_buffer[led.driver][led.r] = red;
g_pwm_buffer[led.driver][led.g] = green;
g_pwm_buffer[led.driver][led.b] = blue;
g_pwm_buffer_update_required[led.driver] = true;
driver_buffers[led.driver].pwm_buffer[led.r] = red;
driver_buffers[led.driver].pwm_buffer[led.g] = green;
driver_buffers[led.driver].pwm_buffer[led.b] = blue;
driver_buffers[led.driver].pwm_buffer_dirty = true;
}
}
@ -206,55 +233,48 @@ void is31fl3733_set_led_control_register(uint8_t index, bool red, bool green, bo
uint8_t bit_b = led.b % 8;
if (red) {
g_led_control_registers[led.driver][control_register_r] |= (1 << bit_r);
driver_buffers[led.driver].led_control_buffer[control_register_r] |= (1 << bit_r);
} else {
g_led_control_registers[led.driver][control_register_r] &= ~(1 << bit_r);
driver_buffers[led.driver].led_control_buffer[control_register_r] &= ~(1 << bit_r);
}
if (green) {
g_led_control_registers[led.driver][control_register_g] |= (1 << bit_g);
driver_buffers[led.driver].led_control_buffer[control_register_g] |= (1 << bit_g);
} else {
g_led_control_registers[led.driver][control_register_g] &= ~(1 << bit_g);
driver_buffers[led.driver].led_control_buffer[control_register_g] &= ~(1 << bit_g);
}
if (blue) {
g_led_control_registers[led.driver][control_register_b] |= (1 << bit_b);
driver_buffers[led.driver].led_control_buffer[control_register_b] |= (1 << bit_b);
} else {
g_led_control_registers[led.driver][control_register_b] &= ~(1 << bit_b);
driver_buffers[led.driver].led_control_buffer[control_register_b] &= ~(1 << bit_b);
}
g_led_control_registers_update_required[led.driver] = true;
driver_buffers[led.driver].led_control_buffer_dirty = true;
}
void is31fl3733_update_pwm_buffers(uint8_t addr, uint8_t index) {
if (g_pwm_buffer_update_required[index]) {
is31fl3733_select_page(addr, IS31FL3733_COMMAND_PWM);
void is31fl3733_update_pwm_buffers(uint8_t index) {
if (driver_buffers[index].pwm_buffer_dirty) {
is31fl3733_select_page(index, IS31FL3733_COMMAND_PWM);
is31fl3733_write_pwm_buffer(addr, index);
is31fl3733_write_pwm_buffer(index);
g_pwm_buffer_update_required[index] = false;
driver_buffers[index].pwm_buffer_dirty = false;
}
}
void is31fl3733_update_led_control_registers(uint8_t addr, uint8_t index) {
if (g_led_control_registers_update_required[index]) {
is31fl3733_select_page(addr, IS31FL3733_COMMAND_LED_CONTROL);
void is31fl3733_update_led_control_registers(uint8_t index) {
if (driver_buffers[index].led_control_buffer_dirty) {
is31fl3733_select_page(index, IS31FL3733_COMMAND_LED_CONTROL);
for (uint8_t i = 0; i < IS31FL3733_LED_CONTROL_REGISTER_COUNT; i++) {
is31fl3733_write_register(addr, i, g_led_control_registers[index][i]);
is31fl3733_write_register(index, i, driver_buffers[index].led_control_buffer[i]);
}
g_led_control_registers_update_required[index] = false;
driver_buffers[index].led_control_buffer_dirty = false;
}
}
void is31fl3733_flush(void) {
is31fl3733_update_pwm_buffers(IS31FL3733_I2C_ADDRESS_1, 0);
#if defined(IS31FL3733_I2C_ADDRESS_2)
is31fl3733_update_pwm_buffers(IS31FL3733_I2C_ADDRESS_2, 1);
# if defined(IS31FL3733_I2C_ADDRESS_3)
is31fl3733_update_pwm_buffers(IS31FL3733_I2C_ADDRESS_3, 2);
# if defined(IS31FL3733_I2C_ADDRESS_4)
is31fl3733_update_pwm_buffers(IS31FL3733_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3733_DRIVER_COUNT; i++) {
is31fl3733_update_pwm_buffers(i);
}
}

View File

@ -140,9 +140,9 @@ typedef struct is31fl3733_led_t {
extern const is31fl3733_led_t PROGMEM g_is31fl3733_leds[IS31FL3733_LED_COUNT];
void is31fl3733_init_drivers(void);
void is31fl3733_init(uint8_t addr, uint8_t sync);
void is31fl3733_write_register(uint8_t addr, uint8_t reg, uint8_t data);
void is31fl3733_select_page(uint8_t addr, uint8_t page);
void is31fl3733_init(uint8_t index);
void is31fl3733_write_register(uint8_t index, uint8_t reg, uint8_t data);
void is31fl3733_select_page(uint8_t index, uint8_t page);
void is31fl3733_set_color(int index, uint8_t red, uint8_t green, uint8_t blue);
void is31fl3733_set_color_all(uint8_t red, uint8_t green, uint8_t blue);
@ -153,8 +153,8 @@ void is31fl3733_set_led_control_register(uint8_t index, bool red, bool green, bo
// (eg. from a timer interrupt).
// Call this while idle (in between matrix scans).
// If the buffer is dirty, it will update the driver with the buffer.
void is31fl3733_update_pwm_buffers(uint8_t addr, uint8_t index);
void is31fl3733_update_led_control_registers(uint8_t addr, uint8_t index);
void is31fl3733_update_pwm_buffers(uint8_t index);
void is31fl3733_update_led_control_registers(uint8_t index);
void is31fl3733_flush(void);
@ -186,206 +186,412 @@ void is31fl3733_flush(void);
#define IS31FL3733_SYNC_MASTER 0b01
#define IS31FL3733_SYNC_SLAVE 0b10
#define A_1 0x00
#define A_2 0x01
#define A_3 0x02
#define A_4 0x03
#define A_5 0x04
#define A_6 0x05
#define A_7 0x06
#define A_8 0x07
#define A_9 0x08
#define A_10 0x09
#define A_11 0x0A
#define A_12 0x0B
#define A_13 0x0C
#define A_14 0x0D
#define A_15 0x0E
#define A_16 0x0F
#define SW1_CS1 0x00
#define SW1_CS2 0x01
#define SW1_CS3 0x02
#define SW1_CS4 0x03
#define SW1_CS5 0x04
#define SW1_CS6 0x05
#define SW1_CS7 0x06
#define SW1_CS8 0x07
#define SW1_CS9 0x08
#define SW1_CS10 0x09
#define SW1_CS11 0x0A
#define SW1_CS12 0x0B
#define SW1_CS13 0x0C
#define SW1_CS14 0x0D
#define SW1_CS15 0x0E
#define SW1_CS16 0x0F
#define B_1 0x10
#define B_2 0x11
#define B_3 0x12
#define B_4 0x13
#define B_5 0x14
#define B_6 0x15
#define B_7 0x16
#define B_8 0x17
#define B_9 0x18
#define B_10 0x19
#define B_11 0x1A
#define B_12 0x1B
#define B_13 0x1C
#define B_14 0x1D
#define B_15 0x1E
#define B_16 0x1F
#define SW2_CS1 0x10
#define SW2_CS2 0x11
#define SW2_CS3 0x12
#define SW2_CS4 0x13
#define SW2_CS5 0x14
#define SW2_CS6 0x15
#define SW2_CS7 0x16
#define SW2_CS8 0x17
#define SW2_CS9 0x18
#define SW2_CS10 0x19
#define SW2_CS11 0x1A
#define SW2_CS12 0x1B
#define SW2_CS13 0x1C
#define SW2_CS14 0x1D
#define SW2_CS15 0x1E
#define SW2_CS16 0x1F
#define C_1 0x20
#define C_2 0x21
#define C_3 0x22
#define C_4 0x23
#define C_5 0x24
#define C_6 0x25
#define C_7 0x26
#define C_8 0x27
#define C_9 0x28
#define C_10 0x29
#define C_11 0x2A
#define C_12 0x2B
#define C_13 0x2C
#define C_14 0x2D
#define C_15 0x2E
#define C_16 0x2F
#define SW3_CS1 0x20
#define SW3_CS2 0x21
#define SW3_CS3 0x22
#define SW3_CS4 0x23
#define SW3_CS5 0x24
#define SW3_CS6 0x25
#define SW3_CS7 0x26
#define SW3_CS8 0x27
#define SW3_CS9 0x28
#define SW3_CS10 0x29
#define SW3_CS11 0x2A
#define SW3_CS12 0x2B
#define SW3_CS13 0x2C
#define SW3_CS14 0x2D
#define SW3_CS15 0x2E
#define SW3_CS16 0x2F
#define D_1 0x30
#define D_2 0x31
#define D_3 0x32
#define D_4 0x33
#define D_5 0x34
#define D_6 0x35
#define D_7 0x36
#define D_8 0x37
#define D_9 0x38
#define D_10 0x39
#define D_11 0x3A
#define D_12 0x3B
#define D_13 0x3C
#define D_14 0x3D
#define D_15 0x3E
#define D_16 0x3F
#define SW4_CS1 0x30
#define SW4_CS2 0x31
#define SW4_CS3 0x32
#define SW4_CS4 0x33
#define SW4_CS5 0x34
#define SW4_CS6 0x35
#define SW4_CS7 0x36
#define SW4_CS8 0x37
#define SW4_CS9 0x38
#define SW4_CS10 0x39
#define SW4_CS11 0x3A
#define SW4_CS12 0x3B
#define SW4_CS13 0x3C
#define SW4_CS14 0x3D
#define SW4_CS15 0x3E
#define SW4_CS16 0x3F
#define E_1 0x40
#define E_2 0x41
#define E_3 0x42
#define E_4 0x43
#define E_5 0x44
#define E_6 0x45
#define E_7 0x46
#define E_8 0x47
#define E_9 0x48
#define E_10 0x49
#define E_11 0x4A
#define E_12 0x4B
#define E_13 0x4C
#define E_14 0x4D
#define E_15 0x4E
#define E_16 0x4F
#define SW5_CS1 0x40
#define SW5_CS2 0x41
#define SW5_CS3 0x42
#define SW5_CS4 0x43
#define SW5_CS5 0x44
#define SW5_CS6 0x45
#define SW5_CS7 0x46
#define SW5_CS8 0x47
#define SW5_CS9 0x48
#define SW5_CS10 0x49
#define SW5_CS11 0x4A
#define SW5_CS12 0x4B
#define SW5_CS13 0x4C
#define SW5_CS14 0x4D
#define SW5_CS15 0x4E
#define SW5_CS16 0x4F
#define F_1 0x50
#define F_2 0x51
#define F_3 0x52
#define F_4 0x53
#define F_5 0x54
#define F_6 0x55
#define F_7 0x56
#define F_8 0x57
#define F_9 0x58
#define F_10 0x59
#define F_11 0x5A
#define F_12 0x5B
#define F_13 0x5C
#define F_14 0x5D
#define F_15 0x5E
#define F_16 0x5F
#define SW6_CS1 0x50
#define SW6_CS2 0x51
#define SW6_CS3 0x52
#define SW6_CS4 0x53
#define SW6_CS5 0x54
#define SW6_CS6 0x55
#define SW6_CS7 0x56
#define SW6_CS8 0x57
#define SW6_CS9 0x58
#define SW6_CS10 0x59
#define SW6_CS11 0x5A
#define SW6_CS12 0x5B
#define SW6_CS13 0x5C
#define SW6_CS14 0x5D
#define SW6_CS15 0x5E
#define SW6_CS16 0x5F
#define G_1 0x60
#define G_2 0x61
#define G_3 0x62
#define G_4 0x63
#define G_5 0x64
#define G_6 0x65
#define G_7 0x66
#define G_8 0x67
#define G_9 0x68
#define G_10 0x69
#define G_11 0x6A
#define G_12 0x6B
#define G_13 0x6C
#define G_14 0x6D
#define G_15 0x6E
#define G_16 0x6F
#define SW7_CS1 0x60
#define SW7_CS2 0x61
#define SW7_CS3 0x62
#define SW7_CS4 0x63
#define SW7_CS5 0x64
#define SW7_CS6 0x65
#define SW7_CS7 0x66
#define SW7_CS8 0x67
#define SW7_CS9 0x68
#define SW7_CS10 0x69
#define SW7_CS11 0x6A
#define SW7_CS12 0x6B
#define SW7_CS13 0x6C
#define SW7_CS14 0x6D
#define SW7_CS15 0x6E
#define SW7_CS16 0x6F
#define H_1 0x70
#define H_2 0x71
#define H_3 0x72
#define H_4 0x73
#define H_5 0x74
#define H_6 0x75
#define H_7 0x76
#define H_8 0x77
#define H_9 0x78
#define H_10 0x79
#define H_11 0x7A
#define H_12 0x7B
#define H_13 0x7C
#define H_14 0x7D
#define H_15 0x7E
#define H_16 0x7F
#define SW8_CS1 0x70
#define SW8_CS2 0x71
#define SW8_CS3 0x72
#define SW8_CS4 0x73
#define SW8_CS5 0x74
#define SW8_CS6 0x75
#define SW8_CS7 0x76
#define SW8_CS8 0x77
#define SW8_CS9 0x78
#define SW8_CS10 0x79
#define SW8_CS11 0x7A
#define SW8_CS12 0x7B
#define SW8_CS13 0x7C
#define SW8_CS14 0x7D
#define SW8_CS15 0x7E
#define SW8_CS16 0x7F
#define I_1 0x80
#define I_2 0x81
#define I_3 0x82
#define I_4 0x83
#define I_5 0x84
#define I_6 0x85
#define I_7 0x86
#define I_8 0x87
#define I_9 0x88
#define I_10 0x89
#define I_11 0x8A
#define I_12 0x8B
#define I_13 0x8C
#define I_14 0x8D
#define I_15 0x8E
#define I_16 0x8F
#define SW9_CS1 0x80
#define SW9_CS2 0x81
#define SW9_CS3 0x82
#define SW9_CS4 0x83
#define SW9_CS5 0x84
#define SW9_CS6 0x85
#define SW9_CS7 0x86
#define SW9_CS8 0x87
#define SW9_CS9 0x88
#define SW9_CS10 0x89
#define SW9_CS11 0x8A
#define SW9_CS12 0x8B
#define SW9_CS13 0x8C
#define SW9_CS14 0x8D
#define SW9_CS15 0x8E
#define SW9_CS16 0x8F
#define J_1 0x90
#define J_2 0x91
#define J_3 0x92
#define J_4 0x93
#define J_5 0x94
#define J_6 0x95
#define J_7 0x96
#define J_8 0x97
#define J_9 0x98
#define J_10 0x99
#define J_11 0x9A
#define J_12 0x9B
#define J_13 0x9C
#define J_14 0x9D
#define J_15 0x9E
#define J_16 0x9F
#define SW10_CS1 0x90
#define SW10_CS2 0x91
#define SW10_CS3 0x92
#define SW10_CS4 0x93
#define SW10_CS5 0x94
#define SW10_CS6 0x95
#define SW10_CS7 0x96
#define SW10_CS8 0x97
#define SW10_CS9 0x98
#define SW10_CS10 0x99
#define SW10_CS11 0x9A
#define SW10_CS12 0x9B
#define SW10_CS13 0x9C
#define SW10_CS14 0x9D
#define SW10_CS15 0x9E
#define SW10_CS16 0x9F
#define K_1 0xA0
#define K_2 0xA1
#define K_3 0xA2
#define K_4 0xA3
#define K_5 0xA4
#define K_6 0xA5
#define K_7 0xA6
#define K_8 0xA7
#define K_9 0xA8
#define K_10 0xA9
#define K_11 0xAA
#define K_12 0xAB
#define K_13 0xAC
#define K_14 0xAD
#define K_15 0xAE
#define K_16 0xAF
#define SW11_CS1 0xA0
#define SW11_CS2 0xA1
#define SW11_CS3 0xA2
#define SW11_CS4 0xA3
#define SW11_CS5 0xA4
#define SW11_CS6 0xA5
#define SW11_CS7 0xA6
#define SW11_CS8 0xA7
#define SW11_CS9 0xA8
#define SW11_CS10 0xA9
#define SW11_CS11 0xAA
#define SW11_CS12 0xAB
#define SW11_CS13 0xAC
#define SW11_CS14 0xAD
#define SW11_CS15 0xAE
#define SW11_CS16 0xAF
#define L_1 0xB0
#define L_2 0xB1
#define L_3 0xB2
#define L_4 0xB3
#define L_5 0xB4
#define L_6 0xB5
#define L_7 0xB6
#define L_8 0xB7
#define L_9 0xB8
#define L_10 0xB9
#define L_11 0xBA
#define L_12 0xBB
#define L_13 0xBC
#define L_14 0xBD
#define L_15 0xBE
#define L_16 0xBF
#define SW12_CS1 0xB0
#define SW12_CS2 0xB1
#define SW12_CS3 0xB2
#define SW12_CS4 0xB3
#define SW12_CS5 0xB4
#define SW12_CS6 0xB5
#define SW12_CS7 0xB6
#define SW12_CS8 0xB7
#define SW12_CS9 0xB8
#define SW12_CS10 0xB9
#define SW12_CS11 0xBA
#define SW12_CS12 0xBB
#define SW12_CS13 0xBC
#define SW12_CS14 0xBD
#define SW12_CS15 0xBE
#define SW12_CS16 0xBF
// DEPRECATED - DO NOT USE
#define A_1 SW1_CS1
#define A_2 SW1_CS2
#define A_3 SW1_CS3
#define A_4 SW1_CS4
#define A_5 SW1_CS5
#define A_6 SW1_CS6
#define A_7 SW1_CS7
#define A_8 SW1_CS8
#define A_9 SW1_CS9
#define A_10 SW1_CS10
#define A_11 SW1_CS11
#define A_12 SW1_CS12
#define A_13 SW1_CS13
#define A_14 SW1_CS14
#define A_15 SW1_CS15
#define A_16 SW1_CS16
#define B_1 SW2_CS1
#define B_2 SW2_CS2
#define B_3 SW2_CS3
#define B_4 SW2_CS4
#define B_5 SW2_CS5
#define B_6 SW2_CS6
#define B_7 SW2_CS7
#define B_8 SW2_CS8
#define B_9 SW2_CS9
#define B_10 SW2_CS10
#define B_11 SW2_CS11
#define B_12 SW2_CS12
#define B_13 SW2_CS13
#define B_14 SW2_CS14
#define B_15 SW2_CS15
#define B_16 SW2_CS16
#define C_1 SW3_CS1
#define C_2 SW3_CS2
#define C_3 SW3_CS3
#define C_4 SW3_CS4
#define C_5 SW3_CS5
#define C_6 SW3_CS6
#define C_7 SW3_CS7
#define C_8 SW3_CS8
#define C_9 SW3_CS9
#define C_10 SW3_CS10
#define C_11 SW3_CS11
#define C_12 SW3_CS12
#define C_13 SW3_CS13
#define C_14 SW3_CS14
#define C_15 SW3_CS15
#define C_16 SW3_CS16
#define D_1 SW4_CS1
#define D_2 SW4_CS2
#define D_3 SW4_CS3
#define D_4 SW4_CS4
#define D_5 SW4_CS5
#define D_6 SW4_CS6
#define D_7 SW4_CS7
#define D_8 SW4_CS8
#define D_9 SW4_CS9
#define D_10 SW4_CS10
#define D_11 SW4_CS11
#define D_12 SW4_CS12
#define D_13 SW4_CS13
#define D_14 SW4_CS14
#define D_15 SW4_CS15
#define D_16 SW4_CS16
#define E_1 SW5_CS1
#define E_2 SW5_CS2
#define E_3 SW5_CS3
#define E_4 SW5_CS4
#define E_5 SW5_CS5
#define E_6 SW5_CS6
#define E_7 SW5_CS7
#define E_8 SW5_CS8
#define E_9 SW5_CS9
#define E_10 SW5_CS10
#define E_11 SW5_CS11
#define E_12 SW5_CS12
#define E_13 SW5_CS13
#define E_14 SW5_CS14
#define E_15 SW5_CS15
#define E_16 SW5_CS16
#define F_1 SW6_CS1
#define F_2 SW6_CS2
#define F_3 SW6_CS3
#define F_4 SW6_CS4
#define F_5 SW6_CS5
#define F_6 SW6_CS6
#define F_7 SW6_CS7
#define F_8 SW6_CS8
#define F_9 SW6_CS9
#define F_10 SW6_CS10
#define F_11 SW6_CS11
#define F_12 SW6_CS12
#define F_13 SW6_CS13
#define F_14 SW6_CS14
#define F_15 SW6_CS15
#define F_16 SW6_CS16
#define G_1 SW7_CS1
#define G_2 SW7_CS2
#define G_3 SW7_CS3
#define G_4 SW7_CS4
#define G_5 SW7_CS5
#define G_6 SW7_CS6
#define G_7 SW7_CS7
#define G_8 SW7_CS8
#define G_9 SW7_CS9
#define G_10 SW7_CS10
#define G_11 SW7_CS11
#define G_12 SW7_CS12
#define G_13 SW7_CS13
#define G_14 SW7_CS14
#define G_15 SW7_CS15
#define G_16 SW7_CS16
#define H_1 SW8_CS1
#define H_2 SW8_CS2
#define H_3 SW8_CS3
#define H_4 SW8_CS4
#define H_5 SW8_CS5
#define H_6 SW8_CS6
#define H_7 SW8_CS7
#define H_8 SW8_CS8
#define H_9 SW8_CS9
#define H_10 SW8_CS10
#define H_11 SW8_CS11
#define H_12 SW8_CS12
#define H_13 SW8_CS13
#define H_14 SW8_CS14
#define H_15 SW8_CS15
#define H_16 SW8_CS16
#define I_1 SW9_CS1
#define I_2 SW9_CS2
#define I_3 SW9_CS3
#define I_4 SW9_CS4
#define I_5 SW9_CS5
#define I_6 SW9_CS6
#define I_7 SW9_CS7
#define I_8 SW9_CS8
#define I_9 SW9_CS9
#define I_10 SW9_CS10
#define I_11 SW9_CS11
#define I_12 SW9_CS12
#define I_13 SW9_CS13
#define I_14 SW9_CS14
#define I_15 SW9_CS15
#define I_16 SW9_CS16
#define J_1 SW10_CS1
#define J_2 SW10_CS2
#define J_3 SW10_CS3
#define J_4 SW10_CS4
#define J_5 SW10_CS5
#define J_6 SW10_CS6
#define J_7 SW10_CS7
#define J_8 SW10_CS8
#define J_9 SW10_CS9
#define J_10 SW10_CS10
#define J_11 SW10_CS11
#define J_12 SW10_CS12
#define J_13 SW10_CS13
#define J_14 SW10_CS14
#define J_15 SW10_CS15
#define J_16 SW10_CS16
#define K_1 SW11_CS1
#define K_2 SW11_CS2
#define K_3 SW11_CS3
#define K_4 SW11_CS4
#define K_5 SW11_CS5
#define K_6 SW11_CS6
#define K_7 SW11_CS7
#define K_8 SW11_CS8
#define K_9 SW11_CS9
#define K_10 SW11_CS10
#define K_11 SW11_CS11
#define K_12 SW11_CS12
#define K_13 SW11_CS13
#define K_14 SW11_CS14
#define K_15 SW11_CS15
#define K_16 SW11_CS16
#define L_1 SW12_CS1
#define L_2 SW12_CS2
#define L_3 SW12_CS3
#define L_4 SW12_CS4
#define L_5 SW12_CS5
#define L_6 SW12_CS6
#define L_7 SW12_CS7
#define L_8 SW12_CS8
#define L_9 SW12_CS9
#define L_10 SW12_CS10
#define L_11 SW12_CS11
#define L_12 SW12_CS12
#define L_13 SW12_CS13
#define L_14 SW12_CS14
#define L_15 SW12_CS15
#define L_16 SW12_CS16

View File

@ -17,6 +17,7 @@
#include "is31fl3736-mono.h"
#include "i2c_master.h"
#include "gpio.h"
#include "wait.h"
#define IS31FL3736_PWM_REGISTER_COUNT 192 // actually 96
@ -46,34 +47,55 @@
# define IS31FL3736_GLOBAL_CURRENT 0xFF
#endif
const uint8_t i2c_addresses[IS31FL3736_DRIVER_COUNT] = {
IS31FL3736_I2C_ADDRESS_1,
#ifdef IS31FL3736_I2C_ADDRESS_2
IS31FL3736_I2C_ADDRESS_2,
# ifdef IS31FL3736_I2C_ADDRESS_3
IS31FL3736_I2C_ADDRESS_3,
# ifdef IS31FL3736_I2C_ADDRESS_4
IS31FL3736_I2C_ADDRESS_4,
# endif
# endif
#endif
};
// These buffers match the IS31FL3736 PWM registers.
// The control buffers match the page 0 LED On/Off registers.
// Storing them like this is optimal for I2C transfers to the registers.
// We could optimize this and take out the unused registers from these
// buffers and the transfers in is31fl3736_write_pwm_buffer() but it's
// probably not worth the extra complexity.
uint8_t g_pwm_buffer[IS31FL3736_DRIVER_COUNT][IS31FL3736_PWM_REGISTER_COUNT];
bool g_pwm_buffer_update_required[IS31FL3736_DRIVER_COUNT] = {false};
typedef struct is31fl3736_driver_t {
uint8_t pwm_buffer[IS31FL3736_PWM_REGISTER_COUNT];
bool pwm_buffer_dirty;
uint8_t led_control_buffer[IS31FL3736_LED_CONTROL_REGISTER_COUNT];
bool led_control_buffer_dirty;
} PACKED is31fl3736_driver_t;
uint8_t g_led_control_registers[IS31FL3736_DRIVER_COUNT][IS31FL3736_LED_CONTROL_REGISTER_COUNT] = {0};
bool g_led_control_registers_update_required[IS31FL3736_DRIVER_COUNT] = {false};
is31fl3736_driver_t driver_buffers[IS31FL3736_DRIVER_COUNT] = {{
.pwm_buffer = {0},
.pwm_buffer_dirty = false,
.led_control_buffer = {0},
.led_control_buffer_dirty = false,
}};
void is31fl3736_write_register(uint8_t addr, uint8_t reg, uint8_t data) {
void is31fl3736_write_register(uint8_t index, uint8_t reg, uint8_t data) {
#if IS31FL3736_I2C_PERSISTENCE > 0
for (uint8_t i = 0; i < IS31FL3736_I2C_PERSISTENCE; i++) {
if (i2c_write_register(addr << 1, reg, &data, 1, IS31FL3736_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3736_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, reg, &data, 1, IS31FL3736_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3736_I2C_TIMEOUT);
#endif
}
void is31fl3736_select_page(uint8_t addr, uint8_t page) {
is31fl3736_write_register(addr, IS31FL3736_REG_COMMAND_WRITE_LOCK, IS31FL3736_COMMAND_WRITE_LOCK_MAGIC);
is31fl3736_write_register(addr, IS31FL3736_REG_COMMAND, page);
void is31fl3736_select_page(uint8_t index, uint8_t page) {
is31fl3736_write_register(index, IS31FL3736_REG_COMMAND_WRITE_LOCK, IS31FL3736_COMMAND_WRITE_LOCK_MAGIC);
is31fl3736_write_register(index, IS31FL3736_REG_COMMAND, page);
}
void is31fl3736_write_pwm_buffer(uint8_t addr, uint8_t index) {
void is31fl3736_write_pwm_buffer(uint8_t index) {
// Assumes page 1 is already selected.
// Transmit PWM registers in 12 transfers of 16 bytes.
@ -81,10 +103,10 @@ void is31fl3736_write_pwm_buffer(uint8_t addr, uint8_t index) {
for (uint8_t i = 0; i < IS31FL3736_PWM_REGISTER_COUNT; i += 16) {
#if IS31FL3736_I2C_PERSISTENCE > 0
for (uint8_t j = 0; j < IS31FL3736_I2C_PERSISTENCE; j++) {
if (i2c_write_register(addr << 1, i, g_pwm_buffer[index] + i, 16, IS31FL3736_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3736_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, i, g_pwm_buffer[index] + i, 16, IS31FL3736_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3736_I2C_TIMEOUT);
#endif
}
}
@ -92,64 +114,55 @@ void is31fl3736_write_pwm_buffer(uint8_t addr, uint8_t index) {
void is31fl3736_init_drivers(void) {
i2c_init();
is31fl3736_init(IS31FL3736_I2C_ADDRESS_1);
#if defined(IS31FL3736_I2C_ADDRESS_2)
is31fl3736_init(IS31FL3736_I2C_ADDRESS_2);
# if defined(IS31FL3736_I2C_ADDRESS_3)
is31fl3736_init(IS31FL3736_I2C_ADDRESS_3);
# if defined(IS31FL3736_I2C_ADDRESS_4)
is31fl3736_init(IS31FL3736_I2C_ADDRESS_4);
# endif
# endif
#if defined(IS31FL3736_SDB_PIN)
gpio_set_pin_output(IS31FL3736_SDB_PIN);
gpio_write_pin_high(IS31FL3736_SDB_PIN);
#endif
for (uint8_t i = 0; i < IS31FL3736_DRIVER_COUNT; i++) {
is31fl3736_init(i);
}
for (int i = 0; i < IS31FL3736_LED_COUNT; i++) {
is31fl3736_set_led_control_register(i, true);
}
is31fl3736_update_led_control_registers(IS31FL3736_I2C_ADDRESS_1, 0);
#if defined(IS31FL3736_I2C_ADDRESS_2)
is31fl3736_update_led_control_registers(IS31FL3736_I2C_ADDRESS_2, 1);
# if defined(IS31FL3736_I2C_ADDRESS_3)
is31fl3736_update_led_control_registers(IS31FL3736_I2C_ADDRESS_3, 2);
# if defined(IS31FL3736_I2C_ADDRESS_4)
is31fl3736_update_led_control_registers(IS31FL3736_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3736_DRIVER_COUNT; i++) {
is31fl3736_update_led_control_registers(i);
}
}
void is31fl3736_init(uint8_t addr) {
void is31fl3736_init(uint8_t index) {
// In order to avoid the LEDs being driven with garbage data
// in the LED driver's PWM registers, shutdown is enabled last.
// Set up the mode and other settings, clear the PWM registers,
// then disable software shutdown.
is31fl3736_select_page(addr, IS31FL3736_COMMAND_LED_CONTROL);
is31fl3736_select_page(index, IS31FL3736_COMMAND_LED_CONTROL);
// Turn off all LEDs.
for (uint8_t i = 0; i < IS31FL3736_LED_CONTROL_REGISTER_COUNT; i++) {
is31fl3736_write_register(addr, i, 0x00);
is31fl3736_write_register(index, i, 0x00);
}
is31fl3736_select_page(addr, IS31FL3736_COMMAND_PWM);
is31fl3736_select_page(index, IS31FL3736_COMMAND_PWM);
// Set PWM on all LEDs to 0
// No need to setup Breath registers to PWM as that is the default.
for (uint8_t i = 0; i < IS31FL3736_PWM_REGISTER_COUNT; i++) {
is31fl3736_write_register(addr, i, 0x00);
is31fl3736_write_register(index, i, 0x00);
}
is31fl3736_select_page(addr, IS31FL3736_COMMAND_FUNCTION);
is31fl3736_select_page(index, IS31FL3736_COMMAND_FUNCTION);
// Set de-ghost pull-up resistors (SWx)
is31fl3736_write_register(addr, IS31FL3736_FUNCTION_REG_SW_PULLUP, IS31FL3736_SW_PULLUP);
is31fl3736_write_register(index, IS31FL3736_FUNCTION_REG_SW_PULLUP, IS31FL3736_SW_PULLUP);
// Set de-ghost pull-down resistors (CSx)
is31fl3736_write_register(addr, IS31FL3736_FUNCTION_REG_CS_PULLDOWN, IS31FL3736_CS_PULLDOWN);
is31fl3736_write_register(index, IS31FL3736_FUNCTION_REG_CS_PULLDOWN, IS31FL3736_CS_PULLDOWN);
// Set global current to maximum.
is31fl3736_write_register(addr, IS31FL3736_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3736_GLOBAL_CURRENT);
is31fl3736_write_register(index, IS31FL3736_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3736_GLOBAL_CURRENT);
// Disable software shutdown.
is31fl3736_write_register(addr, IS31FL3736_FUNCTION_REG_CONFIGURATION, ((IS31FL3736_PWM_FREQUENCY & 0b111) << 3) | 0x01);
is31fl3736_write_register(index, IS31FL3736_FUNCTION_REG_CONFIGURATION, ((IS31FL3736_PWM_FREQUENCY & 0b111) << 3) | 0x01);
// Wait 10ms to ensure the device has woken up.
wait_ms(10);
@ -161,12 +174,12 @@ void is31fl3736_set_value(int index, uint8_t value) {
if (index >= 0 && index < IS31FL3736_LED_COUNT) {
memcpy_P(&led, (&g_is31fl3736_leds[index]), sizeof(led));
if (g_pwm_buffer[led.driver][led.v] == value) {
if (driver_buffers[led.driver].pwm_buffer[led.v] == value) {
return;
}
g_pwm_buffer[led.driver][led.v] = value;
g_pwm_buffer_update_required[led.driver] = true;
driver_buffers[led.driver].pwm_buffer[led.v] = value;
driver_buffers[led.driver].pwm_buffer_dirty = true;
}
}
@ -190,45 +203,38 @@ void is31fl3736_set_led_control_register(uint8_t index, bool value) {
uint8_t bit_value = led.v % 8;
if (value) {
g_led_control_registers[led.driver][control_register] |= (1 << bit_value);
driver_buffers[led.driver].led_control_buffer[control_register] |= (1 << bit_value);
} else {
g_led_control_registers[led.driver][control_register] &= ~(1 << bit_value);
driver_buffers[led.driver].led_control_buffer[control_register] &= ~(1 << bit_value);
}
g_led_control_registers_update_required[led.driver] = true;
driver_buffers[led.driver].led_control_buffer_dirty = true;
}
void is31fl3736_update_pwm_buffers(uint8_t addr, uint8_t index) {
if (g_pwm_buffer_update_required[index]) {
is31fl3736_select_page(addr, IS31FL3736_COMMAND_PWM);
void is31fl3736_update_pwm_buffers(uint8_t index) {
if (driver_buffers[index].pwm_buffer_dirty) {
is31fl3736_select_page(index, IS31FL3736_COMMAND_PWM);
is31fl3736_write_pwm_buffer(addr, index);
is31fl3736_write_pwm_buffer(index);
g_pwm_buffer_update_required[index] = false;
driver_buffers[index].pwm_buffer_dirty = false;
}
}
void is31fl3736_update_led_control_registers(uint8_t addr, uint8_t index) {
if (g_led_control_registers_update_required[index]) {
is31fl3736_select_page(addr, IS31FL3736_COMMAND_LED_CONTROL);
void is31fl3736_update_led_control_registers(uint8_t index) {
if (driver_buffers[index].led_control_buffer_dirty) {
is31fl3736_select_page(index, IS31FL3736_COMMAND_LED_CONTROL);
for (uint8_t i = 0; i < IS31FL3736_LED_CONTROL_REGISTER_COUNT; i++) {
is31fl3736_write_register(addr, i, g_led_control_registers[index][i]);
is31fl3736_write_register(index, i, driver_buffers[index].led_control_buffer[i]);
}
g_led_control_registers_update_required[index] = false;
driver_buffers[index].led_control_buffer_dirty = false;
}
}
void is31fl3736_flush(void) {
is31fl3736_update_pwm_buffers(IS31FL3736_I2C_ADDRESS_1, 0);
#if defined(IS31FL3736_I2C_ADDRESS_2)
is31fl3736_update_pwm_buffers(IS31FL3736_I2C_ADDRESS_2, 1);
# if defined(IS31FL3736_I2C_ADDRESS_3)
is31fl3736_update_pwm_buffers(IS31FL3736_I2C_ADDRESS_3, 2);
# if defined(IS31FL3736_I2C_ADDRESS_4)
is31fl3736_update_pwm_buffers(IS31FL3736_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3736_DRIVER_COUNT; i++) {
is31fl3736_update_pwm_buffers(i);
}
}

View File

@ -110,9 +110,9 @@ typedef struct is31fl3736_led_t {
extern const is31fl3736_led_t PROGMEM g_is31fl3736_leds[IS31FL3736_LED_COUNT];
void is31fl3736_init_drivers(void);
void is31fl3736_init(uint8_t addr);
void is31fl3736_write_register(uint8_t addr, uint8_t reg, uint8_t data);
void is31fl3736_select_page(uint8_t addr, uint8_t page);
void is31fl3736_init(uint8_t index);
void is31fl3736_write_register(uint8_t index, uint8_t reg, uint8_t data);
void is31fl3736_select_page(uint8_t index, uint8_t page);
void is31fl3736_set_value(int index, uint8_t value);
void is31fl3736_set_value_all(uint8_t value);
@ -123,8 +123,8 @@ void is31fl3736_set_led_control_register(uint8_t index, bool value);
// (eg. from a timer interrupt).
// Call this while idle (in between matrix scans).
// If the buffer is dirty, it will update the driver with the buffer.
void is31fl3736_update_pwm_buffers(uint8_t addr, uint8_t index);
void is31fl3736_update_led_control_registers(uint8_t addr, uint8_t index);
void is31fl3736_update_pwm_buffers(uint8_t index);
void is31fl3736_update_led_control_registers(uint8_t index);
void is31fl3736_flush(void);
@ -152,110 +152,220 @@ void is31fl3736_flush(void);
#define IS31FL3736_PWM_FREQUENCY_2K1_HZ 0b011
#define IS31FL3736_PWM_FREQUENCY_1K05_HZ 0b100
#define A_1 0x00
#define A_2 0x02
#define A_3 0x04
#define A_4 0x06
#define A_5 0x08
#define A_6 0x0A
#define A_7 0x0C
#define A_8 0x0E
#define SW1_CS1 0x00
#define SW1_CS2 0x02
#define SW1_CS3 0x04
#define SW1_CS4 0x06
#define SW1_CS5 0x08
#define SW1_CS6 0x0A
#define SW1_CS7 0x0C
#define SW1_CS8 0x0E
#define B_1 0x10
#define B_2 0x12
#define B_3 0x14
#define B_4 0x16
#define B_5 0x18
#define B_6 0x1A
#define B_7 0x1C
#define B_8 0x1E
#define SW2_CS1 0x10
#define SW2_CS2 0x12
#define SW2_CS3 0x14
#define SW2_CS4 0x16
#define SW2_CS5 0x18
#define SW2_CS6 0x1A
#define SW2_CS7 0x1C
#define SW2_CS8 0x1E
#define C_1 0x20
#define C_2 0x22
#define C_3 0x24
#define C_4 0x26
#define C_5 0x28
#define C_6 0x2A
#define C_7 0x2C
#define C_8 0x2E
#define SW3_CS1 0x20
#define SW3_CS2 0x22
#define SW3_CS3 0x24
#define SW3_CS4 0x26
#define SW3_CS5 0x28
#define SW3_CS6 0x2A
#define SW3_CS7 0x2C
#define SW3_CS8 0x2E
#define D_1 0x30
#define D_2 0x32
#define D_3 0x34
#define D_4 0x36
#define D_5 0x38
#define D_6 0x3A
#define D_7 0x3C
#define D_8 0x3E
#define SW4_CS1 0x30
#define SW4_CS2 0x32
#define SW4_CS3 0x34
#define SW4_CS4 0x36
#define SW4_CS5 0x38
#define SW4_CS6 0x3A
#define SW4_CS7 0x3C
#define SW4_CS8 0x3E
#define E_1 0x40
#define E_2 0x42
#define E_3 0x44
#define E_4 0x46
#define E_5 0x48
#define E_6 0x4A
#define E_7 0x4C
#define E_8 0x4E
#define SW5_CS1 0x40
#define SW5_CS2 0x42
#define SW5_CS3 0x44
#define SW5_CS4 0x46
#define SW5_CS5 0x48
#define SW5_CS6 0x4A
#define SW5_CS7 0x4C
#define SW5_CS8 0x4E
#define F_1 0x50
#define F_2 0x52
#define F_3 0x54
#define F_4 0x56
#define F_5 0x58
#define F_6 0x5A
#define F_7 0x5C
#define F_8 0x5E
#define SW6_CS1 0x50
#define SW6_CS2 0x52
#define SW6_CS3 0x54
#define SW6_CS4 0x56
#define SW6_CS5 0x58
#define SW6_CS6 0x5A
#define SW6_CS7 0x5C
#define SW6_CS8 0x5E
#define G_1 0x60
#define G_2 0x62
#define G_3 0x64
#define G_4 0x66
#define G_5 0x68
#define G_6 0x6A
#define G_7 0x6C
#define G_8 0x6E
#define SW7_CS1 0x60
#define SW7_CS2 0x62
#define SW7_CS3 0x64
#define SW7_CS4 0x66
#define SW7_CS5 0x68
#define SW7_CS6 0x6A
#define SW7_CS7 0x6C
#define SW7_CS8 0x6E
#define H_1 0x70
#define H_2 0x72
#define H_3 0x74
#define H_4 0x76
#define H_5 0x78
#define H_6 0x7A
#define H_7 0x7C
#define H_8 0x7E
#define SW8_CS1 0x70
#define SW8_CS2 0x72
#define SW8_CS3 0x74
#define SW8_CS4 0x76
#define SW8_CS5 0x78
#define SW8_CS6 0x7A
#define SW8_CS7 0x7C
#define SW8_CS8 0x7E
#define I_1 0x80
#define I_2 0x82
#define I_3 0x84
#define I_4 0x86
#define I_5 0x88
#define I_6 0x8A
#define I_7 0x8C
#define I_8 0x8E
#define SW9_CS1 0x80
#define SW9_CS2 0x82
#define SW9_CS3 0x84
#define SW9_CS4 0x86
#define SW9_CS5 0x88
#define SW9_CS6 0x8A
#define SW9_CS7 0x8C
#define SW9_CS8 0x8E
#define J_1 0x90
#define J_2 0x92
#define J_3 0x94
#define J_4 0x96
#define J_5 0x98
#define J_6 0x9A
#define J_7 0x9C
#define J_8 0x9E
#define SW10_CS1 0x90
#define SW10_CS2 0x92
#define SW10_CS3 0x94
#define SW10_CS4 0x96
#define SW10_CS5 0x98
#define SW10_CS6 0x9A
#define SW10_CS7 0x9C
#define SW10_CS8 0x9E
#define K_1 0xA0
#define K_2 0xA2
#define K_3 0xA4
#define K_4 0xA6
#define K_5 0xA8
#define K_6 0xAA
#define K_7 0xAC
#define K_8 0xAE
#define SW11_CS1 0xA0
#define SW11_CS2 0xA2
#define SW11_CS3 0xA4
#define SW11_CS4 0xA6
#define SW11_CS5 0xA8
#define SW11_CS6 0xAA
#define SW11_CS7 0xAC
#define SW11_CS8 0xAE
#define L_1 0xB0
#define L_2 0xB2
#define L_3 0xB4
#define L_4 0xB6
#define L_5 0xB8
#define L_6 0xBA
#define L_7 0xBC
#define L_8 0xBE
#define SW12_CS1 0xB0
#define SW12_CS2 0xB2
#define SW12_CS3 0xB4
#define SW12_CS4 0xB6
#define SW12_CS5 0xB8
#define SW12_CS6 0xBA
#define SW12_CS7 0xBC
#define SW12_CS8 0xBE
// DEPRECATED - DO NOT USE
#define A_1 SW1_CS1
#define A_2 SW1_CS2
#define A_3 SW1_CS3
#define A_4 SW1_CS4
#define A_5 SW1_CS5
#define A_6 SW1_CS6
#define A_7 SW1_CS7
#define A_8 SW1_CS8
#define B_1 SW2_CS1
#define B_2 SW2_CS2
#define B_3 SW2_CS3
#define B_4 SW2_CS4
#define B_5 SW2_CS5
#define B_6 SW2_CS6
#define B_7 SW2_CS7
#define B_8 SW2_CS8
#define C_1 SW3_CS1
#define C_2 SW3_CS2
#define C_3 SW3_CS3
#define C_4 SW3_CS4
#define C_5 SW3_CS5
#define C_6 SW3_CS6
#define C_7 SW3_CS7
#define C_8 SW3_CS8
#define D_1 SW4_CS1
#define D_2 SW4_CS2
#define D_3 SW4_CS3
#define D_4 SW4_CS4
#define D_5 SW4_CS5
#define D_6 SW4_CS6
#define D_7 SW4_CS7
#define D_8 SW4_CS8
#define E_1 SW5_CS1
#define E_2 SW5_CS2
#define E_3 SW5_CS3
#define E_4 SW5_CS4
#define E_5 SW5_CS5
#define E_6 SW5_CS6
#define E_7 SW5_CS7
#define E_8 SW5_CS8
#define F_1 SW6_CS1
#define F_2 SW6_CS2
#define F_3 SW6_CS3
#define F_4 SW6_CS4
#define F_5 SW6_CS5
#define F_6 SW6_CS6
#define F_7 SW6_CS7
#define F_8 SW6_CS8
#define G_1 SW7_CS1
#define G_2 SW7_CS2
#define G_3 SW7_CS3
#define G_4 SW7_CS4
#define G_5 SW7_CS5
#define G_6 SW7_CS6
#define G_7 SW7_CS7
#define G_8 SW7_CS8
#define H_1 SW8_CS1
#define H_2 SW8_CS2
#define H_3 SW8_CS3
#define H_4 SW8_CS4
#define H_5 SW8_CS5
#define H_6 SW8_CS6
#define H_7 SW8_CS7
#define H_8 SW8_CS8
#define I_1 SW9_CS1
#define I_2 SW9_CS2
#define I_3 SW9_CS3
#define I_4 SW9_CS4
#define I_5 SW9_CS5
#define I_6 SW9_CS6
#define I_7 SW9_CS7
#define I_8 SW9_CS8
#define J_1 SW10_CS1
#define J_2 SW10_CS2
#define J_3 SW10_CS3
#define J_4 SW10_CS4
#define J_5 SW10_CS5
#define J_6 SW10_CS6
#define J_7 SW10_CS7
#define J_8 SW10_CS8
#define K_1 SW11_CS1
#define K_2 SW11_CS2
#define K_3 SW11_CS3
#define K_4 SW11_CS4
#define K_5 SW11_CS5
#define K_6 SW11_CS6
#define K_7 SW11_CS7
#define K_8 SW11_CS8
#define L_1 SW12_CS1
#define L_2 SW12_CS2
#define L_3 SW12_CS3
#define L_4 SW12_CS4
#define L_5 SW12_CS5
#define L_6 SW12_CS6
#define L_7 SW12_CS7
#define L_8 SW12_CS8

View File

@ -17,6 +17,7 @@
#include "is31fl3736.h"
#include "i2c_master.h"
#include "gpio.h"
#include "wait.h"
#define IS31FL3736_PWM_REGISTER_COUNT 192 // actually 96
@ -46,34 +47,55 @@
# define IS31FL3736_GLOBAL_CURRENT 0xFF
#endif
const uint8_t i2c_addresses[IS31FL3736_DRIVER_COUNT] = {
IS31FL3736_I2C_ADDRESS_1,
#ifdef IS31FL3736_I2C_ADDRESS_2
IS31FL3736_I2C_ADDRESS_2,
# ifdef IS31FL3736_I2C_ADDRESS_3
IS31FL3736_I2C_ADDRESS_3,
# ifdef IS31FL3736_I2C_ADDRESS_4
IS31FL3736_I2C_ADDRESS_4,
# endif
# endif
#endif
};
// These buffers match the IS31FL3736 PWM registers.
// The control buffers match the page 0 LED On/Off registers.
// Storing them like this is optimal for I2C transfers to the registers.
// We could optimize this and take out the unused registers from these
// buffers and the transfers in is31fl3736_write_pwm_buffer() but it's
// probably not worth the extra complexity.
uint8_t g_pwm_buffer[IS31FL3736_DRIVER_COUNT][IS31FL3736_PWM_REGISTER_COUNT];
bool g_pwm_buffer_update_required[IS31FL3736_DRIVER_COUNT] = {false};
typedef struct is31fl3736_driver_t {
uint8_t pwm_buffer[IS31FL3736_PWM_REGISTER_COUNT];
bool pwm_buffer_dirty;
uint8_t led_control_buffer[IS31FL3736_LED_CONTROL_REGISTER_COUNT];
bool led_control_buffer_dirty;
} PACKED is31fl3736_driver_t;
uint8_t g_led_control_registers[IS31FL3736_DRIVER_COUNT][IS31FL3736_LED_CONTROL_REGISTER_COUNT] = {0};
bool g_led_control_registers_update_required[IS31FL3736_DRIVER_COUNT] = {false};
is31fl3736_driver_t driver_buffers[IS31FL3736_DRIVER_COUNT] = {{
.pwm_buffer = {0},
.pwm_buffer_dirty = false,
.led_control_buffer = {0},
.led_control_buffer_dirty = false,
}};
void is31fl3736_write_register(uint8_t addr, uint8_t reg, uint8_t data) {
void is31fl3736_write_register(uint8_t index, uint8_t reg, uint8_t data) {
#if IS31FL3736_I2C_PERSISTENCE > 0
for (uint8_t i = 0; i < IS31FL3736_I2C_PERSISTENCE; i++) {
if (i2c_write_register(addr << 1, reg, &data, 1, IS31FL3736_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3736_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, reg, &data, 1, IS31FL3736_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3736_I2C_TIMEOUT);
#endif
}
void is31fl3736_select_page(uint8_t addr, uint8_t page) {
is31fl3736_write_register(addr, IS31FL3736_REG_COMMAND_WRITE_LOCK, IS31FL3736_COMMAND_WRITE_LOCK_MAGIC);
is31fl3736_write_register(addr, IS31FL3736_REG_COMMAND, page);
void is31fl3736_select_page(uint8_t index, uint8_t page) {
is31fl3736_write_register(index, IS31FL3736_REG_COMMAND_WRITE_LOCK, IS31FL3736_COMMAND_WRITE_LOCK_MAGIC);
is31fl3736_write_register(index, IS31FL3736_REG_COMMAND, page);
}
void is31fl3736_write_pwm_buffer(uint8_t addr, uint8_t index) {
void is31fl3736_write_pwm_buffer(uint8_t index) {
// Assumes page 1 is already selected.
// Transmit PWM registers in 12 transfers of 16 bytes.
@ -81,10 +103,10 @@ void is31fl3736_write_pwm_buffer(uint8_t addr, uint8_t index) {
for (uint8_t i = 0; i < IS31FL3736_PWM_REGISTER_COUNT; i += 16) {
#if IS31FL3736_I2C_PERSISTENCE > 0
for (uint8_t j = 0; j < IS31FL3736_I2C_PERSISTENCE; j++) {
if (i2c_write_register(addr << 1, i, g_pwm_buffer[index] + i, 16, IS31FL3736_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3736_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, i, g_pwm_buffer[index] + i, 16, IS31FL3736_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3736_I2C_TIMEOUT);
#endif
}
}
@ -92,64 +114,55 @@ void is31fl3736_write_pwm_buffer(uint8_t addr, uint8_t index) {
void is31fl3736_init_drivers(void) {
i2c_init();
is31fl3736_init(IS31FL3736_I2C_ADDRESS_1);
#if defined(IS31FL3736_I2C_ADDRESS_2)
is31fl3736_init(IS31FL3736_I2C_ADDRESS_2);
# if defined(IS31FL3736_I2C_ADDRESS_3)
is31fl3736_init(IS31FL3736_I2C_ADDRESS_3);
# if defined(IS31FL3736_I2C_ADDRESS_4)
is31fl3736_init(IS31FL3736_I2C_ADDRESS_4);
# endif
# endif
#if defined(IS31FL3736_SDB_PIN)
gpio_set_pin_output(IS31FL3736_SDB_PIN);
gpio_write_pin_high(IS31FL3736_SDB_PIN);
#endif
for (uint8_t i = 0; i < IS31FL3736_DRIVER_COUNT; i++) {
is31fl3736_init(i);
}
for (int i = 0; i < IS31FL3736_LED_COUNT; i++) {
is31fl3736_set_led_control_register(i, true, true, true);
}
is31fl3736_update_led_control_registers(IS31FL3736_I2C_ADDRESS_1, 0);
#if defined(IS31FL3736_I2C_ADDRESS_2)
is31fl3736_update_led_control_registers(IS31FL3736_I2C_ADDRESS_2, 1);
# if defined(IS31FL3736_I2C_ADDRESS_3)
is31fl3736_update_led_control_registers(IS31FL3736_I2C_ADDRESS_3, 2);
# if defined(IS31FL3736_I2C_ADDRESS_4)
is31fl3736_update_led_control_registers(IS31FL3736_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3736_DRIVER_COUNT; i++) {
is31fl3736_update_led_control_registers(i);
}
}
void is31fl3736_init(uint8_t addr) {
void is31fl3736_init(uint8_t index) {
// In order to avoid the LEDs being driven with garbage data
// in the LED driver's PWM registers, shutdown is enabled last.
// Set up the mode and other settings, clear the PWM registers,
// then disable software shutdown.
is31fl3736_select_page(addr, IS31FL3736_COMMAND_LED_CONTROL);
is31fl3736_select_page(index, IS31FL3736_COMMAND_LED_CONTROL);
// Turn off all LEDs.
for (uint8_t i = 0; i < IS31FL3736_LED_CONTROL_REGISTER_COUNT; i++) {
is31fl3736_write_register(addr, i, 0x00);
is31fl3736_write_register(index, i, 0x00);
}
is31fl3736_select_page(addr, IS31FL3736_COMMAND_PWM);
is31fl3736_select_page(index, IS31FL3736_COMMAND_PWM);
// Set PWM on all LEDs to 0
// No need to setup Breath registers to PWM as that is the default.
for (uint8_t i = 0; i < IS31FL3736_PWM_REGISTER_COUNT; i++) {
is31fl3736_write_register(addr, i, 0x00);
is31fl3736_write_register(index, i, 0x00);
}
is31fl3736_select_page(addr, IS31FL3736_COMMAND_FUNCTION);
is31fl3736_select_page(index, IS31FL3736_COMMAND_FUNCTION);
// Set de-ghost pull-up resistors (SWx)
is31fl3736_write_register(addr, IS31FL3736_FUNCTION_REG_SW_PULLUP, IS31FL3736_SW_PULLUP);
is31fl3736_write_register(index, IS31FL3736_FUNCTION_REG_SW_PULLUP, IS31FL3736_SW_PULLUP);
// Set de-ghost pull-down resistors (CSx)
is31fl3736_write_register(addr, IS31FL3736_FUNCTION_REG_CS_PULLDOWN, IS31FL3736_CS_PULLDOWN);
is31fl3736_write_register(index, IS31FL3736_FUNCTION_REG_CS_PULLDOWN, IS31FL3736_CS_PULLDOWN);
// Set global current to maximum.
is31fl3736_write_register(addr, IS31FL3736_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3736_GLOBAL_CURRENT);
is31fl3736_write_register(index, IS31FL3736_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3736_GLOBAL_CURRENT);
// Disable software shutdown.
is31fl3736_write_register(addr, IS31FL3736_FUNCTION_REG_CONFIGURATION, ((IS31FL3736_PWM_FREQUENCY & 0b111) << 3) | 0x01);
is31fl3736_write_register(index, IS31FL3736_FUNCTION_REG_CONFIGURATION, ((IS31FL3736_PWM_FREQUENCY & 0b111) << 3) | 0x01);
// Wait 10ms to ensure the device has woken up.
wait_ms(10);
@ -161,14 +174,14 @@ void is31fl3736_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
if (index >= 0 && index < IS31FL3736_LED_COUNT) {
memcpy_P(&led, (&g_is31fl3736_leds[index]), sizeof(led));
if (g_pwm_buffer[led.driver][led.r] == red && g_pwm_buffer[led.driver][led.g] == green && g_pwm_buffer[led.driver][led.b] == blue) {
if (driver_buffers[led.driver].pwm_buffer[led.r] == red && driver_buffers[led.driver].pwm_buffer[led.g] == green && driver_buffers[led.driver].pwm_buffer[led.b] == blue) {
return;
}
g_pwm_buffer[led.driver][led.r] = red;
g_pwm_buffer[led.driver][led.g] = green;
g_pwm_buffer[led.driver][led.b] = blue;
g_pwm_buffer_update_required[led.driver] = true;
driver_buffers[led.driver].pwm_buffer[led.r] = red;
driver_buffers[led.driver].pwm_buffer[led.g] = green;
driver_buffers[led.driver].pwm_buffer[led.b] = blue;
driver_buffers[led.driver].pwm_buffer_dirty = true;
}
}
@ -197,55 +210,48 @@ void is31fl3736_set_led_control_register(uint8_t index, bool red, bool green, bo
uint8_t bit_b = led.b % 8;
if (red) {
g_led_control_registers[led.driver][control_register_r] |= (1 << bit_r);
driver_buffers[led.driver].led_control_buffer[control_register_r] |= (1 << bit_r);
} else {
g_led_control_registers[led.driver][control_register_r] &= ~(1 << bit_r);
driver_buffers[led.driver].led_control_buffer[control_register_r] &= ~(1 << bit_r);
}
if (green) {
g_led_control_registers[led.driver][control_register_g] |= (1 << bit_g);
driver_buffers[led.driver].led_control_buffer[control_register_g] |= (1 << bit_g);
} else {
g_led_control_registers[led.driver][control_register_g] &= ~(1 << bit_g);
driver_buffers[led.driver].led_control_buffer[control_register_g] &= ~(1 << bit_g);
}
if (blue) {
g_led_control_registers[led.driver][control_register_b] |= (1 << bit_b);
driver_buffers[led.driver].led_control_buffer[control_register_b] |= (1 << bit_b);
} else {
g_led_control_registers[led.driver][control_register_b] &= ~(1 << bit_b);
driver_buffers[led.driver].led_control_buffer[control_register_b] &= ~(1 << bit_b);
}
g_led_control_registers_update_required[led.driver] = true;
driver_buffers[led.driver].led_control_buffer_dirty = true;
}
void is31fl3736_update_pwm_buffers(uint8_t addr, uint8_t index) {
if (g_pwm_buffer_update_required[index]) {
is31fl3736_select_page(addr, IS31FL3736_COMMAND_PWM);
void is31fl3736_update_pwm_buffers(uint8_t index) {
if (driver_buffers[index].pwm_buffer_dirty) {
is31fl3736_select_page(index, IS31FL3736_COMMAND_PWM);
is31fl3736_write_pwm_buffer(addr, index);
is31fl3736_write_pwm_buffer(index);
g_pwm_buffer_update_required[index] = false;
driver_buffers[index].pwm_buffer_dirty = false;
}
}
void is31fl3736_update_led_control_registers(uint8_t addr, uint8_t index) {
if (g_led_control_registers_update_required[index]) {
is31fl3736_select_page(addr, IS31FL3736_COMMAND_LED_CONTROL);
void is31fl3736_update_led_control_registers(uint8_t index) {
if (driver_buffers[index].led_control_buffer_dirty) {
is31fl3736_select_page(index, IS31FL3736_COMMAND_LED_CONTROL);
for (uint8_t i = 0; i < IS31FL3736_LED_CONTROL_REGISTER_COUNT; i++) {
is31fl3736_write_register(addr, i, g_led_control_registers[index][i]);
is31fl3736_write_register(index, i, driver_buffers[index].led_control_buffer[i]);
}
g_led_control_registers_update_required[index] = false;
driver_buffers[index].led_control_buffer_dirty = false;
}
}
void is31fl3736_flush(void) {
is31fl3736_update_pwm_buffers(IS31FL3736_I2C_ADDRESS_1, 0);
#if defined(IS31FL3736_I2C_ADDRESS_2)
is31fl3736_update_pwm_buffers(IS31FL3736_I2C_ADDRESS_2, 1);
# if defined(IS31FL3736_I2C_ADDRESS_3)
is31fl3736_update_pwm_buffers(IS31FL3736_I2C_ADDRESS_3, 2);
# if defined(IS31FL3736_I2C_ADDRESS_4)
is31fl3736_update_pwm_buffers(IS31FL3736_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3736_DRIVER_COUNT; i++) {
is31fl3736_update_pwm_buffers(i);
}
}

View File

@ -124,9 +124,9 @@ typedef struct is31fl3736_led_t {
extern const is31fl3736_led_t PROGMEM g_is31fl3736_leds[IS31FL3736_LED_COUNT];
void is31fl3736_init_drivers(void);
void is31fl3736_init(uint8_t addr);
void is31fl3736_write_register(uint8_t addr, uint8_t reg, uint8_t data);
void is31fl3736_select_page(uint8_t addr, uint8_t page);
void is31fl3736_init(uint8_t index);
void is31fl3736_write_register(uint8_t index, uint8_t reg, uint8_t data);
void is31fl3736_select_page(uint8_t index, uint8_t page);
void is31fl3736_set_color(int index, uint8_t red, uint8_t green, uint8_t blue);
void is31fl3736_set_color_all(uint8_t red, uint8_t green, uint8_t blue);
@ -137,8 +137,8 @@ void is31fl3736_set_led_control_register(uint8_t index, bool red, bool green, bo
// (eg. from a timer interrupt).
// Call this while idle (in between matrix scans).
// If the buffer is dirty, it will update the driver with the buffer.
void is31fl3736_update_pwm_buffers(uint8_t addr, uint8_t index);
void is31fl3736_update_led_control_registers(uint8_t addr, uint8_t index);
void is31fl3736_update_pwm_buffers(uint8_t index);
void is31fl3736_update_led_control_registers(uint8_t index);
void is31fl3736_flush(void);
@ -166,110 +166,110 @@ void is31fl3736_flush(void);
#define IS31FL3736_PWM_FREQUENCY_2K1_HZ 0b011
#define IS31FL3736_PWM_FREQUENCY_1K05_HZ 0b100
#define A_1 0x00
#define A_2 0x02
#define A_3 0x04
#define A_4 0x06
#define A_5 0x08
#define A_6 0x0A
#define A_7 0x0C
#define A_8 0x0E
#define SW1_CS1 0x00
#define SW1_CS2 0x02
#define SW1_CS3 0x04
#define SW1_CS4 0x06
#define SW1_CS5 0x08
#define SW1_CS6 0x0A
#define SW1_CS7 0x0C
#define SW1_CS8 0x0E
#define B_1 0x10
#define B_2 0x12
#define B_3 0x14
#define B_4 0x16
#define B_5 0x18
#define B_6 0x1A
#define B_7 0x1C
#define B_8 0x1E
#define SW2_CS1 0x10
#define SW2_CS2 0x12
#define SW2_CS3 0x14
#define SW2_CS4 0x16
#define SW2_CS5 0x18
#define SW2_CS6 0x1A
#define SW2_CS7 0x1C
#define SW2_CS8 0x1E
#define C_1 0x20
#define C_2 0x22
#define C_3 0x24
#define C_4 0x26
#define C_5 0x28
#define C_6 0x2A
#define C_7 0x2C
#define C_8 0x2E
#define SW3_CS1 0x20
#define SW3_CS2 0x22
#define SW3_CS3 0x24
#define SW3_CS4 0x26
#define SW3_CS5 0x28
#define SW3_CS6 0x2A
#define SW3_CS7 0x2C
#define SW3_CS8 0x2E
#define D_1 0x30
#define D_2 0x32
#define D_3 0x34
#define D_4 0x36
#define D_5 0x38
#define D_6 0x3A
#define D_7 0x3C
#define D_8 0x3E
#define SW4_CS1 0x30
#define SW4_CS2 0x32
#define SW4_CS3 0x34
#define SW4_CS4 0x36
#define SW4_CS5 0x38
#define SW4_CS6 0x3A
#define SW4_CS7 0x3C
#define SW4_CS8 0x3E
#define E_1 0x40
#define E_2 0x42
#define E_3 0x44
#define E_4 0x46
#define E_5 0x48
#define E_6 0x4A
#define E_7 0x4C
#define E_8 0x4E
#define SW5_CS1 0x40
#define SW5_CS2 0x42
#define SW5_CS3 0x44
#define SW5_CS4 0x46
#define SW5_CS5 0x48
#define SW5_CS6 0x4A
#define SW5_CS7 0x4C
#define SW5_CS8 0x4E
#define F_1 0x50
#define F_2 0x52
#define F_3 0x54
#define F_4 0x56
#define F_5 0x58
#define F_6 0x5A
#define F_7 0x5C
#define F_8 0x5E
#define SW6_CS1 0x50
#define SW6_CS2 0x52
#define SW6_CS3 0x54
#define SW6_CS4 0x56
#define SW6_CS5 0x58
#define SW6_CS6 0x5A
#define SW6_CS7 0x5C
#define SW6_CS8 0x5E
#define G_1 0x60
#define G_2 0x62
#define G_3 0x64
#define G_4 0x66
#define G_5 0x68
#define G_6 0x6A
#define G_7 0x6C
#define G_8 0x6E
#define SW7_CS1 0x60
#define SW7_CS2 0x62
#define SW7_CS3 0x64
#define SW7_CS4 0x66
#define SW7_CS5 0x68
#define SW7_CS6 0x6A
#define SW7_CS7 0x6C
#define SW7_CS8 0x6E
#define H_1 0x70
#define H_2 0x72
#define H_3 0x74
#define H_4 0x76
#define H_5 0x78
#define H_6 0x7A
#define H_7 0x7C
#define H_8 0x7E
#define SW8_CS1 0x70
#define SW8_CS2 0x72
#define SW8_CS3 0x74
#define SW8_CS4 0x76
#define SW8_CS5 0x78
#define SW8_CS6 0x7A
#define SW8_CS7 0x7C
#define SW8_CS8 0x7E
#define I_1 0x80
#define I_2 0x82
#define I_3 0x84
#define I_4 0x86
#define I_5 0x88
#define I_6 0x8A
#define I_7 0x8C
#define I_8 0x8E
#define SW9_CS1 0x80
#define SW9_CS2 0x82
#define SW9_CS3 0x84
#define SW9_CS4 0x86
#define SW9_CS5 0x88
#define SW9_CS6 0x8A
#define SW9_CS7 0x8C
#define SW9_CS8 0x8E
#define J_1 0x90
#define J_2 0x92
#define J_3 0x94
#define J_4 0x96
#define J_5 0x98
#define J_6 0x9A
#define J_7 0x9C
#define J_8 0x9E
#define SW10_CS1 0x90
#define SW10_CS2 0x92
#define SW10_CS3 0x94
#define SW10_CS4 0x96
#define SW10_CS5 0x98
#define SW10_CS6 0x9A
#define SW10_CS7 0x9C
#define SW10_CS8 0x9E
#define K_1 0xA0
#define K_2 0xA2
#define K_3 0xA4
#define K_4 0xA6
#define K_5 0xA8
#define K_6 0xAA
#define K_7 0xAC
#define K_8 0xAE
#define SW11_CS1 0xA0
#define SW11_CS2 0xA2
#define SW11_CS3 0xA4
#define SW11_CS4 0xA6
#define SW11_CS5 0xA8
#define SW11_CS6 0xAA
#define SW11_CS7 0xAC
#define SW11_CS8 0xAE
#define L_1 0xB0
#define L_2 0xB2
#define L_3 0xB4
#define L_4 0xB6
#define L_5 0xB8
#define L_6 0xBA
#define L_7 0xBC
#define L_8 0xBE
#define SW12_CS1 0xB0
#define SW12_CS2 0xB2
#define SW12_CS3 0xB4
#define SW12_CS4 0xB6
#define SW12_CS5 0xB8
#define SW12_CS6 0xBA
#define SW12_CS7 0xBC
#define SW12_CS8 0xBE

View File

@ -19,6 +19,7 @@
#include "is31fl3737-mono.h"
#include "i2c_master.h"
#include "gpio.h"
#include "wait.h"
#define IS31FL3737_PWM_REGISTER_COUNT 192 // actually 144
@ -48,35 +49,55 @@
# define IS31FL3737_GLOBAL_CURRENT 0xFF
#endif
const uint8_t i2c_addresses[IS31FL3737_DRIVER_COUNT] = {
IS31FL3737_I2C_ADDRESS_1,
#ifdef IS31FL3737_I2C_ADDRESS_2
IS31FL3737_I2C_ADDRESS_2,
# ifdef IS31FL3737_I2C_ADDRESS_3
IS31FL3737_I2C_ADDRESS_3,
# ifdef IS31FL3737_I2C_ADDRESS_4
IS31FL3737_I2C_ADDRESS_4,
# endif
# endif
#endif
};
// These buffers match the IS31FL3737 PWM registers.
// The control buffers match the page 0 LED On/Off registers.
// Storing them like this is optimal for I2C transfers to the registers.
// We could optimize this and take out the unused registers from these
// buffers and the transfers in is31fl3737_write_pwm_buffer() but it's
// probably not worth the extra complexity.
typedef struct is31fl3737_driver_t {
uint8_t pwm_buffer[IS31FL3737_PWM_REGISTER_COUNT];
bool pwm_buffer_dirty;
uint8_t led_control_buffer[IS31FL3737_LED_CONTROL_REGISTER_COUNT];
bool led_control_buffer_dirty;
} PACKED is31fl3737_driver_t;
uint8_t g_pwm_buffer[IS31FL3737_DRIVER_COUNT][IS31FL3737_PWM_REGISTER_COUNT];
bool g_pwm_buffer_update_required[IS31FL3737_DRIVER_COUNT] = {false};
is31fl3737_driver_t driver_buffers[IS31FL3737_DRIVER_COUNT] = {{
.pwm_buffer = {0},
.pwm_buffer_dirty = false,
.led_control_buffer = {0},
.led_control_buffer_dirty = false,
}};
uint8_t g_led_control_registers[IS31FL3737_DRIVER_COUNT][IS31FL3737_LED_CONTROL_REGISTER_COUNT] = {0};
bool g_led_control_registers_update_required[IS31FL3737_DRIVER_COUNT] = {false};
void is31fl3737_write_register(uint8_t addr, uint8_t reg, uint8_t data) {
void is31fl3737_write_register(uint8_t index, uint8_t reg, uint8_t data) {
#if IS31FL3737_I2C_PERSISTENCE > 0
for (uint8_t i = 0; i < IS31FL3737_I2C_PERSISTENCE; i++) {
if (i2c_write_register(addr << 1, reg, &data, 1, IS31FL3737_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3737_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, reg, &data, 1, IS31FL3737_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3737_I2C_TIMEOUT);
#endif
}
void is31fl3737_select_page(uint8_t addr, uint8_t page) {
is31fl3737_write_register(addr, IS31FL3737_REG_COMMAND_WRITE_LOCK, IS31FL3737_COMMAND_WRITE_LOCK_MAGIC);
is31fl3737_write_register(addr, IS31FL3737_REG_COMMAND, page);
void is31fl3737_select_page(uint8_t index, uint8_t page) {
is31fl3737_write_register(index, IS31FL3737_REG_COMMAND_WRITE_LOCK, IS31FL3737_COMMAND_WRITE_LOCK_MAGIC);
is31fl3737_write_register(index, IS31FL3737_REG_COMMAND, page);
}
void is31fl3737_write_pwm_buffer(uint8_t addr, uint8_t index) {
void is31fl3737_write_pwm_buffer(uint8_t index) {
// Assumes page 1 is already selected.
// Transmit PWM registers in 12 transfers of 16 bytes.
@ -84,10 +105,10 @@ void is31fl3737_write_pwm_buffer(uint8_t addr, uint8_t index) {
for (uint8_t i = 0; i < IS31FL3737_PWM_REGISTER_COUNT; i += 16) {
#if IS31FL3737_I2C_PERSISTENCE > 0
for (uint8_t j = 0; j < IS31FL3737_I2C_PERSISTENCE; j++) {
if (i2c_write_register(addr << 1, i, g_pwm_buffer[index] + i, 16, IS31FL3737_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3737_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, i, g_pwm_buffer[index] + i, 16, IS31FL3737_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3737_I2C_TIMEOUT);
#endif
}
}
@ -95,64 +116,55 @@ void is31fl3737_write_pwm_buffer(uint8_t addr, uint8_t index) {
void is31fl3737_init_drivers(void) {
i2c_init();
is31fl3737_init(IS31FL3737_I2C_ADDRESS_1);
#if defined(IS31FL3737_I2C_ADDRESS_2)
is31fl3737_init(IS31FL3737_I2C_ADDRESS_2);
# if defined(IS31FL3737_I2C_ADDRESS_3)
is31fl3737_init(IS31FL3737_I2C_ADDRESS_3);
# if defined(IS31FL3737_I2C_ADDRESS_4)
is31fl3737_init(IS31FL3737_I2C_ADDRESS_4);
# endif
# endif
#if defined(IS31FL3737_SDB_PIN)
gpio_set_pin_output(IS31FL3737_SDB_PIN);
gpio_write_pin_high(IS31FL3737_SDB_PIN);
#endif
for (uint8_t i = 0; i < IS31FL3737_DRIVER_COUNT; i++) {
is31fl3737_init(i);
}
for (int i = 0; i < IS31FL3737_LED_COUNT; i++) {
is31fl3737_set_led_control_register(i, true);
}
is31fl3737_update_led_control_registers(IS31FL3737_I2C_ADDRESS_1, 0);
#if defined(IS31FL3737_I2C_ADDRESS_2)
is31fl3737_update_led_control_registers(IS31FL3737_I2C_ADDRESS_2, 1);
# if defined(IS31FL3737_I2C_ADDRESS_3)
is31fl3737_update_led_control_registers(IS31FL3737_I2C_ADDRESS_3, 2);
# if defined(IS31FL3737_I2C_ADDRESS_4)
is31fl3737_update_led_control_registers(IS31FL3737_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3737_DRIVER_COUNT; i++) {
is31fl3737_update_led_control_registers(i);
}
}
void is31fl3737_init(uint8_t addr) {
void is31fl3737_init(uint8_t index) {
// In order to avoid the LEDs being driven with garbage data
// in the LED driver's PWM registers, shutdown is enabled last.
// Set up the mode and other settings, clear the PWM registers,
// then disable software shutdown.
is31fl3737_select_page(addr, IS31FL3737_COMMAND_LED_CONTROL);
is31fl3737_select_page(index, IS31FL3737_COMMAND_LED_CONTROL);
// Turn off all LEDs.
for (uint8_t i = 0; i < IS31FL3737_LED_CONTROL_REGISTER_COUNT; i++) {
is31fl3737_write_register(addr, i, 0x00);
is31fl3737_write_register(index, i, 0x00);
}
is31fl3737_select_page(addr, IS31FL3737_COMMAND_PWM);
is31fl3737_select_page(index, IS31FL3737_COMMAND_PWM);
// Set PWM on all LEDs to 0
// No need to setup Breath registers to PWM as that is the default.
for (uint8_t i = 0; i < IS31FL3737_PWM_REGISTER_COUNT; i++) {
is31fl3737_write_register(addr, i, 0x00);
is31fl3737_write_register(index, i, 0x00);
}
is31fl3737_select_page(addr, IS31FL3737_COMMAND_FUNCTION);
is31fl3737_select_page(index, IS31FL3737_COMMAND_FUNCTION);
// Set de-ghost pull-up resistors (SWx)
is31fl3737_write_register(addr, IS31FL3737_FUNCTION_REG_SW_PULLUP, IS31FL3737_SW_PULLUP);
is31fl3737_write_register(index, IS31FL3737_FUNCTION_REG_SW_PULLUP, IS31FL3737_SW_PULLUP);
// Set de-ghost pull-down resistors (CSx)
is31fl3737_write_register(addr, IS31FL3737_FUNCTION_REG_CS_PULLDOWN, IS31FL3737_CS_PULLDOWN);
is31fl3737_write_register(index, IS31FL3737_FUNCTION_REG_CS_PULLDOWN, IS31FL3737_CS_PULLDOWN);
// Set global current to maximum.
is31fl3737_write_register(addr, IS31FL3737_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3737_GLOBAL_CURRENT);
is31fl3737_write_register(index, IS31FL3737_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3737_GLOBAL_CURRENT);
// Disable software shutdown.
is31fl3737_write_register(addr, IS31FL3737_FUNCTION_REG_CONFIGURATION, ((IS31FL3737_PWM_FREQUENCY & 0b111) << 3) | 0x01);
is31fl3737_write_register(index, IS31FL3737_FUNCTION_REG_CONFIGURATION, ((IS31FL3737_PWM_FREQUENCY & 0b111) << 3) | 0x01);
// Wait 10ms to ensure the device has woken up.
wait_ms(10);
@ -164,12 +176,12 @@ void is31fl3737_set_value(int index, uint8_t value) {
if (index >= 0 && index < IS31FL3737_LED_COUNT) {
memcpy_P(&led, (&g_is31fl3737_leds[index]), sizeof(led));
if (g_pwm_buffer[led.driver][led.v] == value) {
if (driver_buffers[led.driver].pwm_buffer[led.v] == value) {
return;
}
g_pwm_buffer[led.driver][led.v] = value;
g_pwm_buffer_update_required[led.driver] = true;
driver_buffers[led.driver].pwm_buffer[led.v] = value;
driver_buffers[led.driver].pwm_buffer_dirty = true;
}
}
@ -187,45 +199,38 @@ void is31fl3737_set_led_control_register(uint8_t index, bool value) {
uint8_t bit_value = led.v % 8;
if (value) {
g_led_control_registers[led.driver][control_register] |= (1 << bit_value);
driver_buffers[led.driver].led_control_buffer[control_register] |= (1 << bit_value);
} else {
g_led_control_registers[led.driver][control_register] &= ~(1 << bit_value);
driver_buffers[led.driver].led_control_buffer[control_register] &= ~(1 << bit_value);
}
g_led_control_registers_update_required[led.driver] = true;
driver_buffers[led.driver].led_control_buffer_dirty = true;
}
void is31fl3737_update_pwm_buffers(uint8_t addr, uint8_t index) {
if (g_pwm_buffer_update_required[index]) {
is31fl3737_select_page(addr, IS31FL3737_COMMAND_PWM);
void is31fl3737_update_pwm_buffers(uint8_t index) {
if (driver_buffers[index].pwm_buffer_dirty) {
is31fl3737_select_page(index, IS31FL3737_COMMAND_PWM);
is31fl3737_write_pwm_buffer(addr, index);
is31fl3737_write_pwm_buffer(index);
g_pwm_buffer_update_required[index] = false;
driver_buffers[index].pwm_buffer_dirty = false;
}
}
void is31fl3737_update_led_control_registers(uint8_t addr, uint8_t index) {
if (g_led_control_registers_update_required[index]) {
is31fl3737_select_page(addr, IS31FL3737_COMMAND_LED_CONTROL);
void is31fl3737_update_led_control_registers(uint8_t index) {
if (driver_buffers[index].led_control_buffer_dirty) {
is31fl3737_select_page(index, IS31FL3737_COMMAND_LED_CONTROL);
for (uint8_t i = 0; i < IS31FL3737_LED_CONTROL_REGISTER_COUNT; i++) {
is31fl3737_write_register(addr, i, g_led_control_registers[index][i]);
is31fl3737_write_register(index, i, driver_buffers[index].led_control_buffer[i]);
}
g_led_control_registers_update_required[index] = false;
driver_buffers[index].led_control_buffer_dirty = false;
}
}
void is31fl3737_flush(void) {
is31fl3737_update_pwm_buffers(IS31FL3737_I2C_ADDRESS_1, 0);
#if defined(IS31FL3737_I2C_ADDRESS_2)
is31fl3737_update_pwm_buffers(IS31FL3737_I2C_ADDRESS_2, 1);
# if defined(IS31FL3737_I2C_ADDRESS_3)
is31fl3737_update_pwm_buffers(IS31FL3737_I2C_ADDRESS_3, 2);
# if defined(IS31FL3737_I2C_ADDRESS_4)
is31fl3737_update_pwm_buffers(IS31FL3737_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3737_DRIVER_COUNT; i++) {
is31fl3737_update_pwm_buffers(i);
}
}

View File

@ -100,9 +100,9 @@ typedef struct is31fl3737_led_t {
extern const is31fl3737_led_t PROGMEM g_is31fl3737_leds[IS31FL3737_LED_COUNT];
void is31fl3737_init_drivers(void);
void is31fl3737_init(uint8_t addr);
void is31fl3737_write_register(uint8_t addr, uint8_t reg, uint8_t data);
void is31fl3737_select_page(uint8_t addr, uint8_t page);
void is31fl3737_init(uint8_t index);
void is31fl3737_write_register(uint8_t index, uint8_t reg, uint8_t data);
void is31fl3737_select_page(uint8_t index, uint8_t page);
void is31fl3737_set_value(int index, uint8_t value);
void is31fl3737_set_value_all(uint8_t value);
@ -113,8 +113,8 @@ void is31fl3737_set_led_control_register(uint8_t index, bool value);
// (eg. from a timer interrupt).
// Call this while idle (in between matrix scans).
// If the buffer is dirty, it will update the driver with the buffer.
void is31fl3737_update_pwm_buffers(uint8_t addr, uint8_t index);
void is31fl3737_update_led_control_registers(uint8_t addr, uint8_t index);
void is31fl3737_update_pwm_buffers(uint8_t index);
void is31fl3737_update_led_control_registers(uint8_t index);
void is31fl3737_flush(void);
@ -142,158 +142,158 @@ void is31fl3737_flush(void);
#define IS31FL3737_PWM_FREQUENCY_2K1_HZ 0b011
#define IS31FL3737_PWM_FREQUENCY_1K05_HZ 0b100
#define A_1 0x00
#define A_2 0x01
#define A_3 0x02
#define A_4 0x03
#define A_5 0x04
#define A_6 0x05
#define A_7 0x08
#define A_8 0x09
#define A_9 0x0A
#define A_10 0x0B
#define A_11 0x0C
#define A_12 0x0D
#define SW1_CS1 0x00
#define SW1_CS2 0x01
#define SW1_CS3 0x02
#define SW1_CS4 0x03
#define SW1_CS5 0x04
#define SW1_CS6 0x05
#define SW1_CS7 0x08
#define SW1_CS8 0x09
#define SW1_CS9 0x0A
#define SW1_CS10 0x0B
#define SW1_CS11 0x0C
#define SW1_CS12 0x0D
#define B_1 0x10
#define B_2 0x11
#define B_3 0x12
#define B_4 0x13
#define B_5 0x14
#define B_6 0x15
#define B_7 0x18
#define B_8 0x19
#define B_9 0x1A
#define B_10 0x1B
#define B_11 0x1C
#define B_12 0x1D
#define SW2_CS1 0x10
#define SW2_CS2 0x11
#define SW2_CS3 0x12
#define SW2_CS4 0x13
#define SW2_CS5 0x14
#define SW2_CS6 0x15
#define SW2_CS7 0x18
#define SW2_CS8 0x19
#define SW2_CS9 0x1A
#define SW2_CS10 0x1B
#define SW2_CS11 0x1C
#define SW2_CS12 0x1D
#define C_1 0x20
#define C_2 0x21
#define C_3 0x22
#define C_4 0x23
#define C_5 0x24
#define C_6 0x25
#define C_7 0x28
#define C_8 0x29
#define C_9 0x2A
#define C_10 0x2B
#define C_11 0x2C
#define C_12 0x2D
#define SW3_CS1 0x20
#define SW3_CS2 0x21
#define SW3_CS3 0x22
#define SW3_CS4 0x23
#define SW3_CS5 0x24
#define SW3_CS6 0x25
#define SW3_CS7 0x28
#define SW3_CS8 0x29
#define SW3_CS9 0x2A
#define SW3_CS10 0x2B
#define SW3_CS11 0x2C
#define SW3_CS12 0x2D
#define D_1 0x30
#define D_2 0x31
#define D_3 0x32
#define D_4 0x33
#define D_5 0x34
#define D_6 0x35
#define D_7 0x38
#define D_8 0x39
#define D_9 0x3A
#define D_10 0x3B
#define D_11 0x3C
#define D_12 0x3D
#define SW4_CS1 0x30
#define SW4_CS2 0x31
#define SW4_CS3 0x32
#define SW4_CS4 0x33
#define SW4_CS5 0x34
#define SW4_CS6 0x35
#define SW4_CS7 0x38
#define SW4_CS8 0x39
#define SW4_CS9 0x3A
#define SW4_CS10 0x3B
#define SW4_CS11 0x3C
#define SW4_CS12 0x3D
#define E_1 0x40
#define E_2 0x41
#define E_3 0x42
#define E_4 0x43
#define E_5 0x44
#define E_6 0x45
#define E_7 0x48
#define E_8 0x49
#define E_9 0x4A
#define E_10 0x4B
#define E_11 0x4C
#define E_12 0x4D
#define SW5_CS1 0x40
#define SW5_CS2 0x41
#define SW5_CS3 0x42
#define SW5_CS4 0x43
#define SW5_CS5 0x44
#define SW5_CS6 0x45
#define SW5_CS7 0x48
#define SW5_CS8 0x49
#define SW5_CS9 0x4A
#define SW5_CS10 0x4B
#define SW5_CS11 0x4C
#define SW5_CS12 0x4D
#define F_1 0x50
#define F_2 0x51
#define F_3 0x52
#define F_4 0x53
#define F_5 0x54
#define F_6 0x55
#define F_7 0x58
#define F_8 0x59
#define F_9 0x5A
#define F_10 0x5B
#define F_11 0x5C
#define F_12 0x5D
#define SW6_CS1 0x50
#define SW6_CS2 0x51
#define SW6_CS3 0x52
#define SW6_CS4 0x53
#define SW6_CS5 0x54
#define SW6_CS6 0x55
#define SW6_CS7 0x58
#define SW6_CS8 0x59
#define SW6_CS9 0x5A
#define SW6_CS10 0x5B
#define SW6_CS11 0x5C
#define SW6_CS12 0x5D
#define G_1 0x60
#define G_2 0x61
#define G_3 0x62
#define G_4 0x63
#define G_5 0x64
#define G_6 0x65
#define G_7 0x68
#define G_8 0x69
#define G_9 0x6A
#define G_10 0x6B
#define G_11 0x6C
#define G_12 0x6D
#define SW7_CS1 0x60
#define SW7_CS2 0x61
#define SW7_CS3 0x62
#define SW7_CS4 0x63
#define SW7_CS5 0x64
#define SW7_CS6 0x65
#define SW7_CS7 0x68
#define SW7_CS8 0x69
#define SW7_CS9 0x6A
#define SW7_CS10 0x6B
#define SW7_CS11 0x6C
#define SW7_CS12 0x6D
#define H_1 0x70
#define H_2 0x71
#define H_3 0x72
#define H_4 0x73
#define H_5 0x74
#define H_6 0x75
#define H_7 0x78
#define H_8 0x79
#define H_9 0x7A
#define H_10 0x7B
#define H_11 0x7C
#define H_12 0x7D
#define SW8_CS1 0x70
#define SW8_CS2 0x71
#define SW8_CS3 0x72
#define SW8_CS4 0x73
#define SW8_CS5 0x74
#define SW8_CS6 0x75
#define SW8_CS7 0x78
#define SW8_CS8 0x79
#define SW8_CS9 0x7A
#define SW8_CS10 0x7B
#define SW8_CS11 0x7C
#define SW8_CS12 0x7D
#define I_1 0x80
#define I_2 0x81
#define I_3 0x82
#define I_4 0x83
#define I_5 0x84
#define I_6 0x85
#define I_7 0x88
#define I_8 0x89
#define I_9 0x8A
#define I_10 0x8B
#define I_11 0x8C
#define I_12 0x8D
#define SW9_CS1 0x80
#define SW9_CS2 0x81
#define SW9_CS3 0x82
#define SW9_CS4 0x83
#define SW9_CS5 0x84
#define SW9_CS6 0x85
#define SW9_CS7 0x88
#define SW9_CS8 0x89
#define SW9_CS9 0x8A
#define SW9_CS10 0x8B
#define SW9_CS11 0x8C
#define SW9_CS12 0x8D
#define J_1 0x90
#define J_2 0x91
#define J_3 0x92
#define J_4 0x93
#define J_5 0x94
#define J_6 0x95
#define J_7 0x98
#define J_8 0x99
#define J_9 0x9A
#define J_10 0x9B
#define J_11 0x9C
#define J_12 0x9D
#define SW10_CS1 0x90
#define SW10_CS2 0x91
#define SW10_CS3 0x92
#define SW10_CS4 0x93
#define SW10_CS5 0x94
#define SW10_CS6 0x95
#define SW10_CS7 0x98
#define SW10_CS8 0x99
#define SW10_CS9 0x9A
#define SW10_CS10 0x9B
#define SW10_CS11 0x9C
#define SW10_CS12 0x9D
#define K_1 0xA0
#define K_2 0xA1
#define K_3 0xA2
#define K_4 0xA3
#define K_5 0xA4
#define K_6 0xA5
#define K_7 0xA8
#define K_8 0xA9
#define K_9 0xAA
#define K_10 0xAB
#define K_11 0xAC
#define K_12 0xAD
#define SW11_CS1 0xA0
#define SW11_CS2 0xA1
#define SW11_CS3 0xA2
#define SW11_CS4 0xA3
#define SW11_CS5 0xA4
#define SW11_CS6 0xA5
#define SW11_CS7 0xA8
#define SW11_CS8 0xA9
#define SW11_CS9 0xAA
#define SW11_CS10 0xAB
#define SW11_CS11 0xAC
#define SW11_CS12 0xAD
#define L_1 0xB0
#define L_2 0xB1
#define L_3 0xB2
#define L_4 0xB3
#define L_5 0xB4
#define L_6 0xB5
#define L_7 0xB8
#define L_8 0xB9
#define L_9 0xBA
#define L_10 0xBB
#define L_11 0xBC
#define L_12 0xBD
#define SW12_CS1 0xB0
#define SW12_CS2 0xB1
#define SW12_CS3 0xB2
#define SW12_CS4 0xB3
#define SW12_CS5 0xB4
#define SW12_CS6 0xB5
#define SW12_CS7 0xB8
#define SW12_CS8 0xB9
#define SW12_CS9 0xBA
#define SW12_CS10 0xBB
#define SW12_CS11 0xBC
#define SW12_CS12 0xBD

View File

@ -19,6 +19,7 @@
#include "is31fl3737.h"
#include "i2c_master.h"
#include "gpio.h"
#include "wait.h"
#define IS31FL3737_PWM_REGISTER_COUNT 192 // actually 144
@ -48,35 +49,55 @@
# define IS31FL3737_GLOBAL_CURRENT 0xFF
#endif
const uint8_t i2c_addresses[IS31FL3737_DRIVER_COUNT] = {
IS31FL3737_I2C_ADDRESS_1,
#ifdef IS31FL3737_I2C_ADDRESS_2
IS31FL3737_I2C_ADDRESS_2,
# ifdef IS31FL3737_I2C_ADDRESS_3
IS31FL3737_I2C_ADDRESS_3,
# ifdef IS31FL3737_I2C_ADDRESS_4
IS31FL3737_I2C_ADDRESS_4,
# endif
# endif
#endif
};
// These buffers match the IS31FL3737 PWM registers.
// The control buffers match the page 0 LED On/Off registers.
// Storing them like this is optimal for I2C transfers to the registers.
// We could optimize this and take out the unused registers from these
// buffers and the transfers in is31fl3737_write_pwm_buffer() but it's
// probably not worth the extra complexity.
typedef struct is31fl3737_driver_t {
uint8_t pwm_buffer[IS31FL3737_PWM_REGISTER_COUNT];
bool pwm_buffer_dirty;
uint8_t led_control_buffer[IS31FL3737_LED_CONTROL_REGISTER_COUNT];
bool led_control_buffer_dirty;
} PACKED is31fl3737_driver_t;
uint8_t g_pwm_buffer[IS31FL3737_DRIVER_COUNT][IS31FL3737_PWM_REGISTER_COUNT];
bool g_pwm_buffer_update_required[IS31FL3737_DRIVER_COUNT] = {false};
is31fl3737_driver_t driver_buffers[IS31FL3737_DRIVER_COUNT] = {{
.pwm_buffer = {0},
.pwm_buffer_dirty = false,
.led_control_buffer = {0},
.led_control_buffer_dirty = false,
}};
uint8_t g_led_control_registers[IS31FL3737_DRIVER_COUNT][IS31FL3737_LED_CONTROL_REGISTER_COUNT] = {0};
bool g_led_control_registers_update_required[IS31FL3737_DRIVER_COUNT] = {false};
void is31fl3737_write_register(uint8_t addr, uint8_t reg, uint8_t data) {
void is31fl3737_write_register(uint8_t index, uint8_t reg, uint8_t data) {
#if IS31FL3737_I2C_PERSISTENCE > 0
for (uint8_t i = 0; i < IS31FL3737_I2C_PERSISTENCE; i++) {
if (i2c_write_register(addr << 1, reg, &data, 1, IS31FL3737_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3737_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, reg, &data, 1, IS31FL3737_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3737_I2C_TIMEOUT);
#endif
}
void is31fl3737_select_page(uint8_t addr, uint8_t page) {
is31fl3737_write_register(addr, IS31FL3737_REG_COMMAND_WRITE_LOCK, IS31FL3737_COMMAND_WRITE_LOCK_MAGIC);
is31fl3737_write_register(addr, IS31FL3737_REG_COMMAND, page);
void is31fl3737_select_page(uint8_t index, uint8_t page) {
is31fl3737_write_register(index, IS31FL3737_REG_COMMAND_WRITE_LOCK, IS31FL3737_COMMAND_WRITE_LOCK_MAGIC);
is31fl3737_write_register(index, IS31FL3737_REG_COMMAND, page);
}
void is31fl3737_write_pwm_buffer(uint8_t addr, uint8_t index) {
void is31fl3737_write_pwm_buffer(uint8_t index) {
// Assumes page 1 is already selected.
// Transmit PWM registers in 12 transfers of 16 bytes.
@ -84,10 +105,10 @@ void is31fl3737_write_pwm_buffer(uint8_t addr, uint8_t index) {
for (uint8_t i = 0; i < IS31FL3737_PWM_REGISTER_COUNT; i += 16) {
#if IS31FL3737_I2C_PERSISTENCE > 0
for (uint8_t j = 0; j < IS31FL3737_I2C_PERSISTENCE; j++) {
if (i2c_write_register(addr << 1, i, g_pwm_buffer[index] + i, 16, IS31FL3737_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3737_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, i, g_pwm_buffer[index] + i, 16, IS31FL3737_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, IS31FL3737_I2C_TIMEOUT);
#endif
}
}
@ -95,64 +116,55 @@ void is31fl3737_write_pwm_buffer(uint8_t addr, uint8_t index) {
void is31fl3737_init_drivers(void) {
i2c_init();
is31fl3737_init(IS31FL3737_I2C_ADDRESS_1);
#if defined(IS31FL3737_I2C_ADDRESS_2)
is31fl3737_init(IS31FL3737_I2C_ADDRESS_2);
# if defined(IS31FL3737_I2C_ADDRESS_3)
is31fl3737_init(IS31FL3737_I2C_ADDRESS_3);
# if defined(IS31FL3737_I2C_ADDRESS_4)
is31fl3737_init(IS31FL3737_I2C_ADDRESS_4);
# endif
# endif
#if defined(IS31FL3737_SDB_PIN)
gpio_set_pin_output(IS31FL3737_SDB_PIN);
gpio_write_pin_high(IS31FL3737_SDB_PIN);
#endif
for (uint8_t i = 0; i < IS31FL3737_DRIVER_COUNT; i++) {
is31fl3737_init(i);
}
for (int i = 0; i < IS31FL3737_LED_COUNT; i++) {
is31fl3737_set_led_control_register(i, true, true, true);
}
is31fl3737_update_led_control_registers(IS31FL3737_I2C_ADDRESS_1, 0);
#if defined(IS31FL3737_I2C_ADDRESS_2)
is31fl3737_update_led_control_registers(IS31FL3737_I2C_ADDRESS_2, 1);
# if defined(IS31FL3737_I2C_ADDRESS_3)
is31fl3737_update_led_control_registers(IS31FL3737_I2C_ADDRESS_3, 2);
# if defined(IS31FL3737_I2C_ADDRESS_4)
is31fl3737_update_led_control_registers(IS31FL3737_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3737_DRIVER_COUNT; i++) {
is31fl3737_update_led_control_registers(i);
}
}
void is31fl3737_init(uint8_t addr) {
void is31fl3737_init(uint8_t index) {
// In order to avoid the LEDs being driven with garbage data
// in the LED driver's PWM registers, shutdown is enabled last.
// Set up the mode and other settings, clear the PWM registers,
// then disable software shutdown.
is31fl3737_select_page(addr, IS31FL3737_COMMAND_LED_CONTROL);
is31fl3737_select_page(index, IS31FL3737_COMMAND_LED_CONTROL);
// Turn off all LEDs.
for (uint8_t i = 0; i < IS31FL3737_LED_CONTROL_REGISTER_COUNT; i++) {
is31fl3737_write_register(addr, i, 0x00);
is31fl3737_write_register(index, i, 0x00);
}
is31fl3737_select_page(addr, IS31FL3737_COMMAND_PWM);
is31fl3737_select_page(index, IS31FL3737_COMMAND_PWM);
// Set PWM on all LEDs to 0
// No need to setup Breath registers to PWM as that is the default.
for (uint8_t i = 0; i < IS31FL3737_PWM_REGISTER_COUNT; i++) {
is31fl3737_write_register(addr, i, 0x00);
is31fl3737_write_register(index, i, 0x00);
}
is31fl3737_select_page(addr, IS31FL3737_COMMAND_FUNCTION);
is31fl3737_select_page(index, IS31FL3737_COMMAND_FUNCTION);
// Set de-ghost pull-up resistors (SWx)
is31fl3737_write_register(addr, IS31FL3737_FUNCTION_REG_SW_PULLUP, IS31FL3737_SW_PULLUP);
is31fl3737_write_register(index, IS31FL3737_FUNCTION_REG_SW_PULLUP, IS31FL3737_SW_PULLUP);
// Set de-ghost pull-down resistors (CSx)
is31fl3737_write_register(addr, IS31FL3737_FUNCTION_REG_CS_PULLDOWN, IS31FL3737_CS_PULLDOWN);
is31fl3737_write_register(index, IS31FL3737_FUNCTION_REG_CS_PULLDOWN, IS31FL3737_CS_PULLDOWN);
// Set global current to maximum.
is31fl3737_write_register(addr, IS31FL3737_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3737_GLOBAL_CURRENT);
is31fl3737_write_register(index, IS31FL3737_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3737_GLOBAL_CURRENT);
// Disable software shutdown.
is31fl3737_write_register(addr, IS31FL3737_FUNCTION_REG_CONFIGURATION, ((IS31FL3737_PWM_FREQUENCY & 0b111) << 3) | 0x01);
is31fl3737_write_register(index, IS31FL3737_FUNCTION_REG_CONFIGURATION, ((IS31FL3737_PWM_FREQUENCY & 0b111) << 3) | 0x01);
// Wait 10ms to ensure the device has woken up.
wait_ms(10);
@ -164,14 +176,14 @@ void is31fl3737_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
if (index >= 0 && index < IS31FL3737_LED_COUNT) {
memcpy_P(&led, (&g_is31fl3737_leds[index]), sizeof(led));
if (g_pwm_buffer[led.driver][led.r] == red && g_pwm_buffer[led.driver][led.g] == green && g_pwm_buffer[led.driver][led.b] == blue) {
if (driver_buffers[led.driver].pwm_buffer[led.r] == red && driver_buffers[led.driver].pwm_buffer[led.g] == green && driver_buffers[led.driver].pwm_buffer[led.b] == blue) {
return;
}
g_pwm_buffer[led.driver][led.r] = red;
g_pwm_buffer[led.driver][led.g] = green;
g_pwm_buffer[led.driver][led.b] = blue;
g_pwm_buffer_update_required[led.driver] = true;
driver_buffers[led.driver].pwm_buffer[led.r] = red;
driver_buffers[led.driver].pwm_buffer[led.g] = green;
driver_buffers[led.driver].pwm_buffer[led.b] = blue;
driver_buffers[led.driver].pwm_buffer_dirty = true;
}
}
@ -193,55 +205,48 @@ void is31fl3737_set_led_control_register(uint8_t index, bool red, bool green, bo
uint8_t bit_b = led.b % 8;
if (red) {
g_led_control_registers[led.driver][control_register_r] |= (1 << bit_r);
driver_buffers[led.driver].led_control_buffer[control_register_r] |= (1 << bit_r);
} else {
g_led_control_registers[led.driver][control_register_r] &= ~(1 << bit_r);
driver_buffers[led.driver].led_control_buffer[control_register_r] &= ~(1 << bit_r);
}
if (green) {
g_led_control_registers[led.driver][control_register_g] |= (1 << bit_g);
driver_buffers[led.driver].led_control_buffer[control_register_g] |= (1 << bit_g);
} else {
g_led_control_registers[led.driver][control_register_g] &= ~(1 << bit_g);
driver_buffers[led.driver].led_control_buffer[control_register_g] &= ~(1 << bit_g);
}
if (blue) {
g_led_control_registers[led.driver][control_register_b] |= (1 << bit_b);
driver_buffers[led.driver].led_control_buffer[control_register_b] |= (1 << bit_b);
} else {
g_led_control_registers[led.driver][control_register_b] &= ~(1 << bit_b);
driver_buffers[led.driver].led_control_buffer[control_register_b] &= ~(1 << bit_b);
}
g_led_control_registers_update_required[led.driver] = true;
driver_buffers[led.driver].led_control_buffer_dirty = true;
}
void is31fl3737_update_pwm_buffers(uint8_t addr, uint8_t index) {
if (g_pwm_buffer_update_required[index]) {
is31fl3737_select_page(addr, IS31FL3737_COMMAND_PWM);
void is31fl3737_update_pwm_buffers(uint8_t index) {
if (driver_buffers[index].pwm_buffer_dirty) {
is31fl3737_select_page(index, IS31FL3737_COMMAND_PWM);
is31fl3737_write_pwm_buffer(addr, index);
is31fl3737_write_pwm_buffer(index);
g_pwm_buffer_update_required[index] = false;
driver_buffers[index].pwm_buffer_dirty = false;
}
}
void is31fl3737_update_led_control_registers(uint8_t addr, uint8_t index) {
if (g_led_control_registers_update_required[index]) {
is31fl3737_select_page(addr, IS31FL3737_COMMAND_LED_CONTROL);
void is31fl3737_update_led_control_registers(uint8_t index) {
if (driver_buffers[index].led_control_buffer_dirty) {
is31fl3737_select_page(index, IS31FL3737_COMMAND_LED_CONTROL);
for (uint8_t i = 0; i < IS31FL3737_LED_CONTROL_REGISTER_COUNT; i++) {
is31fl3737_write_register(addr, i, g_led_control_registers[index][i]);
is31fl3737_write_register(index, i, driver_buffers[index].led_control_buffer[i]);
}
g_led_control_registers_update_required[index] = false;
driver_buffers[index].led_control_buffer_dirty = false;
}
}
void is31fl3737_flush(void) {
is31fl3737_update_pwm_buffers(IS31FL3737_I2C_ADDRESS_1, 0);
#if defined(IS31FL3737_I2C_ADDRESS_2)
is31fl3737_update_pwm_buffers(IS31FL3737_I2C_ADDRESS_2, 1);
# if defined(IS31FL3737_I2C_ADDRESS_3)
is31fl3737_update_pwm_buffers(IS31FL3737_I2C_ADDRESS_3, 2);
# if defined(IS31FL3737_I2C_ADDRESS_4)
is31fl3737_update_pwm_buffers(IS31FL3737_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3737_DRIVER_COUNT; i++) {
is31fl3737_update_pwm_buffers(i);
}
}

View File

@ -117,9 +117,9 @@ typedef struct is31fl3737_led_t {
extern const is31fl3737_led_t PROGMEM g_is31fl3737_leds[IS31FL3737_LED_COUNT];
void is31fl3737_init_drivers(void);
void is31fl3737_init(uint8_t addr);
void is31fl3737_write_register(uint8_t addr, uint8_t reg, uint8_t data);
void is31fl3737_select_page(uint8_t addr, uint8_t page);
void is31fl3737_init(uint8_t index);
void is31fl3737_write_register(uint8_t index, uint8_t reg, uint8_t data);
void is31fl3737_select_page(uint8_t index, uint8_t page);
void is31fl3737_set_color(int index, uint8_t red, uint8_t green, uint8_t blue);
void is31fl3737_set_color_all(uint8_t red, uint8_t green, uint8_t blue);
@ -130,8 +130,8 @@ void is31fl3737_set_led_control_register(uint8_t index, bool red, bool green, bo
// (eg. from a timer interrupt).
// Call this while idle (in between matrix scans).
// If the buffer is dirty, it will update the driver with the buffer.
void is31fl3737_update_pwm_buffers(uint8_t addr, uint8_t index);
void is31fl3737_update_led_control_registers(uint8_t addr, uint8_t index);
void is31fl3737_update_pwm_buffers(uint8_t index);
void is31fl3737_update_led_control_registers(uint8_t index);
void is31fl3737_flush(void);
@ -159,158 +159,316 @@ void is31fl3737_flush(void);
#define IS31FL3737_PWM_FREQUENCY_2K1_HZ 0b011
#define IS31FL3737_PWM_FREQUENCY_1K05_HZ 0b100
#define A_1 0x00
#define A_2 0x01
#define A_3 0x02
#define A_4 0x03
#define A_5 0x04
#define A_6 0x05
#define A_7 0x08
#define A_8 0x09
#define A_9 0x0A
#define A_10 0x0B
#define A_11 0x0C
#define A_12 0x0D
#define SW1_CS1 0x00
#define SW1_CS2 0x01
#define SW1_CS3 0x02
#define SW1_CS4 0x03
#define SW1_CS5 0x04
#define SW1_CS6 0x05
#define SW1_CS7 0x08
#define SW1_CS8 0x09
#define SW1_CS9 0x0A
#define SW1_CS10 0x0B
#define SW1_CS11 0x0C
#define SW1_CS12 0x0D
#define B_1 0x10
#define B_2 0x11
#define B_3 0x12
#define B_4 0x13
#define B_5 0x14
#define B_6 0x15
#define B_7 0x18
#define B_8 0x19
#define B_9 0x1A
#define B_10 0x1B
#define B_11 0x1C
#define B_12 0x1D
#define SW2_CS1 0x10
#define SW2_CS2 0x11
#define SW2_CS3 0x12
#define SW2_CS4 0x13
#define SW2_CS5 0x14
#define SW2_CS6 0x15
#define SW2_CS7 0x18
#define SW2_CS8 0x19
#define SW2_CS9 0x1A
#define SW2_CS10 0x1B
#define SW2_CS11 0x1C
#define SW2_CS12 0x1D
#define C_1 0x20
#define C_2 0x21
#define C_3 0x22
#define C_4 0x23
#define C_5 0x24
#define C_6 0x25
#define C_7 0x28
#define C_8 0x29
#define C_9 0x2A
#define C_10 0x2B
#define C_11 0x2C
#define C_12 0x2D
#define SW3_CS1 0x20
#define SW3_CS2 0x21
#define SW3_CS3 0x22
#define SW3_CS4 0x23
#define SW3_CS5 0x24
#define SW3_CS6 0x25
#define SW3_CS7 0x28
#define SW3_CS8 0x29
#define SW3_CS9 0x2A
#define SW3_CS10 0x2B
#define SW3_CS11 0x2C
#define SW3_CS12 0x2D
#define D_1 0x30
#define D_2 0x31
#define D_3 0x32
#define D_4 0x33
#define D_5 0x34
#define D_6 0x35
#define D_7 0x38
#define D_8 0x39
#define D_9 0x3A
#define D_10 0x3B
#define D_11 0x3C
#define D_12 0x3D
#define SW4_CS1 0x30
#define SW4_CS2 0x31
#define SW4_CS3 0x32
#define SW4_CS4 0x33
#define SW4_CS5 0x34
#define SW4_CS6 0x35
#define SW4_CS7 0x38
#define SW4_CS8 0x39
#define SW4_CS9 0x3A
#define SW4_CS10 0x3B
#define SW4_CS11 0x3C
#define SW4_CS12 0x3D
#define E_1 0x40
#define E_2 0x41
#define E_3 0x42
#define E_4 0x43
#define E_5 0x44
#define E_6 0x45
#define E_7 0x48
#define E_8 0x49
#define E_9 0x4A
#define E_10 0x4B
#define E_11 0x4C
#define E_12 0x4D
#define SW5_CS1 0x40
#define SW5_CS2 0x41
#define SW5_CS3 0x42
#define SW5_CS4 0x43
#define SW5_CS5 0x44
#define SW5_CS6 0x45
#define SW5_CS7 0x48
#define SW5_CS8 0x49
#define SW5_CS9 0x4A
#define SW5_CS10 0x4B
#define SW5_CS11 0x4C
#define SW5_CS12 0x4D
#define F_1 0x50
#define F_2 0x51
#define F_3 0x52
#define F_4 0x53
#define F_5 0x54
#define F_6 0x55
#define F_7 0x58
#define F_8 0x59
#define F_9 0x5A
#define F_10 0x5B
#define F_11 0x5C
#define F_12 0x5D
#define SW6_CS1 0x50
#define SW6_CS2 0x51
#define SW6_CS3 0x52
#define SW6_CS4 0x53
#define SW6_CS5 0x54
#define SW6_CS6 0x55
#define SW6_CS7 0x58
#define SW6_CS8 0x59
#define SW6_CS9 0x5A
#define SW6_CS10 0x5B
#define SW6_CS11 0x5C
#define SW6_CS12 0x5D
#define G_1 0x60
#define G_2 0x61
#define G_3 0x62
#define G_4 0x63
#define G_5 0x64
#define G_6 0x65
#define G_7 0x68
#define G_8 0x69
#define G_9 0x6A
#define G_10 0x6B
#define G_11 0x6C
#define G_12 0x6D
#define SW7_CS1 0x60
#define SW7_CS2 0x61
#define SW7_CS3 0x62
#define SW7_CS4 0x63
#define SW7_CS5 0x64
#define SW7_CS6 0x65
#define SW7_CS7 0x68
#define SW7_CS8 0x69
#define SW7_CS9 0x6A
#define SW7_CS10 0x6B
#define SW7_CS11 0x6C
#define SW7_CS12 0x6D
#define H_1 0x70
#define H_2 0x71
#define H_3 0x72
#define H_4 0x73
#define H_5 0x74
#define H_6 0x75
#define H_7 0x78
#define H_8 0x79
#define H_9 0x7A
#define H_10 0x7B
#define H_11 0x7C
#define H_12 0x7D
#define SW8_CS1 0x70
#define SW8_CS2 0x71
#define SW8_CS3 0x72
#define SW8_CS4 0x73
#define SW8_CS5 0x74
#define SW8_CS6 0x75
#define SW8_CS7 0x78
#define SW8_CS8 0x79
#define SW8_CS9 0x7A
#define SW8_CS10 0x7B
#define SW8_CS11 0x7C
#define SW8_CS12 0x7D
#define I_1 0x80
#define I_2 0x81
#define I_3 0x82
#define I_4 0x83
#define I_5 0x84
#define I_6 0x85
#define I_7 0x88
#define I_8 0x89
#define I_9 0x8A
#define I_10 0x8B
#define I_11 0x8C
#define I_12 0x8D
#define SW9_CS1 0x80
#define SW9_CS2 0x81
#define SW9_CS3 0x82
#define SW9_CS4 0x83
#define SW9_CS5 0x84
#define SW9_CS6 0x85
#define SW9_CS7 0x88
#define SW9_CS8 0x89
#define SW9_CS9 0x8A
#define SW9_CS10 0x8B
#define SW9_CS11 0x8C
#define SW9_CS12 0x8D
#define J_1 0x90
#define J_2 0x91
#define J_3 0x92
#define J_4 0x93
#define J_5 0x94
#define J_6 0x95
#define J_7 0x98
#define J_8 0x99
#define J_9 0x9A
#define J_10 0x9B
#define J_11 0x9C
#define J_12 0x9D
#define SW10_CS1 0x90
#define SW10_CS2 0x91
#define SW10_CS3 0x92
#define SW10_CS4 0x93
#define SW10_CS5 0x94
#define SW10_CS6 0x95
#define SW10_CS7 0x98
#define SW10_CS8 0x99
#define SW10_CS9 0x9A
#define SW10_CS10 0x9B
#define SW10_CS11 0x9C
#define SW10_CS12 0x9D
#define K_1 0xA0
#define K_2 0xA1
#define K_3 0xA2
#define K_4 0xA3
#define K_5 0xA4
#define K_6 0xA5
#define K_7 0xA8
#define K_8 0xA9
#define K_9 0xAA
#define K_10 0xAB
#define K_11 0xAC
#define K_12 0xAD
#define SW11_CS1 0xA0
#define SW11_CS2 0xA1
#define SW11_CS3 0xA2
#define SW11_CS4 0xA3
#define SW11_CS5 0xA4
#define SW11_CS6 0xA5
#define SW11_CS7 0xA8
#define SW11_CS8 0xA9
#define SW11_CS9 0xAA
#define SW11_CS10 0xAB
#define SW11_CS11 0xAC
#define SW11_CS12 0xAD
#define L_1 0xB0
#define L_2 0xB1
#define L_3 0xB2
#define L_4 0xB3
#define L_5 0xB4
#define L_6 0xB5
#define L_7 0xB8
#define L_8 0xB9
#define L_9 0xBA
#define L_10 0xBB
#define L_11 0xBC
#define L_12 0xBD
#define SW12_CS1 0xB0
#define SW12_CS2 0xB1
#define SW12_CS3 0xB2
#define SW12_CS4 0xB3
#define SW12_CS5 0xB4
#define SW12_CS6 0xB5
#define SW12_CS7 0xB8
#define SW12_CS8 0xB9
#define SW12_CS9 0xBA
#define SW12_CS10 0xBB
#define SW12_CS11 0xBC
#define SW12_CS12 0xBD
// DEPRECATED - DO NOT USE
#define A_1 SW1_CS1
#define A_2 SW1_CS2
#define A_3 SW1_CS3
#define A_4 SW1_CS4
#define A_5 SW1_CS5
#define A_6 SW1_CS6
#define A_7 SW1_CS7
#define A_8 SW1_CS8
#define A_9 SW1_CS9
#define A_10 SW1_CS10
#define A_11 SW1_CS11
#define A_12 SW1_CS12
#define B_1 SW2_CS1
#define B_2 SW2_CS2
#define B_3 SW2_CS3
#define B_4 SW2_CS4
#define B_5 SW2_CS5
#define B_6 SW2_CS6
#define B_7 SW2_CS7
#define B_8 SW2_CS8
#define B_9 SW2_CS9
#define B_10 SW2_CS10
#define B_11 SW2_CS11
#define B_12 SW2_CS12
#define C_1 SW3_CS1
#define C_2 SW3_CS2
#define C_3 SW3_CS3
#define C_4 SW3_CS4
#define C_5 SW3_CS5
#define C_6 SW3_CS6
#define C_7 SW3_CS7
#define C_8 SW3_CS8
#define C_9 SW3_CS9
#define C_10 SW3_CS10
#define C_11 SW3_CS11
#define C_12 SW3_CS12
#define D_1 SW4_CS1
#define D_2 SW4_CS2
#define D_3 SW4_CS3
#define D_4 SW4_CS4
#define D_5 SW4_CS5
#define D_6 SW4_CS6
#define D_7 SW4_CS7
#define D_8 SW4_CS8
#define D_9 SW4_CS9
#define D_10 SW4_CS10
#define D_11 SW4_CS11
#define D_12 SW4_CS12
#define E_1 SW5_CS1
#define E_2 SW5_CS2
#define E_3 SW5_CS3
#define E_4 SW5_CS4
#define E_5 SW5_CS5
#define E_6 SW5_CS6
#define E_7 SW5_CS7
#define E_8 SW5_CS8
#define E_9 SW5_CS9
#define E_10 SW5_CS10
#define E_11 SW5_CS11
#define E_12 SW5_CS12
#define F_1 SW6_CS1
#define F_2 SW6_CS2
#define F_3 SW6_CS3
#define F_4 SW6_CS4
#define F_5 SW6_CS5
#define F_6 SW6_CS6
#define F_7 SW6_CS7
#define F_8 SW6_CS8
#define F_9 SW6_CS9
#define F_10 SW6_CS10
#define F_11 SW6_CS11
#define F_12 SW6_CS12
#define G_1 SW7_CS1
#define G_2 SW7_CS2
#define G_3 SW7_CS3
#define G_4 SW7_CS4
#define G_5 SW7_CS5
#define G_6 SW7_CS6
#define G_7 SW7_CS7
#define G_8 SW7_CS8
#define G_9 SW7_CS9
#define G_10 SW7_CS10
#define G_11 SW7_CS11
#define G_12 SW7_CS12
#define H_1 SW8_CS1
#define H_2 SW8_CS2
#define H_3 SW8_CS3
#define H_4 SW8_CS4
#define H_5 SW8_CS5
#define H_6 SW8_CS6
#define H_7 SW8_CS7
#define H_8 SW8_CS8
#define H_9 SW8_CS9
#define H_10 SW8_CS10
#define H_11 SW8_CS11
#define H_12 SW8_CS12
#define I_1 SW9_CS1
#define I_2 SW9_CS2
#define I_3 SW9_CS3
#define I_4 SW9_CS4
#define I_5 SW9_CS5
#define I_6 SW9_CS6
#define I_7 SW9_CS7
#define I_8 SW9_CS8
#define I_9 SW9_CS9
#define I_10 SW9_CS10
#define I_11 SW9_CS11
#define I_12 SW9_CS12
#define J_1 SW10_CS1
#define J_2 SW10_CS2
#define J_3 SW10_CS3
#define J_4 SW10_CS4
#define J_5 SW10_CS5
#define J_6 SW10_CS6
#define J_7 SW10_CS7
#define J_8 SW10_CS8
#define J_9 SW10_CS9
#define J_10 SW10_CS10
#define J_11 SW10_CS11
#define J_12 SW10_CS12
#define K_1 SW11_CS1
#define K_2 SW11_CS2
#define K_3 SW11_CS3
#define K_4 SW11_CS4
#define K_5 SW11_CS5
#define K_6 SW11_CS6
#define K_7 SW11_CS7
#define K_8 SW11_CS8
#define K_9 SW11_CS9
#define K_10 SW11_CS10
#define K_11 SW11_CS11
#define K_12 SW11_CS12
#define L_1 SW12_CS1
#define L_2 SW12_CS2
#define L_3 SW12_CS3
#define L_4 SW12_CS4
#define L_5 SW12_CS5
#define L_6 SW12_CS6
#define L_7 SW12_CS7
#define L_8 SW12_CS8
#define L_9 SW12_CS9
#define L_10 SW12_CS10
#define L_11 SW12_CS11
#define L_12 SW12_CS12

View File

@ -19,10 +19,13 @@
#include "is31fl3741-mono.h"
#include "i2c_master.h"
#include "gpio.h"
#include "wait.h"
#define IS31FL3741_PWM_REGISTER_COUNT 351
#define IS31FL3741_SCALING_REGISTER_COUNT 351
#define IS31FL3741_PWM_0_REGISTER_COUNT 180
#define IS31FL3741_PWM_1_REGISTER_COUNT 171
#define IS31FL3741_SCALING_0_REGISTER_COUNT 180
#define IS31FL3741_SCALING_1_REGISTER_COUNT 171
#ifndef IS31FL3741_I2C_TIMEOUT
# define IS31FL3741_I2C_TIMEOUT 100
@ -52,127 +55,164 @@
# define IS31FL3741_GLOBAL_CURRENT 0xFF
#endif
const uint8_t i2c_addresses[IS31FL3741_DRIVER_COUNT] = {
IS31FL3741_I2C_ADDRESS_1,
#ifdef IS31FL3741_I2C_ADDRESS_2
IS31FL3741_I2C_ADDRESS_2,
# ifdef IS31FL3741_I2C_ADDRESS_3
IS31FL3741_I2C_ADDRESS_3,
# ifdef IS31FL3741_I2C_ADDRESS_4
IS31FL3741_I2C_ADDRESS_4,
# endif
# endif
#endif
};
// These buffers match the IS31FL3741 and IS31FL3741A PWM registers.
// The scaling buffers match the page 2 and 3 LED On/Off registers.
// Storing them like this is optimal for I2C transfers to the registers.
// We could optimize this and take out the unused registers from these
// buffers and the transfers in is31fl3741_write_pwm_buffer() but it's
// probably not worth the extra complexity.
uint8_t g_pwm_buffer[IS31FL3741_DRIVER_COUNT][IS31FL3741_PWM_REGISTER_COUNT];
bool g_pwm_buffer_update_required[IS31FL3741_DRIVER_COUNT] = {false};
bool g_scaling_registers_update_required[IS31FL3741_DRIVER_COUNT] = {false};
typedef struct is31fl3741_driver_t {
uint8_t pwm_buffer_0[IS31FL3741_PWM_0_REGISTER_COUNT];
uint8_t pwm_buffer_1[IS31FL3741_PWM_1_REGISTER_COUNT];
bool pwm_buffer_dirty;
uint8_t scaling_buffer_0[IS31FL3741_SCALING_0_REGISTER_COUNT];
uint8_t scaling_buffer_1[IS31FL3741_SCALING_1_REGISTER_COUNT];
bool scaling_buffer_dirty;
} PACKED is31fl3741_driver_t;
uint8_t g_scaling_registers[IS31FL3741_DRIVER_COUNT][IS31FL3741_SCALING_REGISTER_COUNT];
is31fl3741_driver_t driver_buffers[IS31FL3741_DRIVER_COUNT] = {{
.pwm_buffer_0 = {0},
.pwm_buffer_1 = {0},
.pwm_buffer_dirty = false,
.scaling_buffer_0 = {0},
.scaling_buffer_1 = {0},
.scaling_buffer_dirty = false,
}};
void is31fl3741_write_register(uint8_t addr, uint8_t reg, uint8_t data) {
void is31fl3741_write_register(uint8_t index, uint8_t reg, uint8_t data) {
#if IS31FL3741_I2C_PERSISTENCE > 0
for (uint8_t i = 0; i < IS31FL3741_I2C_PERSISTENCE; i++) {
if (i2c_write_register(addr << 1, reg, &data, 1, IS31FL3741_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3741_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, reg, &data, 1, IS31FL3741_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3741_I2C_TIMEOUT);
#endif
}
void is31fl3741_select_page(uint8_t addr, uint8_t page) {
is31fl3741_write_register(addr, IS31FL3741_REG_COMMAND_WRITE_LOCK, IS31FL3741_COMMAND_WRITE_LOCK_MAGIC);
is31fl3741_write_register(addr, IS31FL3741_REG_COMMAND, page);
void is31fl3741_select_page(uint8_t index, uint8_t page) {
is31fl3741_write_register(index, IS31FL3741_REG_COMMAND_WRITE_LOCK, IS31FL3741_COMMAND_WRITE_LOCK_MAGIC);
is31fl3741_write_register(index, IS31FL3741_REG_COMMAND, page);
}
void is31fl3741_write_pwm_buffer(uint8_t addr, uint8_t index) {
// Assume page 0 is already selected
void is31fl3741_write_pwm_buffer(uint8_t index) {
is31fl3741_select_page(index, IS31FL3741_COMMAND_PWM_0);
for (uint16_t i = 0; i < 342; i += 18) {
if (i == 180) {
is31fl3741_select_page(addr, IS31FL3741_COMMAND_PWM_1);
}
// Transmit PWM0 registers in 6 transfers of 30 bytes.
// Iterate over the pwm_buffer_0 contents at 30 byte intervals.
for (uint8_t i = 0; i < IS31FL3741_PWM_0_REGISTER_COUNT; i += 30) {
#if IS31FL3741_I2C_PERSISTENCE > 0
for (uint8_t j = 0; j < IS31FL3741_I2C_PERSISTENCE; j++) {
if (i2c_write_register(addr << 1, i % 180, g_pwm_buffer[index] + i, 18, IS31FL3741_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer_0 + i, 30, IS31FL3741_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, i % 180, g_pwm_buffer[index] + i, 18, IS31FL3741_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer_0 + i, 30, IS31FL3741_I2C_TIMEOUT);
#endif
}
// transfer the left cause the total number is 351
is31fl3741_select_page(index, IS31FL3741_COMMAND_PWM_1);
// Transmit PWM1 registers in 9 transfers of 19 bytes.
// Iterate over the pwm_buffer_1 contents at 19 byte intervals.
for (uint8_t i = 0; i < IS31FL3741_PWM_1_REGISTER_COUNT; i += 19) {
#if IS31FL3741_I2C_PERSISTENCE > 0
for (uint8_t i = 0; i < IS31FL3741_I2C_PERSISTENCE; i++) {
if (i2c_write_register(addr << 1, 162, g_pwm_buffer[index] + 342, 9, IS31FL3741_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
for (uint8_t i = 0; i < IS31FL3741_I2C_PERSISTENCE; i++) {
if (i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer_1 + i, 19, IS31FL3741_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, 162, g_pwm_buffer[index] + 342, 9, IS31FL3741_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer_1 + i, 19, IS31FL3741_I2C_TIMEOUT);
#endif
}
}
void is31fl3741_init_drivers(void) {
i2c_init();
is31fl3741_init(IS31FL3741_I2C_ADDRESS_1);
#if defined(IS31FL3741_I2C_ADDRESS_2)
is31fl3741_init(IS31FL3741_I2C_ADDRESS_2);
# if defined(IS31FL3741_I2C_ADDRESS_3)
is31fl3741_init(IS31FL3741_I2C_ADDRESS_3);
# if defined(IS31FL3741_I2C_ADDRESS_4)
is31fl3741_init(IS31FL3741_I2C_ADDRESS_4);
# endif
# endif
#if defined(IS31FL3741_SDB_PIN)
gpio_set_pin_output(IS31FL3741_SDB_PIN);
gpio_write_pin_high(IS31FL3741_SDB_PIN);
#endif
for (uint8_t i = 0; i < IS31FL3741_DRIVER_COUNT; i++) {
is31fl3741_init(i);
}
for (int i = 0; i < IS31FL3741_LED_COUNT; i++) {
is31fl3741_set_led_control_register(i, true);
}
is31fl3741_update_led_control_registers(IS31FL3741_I2C_ADDRESS_1, 0);
#if defined(IS31FL3741_I2C_ADDRESS_2)
is31fl3741_update_led_control_registers(IS31FL3741_I2C_ADDRESS_2, 1);
# if defined(IS31FL3741_I2C_ADDRESS_3)
is31fl3741_update_led_control_registers(IS31FL3741_I2C_ADDRESS_3, 2);
# if defined(IS31FL3741_I2C_ADDRESS_4)
is31fl3741_update_led_control_registers(IS31FL3741_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3741_DRIVER_COUNT; i++) {
is31fl3741_update_led_control_registers(i);
}
}
void is31fl3741_init(uint8_t addr) {
void is31fl3741_init(uint8_t index) {
// In order to avoid the LEDs being driven with garbage data
// in the LED driver's PWM registers, shutdown is enabled last.
// Set up the mode and other settings, clear the PWM registers,
// then disable software shutdown.
// Unlock the command register.
is31fl3741_select_page(addr, IS31FL3741_COMMAND_FUNCTION);
is31fl3741_select_page(index, IS31FL3741_COMMAND_FUNCTION);
// Set to Normal operation
is31fl3741_write_register(addr, IS31FL3741_FUNCTION_REG_CONFIGURATION, IS31FL3741_CONFIGURATION);
is31fl3741_write_register(index, IS31FL3741_FUNCTION_REG_CONFIGURATION, IS31FL3741_CONFIGURATION);
// Set Golbal Current Control Register
is31fl3741_write_register(addr, IS31FL3741_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3741_GLOBAL_CURRENT);
is31fl3741_write_register(index, IS31FL3741_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3741_GLOBAL_CURRENT);
// Set Pull up & Down for SWx CSy
is31fl3741_write_register(addr, IS31FL3741_FUNCTION_REG_PULLDOWNUP, ((IS31FL3741_CS_PULLDOWN << 4) | IS31FL3741_SW_PULLUP));
is31fl3741_write_register(index, IS31FL3741_FUNCTION_REG_PULLDOWNUP, ((IS31FL3741_CS_PULLDOWN << 4) | IS31FL3741_SW_PULLUP));
// Set PWM frequency
is31fl3741_write_register(addr, IS31FL3741_FUNCTION_REG_PWM_FREQUENCY, (IS31FL3741_PWM_FREQUENCY & 0b1111));
is31fl3741_write_register(index, IS31FL3741_FUNCTION_REG_PWM_FREQUENCY, (IS31FL3741_PWM_FREQUENCY & 0b1111));
// is31fl3741_update_led_scaling_registers(addr, 0xFF, 0xFF, 0xFF);
// is31fl3741_update_led_scaling_registers(index, 0xFF, 0xFF, 0xFF);
// Wait 10ms to ensure the device has woken up.
wait_ms(10);
}
uint8_t get_pwm_value(uint8_t driver, uint16_t reg) {
if (reg & 0x100) {
return driver_buffers[driver].pwm_buffer_1[reg & 0xFF];
} else {
return driver_buffers[driver].pwm_buffer_0[reg];
}
}
void set_pwm_value(uint8_t driver, uint16_t reg, uint8_t value) {
if (reg & 0x100) {
driver_buffers[driver].pwm_buffer_1[reg & 0xFF] = value;
} else {
driver_buffers[driver].pwm_buffer_0[reg] = value;
}
}
void is31fl3741_set_value(int index, uint8_t value) {
is31fl3741_led_t led;
if (index >= 0 && index < IS31FL3741_LED_COUNT) {
memcpy_P(&led, (&g_is31fl3741_leds[index]), sizeof(led));
if (g_pwm_buffer[led.driver][led.v] == value) {
if (get_pwm_value(led.driver, led.v) == value) {
return;
}
g_pwm_buffer_update_required[led.driver] = true;
g_pwm_buffer[led.driver][led.v] = value;
set_pwm_value(led.driver, led.v, value);
driver_buffers[led.driver].pwm_buffer_dirty = true;
}
}
@ -182,68 +222,61 @@ void is31fl3741_set_value_all(uint8_t value) {
}
}
void set_scaling_value(uint8_t driver, uint16_t reg, uint8_t value) {
if (reg & 0x100) {
driver_buffers[driver].scaling_buffer_1[reg & 0xFF] = value;
} else {
driver_buffers[driver].scaling_buffer_0[reg] = value;
}
}
void is31fl3741_set_led_control_register(uint8_t index, bool value) {
is31fl3741_led_t led;
memcpy_P(&led, (&g_is31fl3741_leds[index]), sizeof(led));
if (value) {
g_scaling_registers[led.driver][led.v] = 0xFF;
} else {
g_scaling_registers[led.driver][led.v] = 0x00;
}
set_scaling_value(led.driver, led.v, value ? 0xFF : 0x00);
g_scaling_registers_update_required[led.driver] = true;
driver_buffers[led.driver].scaling_buffer_dirty = true;
}
void is31fl3741_update_pwm_buffers(uint8_t addr, uint8_t index) {
if (g_pwm_buffer_update_required[index]) {
is31fl3741_select_page(addr, IS31FL3741_COMMAND_PWM_0);
void is31fl3741_update_pwm_buffers(uint8_t index) {
if (driver_buffers[index].pwm_buffer_dirty) {
is31fl3741_write_pwm_buffer(index);
is31fl3741_write_pwm_buffer(addr, index);
g_pwm_buffer_update_required[index] = false;
driver_buffers[index].pwm_buffer_dirty = false;
}
}
void is31fl3741_set_pwm_buffer(const is31fl3741_led_t *pled, uint8_t value) {
g_pwm_buffer[pled->driver][pled->v] = value;
g_pwm_buffer_update_required[pled->driver] = true;
set_pwm_value(pled->driver, pled->v, value);
driver_buffers[pled->driver].pwm_buffer_dirty = true;
}
void is31fl3741_update_led_control_registers(uint8_t addr, uint8_t index) {
if (g_scaling_registers_update_required[index]) {
is31fl3741_select_page(addr, IS31FL3741_COMMAND_SCALING_0);
void is31fl3741_update_led_control_registers(uint8_t index) {
if (driver_buffers[index].scaling_buffer_dirty) {
is31fl3741_select_page(index, IS31FL3741_COMMAND_SCALING_0);
// CS1_SW1 to CS30_SW6 are on page 2
for (int i = CS1_SW1; i <= CS30_SW6; ++i) {
is31fl3741_write_register(addr, i, g_scaling_registers[index][i]);
for (uint8_t i = 0; i < IS31FL3741_SCALING_0_REGISTER_COUNT; i++) {
is31fl3741_write_register(index, i, driver_buffers[index].scaling_buffer_0[i]);
}
is31fl3741_select_page(addr, IS31FL3741_COMMAND_SCALING_1);
is31fl3741_select_page(index, IS31FL3741_COMMAND_SCALING_1);
// CS1_SW7 to CS39_SW9 are on page 3
for (int i = CS1_SW7; i <= CS39_SW9; ++i) {
is31fl3741_write_register(addr, i - CS1_SW7, g_scaling_registers[index][i]);
for (uint8_t i = 0; i < IS31FL3741_SCALING_1_REGISTER_COUNT; i++) {
is31fl3741_write_register(index, i, driver_buffers[index].scaling_buffer_1[i]);
}
g_scaling_registers_update_required[index] = false;
driver_buffers[index].scaling_buffer_dirty = false;
}
}
void is31fl3741_set_scaling_registers(const is31fl3741_led_t *pled, uint8_t value) {
g_scaling_registers[pled->driver][pled->v] = value;
g_scaling_registers_update_required[pled->driver] = true;
set_scaling_value(pled->driver, pled->v, value);
driver_buffers[pled->driver].scaling_buffer_dirty = true;
}
void is31fl3741_flush(void) {
is31fl3741_update_pwm_buffers(IS31FL3741_I2C_ADDRESS_1, 0);
#if defined(IS31FL3741_I2C_ADDRESS_2)
is31fl3741_update_pwm_buffers(IS31FL3741_I2C_ADDRESS_2, 1);
# if defined(IS31FL3741_I2C_ADDRESS_3)
is31fl3741_update_pwm_buffers(IS31FL3741_I2C_ADDRESS_3, 2);
# if defined(IS31FL3741_I2C_ADDRESS_4)
is31fl3741_update_pwm_buffers(IS31FL3741_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3741_DRIVER_COUNT; i++) {
is31fl3741_update_pwm_buffers(i);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -19,10 +19,13 @@
#include "is31fl3741.h"
#include "i2c_master.h"
#include "gpio.h"
#include "wait.h"
#define IS31FL3741_PWM_REGISTER_COUNT 351
#define IS31FL3741_SCALING_REGISTER_COUNT 351
#define IS31FL3741_PWM_0_REGISTER_COUNT 180
#define IS31FL3741_PWM_1_REGISTER_COUNT 171
#define IS31FL3741_SCALING_0_REGISTER_COUNT 180
#define IS31FL3741_SCALING_1_REGISTER_COUNT 171
#ifndef IS31FL3741_I2C_TIMEOUT
# define IS31FL3741_I2C_TIMEOUT 100
@ -52,129 +55,166 @@
# define IS31FL3741_GLOBAL_CURRENT 0xFF
#endif
const uint8_t i2c_addresses[IS31FL3741_DRIVER_COUNT] = {
IS31FL3741_I2C_ADDRESS_1,
#ifdef IS31FL3741_I2C_ADDRESS_2
IS31FL3741_I2C_ADDRESS_2,
# ifdef IS31FL3741_I2C_ADDRESS_3
IS31FL3741_I2C_ADDRESS_3,
# ifdef IS31FL3741_I2C_ADDRESS_4
IS31FL3741_I2C_ADDRESS_4,
# endif
# endif
#endif
};
// These buffers match the IS31FL3741 and IS31FL3741A PWM registers.
// The scaling buffers match the page 2 and 3 LED On/Off registers.
// Storing them like this is optimal for I2C transfers to the registers.
// We could optimize this and take out the unused registers from these
// buffers and the transfers in is31fl3741_write_pwm_buffer() but it's
// probably not worth the extra complexity.
uint8_t g_pwm_buffer[IS31FL3741_DRIVER_COUNT][IS31FL3741_PWM_REGISTER_COUNT];
bool g_pwm_buffer_update_required[IS31FL3741_DRIVER_COUNT] = {false};
bool g_scaling_registers_update_required[IS31FL3741_DRIVER_COUNT] = {false};
typedef struct is31fl3741_driver_t {
uint8_t pwm_buffer_0[IS31FL3741_PWM_0_REGISTER_COUNT];
uint8_t pwm_buffer_1[IS31FL3741_PWM_1_REGISTER_COUNT];
bool pwm_buffer_dirty;
uint8_t scaling_buffer_0[IS31FL3741_SCALING_0_REGISTER_COUNT];
uint8_t scaling_buffer_1[IS31FL3741_SCALING_1_REGISTER_COUNT];
bool scaling_buffer_dirty;
} PACKED is31fl3741_driver_t;
uint8_t g_scaling_registers[IS31FL3741_DRIVER_COUNT][IS31FL3741_SCALING_REGISTER_COUNT];
is31fl3741_driver_t driver_buffers[IS31FL3741_DRIVER_COUNT] = {{
.pwm_buffer_0 = {0},
.pwm_buffer_1 = {0},
.pwm_buffer_dirty = false,
.scaling_buffer_0 = {0},
.scaling_buffer_1 = {0},
.scaling_buffer_dirty = false,
}};
void is31fl3741_write_register(uint8_t addr, uint8_t reg, uint8_t data) {
void is31fl3741_write_register(uint8_t index, uint8_t reg, uint8_t data) {
#if IS31FL3741_I2C_PERSISTENCE > 0
for (uint8_t i = 0; i < IS31FL3741_I2C_PERSISTENCE; i++) {
if (i2c_write_register(addr << 1, reg, &data, 1, IS31FL3741_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3741_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, reg, &data, 1, IS31FL3741_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3741_I2C_TIMEOUT);
#endif
}
void is31fl3741_select_page(uint8_t addr, uint8_t page) {
is31fl3741_write_register(addr, IS31FL3741_REG_COMMAND_WRITE_LOCK, IS31FL3741_COMMAND_WRITE_LOCK_MAGIC);
is31fl3741_write_register(addr, IS31FL3741_REG_COMMAND, page);
void is31fl3741_select_page(uint8_t index, uint8_t page) {
is31fl3741_write_register(index, IS31FL3741_REG_COMMAND_WRITE_LOCK, IS31FL3741_COMMAND_WRITE_LOCK_MAGIC);
is31fl3741_write_register(index, IS31FL3741_REG_COMMAND, page);
}
void is31fl3741_write_pwm_buffer(uint8_t addr, uint8_t index) {
// Assume page 0 is already selected
void is31fl3741_write_pwm_buffer(uint8_t index) {
is31fl3741_select_page(index, IS31FL3741_COMMAND_PWM_0);
for (uint16_t i = 0; i < 342; i += 18) {
if (i == 180) {
is31fl3741_select_page(addr, IS31FL3741_COMMAND_PWM_1);
}
// Transmit PWM0 registers in 6 transfers of 30 bytes.
// Iterate over the pwm_buffer_0 contents at 30 byte intervals.
for (uint8_t i = 0; i < IS31FL3741_PWM_0_REGISTER_COUNT; i += 30) {
#if IS31FL3741_I2C_PERSISTENCE > 0
for (uint8_t j = 0; j < IS31FL3741_I2C_PERSISTENCE; j++) {
if (i2c_write_register(addr << 1, i % 180, g_pwm_buffer[index] + i, 18, IS31FL3741_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer_0 + i, 30, IS31FL3741_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, i % 180, g_pwm_buffer[index] + i, 18, IS31FL3741_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer_0 + i, 30, IS31FL3741_I2C_TIMEOUT);
#endif
}
// transfer the left cause the total number is 351
is31fl3741_select_page(index, IS31FL3741_COMMAND_PWM_1);
// Transmit PWM1 registers in 9 transfers of 19 bytes.
// Iterate over the pwm_buffer_1 contents at 19 byte intervals.
for (uint8_t i = 0; i < IS31FL3741_PWM_1_REGISTER_COUNT; i += 19) {
#if IS31FL3741_I2C_PERSISTENCE > 0
for (uint8_t i = 0; i < IS31FL3741_I2C_PERSISTENCE; i++) {
if (i2c_write_register(addr << 1, 162, g_pwm_buffer[index] + 342, 9, IS31FL3741_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
for (uint8_t i = 0; i < IS31FL3741_I2C_PERSISTENCE; i++) {
if (i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer_1 + i, 19, IS31FL3741_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, 162, g_pwm_buffer[index] + 342, 9, IS31FL3741_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer_1 + i, 19, IS31FL3741_I2C_TIMEOUT);
#endif
}
}
void is31fl3741_init_drivers(void) {
i2c_init();
is31fl3741_init(IS31FL3741_I2C_ADDRESS_1);
#if defined(IS31FL3741_I2C_ADDRESS_2)
is31fl3741_init(IS31FL3741_I2C_ADDRESS_2);
# if defined(IS31FL3741_I2C_ADDRESS_3)
is31fl3741_init(IS31FL3741_I2C_ADDRESS_3);
# if defined(IS31FL3741_I2C_ADDRESS_4)
is31fl3741_init(IS31FL3741_I2C_ADDRESS_4);
# endif
# endif
#if defined(IS31FL3741_SDB_PIN)
gpio_set_pin_output(IS31FL3741_SDB_PIN);
gpio_write_pin_high(IS31FL3741_SDB_PIN);
#endif
for (uint8_t i = 0; i < IS31FL3741_DRIVER_COUNT; i++) {
is31fl3741_init(i);
}
for (int i = 0; i < IS31FL3741_LED_COUNT; i++) {
is31fl3741_set_led_control_register(i, true, true, true);
}
is31fl3741_update_led_control_registers(IS31FL3741_I2C_ADDRESS_1, 0);
#if defined(IS31FL3741_I2C_ADDRESS_2)
is31fl3741_update_led_control_registers(IS31FL3741_I2C_ADDRESS_2, 1);
# if defined(IS31FL3741_I2C_ADDRESS_3)
is31fl3741_update_led_control_registers(IS31FL3741_I2C_ADDRESS_3, 2);
# if defined(IS31FL3741_I2C_ADDRESS_4)
is31fl3741_update_led_control_registers(IS31FL3741_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3741_DRIVER_COUNT; i++) {
is31fl3741_update_led_control_registers(i);
}
}
void is31fl3741_init(uint8_t addr) {
void is31fl3741_init(uint8_t index) {
// In order to avoid the LEDs being driven with garbage data
// in the LED driver's PWM registers, shutdown is enabled last.
// Set up the mode and other settings, clear the PWM registers,
// then disable software shutdown.
// Unlock the command register.
is31fl3741_select_page(addr, IS31FL3741_COMMAND_FUNCTION);
is31fl3741_select_page(index, IS31FL3741_COMMAND_FUNCTION);
// Set to Normal operation
is31fl3741_write_register(addr, IS31FL3741_FUNCTION_REG_CONFIGURATION, IS31FL3741_CONFIGURATION);
is31fl3741_write_register(index, IS31FL3741_FUNCTION_REG_CONFIGURATION, IS31FL3741_CONFIGURATION);
// Set Golbal Current Control Register
is31fl3741_write_register(addr, IS31FL3741_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3741_GLOBAL_CURRENT);
is31fl3741_write_register(index, IS31FL3741_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3741_GLOBAL_CURRENT);
// Set Pull up & Down for SWx CSy
is31fl3741_write_register(addr, IS31FL3741_FUNCTION_REG_PULLDOWNUP, ((IS31FL3741_CS_PULLDOWN << 4) | IS31FL3741_SW_PULLUP));
is31fl3741_write_register(index, IS31FL3741_FUNCTION_REG_PULLDOWNUP, ((IS31FL3741_CS_PULLDOWN << 4) | IS31FL3741_SW_PULLUP));
// Set PWM frequency
is31fl3741_write_register(addr, IS31FL3741_FUNCTION_REG_PWM_FREQUENCY, (IS31FL3741_PWM_FREQUENCY & 0b1111));
is31fl3741_write_register(index, IS31FL3741_FUNCTION_REG_PWM_FREQUENCY, (IS31FL3741_PWM_FREQUENCY & 0b1111));
// is31fl3741_update_led_scaling_registers(addr, 0xFF, 0xFF, 0xFF);
// is31fl3741_update_led_scaling_registers(index, 0xFF, 0xFF, 0xFF);
// Wait 10ms to ensure the device has woken up.
wait_ms(10);
}
uint8_t get_pwm_value(uint8_t driver, uint16_t reg) {
if (reg & 0x100) {
return driver_buffers[driver].pwm_buffer_1[reg & 0xFF];
} else {
return driver_buffers[driver].pwm_buffer_0[reg];
}
}
void set_pwm_value(uint8_t driver, uint16_t reg, uint8_t value) {
if (reg & 0x100) {
driver_buffers[driver].pwm_buffer_1[reg & 0xFF] = value;
} else {
driver_buffers[driver].pwm_buffer_0[reg] = value;
}
}
void is31fl3741_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
is31fl3741_led_t led;
if (index >= 0 && index < IS31FL3741_LED_COUNT) {
memcpy_P(&led, (&g_is31fl3741_leds[index]), sizeof(led));
if (g_pwm_buffer[led.driver][led.r] == red && g_pwm_buffer[led.driver][led.g] == green && g_pwm_buffer[led.driver][led.b] == blue) {
if (get_pwm_value(led.driver, led.r) == red && get_pwm_value(led.driver, led.g) == green && get_pwm_value(led.driver, led.b) == blue) {
return;
}
g_pwm_buffer_update_required[led.driver] = true;
g_pwm_buffer[led.driver][led.r] = red;
g_pwm_buffer[led.driver][led.g] = green;
g_pwm_buffer[led.driver][led.b] = blue;
set_pwm_value(led.driver, led.r, red);
set_pwm_value(led.driver, led.g, green);
set_pwm_value(led.driver, led.b, blue);
driver_buffers[led.driver].pwm_buffer_dirty = true;
}
}
@ -184,84 +224,67 @@ void is31fl3741_set_color_all(uint8_t red, uint8_t green, uint8_t blue) {
}
}
void set_scaling_value(uint8_t driver, uint16_t reg, uint8_t value) {
if (reg & 0x100) {
driver_buffers[driver].scaling_buffer_1[reg & 0xFF] = value;
} else {
driver_buffers[driver].scaling_buffer_0[reg] = value;
}
}
void is31fl3741_set_led_control_register(uint8_t index, bool red, bool green, bool blue) {
is31fl3741_led_t led;
memcpy_P(&led, (&g_is31fl3741_leds[index]), sizeof(led));
if (red) {
g_scaling_registers[led.driver][led.r] = 0xFF;
} else {
g_scaling_registers[led.driver][led.r] = 0x00;
}
set_scaling_value(led.driver, led.r, red ? 0xFF : 0x00);
set_scaling_value(led.driver, led.g, green ? 0xFF : 0x00);
set_scaling_value(led.driver, led.b, blue ? 0xFF : 0x00);
if (green) {
g_scaling_registers[led.driver][led.g] = 0xFF;
} else {
g_scaling_registers[led.driver][led.g] = 0x00;
}
if (blue) {
g_scaling_registers[led.driver][led.b] = 0xFF;
} else {
g_scaling_registers[led.driver][led.b] = 0x00;
}
g_scaling_registers_update_required[led.driver] = true;
driver_buffers[led.driver].scaling_buffer_dirty = true;
}
void is31fl3741_update_pwm_buffers(uint8_t addr, uint8_t index) {
if (g_pwm_buffer_update_required[index]) {
is31fl3741_select_page(addr, IS31FL3741_COMMAND_PWM_0);
void is31fl3741_update_pwm_buffers(uint8_t index) {
if (driver_buffers[index].pwm_buffer_dirty) {
is31fl3741_write_pwm_buffer(index);
is31fl3741_write_pwm_buffer(addr, index);
g_pwm_buffer_update_required[index] = false;
driver_buffers[index].pwm_buffer_dirty = false;
}
}
void is31fl3741_set_pwm_buffer(const is31fl3741_led_t *pled, uint8_t red, uint8_t green, uint8_t blue) {
g_pwm_buffer[pled->driver][pled->r] = red;
g_pwm_buffer[pled->driver][pled->g] = green;
g_pwm_buffer[pled->driver][pled->b] = blue;
g_pwm_buffer_update_required[pled->driver] = true;
set_pwm_value(pled->driver, pled->r, red);
set_pwm_value(pled->driver, pled->g, green);
set_pwm_value(pled->driver, pled->b, blue);
driver_buffers[pled->driver].pwm_buffer_dirty = true;
}
void is31fl3741_update_led_control_registers(uint8_t addr, uint8_t index) {
if (g_scaling_registers_update_required[index]) {
is31fl3741_select_page(addr, IS31FL3741_COMMAND_SCALING_0);
void is31fl3741_update_led_control_registers(uint8_t index) {
if (driver_buffers[index].scaling_buffer_dirty) {
is31fl3741_select_page(index, IS31FL3741_COMMAND_SCALING_0);
// CS1_SW1 to CS30_SW6 are on page 2
for (int i = CS1_SW1; i <= CS30_SW6; ++i) {
is31fl3741_write_register(addr, i, g_scaling_registers[index][i]);
for (uint8_t i = 0; i < IS31FL3741_SCALING_0_REGISTER_COUNT; i++) {
is31fl3741_write_register(index, i, driver_buffers[index].scaling_buffer_0[i]);
}
is31fl3741_select_page(addr, IS31FL3741_COMMAND_SCALING_1);
is31fl3741_select_page(index, IS31FL3741_COMMAND_SCALING_1);
// CS1_SW7 to CS39_SW9 are on page 3
for (int i = CS1_SW7; i <= CS39_SW9; ++i) {
is31fl3741_write_register(addr, i - CS1_SW7, g_scaling_registers[index][i]);
for (uint8_t i = 0; i < IS31FL3741_SCALING_1_REGISTER_COUNT; i++) {
is31fl3741_write_register(index, i, driver_buffers[index].scaling_buffer_1[i]);
}
g_scaling_registers_update_required[index] = false;
driver_buffers[index].scaling_buffer_dirty = false;
}
}
void is31fl3741_set_scaling_registers(const is31fl3741_led_t *pled, uint8_t red, uint8_t green, uint8_t blue) {
g_scaling_registers[pled->driver][pled->r] = red;
g_scaling_registers[pled->driver][pled->g] = green;
g_scaling_registers[pled->driver][pled->b] = blue;
g_scaling_registers_update_required[pled->driver] = true;
set_scaling_value(pled->driver, pled->r, red);
set_scaling_value(pled->driver, pled->g, green);
set_scaling_value(pled->driver, pled->b, blue);
driver_buffers[pled->driver].scaling_buffer_dirty = true;
}
void is31fl3741_flush(void) {
is31fl3741_update_pwm_buffers(IS31FL3741_I2C_ADDRESS_1, 0);
#if defined(IS31FL3741_I2C_ADDRESS_2)
is31fl3741_update_pwm_buffers(IS31FL3741_I2C_ADDRESS_2, 1);
# if defined(IS31FL3741_I2C_ADDRESS_3)
is31fl3741_update_pwm_buffers(IS31FL3741_I2C_ADDRESS_3, 2);
# if defined(IS31FL3741_I2C_ADDRESS_4)
is31fl3741_update_pwm_buffers(IS31FL3741_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3741_DRIVER_COUNT; i++) {
is31fl3741_update_pwm_buffers(i);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -20,6 +20,7 @@
#include "is31fl3742a-mono.h"
#include "i2c_master.h"
#include "gpio.h"
#include "wait.h"
#define IS31FL3742A_PWM_REGISTER_COUNT 180
@ -53,28 +54,49 @@
# define IS31FL3742A_GLOBAL_CURRENT 0xFF
#endif
uint8_t g_pwm_buffer[IS31FL3742A_DRIVER_COUNT][IS31FL3742A_PWM_REGISTER_COUNT];
bool g_pwm_buffer_update_required[IS31FL3742A_DRIVER_COUNT] = {false};
bool g_scaling_registers_update_required[IS31FL3742A_DRIVER_COUNT] = {false};
const uint8_t i2c_addresses[IS31FL3742A_DRIVER_COUNT] = {
IS31FL3742A_I2C_ADDRESS_1,
#ifdef IS31FL3742A_I2C_ADDRESS_2
IS31FL3742A_I2C_ADDRESS_2,
# ifdef IS31FL3742A_I2C_ADDRESS_3
IS31FL3742A_I2C_ADDRESS_3,
# ifdef IS31FL3742A_I2C_ADDRESS_4
IS31FL3742A_I2C_ADDRESS_4,
# endif
# endif
#endif
};
uint8_t g_scaling_registers[IS31FL3742A_DRIVER_COUNT][IS31FL3742A_SCALING_REGISTER_COUNT];
typedef struct is31fl3742a_driver_t {
uint8_t pwm_buffer[IS31FL3742A_PWM_REGISTER_COUNT];
bool pwm_buffer_dirty;
uint8_t scaling_buffer[IS31FL3742A_SCALING_REGISTER_COUNT];
bool scaling_buffer_dirty;
} PACKED is31fl3742a_driver_t;
void is31fl3742a_write_register(uint8_t addr, uint8_t reg, uint8_t data) {
is31fl3742a_driver_t driver_buffers[IS31FL3742A_DRIVER_COUNT] = {{
.pwm_buffer = {0},
.pwm_buffer_dirty = false,
.scaling_buffer = {0},
.scaling_buffer_dirty = false,
}};
void is31fl3742a_write_register(uint8_t index, uint8_t reg, uint8_t data) {
#if IS31FL3742A_I2C_PERSISTENCE > 0
for (uint8_t i = 0; i < IS31FL3742A_I2C_PERSISTENCE; i++) {
if (i2c_write_register(addr << 1, reg, &data, 1, IS31FL3742A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3742A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, reg, &data, 1, IS31FL3742A_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3742A_I2C_TIMEOUT);
#endif
}
void is31fl3742a_select_page(uint8_t addr, uint8_t page) {
is31fl3742a_write_register(addr, IS31FL3742A_REG_COMMAND_WRITE_LOCK, IS31FL3742A_COMMAND_WRITE_LOCK_MAGIC);
is31fl3742a_write_register(addr, IS31FL3742A_REG_COMMAND, page);
void is31fl3742a_select_page(uint8_t index, uint8_t page) {
is31fl3742a_write_register(index, IS31FL3742A_REG_COMMAND_WRITE_LOCK, IS31FL3742A_COMMAND_WRITE_LOCK_MAGIC);
is31fl3742a_write_register(index, IS31FL3742A_REG_COMMAND, page);
}
void is31fl3742a_write_pwm_buffer(uint8_t addr, uint8_t index) {
void is31fl3742a_write_pwm_buffer(uint8_t index) {
// Assumes page 0 is already selected.
// Transmit PWM registers in 6 transfers of 30 bytes.
@ -82,10 +104,10 @@ void is31fl3742a_write_pwm_buffer(uint8_t addr, uint8_t index) {
for (uint8_t i = 0; i < IS31FL3742A_PWM_REGISTER_COUNT; i += 30) {
#if IS31FL3742A_I2C_PERSISTENCE > 0
for (uint8_t j = 0; j < IS31FL3742A_I2C_PERSISTENCE; j++) {
if (i2c_write_register(addr << 1, i, g_pwm_buffer[index] + i, 30, IS31FL3742A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 30, IS31FL3742A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, i, g_pwm_buffer[index] + i, 30, IS31FL3742A_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 30, IS31FL3742A_I2C_TIMEOUT);
#endif
}
}
@ -93,58 +115,49 @@ void is31fl3742a_write_pwm_buffer(uint8_t addr, uint8_t index) {
void is31fl3742a_init_drivers(void) {
i2c_init();
is31fl3742a_init(IS31FL3742A_I2C_ADDRESS_1);
#if defined(IS31FL3742A_I2C_ADDRESS_2)
is31fl3742a_init(IS31FL3742A_I2C_ADDRESS_2);
# if defined(IS31FL3742A_I2C_ADDRESS_3)
is31fl3742a_init(IS31FL3742A_I2C_ADDRESS_3);
# if defined(IS31FL3742A_I2C_ADDRESS_4)
is31fl3742a_init(IS31FL3742A_I2C_ADDRESS_4);
# endif
# endif
#if defined(IS31FL3742A_SDB_PIN)
gpio_set_pin_output(IS31FL3742A_SDB_PIN);
gpio_write_pin_high(IS31FL3742A_SDB_PIN);
#endif
for (uint8_t i = 0; i < IS31FL3742A_DRIVER_COUNT; i++) {
is31fl3742a_init(i);
}
for (int i = 0; i < IS31FL3742A_LED_COUNT; i++) {
is31fl3742a_set_scaling_register(i, 0xFF);
}
is31fl3742a_update_scaling_registers(IS31FL3742A_I2C_ADDRESS_1, 0);
#if defined(IS31FL3742A_I2C_ADDRESS_2)
is31fl3742a_update_scaling_registers(IS31FL3742A_I2C_ADDRESS_2, 1);
# if defined(IS31FL3742A_I2C_ADDRESS_3)
is31fl3742a_update_scaling_registers(IS31FL3742A_I2C_ADDRESS_3, 2);
# if defined(IS31FL3742A_I2C_ADDRESS_4)
is31fl3742a_update_scaling_registers(IS31FL3742A_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3742A_DRIVER_COUNT; i++) {
is31fl3742a_update_scaling_registers(i);
}
}
void is31fl3742a_init(uint8_t addr) {
void is31fl3742a_init(uint8_t index) {
// In order to avoid the LEDs being driven with garbage data
// in the LED driver's PWM registers, shutdown is enabled last.
// Set up the mode and other settings, clear the PWM registers,
// then disable software shutdown.
is31fl3742a_select_page(addr, IS31FL3742A_COMMAND_SCALING);
is31fl3742a_select_page(index, IS31FL3742A_COMMAND_SCALING);
// Turn off all LEDs.
for (uint8_t i = 0; i < IS31FL3742A_SCALING_REGISTER_COUNT; i++) {
is31fl3742a_write_register(addr, i, 0x00);
is31fl3742a_write_register(index, i, 0x00);
}
is31fl3742a_select_page(addr, IS31FL3742A_COMMAND_PWM);
is31fl3742a_select_page(index, IS31FL3742A_COMMAND_PWM);
for (uint8_t i = 0; i < IS31FL3742A_PWM_REGISTER_COUNT; i++) {
is31fl3742a_write_register(addr, i, 0x00);
is31fl3742a_write_register(index, i, 0x00);
}
is31fl3742a_select_page(addr, IS31FL3742A_COMMAND_FUNCTION);
is31fl3742a_select_page(index, IS31FL3742A_COMMAND_FUNCTION);
is31fl3742a_write_register(addr, IS31FL3742A_FUNCTION_REG_PULLDOWNUP, (IS31FL3742A_SW_PULLDOWN << 4) | IS31FL3742A_CS_PULLUP);
is31fl3742a_write_register(addr, IS31FL3742A_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3742A_GLOBAL_CURRENT);
is31fl3742a_write_register(addr, IS31FL3742A_FUNCTION_REG_PWM_FREQUENCY, (IS31FL3742A_PWM_FREQUENCY & 0b0111));
is31fl3742a_write_register(addr, IS31FL3742A_FUNCTION_REG_CONFIGURATION, IS31FL3742A_CONFIGURATION);
is31fl3742a_write_register(index, IS31FL3742A_FUNCTION_REG_PULLDOWNUP, (IS31FL3742A_SW_PULLDOWN << 4) | IS31FL3742A_CS_PULLUP);
is31fl3742a_write_register(index, IS31FL3742A_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3742A_GLOBAL_CURRENT);
is31fl3742a_write_register(index, IS31FL3742A_FUNCTION_REG_PWM_FREQUENCY, (IS31FL3742A_PWM_FREQUENCY & 0b0111));
is31fl3742a_write_register(index, IS31FL3742A_FUNCTION_REG_CONFIGURATION, IS31FL3742A_CONFIGURATION);
// Wait 10ms to ensure the device has woken up.
wait_ms(10);
@ -156,12 +169,12 @@ void is31fl3742a_set_value(int index, uint8_t value) {
if (index >= 0 && index < IS31FL3742A_LED_COUNT) {
memcpy_P(&led, (&g_is31fl3742a_leds[index]), sizeof(led));
if (g_pwm_buffer[led.driver][led.v] == value) {
if (driver_buffers[led.driver].pwm_buffer[led.v] == value) {
return;
}
g_pwm_buffer[led.driver][led.v] = value;
g_pwm_buffer_update_required[led.driver] = true;
driver_buffers[led.driver].pwm_buffer[led.v] = value;
driver_buffers[led.driver].pwm_buffer_dirty = true;
}
}
@ -175,41 +188,34 @@ void is31fl3742a_set_scaling_register(uint8_t index, uint8_t value) {
is31fl3742a_led_t led;
memcpy_P(&led, (&g_is31fl3742a_leds[index]), sizeof(led));
g_scaling_registers[led.driver][led.v] = value;
g_scaling_registers_update_required[led.driver] = true;
driver_buffers[led.driver].scaling_buffer[led.v] = value;
driver_buffers[led.driver].scaling_buffer_dirty = true;
}
void is31fl3742a_update_pwm_buffers(uint8_t addr, uint8_t index) {
if (g_pwm_buffer_update_required[index]) {
is31fl3742a_select_page(addr, IS31FL3742A_COMMAND_PWM);
void is31fl3742a_update_pwm_buffers(uint8_t index) {
if (driver_buffers[index].pwm_buffer_dirty) {
is31fl3742a_select_page(index, IS31FL3742A_COMMAND_PWM);
is31fl3742a_write_pwm_buffer(addr, index);
is31fl3742a_write_pwm_buffer(index);
g_pwm_buffer_update_required[index] = false;
driver_buffers[index].pwm_buffer_dirty = false;
}
}
void is31fl3742a_update_scaling_registers(uint8_t addr, uint8_t index) {
if (g_scaling_registers_update_required[index]) {
is31fl3742a_select_page(addr, IS31FL3742A_COMMAND_SCALING);
void is31fl3742a_update_scaling_registers(uint8_t index) {
if (driver_buffers[index].scaling_buffer_dirty) {
is31fl3742a_select_page(index, IS31FL3742A_COMMAND_SCALING);
for (uint8_t i = 0; i < IS31FL3742A_SCALING_REGISTER_COUNT; i++) {
is31fl3742a_write_register(addr, i, g_scaling_registers[index][i]);
is31fl3742a_write_register(index, i, driver_buffers[index].scaling_buffer[i]);
}
g_scaling_registers_update_required[index] = false;
driver_buffers[index].scaling_buffer_dirty = false;
}
}
void is31fl3742a_flush(void) {
is31fl3742a_update_pwm_buffers(IS31FL3742A_I2C_ADDRESS_1, 0);
#if defined(IS31FL3742A_I2C_ADDRESS_2)
is31fl3742a_update_pwm_buffers(IS31FL3742A_I2C_ADDRESS_2, 1);
# if defined(IS31FL3742A_I2C_ADDRESS_3)
is31fl3742a_update_pwm_buffers(IS31FL3742A_I2C_ADDRESS_3, 2);
# if defined(IS31FL3742A_I2C_ADDRESS_4)
is31fl3742a_update_pwm_buffers(IS31FL3742A_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3742A_DRIVER_COUNT; i++) {
is31fl3742a_update_pwm_buffers(i);
}
}

View File

@ -72,17 +72,17 @@ typedef struct is31fl3742a_led_t {
extern const is31fl3742a_led_t PROGMEM g_is31fl3742a_leds[IS31FL3742A_LED_COUNT];
void is31fl3742a_init_drivers(void);
void is31fl3742a_init(uint8_t addr);
void is31fl3742a_write_register(uint8_t addr, uint8_t reg, uint8_t data);
void is31fl3742a_select_page(uint8_t addr, uint8_t page);
void is31fl3742a_init(uint8_t index);
void is31fl3742a_write_register(uint8_t index, uint8_t reg, uint8_t data);
void is31fl3742a_select_page(uint8_t index, uint8_t page);
void is31fl3742a_set_value(int index, uint8_t value);
void is31fl3742a_set_value_all(uint8_t value);
void is31fl3742a_set_scaling_register(uint8_t index, uint8_t value);
void is31fl3742a_update_pwm_buffers(uint8_t addr, uint8_t index);
void is31fl3742a_update_scaling_registers(uint8_t addr, uint8_t index);
void is31fl3742a_update_pwm_buffers(uint8_t index);
void is31fl3742a_update_scaling_registers(uint8_t index);
void is31fl3742a_flush(void);
@ -109,188 +109,188 @@ void is31fl3742a_flush(void);
#define IS31FL3742A_PWM_FREQUENCY_1K8_HZ 0b0111
#define IS31FL3742A_PWM_FREQUENCY_900_HZ 0b1011
#define CS1_SW1 0x00
#define CS2_SW1 0x01
#define CS3_SW1 0x02
#define CS4_SW1 0x03
#define CS5_SW1 0x04
#define CS6_SW1 0x05
#define CS7_SW1 0x06
#define CS8_SW1 0x07
#define CS9_SW1 0x08
#define CS10_SW1 0x09
#define CS11_SW1 0x0A
#define CS12_SW1 0x0B
#define CS13_SW1 0x0C
#define CS14_SW1 0x0D
#define CS15_SW1 0x0E
#define CS16_SW1 0x0F
#define CS17_SW1 0x10
#define CS18_SW1 0x11
#define CS19_SW1 0x12
#define CS20_SW1 0x13
#define CS21_SW1 0x14
#define CS22_SW1 0x15
#define CS23_SW1 0x16
#define CS24_SW1 0x17
#define CS25_SW1 0x18
#define CS26_SW1 0x19
#define CS27_SW1 0x1A
#define CS28_SW1 0x1B
#define CS29_SW1 0x1C
#define CS30_SW1 0x1D
#define SW1_CS1 0x00
#define SW1_CS2 0x01
#define SW1_CS3 0x02
#define SW1_CS4 0x03
#define SW1_CS5 0x04
#define SW1_CS6 0x05
#define SW1_CS7 0x06
#define SW1_CS8 0x07
#define SW1_CS9 0x08
#define SW1_CS10 0x09
#define SW1_CS11 0x0A
#define SW1_CS12 0x0B
#define SW1_CS13 0x0C
#define SW1_CS14 0x0D
#define SW1_CS15 0x0E
#define SW1_CS16 0x0F
#define SW1_CS17 0x10
#define SW1_CS18 0x11
#define SW1_CS19 0x12
#define SW1_CS20 0x13
#define SW1_CS21 0x14
#define SW1_CS22 0x15
#define SW1_CS23 0x16
#define SW1_CS24 0x17
#define SW1_CS25 0x18
#define SW1_CS26 0x19
#define SW1_CS27 0x1A
#define SW1_CS28 0x1B
#define SW1_CS29 0x1C
#define SW1_CS30 0x1D
#define CS1_SW2 0x1E
#define CS2_SW2 0x1F
#define CS3_SW2 0x20
#define CS4_SW2 0x21
#define CS5_SW2 0x22
#define CS6_SW2 0x23
#define CS7_SW2 0x24
#define CS8_SW2 0x25
#define CS9_SW2 0x26
#define CS10_SW2 0x27
#define CS11_SW2 0x28
#define CS12_SW2 0x29
#define CS13_SW2 0x2A
#define CS14_SW2 0x2B
#define CS15_SW2 0x2C
#define CS16_SW2 0x2D
#define CS17_SW2 0x2E
#define CS18_SW2 0x2F
#define CS19_SW2 0x30
#define CS20_SW2 0x31
#define CS21_SW2 0x32
#define CS22_SW2 0x33
#define CS23_SW2 0x34
#define CS24_SW2 0x35
#define CS25_SW2 0x36
#define CS26_SW2 0x37
#define CS27_SW2 0x38
#define CS28_SW2 0x39
#define CS29_SW2 0x3A
#define CS30_SW2 0x3B
#define SW2_CS1 0x1E
#define SW2_CS2 0x1F
#define SW2_CS3 0x20
#define SW2_CS4 0x21
#define SW2_CS5 0x22
#define SW2_CS6 0x23
#define SW2_CS7 0x24
#define SW2_CS8 0x25
#define SW2_CS9 0x26
#define SW2_CS10 0x27
#define SW2_CS11 0x28
#define SW2_CS12 0x29
#define SW2_CS13 0x2A
#define SW2_CS14 0x2B
#define SW2_CS15 0x2C
#define SW2_CS16 0x2D
#define SW2_CS17 0x2E
#define SW2_CS18 0x2F
#define SW2_CS19 0x30
#define SW2_CS20 0x31
#define SW2_CS21 0x32
#define SW2_CS22 0x33
#define SW2_CS23 0x34
#define SW2_CS24 0x35
#define SW2_CS25 0x36
#define SW2_CS26 0x37
#define SW2_CS27 0x38
#define SW2_CS28 0x39
#define SW2_CS29 0x3A
#define SW2_CS30 0x3B
#define CS1_SW3 0x3C
#define CS2_SW3 0x3D
#define CS3_SW3 0x3E
#define CS4_SW3 0x3F
#define CS5_SW3 0x40
#define CS6_SW3 0x41
#define CS7_SW3 0x42
#define CS8_SW3 0x43
#define CS9_SW3 0x44
#define CS10_SW3 0x45
#define CS11_SW3 0x46
#define CS12_SW3 0x47
#define CS13_SW3 0x48
#define CS14_SW3 0x49
#define CS15_SW3 0x4A
#define CS16_SW3 0x4B
#define CS17_SW3 0x4C
#define CS18_SW3 0x4D
#define CS19_SW3 0x4E
#define CS20_SW3 0x4F
#define CS21_SW3 0x50
#define CS22_SW3 0x51
#define CS23_SW3 0x52
#define CS24_SW3 0x53
#define CS25_SW3 0x54
#define CS26_SW3 0x55
#define CS27_SW3 0x56
#define CS28_SW3 0x57
#define CS29_SW3 0x58
#define CS30_SW3 0x59
#define SW3_CS1 0x3C
#define SW3_CS2 0x3D
#define SW3_CS3 0x3E
#define SW3_CS4 0x3F
#define SW3_CS5 0x40
#define SW3_CS6 0x41
#define SW3_CS7 0x42
#define SW3_CS8 0x43
#define SW3_CS9 0x44
#define SW3_CS10 0x45
#define SW3_CS11 0x46
#define SW3_CS12 0x47
#define SW3_CS13 0x48
#define SW3_CS14 0x49
#define SW3_CS15 0x4A
#define SW3_CS16 0x4B
#define SW3_CS17 0x4C
#define SW3_CS18 0x4D
#define SW3_CS19 0x4E
#define SW3_CS20 0x4F
#define SW3_CS21 0x50
#define SW3_CS22 0x51
#define SW3_CS23 0x52
#define SW3_CS24 0x53
#define SW3_CS25 0x54
#define SW3_CS26 0x55
#define SW3_CS27 0x56
#define SW3_CS28 0x57
#define SW3_CS29 0x58
#define SW3_CS30 0x59
#define CS1_SW4 0x5A
#define CS2_SW4 0x5B
#define CS3_SW4 0x5C
#define CS4_SW4 0x5D
#define CS5_SW4 0x5E
#define CS6_SW4 0x5F
#define CS7_SW4 0x60
#define CS8_SW4 0x61
#define CS9_SW4 0x62
#define CS10_SW4 0x63
#define CS11_SW4 0x64
#define CS12_SW4 0x65
#define CS13_SW4 0x66
#define CS14_SW4 0x67
#define CS15_SW4 0x68
#define CS16_SW4 0x69
#define CS17_SW4 0x6A
#define CS18_SW4 0x6B
#define CS19_SW4 0x6C
#define CS20_SW4 0x6D
#define CS21_SW4 0x6E
#define CS22_SW4 0x6F
#define CS23_SW4 0x70
#define CS24_SW4 0x71
#define CS25_SW4 0x72
#define CS26_SW4 0x73
#define CS27_SW4 0x74
#define CS28_SW4 0x75
#define CS29_SW4 0x76
#define CS30_SW4 0x77
#define SW4_CS1 0x5A
#define SW4_CS2 0x5B
#define SW4_CS3 0x5C
#define SW4_CS4 0x5D
#define SW4_CS5 0x5E
#define SW4_CS6 0x5F
#define SW4_CS7 0x60
#define SW4_CS8 0x61
#define SW4_CS9 0x62
#define SW4_CS10 0x63
#define SW4_CS11 0x64
#define SW4_CS12 0x65
#define SW4_CS13 0x66
#define SW4_CS14 0x67
#define SW4_CS15 0x68
#define SW4_CS16 0x69
#define SW4_CS17 0x6A
#define SW4_CS18 0x6B
#define SW4_CS19 0x6C
#define SW4_CS20 0x6D
#define SW4_CS21 0x6E
#define SW4_CS22 0x6F
#define SW4_CS23 0x70
#define SW4_CS24 0x71
#define SW4_CS25 0x72
#define SW4_CS26 0x73
#define SW4_CS27 0x74
#define SW4_CS28 0x75
#define SW4_CS29 0x76
#define SW4_CS30 0x77
#define CS1_SW5 0x78
#define CS2_SW5 0x79
#define CS3_SW5 0x7A
#define CS4_SW5 0x7B
#define CS5_SW5 0x7C
#define CS6_SW5 0x7D
#define CS7_SW5 0x7E
#define CS8_SW5 0x7F
#define CS9_SW5 0x80
#define CS10_SW5 0x81
#define CS11_SW5 0x82
#define CS12_SW5 0x83
#define CS13_SW5 0x84
#define CS14_SW5 0x85
#define CS15_SW5 0x86
#define CS16_SW5 0x87
#define CS17_SW5 0x88
#define CS18_SW5 0x89
#define CS19_SW5 0x8A
#define CS20_SW5 0x8B
#define CS21_SW5 0x8C
#define CS22_SW5 0x8D
#define CS23_SW5 0x8E
#define CS24_SW5 0x8F
#define CS25_SW5 0x90
#define CS26_SW5 0x91
#define CS27_SW5 0x92
#define CS28_SW5 0x93
#define CS29_SW5 0x94
#define CS30_SW5 0x95
#define SW5_CS1 0x78
#define SW5_CS2 0x79
#define SW5_CS3 0x7A
#define SW5_CS4 0x7B
#define SW5_CS5 0x7C
#define SW5_CS6 0x7D
#define SW5_CS7 0x7E
#define SW5_CS8 0x7F
#define SW5_CS9 0x80
#define SW5_CS10 0x81
#define SW5_CS11 0x82
#define SW5_CS12 0x83
#define SW5_CS13 0x84
#define SW5_CS14 0x85
#define SW5_CS15 0x86
#define SW5_CS16 0x87
#define SW5_CS17 0x88
#define SW5_CS18 0x89
#define SW5_CS19 0x8A
#define SW5_CS20 0x8B
#define SW5_CS21 0x8C
#define SW5_CS22 0x8D
#define SW5_CS23 0x8E
#define SW5_CS24 0x8F
#define SW5_CS25 0x90
#define SW5_CS26 0x91
#define SW5_CS27 0x92
#define SW5_CS28 0x93
#define SW5_CS29 0x94
#define SW5_CS30 0x95
#define CS1_SW6 0x96
#define CS2_SW6 0x97
#define CS3_SW6 0x98
#define CS4_SW6 0x99
#define CS5_SW6 0x9A
#define CS6_SW6 0x9B
#define CS7_SW6 0x9C
#define CS8_SW6 0x9D
#define CS9_SW6 0x9E
#define CS10_SW6 0x9F
#define CS11_SW6 0xA0
#define CS12_SW6 0xA1
#define CS13_SW6 0xA2
#define CS14_SW6 0xA3
#define CS15_SW6 0xA4
#define CS16_SW6 0xA5
#define CS17_SW6 0xA6
#define CS18_SW6 0xA7
#define CS19_SW6 0xA8
#define CS20_SW6 0xA9
#define CS21_SW6 0xAA
#define CS22_SW6 0xAB
#define CS23_SW6 0xAC
#define CS24_SW6 0xAD
#define CS25_SW6 0xAE
#define CS26_SW6 0xAF
#define CS27_SW6 0xB0
#define CS28_SW6 0xB1
#define CS29_SW6 0xB2
#define CS30_SW6 0xB3
#define SW6_CS1 0x96
#define SW6_CS2 0x97
#define SW6_CS3 0x98
#define SW6_CS4 0x99
#define SW6_CS5 0x9A
#define SW6_CS6 0x9B
#define SW6_CS7 0x9C
#define SW6_CS8 0x9D
#define SW6_CS9 0x9E
#define SW6_CS10 0x9F
#define SW6_CS11 0xA0
#define SW6_CS12 0xA1
#define SW6_CS13 0xA2
#define SW6_CS14 0xA3
#define SW6_CS15 0xA4
#define SW6_CS16 0xA5
#define SW6_CS17 0xA6
#define SW6_CS18 0xA7
#define SW6_CS19 0xA8
#define SW6_CS20 0xA9
#define SW6_CS21 0xAA
#define SW6_CS22 0xAB
#define SW6_CS23 0xAC
#define SW6_CS24 0xAD
#define SW6_CS25 0xAE
#define SW6_CS26 0xAF
#define SW6_CS27 0xB0
#define SW6_CS28 0xB1
#define SW6_CS29 0xB2
#define SW6_CS30 0xB3

View File

@ -20,6 +20,7 @@
#include "is31fl3742a.h"
#include "i2c_master.h"
#include "gpio.h"
#include "wait.h"
#define IS31FL3742A_PWM_REGISTER_COUNT 180
@ -53,28 +54,49 @@
# define IS31FL3742A_GLOBAL_CURRENT 0xFF
#endif
uint8_t g_pwm_buffer[IS31FL3742A_DRIVER_COUNT][IS31FL3742A_PWM_REGISTER_COUNT];
bool g_pwm_buffer_update_required[IS31FL3742A_DRIVER_COUNT] = {false};
bool g_scaling_registers_update_required[IS31FL3742A_DRIVER_COUNT] = {false};
const uint8_t i2c_addresses[IS31FL3742A_DRIVER_COUNT] = {
IS31FL3742A_I2C_ADDRESS_1,
#ifdef IS31FL3742A_I2C_ADDRESS_2
IS31FL3742A_I2C_ADDRESS_2,
# ifdef IS31FL3742A_I2C_ADDRESS_3
IS31FL3742A_I2C_ADDRESS_3,
# ifdef IS31FL3742A_I2C_ADDRESS_4
IS31FL3742A_I2C_ADDRESS_4,
# endif
# endif
#endif
};
uint8_t g_scaling_registers[IS31FL3742A_DRIVER_COUNT][IS31FL3742A_SCALING_REGISTER_COUNT];
typedef struct is31fl3742a_driver_t {
uint8_t pwm_buffer[IS31FL3742A_PWM_REGISTER_COUNT];
bool pwm_buffer_dirty;
uint8_t scaling_buffer[IS31FL3742A_SCALING_REGISTER_COUNT];
bool scaling_buffer_dirty;
} PACKED is31fl3742a_driver_t;
void is31fl3742a_write_register(uint8_t addr, uint8_t reg, uint8_t data) {
is31fl3742a_driver_t driver_buffers[IS31FL3742A_DRIVER_COUNT] = {{
.pwm_buffer = {0},
.pwm_buffer_dirty = false,
.scaling_buffer = {0},
.scaling_buffer_dirty = false,
}};
void is31fl3742a_write_register(uint8_t index, uint8_t reg, uint8_t data) {
#if IS31FL3742A_I2C_PERSISTENCE > 0
for (uint8_t i = 0; i < IS31FL3742A_I2C_PERSISTENCE; i++) {
if (i2c_write_register(addr << 1, reg, &data, 1, IS31FL3742A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3742A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, reg, &data, 1, IS31FL3742A_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3742A_I2C_TIMEOUT);
#endif
}
void is31fl3742a_select_page(uint8_t addr, uint8_t page) {
is31fl3742a_write_register(addr, IS31FL3742A_REG_COMMAND_WRITE_LOCK, IS31FL3742A_COMMAND_WRITE_LOCK_MAGIC);
is31fl3742a_write_register(addr, IS31FL3742A_REG_COMMAND, page);
void is31fl3742a_select_page(uint8_t index, uint8_t page) {
is31fl3742a_write_register(index, IS31FL3742A_REG_COMMAND_WRITE_LOCK, IS31FL3742A_COMMAND_WRITE_LOCK_MAGIC);
is31fl3742a_write_register(index, IS31FL3742A_REG_COMMAND, page);
}
void is31fl3742a_write_pwm_buffer(uint8_t addr, uint8_t index) {
void is31fl3742a_write_pwm_buffer(uint8_t index) {
// Assumes page 0 is already selected.
// Transmit PWM registers in 6 transfers of 30 bytes.
@ -82,10 +104,10 @@ void is31fl3742a_write_pwm_buffer(uint8_t addr, uint8_t index) {
for (uint8_t i = 0; i < IS31FL3742A_PWM_REGISTER_COUNT; i += 30) {
#if IS31FL3742A_I2C_PERSISTENCE > 0
for (uint8_t j = 0; j < IS31FL3742A_I2C_PERSISTENCE; j++) {
if (i2c_write_register(addr << 1, i, g_pwm_buffer[index] + i, 30, IS31FL3742A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 30, IS31FL3742A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, i, g_pwm_buffer[index] + i, 30, IS31FL3742A_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 30, IS31FL3742A_I2C_TIMEOUT);
#endif
}
}
@ -93,58 +115,49 @@ void is31fl3742a_write_pwm_buffer(uint8_t addr, uint8_t index) {
void is31fl3742a_init_drivers(void) {
i2c_init();
is31fl3742a_init(IS31FL3742A_I2C_ADDRESS_1);
#if defined(IS31FL3742A_I2C_ADDRESS_2)
is31fl3742a_init(IS31FL3742A_I2C_ADDRESS_2);
# if defined(IS31FL3742A_I2C_ADDRESS_3)
is31fl3742a_init(IS31FL3742A_I2C_ADDRESS_3);
# if defined(IS31FL3742A_I2C_ADDRESS_4)
is31fl3742a_init(IS31FL3742A_I2C_ADDRESS_4);
# endif
# endif
#if defined(IS31FL3742A_SDB_PIN)
gpio_set_pin_output(IS31FL3742A_SDB_PIN);
gpio_write_pin_high(IS31FL3742A_SDB_PIN);
#endif
for (uint8_t i = 0; i < IS31FL3742A_DRIVER_COUNT; i++) {
is31fl3742a_init(i);
}
for (int i = 0; i < IS31FL3742A_LED_COUNT; i++) {
is31fl3742a_set_scaling_register(i, 0xFF, 0xFF, 0xFF);
}
is31fl3742a_update_scaling_registers(IS31FL3742A_I2C_ADDRESS_1, 0);
#if defined(IS31FL3742A_I2C_ADDRESS_2)
is31fl3742a_update_scaling_registers(IS31FL3742A_I2C_ADDRESS_2, 1);
# if defined(IS31FL3742A_I2C_ADDRESS_3)
is31fl3742a_update_scaling_registers(IS31FL3742A_I2C_ADDRESS_3, 2);
# if defined(IS31FL3742A_I2C_ADDRESS_4)
is31fl3742a_update_scaling_registers(IS31FL3742A_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3742A_DRIVER_COUNT; i++) {
is31fl3742a_update_scaling_registers(i);
}
}
void is31fl3742a_init(uint8_t addr) {
void is31fl3742a_init(uint8_t index) {
// In order to avoid the LEDs being driven with garbage data
// in the LED driver's PWM registers, shutdown is enabled last.
// Set up the mode and other settings, clear the PWM registers,
// then disable software shutdown.
is31fl3742a_select_page(addr, IS31FL3742A_COMMAND_SCALING);
is31fl3742a_select_page(index, IS31FL3742A_COMMAND_SCALING);
// Turn off all LEDs.
for (uint8_t i = 0; i < IS31FL3742A_SCALING_REGISTER_COUNT; i++) {
is31fl3742a_write_register(addr, i, 0x00);
is31fl3742a_write_register(index, i, 0x00);
}
is31fl3742a_select_page(addr, IS31FL3742A_COMMAND_PWM);
is31fl3742a_select_page(index, IS31FL3742A_COMMAND_PWM);
for (uint8_t i = 0; i < IS31FL3742A_PWM_REGISTER_COUNT; i++) {
is31fl3742a_write_register(addr, i, 0x00);
is31fl3742a_write_register(index, i, 0x00);
}
is31fl3742a_select_page(addr, IS31FL3742A_COMMAND_FUNCTION);
is31fl3742a_select_page(index, IS31FL3742A_COMMAND_FUNCTION);
is31fl3742a_write_register(addr, IS31FL3742A_FUNCTION_REG_PULLDOWNUP, (IS31FL3742A_SW_PULLDOWN << 4) | IS31FL3742A_CS_PULLUP);
is31fl3742a_write_register(addr, IS31FL3742A_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3742A_GLOBAL_CURRENT);
is31fl3742a_write_register(addr, IS31FL3742A_FUNCTION_REG_PWM_FREQUENCY, (IS31FL3742A_PWM_FREQUENCY & 0b0111));
is31fl3742a_write_register(addr, IS31FL3742A_FUNCTION_REG_CONFIGURATION, IS31FL3742A_CONFIGURATION);
is31fl3742a_write_register(index, IS31FL3742A_FUNCTION_REG_PULLDOWNUP, (IS31FL3742A_SW_PULLDOWN << 4) | IS31FL3742A_CS_PULLUP);
is31fl3742a_write_register(index, IS31FL3742A_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3742A_GLOBAL_CURRENT);
is31fl3742a_write_register(index, IS31FL3742A_FUNCTION_REG_PWM_FREQUENCY, (IS31FL3742A_PWM_FREQUENCY & 0b0111));
is31fl3742a_write_register(index, IS31FL3742A_FUNCTION_REG_CONFIGURATION, IS31FL3742A_CONFIGURATION);
// Wait 10ms to ensure the device has woken up.
wait_ms(10);
@ -156,14 +169,14 @@ void is31fl3742a_set_color(int index, uint8_t red, uint8_t green, uint8_t blue)
if (index >= 0 && index < IS31FL3742A_LED_COUNT) {
memcpy_P(&led, (&g_is31fl3742a_leds[index]), sizeof(led));
if (g_pwm_buffer[led.driver][led.r] == red && g_pwm_buffer[led.driver][led.g] == green && g_pwm_buffer[led.driver][led.b] == blue) {
if (driver_buffers[led.driver].pwm_buffer[led.r] == red && driver_buffers[led.driver].pwm_buffer[led.g] == green && driver_buffers[led.driver].pwm_buffer[led.b] == blue) {
return;
}
g_pwm_buffer[led.driver][led.r] = red;
g_pwm_buffer[led.driver][led.g] = green;
g_pwm_buffer[led.driver][led.b] = blue;
g_pwm_buffer_update_required[led.driver] = true;
driver_buffers[led.driver].pwm_buffer[led.r] = red;
driver_buffers[led.driver].pwm_buffer[led.g] = green;
driver_buffers[led.driver].pwm_buffer[led.b] = blue;
driver_buffers[led.driver].pwm_buffer_dirty = true;
}
}
@ -177,43 +190,36 @@ void is31fl3742a_set_scaling_register(uint8_t index, uint8_t red, uint8_t green,
is31fl3742a_led_t led;
memcpy_P(&led, (&g_is31fl3742a_leds[index]), sizeof(led));
g_scaling_registers[led.driver][led.r] = red;
g_scaling_registers[led.driver][led.g] = green;
g_scaling_registers[led.driver][led.b] = blue;
g_scaling_registers_update_required[led.driver] = true;
driver_buffers[led.driver].scaling_buffer[led.r] = red;
driver_buffers[led.driver].scaling_buffer[led.g] = green;
driver_buffers[led.driver].scaling_buffer[led.b] = blue;
driver_buffers[led.driver].scaling_buffer_dirty = true;
}
void is31fl3742a_update_pwm_buffers(uint8_t addr, uint8_t index) {
if (g_pwm_buffer_update_required[index]) {
is31fl3742a_select_page(addr, IS31FL3742A_COMMAND_PWM);
void is31fl3742a_update_pwm_buffers(uint8_t index) {
if (driver_buffers[index].pwm_buffer_dirty) {
is31fl3742a_select_page(index, IS31FL3742A_COMMAND_PWM);
is31fl3742a_write_pwm_buffer(addr, index);
is31fl3742a_write_pwm_buffer(index);
g_pwm_buffer_update_required[index] = false;
driver_buffers[index].pwm_buffer_dirty = false;
}
}
void is31fl3742a_update_scaling_registers(uint8_t addr, uint8_t index) {
if (g_scaling_registers_update_required[index]) {
is31fl3742a_select_page(addr, IS31FL3742A_COMMAND_SCALING);
void is31fl3742a_update_scaling_registers(uint8_t index) {
if (driver_buffers[index].scaling_buffer_dirty) {
is31fl3742a_select_page(index, IS31FL3742A_COMMAND_SCALING);
for (uint8_t i = 0; i < IS31FL3742A_SCALING_REGISTER_COUNT; i++) {
is31fl3742a_write_register(addr, i, g_scaling_registers[index][i]);
is31fl3742a_write_register(index, i, driver_buffers[index].scaling_buffer[i]);
}
g_scaling_registers_update_required[index] = false;
driver_buffers[index].scaling_buffer_dirty = false;
}
}
void is31fl3742a_flush(void) {
is31fl3742a_update_pwm_buffers(IS31FL3742A_I2C_ADDRESS_1, 0);
#if defined(IS31FL3742A_I2C_ADDRESS_2)
is31fl3742a_update_pwm_buffers(IS31FL3742A_I2C_ADDRESS_2, 1);
# if defined(IS31FL3742A_I2C_ADDRESS_3)
is31fl3742a_update_pwm_buffers(IS31FL3742A_I2C_ADDRESS_3, 2);
# if defined(IS31FL3742A_I2C_ADDRESS_4)
is31fl3742a_update_pwm_buffers(IS31FL3742A_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3742A_DRIVER_COUNT; i++) {
is31fl3742a_update_pwm_buffers(i);
}
}

View File

@ -74,17 +74,17 @@ typedef struct is31fl3742a_led_t {
extern const is31fl3742a_led_t PROGMEM g_is31fl3742a_leds[IS31FL3742A_LED_COUNT];
void is31fl3742a_init_drivers(void);
void is31fl3742a_init(uint8_t addr);
void is31fl3742a_write_register(uint8_t addr, uint8_t reg, uint8_t data);
void is31fl3742a_select_page(uint8_t addr, uint8_t page);
void is31fl3742a_init(uint8_t index);
void is31fl3742a_write_register(uint8_t index, uint8_t reg, uint8_t data);
void is31fl3742a_select_page(uint8_t index, uint8_t page);
void is31fl3742a_set_color(int index, uint8_t red, uint8_t green, uint8_t blue);
void is31fl3742a_set_color_all(uint8_t red, uint8_t green, uint8_t blue);
void is31fl3742a_set_scaling_register(uint8_t index, uint8_t red, uint8_t green, uint8_t blue);
void is31fl3742a_update_pwm_buffers(uint8_t addr, uint8_t index);
void is31fl3742a_update_scaling_registers(uint8_t addr, uint8_t index);
void is31fl3742a_update_pwm_buffers(uint8_t index);
void is31fl3742a_update_scaling_registers(uint8_t index);
void is31fl3742a_flush(void);
@ -111,188 +111,188 @@ void is31fl3742a_flush(void);
#define IS31FL3742A_PWM_FREQUENCY_1K8_HZ 0b0111
#define IS31FL3742A_PWM_FREQUENCY_900_HZ 0b1011
#define CS1_SW1 0x00
#define CS2_SW1 0x01
#define CS3_SW1 0x02
#define CS4_SW1 0x03
#define CS5_SW1 0x04
#define CS6_SW1 0x05
#define CS7_SW1 0x06
#define CS8_SW1 0x07
#define CS9_SW1 0x08
#define CS10_SW1 0x09
#define CS11_SW1 0x0A
#define CS12_SW1 0x0B
#define CS13_SW1 0x0C
#define CS14_SW1 0x0D
#define CS15_SW1 0x0E
#define CS16_SW1 0x0F
#define CS17_SW1 0x10
#define CS18_SW1 0x11
#define CS19_SW1 0x12
#define CS20_SW1 0x13
#define CS21_SW1 0x14
#define CS22_SW1 0x15
#define CS23_SW1 0x16
#define CS24_SW1 0x17
#define CS25_SW1 0x18
#define CS26_SW1 0x19
#define CS27_SW1 0x1A
#define CS28_SW1 0x1B
#define CS29_SW1 0x1C
#define CS30_SW1 0x1D
#define SW1_CS1 0x00
#define SW1_CS2 0x01
#define SW1_CS3 0x02
#define SW1_CS4 0x03
#define SW1_CS5 0x04
#define SW1_CS6 0x05
#define SW1_CS7 0x06
#define SW1_CS8 0x07
#define SW1_CS9 0x08
#define SW1_CS10 0x09
#define SW1_CS11 0x0A
#define SW1_CS12 0x0B
#define SW1_CS13 0x0C
#define SW1_CS14 0x0D
#define SW1_CS15 0x0E
#define SW1_CS16 0x0F
#define SW1_CS17 0x10
#define SW1_CS18 0x11
#define SW1_CS19 0x12
#define SW1_CS20 0x13
#define SW1_CS21 0x14
#define SW1_CS22 0x15
#define SW1_CS23 0x16
#define SW1_CS24 0x17
#define SW1_CS25 0x18
#define SW1_CS26 0x19
#define SW1_CS27 0x1A
#define SW1_CS28 0x1B
#define SW1_CS29 0x1C
#define SW1_CS30 0x1D
#define CS1_SW2 0x1E
#define CS2_SW2 0x1F
#define CS3_SW2 0x20
#define CS4_SW2 0x21
#define CS5_SW2 0x22
#define CS6_SW2 0x23
#define CS7_SW2 0x24
#define CS8_SW2 0x25
#define CS9_SW2 0x26
#define CS10_SW2 0x27
#define CS11_SW2 0x28
#define CS12_SW2 0x29
#define CS13_SW2 0x2A
#define CS14_SW2 0x2B
#define CS15_SW2 0x2C
#define CS16_SW2 0x2D
#define CS17_SW2 0x2E
#define CS18_SW2 0x2F
#define CS19_SW2 0x30
#define CS20_SW2 0x31
#define CS21_SW2 0x32
#define CS22_SW2 0x33
#define CS23_SW2 0x34
#define CS24_SW2 0x35
#define CS25_SW2 0x36
#define CS26_SW2 0x37
#define CS27_SW2 0x38
#define CS28_SW2 0x39
#define CS29_SW2 0x3A
#define CS30_SW2 0x3B
#define SW2_CS1 0x1E
#define SW2_CS2 0x1F
#define SW2_CS3 0x20
#define SW2_CS4 0x21
#define SW2_CS5 0x22
#define SW2_CS6 0x23
#define SW2_CS7 0x24
#define SW2_CS8 0x25
#define SW2_CS9 0x26
#define SW2_CS10 0x27
#define SW2_CS11 0x28
#define SW2_CS12 0x29
#define SW2_CS13 0x2A
#define SW2_CS14 0x2B
#define SW2_CS15 0x2C
#define SW2_CS16 0x2D
#define SW2_CS17 0x2E
#define SW2_CS18 0x2F
#define SW2_CS19 0x30
#define SW2_CS20 0x31
#define SW2_CS21 0x32
#define SW2_CS22 0x33
#define SW2_CS23 0x34
#define SW2_CS24 0x35
#define SW2_CS25 0x36
#define SW2_CS26 0x37
#define SW2_CS27 0x38
#define SW2_CS28 0x39
#define SW2_CS29 0x3A
#define SW2_CS30 0x3B
#define CS1_SW3 0x3C
#define CS2_SW3 0x3D
#define CS3_SW3 0x3E
#define CS4_SW3 0x3F
#define CS5_SW3 0x40
#define CS6_SW3 0x41
#define CS7_SW3 0x42
#define CS8_SW3 0x43
#define CS9_SW3 0x44
#define CS10_SW3 0x45
#define CS11_SW3 0x46
#define CS12_SW3 0x47
#define CS13_SW3 0x48
#define CS14_SW3 0x49
#define CS15_SW3 0x4A
#define CS16_SW3 0x4B
#define CS17_SW3 0x4C
#define CS18_SW3 0x4D
#define CS19_SW3 0x4E
#define CS20_SW3 0x4F
#define CS21_SW3 0x50
#define CS22_SW3 0x51
#define CS23_SW3 0x52
#define CS24_SW3 0x53
#define CS25_SW3 0x54
#define CS26_SW3 0x55
#define CS27_SW3 0x56
#define CS28_SW3 0x57
#define CS29_SW3 0x58
#define CS30_SW3 0x59
#define SW3_CS1 0x3C
#define SW3_CS2 0x3D
#define SW3_CS3 0x3E
#define SW3_CS4 0x3F
#define SW3_CS5 0x40
#define SW3_CS6 0x41
#define SW3_CS7 0x42
#define SW3_CS8 0x43
#define SW3_CS9 0x44
#define SW3_CS10 0x45
#define SW3_CS11 0x46
#define SW3_CS12 0x47
#define SW3_CS13 0x48
#define SW3_CS14 0x49
#define SW3_CS15 0x4A
#define SW3_CS16 0x4B
#define SW3_CS17 0x4C
#define SW3_CS18 0x4D
#define SW3_CS19 0x4E
#define SW3_CS20 0x4F
#define SW3_CS21 0x50
#define SW3_CS22 0x51
#define SW3_CS23 0x52
#define SW3_CS24 0x53
#define SW3_CS25 0x54
#define SW3_CS26 0x55
#define SW3_CS27 0x56
#define SW3_CS28 0x57
#define SW3_CS29 0x58
#define SW3_CS30 0x59
#define CS1_SW4 0x5A
#define CS2_SW4 0x5B
#define CS3_SW4 0x5C
#define CS4_SW4 0x5D
#define CS5_SW4 0x5E
#define CS6_SW4 0x5F
#define CS7_SW4 0x60
#define CS8_SW4 0x61
#define CS9_SW4 0x62
#define CS10_SW4 0x63
#define CS11_SW4 0x64
#define CS12_SW4 0x65
#define CS13_SW4 0x66
#define CS14_SW4 0x67
#define CS15_SW4 0x68
#define CS16_SW4 0x69
#define CS17_SW4 0x6A
#define CS18_SW4 0x6B
#define CS19_SW4 0x6C
#define CS20_SW4 0x6D
#define CS21_SW4 0x6E
#define CS22_SW4 0x6F
#define CS23_SW4 0x70
#define CS24_SW4 0x71
#define CS25_SW4 0x72
#define CS26_SW4 0x73
#define CS27_SW4 0x74
#define CS28_SW4 0x75
#define CS29_SW4 0x76
#define CS30_SW4 0x77
#define SW4_CS1 0x5A
#define SW4_CS2 0x5B
#define SW4_CS3 0x5C
#define SW4_CS4 0x5D
#define SW4_CS5 0x5E
#define SW4_CS6 0x5F
#define SW4_CS7 0x60
#define SW4_CS8 0x61
#define SW4_CS9 0x62
#define SW4_CS10 0x63
#define SW4_CS11 0x64
#define SW4_CS12 0x65
#define SW4_CS13 0x66
#define SW4_CS14 0x67
#define SW4_CS15 0x68
#define SW4_CS16 0x69
#define SW4_CS17 0x6A
#define SW4_CS18 0x6B
#define SW4_CS19 0x6C
#define SW4_CS20 0x6D
#define SW4_CS21 0x6E
#define SW4_CS22 0x6F
#define SW4_CS23 0x70
#define SW4_CS24 0x71
#define SW4_CS25 0x72
#define SW4_CS26 0x73
#define SW4_CS27 0x74
#define SW4_CS28 0x75
#define SW4_CS29 0x76
#define SW4_CS30 0x77
#define CS1_SW5 0x78
#define CS2_SW5 0x79
#define CS3_SW5 0x7A
#define CS4_SW5 0x7B
#define CS5_SW5 0x7C
#define CS6_SW5 0x7D
#define CS7_SW5 0x7E
#define CS8_SW5 0x7F
#define CS9_SW5 0x80
#define CS10_SW5 0x81
#define CS11_SW5 0x82
#define CS12_SW5 0x83
#define CS13_SW5 0x84
#define CS14_SW5 0x85
#define CS15_SW5 0x86
#define CS16_SW5 0x87
#define CS17_SW5 0x88
#define CS18_SW5 0x89
#define CS19_SW5 0x8A
#define CS20_SW5 0x8B
#define CS21_SW5 0x8C
#define CS22_SW5 0x8D
#define CS23_SW5 0x8E
#define CS24_SW5 0x8F
#define CS25_SW5 0x90
#define CS26_SW5 0x91
#define CS27_SW5 0x92
#define CS28_SW5 0x93
#define CS29_SW5 0x94
#define CS30_SW5 0x95
#define SW5_CS1 0x78
#define SW5_CS2 0x79
#define SW5_CS3 0x7A
#define SW5_CS4 0x7B
#define SW5_CS5 0x7C
#define SW5_CS6 0x7D
#define SW5_CS7 0x7E
#define SW5_CS8 0x7F
#define SW5_CS9 0x80
#define SW5_CS10 0x81
#define SW5_CS11 0x82
#define SW5_CS12 0x83
#define SW5_CS13 0x84
#define SW5_CS14 0x85
#define SW5_CS15 0x86
#define SW5_CS16 0x87
#define SW5_CS17 0x88
#define SW5_CS18 0x89
#define SW5_CS19 0x8A
#define SW5_CS20 0x8B
#define SW5_CS21 0x8C
#define SW5_CS22 0x8D
#define SW5_CS23 0x8E
#define SW5_CS24 0x8F
#define SW5_CS25 0x90
#define SW5_CS26 0x91
#define SW5_CS27 0x92
#define SW5_CS28 0x93
#define SW5_CS29 0x94
#define SW5_CS30 0x95
#define CS1_SW6 0x96
#define CS2_SW6 0x97
#define CS3_SW6 0x98
#define CS4_SW6 0x99
#define CS5_SW6 0x9A
#define CS6_SW6 0x9B
#define CS7_SW6 0x9C
#define CS8_SW6 0x9D
#define CS9_SW6 0x9E
#define CS10_SW6 0x9F
#define CS11_SW6 0xA0
#define CS12_SW6 0xA1
#define CS13_SW6 0xA2
#define CS14_SW6 0xA3
#define CS15_SW6 0xA4
#define CS16_SW6 0xA5
#define CS17_SW6 0xA6
#define CS18_SW6 0xA7
#define CS19_SW6 0xA8
#define CS20_SW6 0xA9
#define CS21_SW6 0xAA
#define CS22_SW6 0xAB
#define CS23_SW6 0xAC
#define CS24_SW6 0xAD
#define CS25_SW6 0xAE
#define CS26_SW6 0xAF
#define CS27_SW6 0xB0
#define CS28_SW6 0xB1
#define CS29_SW6 0xB2
#define CS30_SW6 0xB3
#define SW6_CS1 0x96
#define SW6_CS2 0x97
#define SW6_CS3 0x98
#define SW6_CS4 0x99
#define SW6_CS5 0x9A
#define SW6_CS6 0x9B
#define SW6_CS7 0x9C
#define SW6_CS8 0x9D
#define SW6_CS9 0x9E
#define SW6_CS10 0x9F
#define SW6_CS11 0xA0
#define SW6_CS12 0xA1
#define SW6_CS13 0xA2
#define SW6_CS14 0xA3
#define SW6_CS15 0xA4
#define SW6_CS16 0xA5
#define SW6_CS17 0xA6
#define SW6_CS18 0xA7
#define SW6_CS19 0xA8
#define SW6_CS20 0xA9
#define SW6_CS21 0xAA
#define SW6_CS22 0xAB
#define SW6_CS23 0xAC
#define SW6_CS24 0xAD
#define SW6_CS25 0xAE
#define SW6_CS26 0xAF
#define SW6_CS27 0xB0
#define SW6_CS28 0xB1
#define SW6_CS29 0xB2
#define SW6_CS30 0xB3

View File

@ -20,6 +20,7 @@
#include "is31fl3743a-mono.h"
#include "i2c_master.h"
#include "gpio.h"
#include "wait.h"
#define IS31FL3743A_PWM_REGISTER_COUNT 198
@ -62,28 +63,62 @@
# define IS31FL3743A_SYNC_4 IS31FL3743A_SYNC_NONE
#endif
uint8_t g_pwm_buffer[IS31FL3743A_DRIVER_COUNT][IS31FL3743A_PWM_REGISTER_COUNT];
bool g_pwm_buffer_update_required[IS31FL3743A_DRIVER_COUNT] = {false};
bool g_scaling_registers_update_required[IS31FL3743A_DRIVER_COUNT] = {false};
const uint8_t i2c_addresses[IS31FL3743A_DRIVER_COUNT] = {
IS31FL3743A_I2C_ADDRESS_1,
#ifdef IS31FL3743A_I2C_ADDRESS_2
IS31FL3743A_I2C_ADDRESS_2,
# ifdef IS31FL3743A_I2C_ADDRESS_3
IS31FL3743A_I2C_ADDRESS_3,
# ifdef IS31FL3743A_I2C_ADDRESS_4
IS31FL3743A_I2C_ADDRESS_4,
# endif
# endif
#endif
};
uint8_t g_scaling_registers[IS31FL3743A_DRIVER_COUNT][IS31FL3743A_SCALING_REGISTER_COUNT];
const uint8_t driver_sync[IS31FL3743A_DRIVER_COUNT] = {
IS31FL3743A_SYNC_1,
#ifdef IS31FL3743A_I2C_ADDRESS_2
IS31FL3743A_SYNC_2,
# ifdef IS31FL3743A_I2C_ADDRESS_3
IS31FL3743A_SYNC_3,
# ifdef IS31FL3743A_I2C_ADDRESS_4
IS31FL3743A_SYNC_4,
# endif
# endif
#endif
};
void is31fl3743a_write_register(uint8_t addr, uint8_t reg, uint8_t data) {
typedef struct is31fl3743a_driver_t {
uint8_t pwm_buffer[IS31FL3743A_PWM_REGISTER_COUNT];
bool pwm_buffer_dirty;
uint8_t scaling_buffer[IS31FL3743A_SCALING_REGISTER_COUNT];
bool scaling_buffer_dirty;
} PACKED is31fl3743a_driver_t;
is31fl3743a_driver_t driver_buffers[IS31FL3743A_DRIVER_COUNT] = {{
.pwm_buffer = {0},
.pwm_buffer_dirty = false,
.scaling_buffer = {0},
.scaling_buffer_dirty = false,
}};
void is31fl3743a_write_register(uint8_t index, uint8_t reg, uint8_t data) {
#if IS31FL3743A_I2C_PERSISTENCE > 0
for (uint8_t i = 0; i < IS31FL3743A_I2C_PERSISTENCE; i++) {
if (i2c_write_register(addr << 1, reg, &data, 1, IS31FL3743A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3743A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, reg, &data, 1, IS31FL3743A_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3743A_I2C_TIMEOUT);
#endif
}
void is31fl3743a_select_page(uint8_t addr, uint8_t page) {
is31fl3743a_write_register(addr, IS31FL3743A_REG_COMMAND_WRITE_LOCK, IS31FL3743A_COMMAND_WRITE_LOCK_MAGIC);
is31fl3743a_write_register(addr, IS31FL3743A_REG_COMMAND, page);
void is31fl3743a_select_page(uint8_t index, uint8_t page) {
is31fl3743a_write_register(index, IS31FL3743A_REG_COMMAND_WRITE_LOCK, IS31FL3743A_COMMAND_WRITE_LOCK_MAGIC);
is31fl3743a_write_register(index, IS31FL3743A_REG_COMMAND, page);
}
void is31fl3743a_write_pwm_buffer(uint8_t addr, uint8_t index) {
void is31fl3743a_write_pwm_buffer(uint8_t index) {
// Assumes page 0 is already selected.
// Transmit PWM registers in 11 transfers of 18 bytes.
@ -91,10 +126,10 @@ void is31fl3743a_write_pwm_buffer(uint8_t addr, uint8_t index) {
for (uint8_t i = 0; i < IS31FL3743A_PWM_REGISTER_COUNT; i += 18) {
#if IS31FL3743A_I2C_PERSISTENCE > 0
for (uint8_t j = 0; j < IS31FL3743A_I2C_PERSISTENCE; j++) {
if (i2c_write_register(addr << 1, i + 1, g_pwm_buffer[index] + i, 18, IS31FL3743A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, i + 1, driver_buffers[index].pwm_buffer + i, 18, IS31FL3743A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, i + 1, g_pwm_buffer[index] + i, 18, IS31FL3743A_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, i + 1, driver_buffers[index].pwm_buffer + i, 18, IS31FL3743A_I2C_TIMEOUT);
#endif
}
}
@ -102,58 +137,51 @@ void is31fl3743a_write_pwm_buffer(uint8_t addr, uint8_t index) {
void is31fl3743a_init_drivers(void) {
i2c_init();
is31fl3743a_init(IS31FL3743A_I2C_ADDRESS_1, IS31FL3743A_SYNC_1);
#if defined(IS31FL3743A_I2C_ADDRESS_2)
is31fl3743a_init(IS31FL3743A_I2C_ADDRESS_2, IS31FL3743A_SYNC_2);
# if defined(IS31FL3743A_I2C_ADDRESS_3)
is31fl3743a_init(IS31FL3743A_I2C_ADDRESS_3, IS31FL3743A_SYNC_3);
# if defined(IS31FL3743A_I2C_ADDRESS_4)
is31fl3743a_init(IS31FL3743A_I2C_ADDRESS_4, IS31FL3743A_SYNC_4);
# endif
# endif
#if defined(IS31FL3743A_SDB_PIN)
gpio_set_pin_output(IS31FL3743A_SDB_PIN);
gpio_write_pin_high(IS31FL3743A_SDB_PIN);
#endif
for (uint8_t i = 0; i < IS31FL3743A_DRIVER_COUNT; i++) {
is31fl3743a_init(i);
}
for (int i = 0; i < IS31FL3743A_LED_COUNT; i++) {
is31fl3743a_set_scaling_register(i, 0xFF);
}
is31fl3743a_update_scaling_registers(IS31FL3743A_I2C_ADDRESS_1, 0);
#if defined(IS31FL3743A_I2C_ADDRESS_2)
is31fl3743a_update_scaling_registers(IS31FL3743A_I2C_ADDRESS_2, 1);
# if defined(IS31FL3743A_I2C_ADDRESS_3)
is31fl3743a_update_scaling_registers(IS31FL3743A_I2C_ADDRESS_3, 2);
# if defined(IS31FL3743A_I2C_ADDRESS_4)
is31fl3743a_update_scaling_registers(IS31FL3743A_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3743A_DRIVER_COUNT; i++) {
is31fl3743a_update_scaling_registers(i);
}
}
void is31fl3743a_init(uint8_t addr, uint8_t sync) {
void is31fl3743a_init(uint8_t index) {
// In order to avoid the LEDs being driven with garbage data
// in the LED driver's PWM registers, shutdown is enabled last.
// Set up the mode and other settings, clear the PWM registers,
// then disable software shutdown.
is31fl3743a_select_page(addr, IS31FL3743A_COMMAND_SCALING);
is31fl3743a_select_page(index, IS31FL3743A_COMMAND_SCALING);
// Turn off all LEDs.
for (uint8_t i = 0; i < IS31FL3743A_SCALING_REGISTER_COUNT; i++) {
is31fl3743a_write_register(addr, i + 1, 0x00);
is31fl3743a_write_register(index, i + 1, 0x00);
}
is31fl3743a_select_page(addr, IS31FL3743A_COMMAND_PWM);
is31fl3743a_select_page(index, IS31FL3743A_COMMAND_PWM);
for (uint8_t i = 0; i < IS31FL3743A_PWM_REGISTER_COUNT; i++) {
is31fl3743a_write_register(addr, i + 1, 0x00);
is31fl3743a_write_register(index, i + 1, 0x00);
}
is31fl3743a_select_page(addr, IS31FL3743A_COMMAND_FUNCTION);
is31fl3743a_select_page(index, IS31FL3743A_COMMAND_FUNCTION);
is31fl3743a_write_register(addr, IS31FL3743A_FUNCTION_REG_PULLDOWNUP, (IS31FL3743A_SW_PULLDOWN << 4) | IS31FL3743A_CS_PULLUP);
is31fl3743a_write_register(addr, IS31FL3743A_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3743A_GLOBAL_CURRENT);
is31fl3743a_write_register(addr, IS31FL3743A_FUNCTION_REG_SPREAD_SPECTRUM, (sync & 0b11) << 6);
is31fl3743a_write_register(addr, IS31FL3743A_FUNCTION_REG_CONFIGURATION, IS31FL3743A_CONFIGURATION);
uint8_t sync = driver_sync[index];
is31fl3743a_write_register(index, IS31FL3743A_FUNCTION_REG_PULLDOWNUP, (IS31FL3743A_SW_PULLDOWN << 4) | IS31FL3743A_CS_PULLUP);
is31fl3743a_write_register(index, IS31FL3743A_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3743A_GLOBAL_CURRENT);
is31fl3743a_write_register(index, IS31FL3743A_FUNCTION_REG_SPREAD_SPECTRUM, (sync & 0b11) << 6);
is31fl3743a_write_register(index, IS31FL3743A_FUNCTION_REG_CONFIGURATION, IS31FL3743A_CONFIGURATION);
// Wait 10ms to ensure the device has woken up.
wait_ms(10);
@ -165,12 +193,12 @@ void is31fl3743a_set_value(int index, uint8_t value) {
if (index >= 0 && index < IS31FL3743A_LED_COUNT) {
memcpy_P(&led, (&g_is31fl3743a_leds[index]), sizeof(led));
if (g_pwm_buffer[led.driver][led.v] == value) {
if (driver_buffers[led.driver].pwm_buffer[led.v] == value) {
return;
}
g_pwm_buffer_update_required[led.driver] = true;
g_pwm_buffer[led.driver][led.v] = value;
driver_buffers[led.driver].pwm_buffer[led.v] = value;
driver_buffers[led.driver].pwm_buffer_dirty = true;
}
}
@ -184,41 +212,34 @@ void is31fl3743a_set_scaling_register(uint8_t index, uint8_t value) {
is31fl3743a_led_t led;
memcpy_P(&led, (&g_is31fl3743a_leds[index]), sizeof(led));
g_scaling_registers[led.driver][led.v] = value;
g_scaling_registers_update_required[led.driver] = true;
driver_buffers[led.driver].scaling_buffer[led.v] = value;
driver_buffers[led.driver].scaling_buffer_dirty = true;
}
void is31fl3743a_update_pwm_buffers(uint8_t addr, uint8_t index) {
if (g_pwm_buffer_update_required[index]) {
is31fl3743a_select_page(addr, IS31FL3743A_COMMAND_PWM);
void is31fl3743a_update_pwm_buffers(uint8_t index) {
if (driver_buffers[index].pwm_buffer_dirty) {
is31fl3743a_select_page(index, IS31FL3743A_COMMAND_PWM);
is31fl3743a_write_pwm_buffer(addr, index);
is31fl3743a_write_pwm_buffer(index);
g_pwm_buffer_update_required[index] = false;
driver_buffers[index].pwm_buffer_dirty = false;
}
}
void is31fl3743a_update_scaling_registers(uint8_t addr, uint8_t index) {
if (g_scaling_registers_update_required[index]) {
is31fl3743a_select_page(addr, IS31FL3743A_COMMAND_SCALING);
void is31fl3743a_update_scaling_registers(uint8_t index) {
if (driver_buffers[index].scaling_buffer_dirty) {
is31fl3743a_select_page(index, IS31FL3743A_COMMAND_SCALING);
for (uint8_t i = 0; i < IS31FL3743A_SCALING_REGISTER_COUNT; i++) {
is31fl3743a_write_register(addr, i + 1, g_scaling_registers[index][i]);
is31fl3743a_write_register(index, i + 1, driver_buffers[index].scaling_buffer[i]);
}
g_scaling_registers_update_required[index] = false;
driver_buffers[index].scaling_buffer_dirty = false;
}
}
void is31fl3743a_flush(void) {
is31fl3743a_update_pwm_buffers(IS31FL3743A_I2C_ADDRESS_1, 0);
#if defined(IS31FL3743A_I2C_ADDRESS_2)
is31fl3743a_update_pwm_buffers(IS31FL3743A_I2C_ADDRESS_2, 1);
# if defined(IS31FL3743A_I2C_ADDRESS_3)
is31fl3743a_update_pwm_buffers(IS31FL3743A_I2C_ADDRESS_3, 2);
# if defined(IS31FL3743A_I2C_ADDRESS_4)
is31fl3743a_update_pwm_buffers(IS31FL3743A_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3743A_DRIVER_COUNT; i++) {
is31fl3743a_update_pwm_buffers(i);
}
}

View File

@ -82,17 +82,17 @@ typedef struct is31fl3743a_led_t {
extern const is31fl3743a_led_t PROGMEM g_is31fl3743a_leds[IS31FL3743A_LED_COUNT];
void is31fl3743a_init_drivers(void);
void is31fl3743a_init(uint8_t addr, uint8_t sync);
void is31fl3743a_write_register(uint8_t addr, uint8_t reg, uint8_t data);
void is31fl3743a_select_page(uint8_t addr, uint8_t page);
void is31fl3743a_init(uint8_t index);
void is31fl3743a_write_register(uint8_t index, uint8_t reg, uint8_t data);
void is31fl3743a_select_page(uint8_t index, uint8_t page);
void is31fl3743a_set_value(int index, uint8_t value);
void is31fl3743a_set_value_all(uint8_t value);
void is31fl3743a_set_scaling_register(uint8_t index, uint8_t value);
void is31fl3743a_update_pwm_buffers(uint8_t addr, uint8_t index);
void is31fl3743a_update_scaling_registers(uint8_t addr, uint8_t index);
void is31fl3743a_update_pwm_buffers(uint8_t index);
void is31fl3743a_update_scaling_registers(uint8_t index);
void is31fl3743a_flush(void);
@ -118,211 +118,211 @@ void is31fl3743a_flush(void);
#define IS31FL3743A_SYNC_SLAVE 0b10
#define IS31FL3743A_SYNC_MASTER 0b11
#define CS1_SW1 0x00
#define CS2_SW1 0x01
#define CS3_SW1 0x02
#define CS4_SW1 0x03
#define CS5_SW1 0x04
#define CS6_SW1 0x05
#define CS7_SW1 0x06
#define CS8_SW1 0x07
#define CS9_SW1 0x08
#define CS10_SW1 0x09
#define CS11_SW1 0x0A
#define CS12_SW1 0x0B
#define CS13_SW1 0x0C
#define CS14_SW1 0x0D
#define CS15_SW1 0x0E
#define CS16_SW1 0x0F
#define CS17_SW1 0x10
#define CS18_SW1 0x11
#define SW1_CS1 0x00
#define SW1_CS2 0x01
#define SW1_CS3 0x02
#define SW1_CS4 0x03
#define SW1_CS5 0x04
#define SW1_CS6 0x05
#define SW1_CS7 0x06
#define SW1_CS8 0x07
#define SW1_CS9 0x08
#define SW1_CS10 0x09
#define SW1_CS11 0x0A
#define SW1_CS12 0x0B
#define SW1_CS13 0x0C
#define SW1_CS14 0x0D
#define SW1_CS15 0x0E
#define SW1_CS16 0x0F
#define SW1_CS17 0x10
#define SW1_CS18 0x11
#define CS1_SW2 0x12
#define CS2_SW2 0x13
#define CS3_SW2 0x14
#define CS4_SW2 0x15
#define CS5_SW2 0x16
#define CS6_SW2 0x17
#define CS7_SW2 0x18
#define CS8_SW2 0x19
#define CS9_SW2 0x1A
#define CS10_SW2 0x1B
#define CS11_SW2 0x1C
#define CS12_SW2 0x1D
#define CS13_SW2 0x1E
#define CS14_SW2 0x1F
#define CS15_SW2 0x20
#define CS16_SW2 0x21
#define CS17_SW2 0x22
#define CS18_SW2 0x23
#define SW2_CS1 0x12
#define SW2_CS2 0x13
#define SW2_CS3 0x14
#define SW2_CS4 0x15
#define SW2_CS5 0x16
#define SW2_CS6 0x17
#define SW2_CS7 0x18
#define SW2_CS8 0x19
#define SW2_CS9 0x1A
#define SW2_CS10 0x1B
#define SW2_CS11 0x1C
#define SW2_CS12 0x1D
#define SW2_CS13 0x1E
#define SW2_CS14 0x1F
#define SW2_CS15 0x20
#define SW2_CS16 0x21
#define SW2_CS17 0x22
#define SW2_CS18 0x23
#define CS1_SW3 0x24
#define CS2_SW3 0x25
#define CS3_SW3 0x26
#define CS4_SW3 0x27
#define CS5_SW3 0x28
#define CS6_SW3 0x29
#define CS7_SW3 0x2A
#define CS8_SW3 0x2B
#define CS9_SW3 0x2C
#define CS10_SW3 0x2D
#define CS11_SW3 0x2E
#define CS12_SW3 0x2F
#define CS13_SW3 0x30
#define CS14_SW3 0x31
#define CS15_SW3 0x32
#define CS16_SW3 0x33
#define CS17_SW3 0x34
#define CS18_SW3 0x35
#define SW3_CS1 0x24
#define SW3_CS2 0x25
#define SW3_CS3 0x26
#define SW3_CS4 0x27
#define SW3_CS5 0x28
#define SW3_CS6 0x29
#define SW3_CS7 0x2A
#define SW3_CS8 0x2B
#define SW3_CS9 0x2C
#define SW3_CS10 0x2D
#define SW3_CS11 0x2E
#define SW3_CS12 0x2F
#define SW3_CS13 0x30
#define SW3_CS14 0x31
#define SW3_CS15 0x32
#define SW3_CS16 0x33
#define SW3_CS17 0x34
#define SW3_CS18 0x35
#define CS1_SW4 0x36
#define CS2_SW4 0x37
#define CS3_SW4 0x38
#define CS4_SW4 0x39
#define CS5_SW4 0x3A
#define CS6_SW4 0x3B
#define CS7_SW4 0x3C
#define CS8_SW4 0x3D
#define CS9_SW4 0x3E
#define CS10_SW4 0x3F
#define CS11_SW4 0x40
#define CS12_SW4 0x41
#define CS13_SW4 0x42
#define CS14_SW4 0x43
#define CS15_SW4 0x44
#define CS16_SW4 0x45
#define CS17_SW4 0x46
#define CS18_SW4 0x47
#define SW4_CS1 0x36
#define SW4_CS2 0x37
#define SW4_CS3 0x38
#define SW4_CS4 0x39
#define SW4_CS5 0x3A
#define SW4_CS6 0x3B
#define SW4_CS7 0x3C
#define SW4_CS8 0x3D
#define SW4_CS9 0x3E
#define SW4_CS10 0x3F
#define SW4_CS11 0x40
#define SW4_CS12 0x41
#define SW4_CS13 0x42
#define SW4_CS14 0x43
#define SW4_CS15 0x44
#define SW4_CS16 0x45
#define SW4_CS17 0x46
#define SW4_CS18 0x47
#define CS1_SW5 0x48
#define CS2_SW5 0x49
#define CS3_SW5 0x4A
#define CS4_SW5 0x4B
#define CS5_SW5 0x4C
#define CS6_SW5 0x4D
#define CS7_SW5 0x4E
#define CS8_SW5 0x4F
#define CS9_SW5 0x50
#define CS10_SW5 0x51
#define CS11_SW5 0x52
#define CS12_SW5 0x53
#define CS13_SW5 0x54
#define CS14_SW5 0x55
#define CS15_SW5 0x56
#define CS16_SW5 0x57
#define CS17_SW5 0x58
#define CS18_SW5 0x59
#define SW5_CS1 0x48
#define SW5_CS2 0x49
#define SW5_CS3 0x4A
#define SW5_CS4 0x4B
#define SW5_CS5 0x4C
#define SW5_CS6 0x4D
#define SW5_CS7 0x4E
#define SW5_CS8 0x4F
#define SW5_CS9 0x50
#define SW5_CS10 0x51
#define SW5_CS11 0x52
#define SW5_CS12 0x53
#define SW5_CS13 0x54
#define SW5_CS14 0x55
#define SW5_CS15 0x56
#define SW5_CS16 0x57
#define SW5_CS17 0x58
#define SW5_CS18 0x59
#define CS1_SW6 0x5A
#define CS2_SW6 0x5B
#define CS3_SW6 0x5C
#define CS4_SW6 0x5D
#define CS5_SW6 0x5E
#define CS6_SW6 0x5F
#define CS7_SW6 0x60
#define CS8_SW6 0x61
#define CS9_SW6 0x62
#define CS10_SW6 0x63
#define CS11_SW6 0x64
#define CS12_SW6 0x65
#define CS13_SW6 0x66
#define CS14_SW6 0x67
#define CS15_SW6 0x68
#define CS16_SW6 0x69
#define CS17_SW6 0x6A
#define CS18_SW6 0x6B
#define SW6_CS1 0x5A
#define SW6_CS2 0x5B
#define SW6_CS3 0x5C
#define SW6_CS4 0x5D
#define SW6_CS5 0x5E
#define SW6_CS6 0x5F
#define SW6_CS7 0x60
#define SW6_CS8 0x61
#define SW6_CS9 0x62
#define SW6_CS10 0x63
#define SW6_CS11 0x64
#define SW6_CS12 0x65
#define SW6_CS13 0x66
#define SW6_CS14 0x67
#define SW6_CS15 0x68
#define SW6_CS16 0x69
#define SW6_CS17 0x6A
#define SW6_CS18 0x6B
#define CS1_SW7 0x6C
#define CS2_SW7 0x6D
#define CS3_SW7 0x6E
#define CS4_SW7 0x6F
#define CS5_SW7 0x70
#define CS6_SW7 0x71
#define CS7_SW7 0x72
#define CS8_SW7 0x73
#define CS9_SW7 0x74
#define CS10_SW7 0x75
#define CS11_SW7 0x76
#define CS12_SW7 0x77
#define CS13_SW7 0x78
#define CS14_SW7 0x79
#define CS15_SW7 0x7A
#define CS16_SW7 0x7B
#define CS17_SW7 0x7C
#define CS18_SW7 0x7D
#define SW7_CS1 0x6C
#define SW7_CS2 0x6D
#define SW7_CS3 0x6E
#define SW7_CS4 0x6F
#define SW7_CS5 0x70
#define SW7_CS6 0x71
#define SW7_CS7 0x72
#define SW7_CS8 0x73
#define SW7_CS9 0x74
#define SW7_CS10 0x75
#define SW7_CS11 0x76
#define SW7_CS12 0x77
#define SW7_CS13 0x78
#define SW7_CS14 0x79
#define SW7_CS15 0x7A
#define SW7_CS16 0x7B
#define SW7_CS17 0x7C
#define SW7_CS18 0x7D
#define CS1_SW8 0x7E
#define CS2_SW8 0x7F
#define CS3_SW8 0x80
#define CS4_SW8 0x81
#define CS5_SW8 0x82
#define CS6_SW8 0x83
#define CS7_SW8 0x84
#define CS8_SW8 0x85
#define CS9_SW8 0x86
#define CS10_SW8 0x87
#define CS11_SW8 0x88
#define CS12_SW8 0x89
#define CS13_SW8 0x8A
#define CS14_SW8 0x8B
#define CS15_SW8 0x8C
#define CS16_SW8 0x8D
#define CS17_SW8 0x8E
#define CS18_SW8 0x8F
#define SW8_CS1 0x7E
#define SW8_CS2 0x7F
#define SW8_CS3 0x80
#define SW8_CS4 0x81
#define SW8_CS5 0x82
#define SW8_CS6 0x83
#define SW8_CS7 0x84
#define SW8_CS8 0x85
#define SW8_CS9 0x86
#define SW8_CS10 0x87
#define SW8_CS11 0x88
#define SW8_CS12 0x89
#define SW8_CS13 0x8A
#define SW8_CS14 0x8B
#define SW8_CS15 0x8C
#define SW8_CS16 0x8D
#define SW8_CS17 0x8E
#define SW8_CS18 0x8F
#define CS1_SW9 0x90
#define CS2_SW9 0x91
#define CS3_SW9 0x92
#define CS4_SW9 0x93
#define CS5_SW9 0x94
#define CS6_SW9 0x95
#define CS7_SW9 0x96
#define CS8_SW9 0x97
#define CS9_SW9 0x98
#define CS10_SW9 0x99
#define CS11_SW9 0x9A
#define CS12_SW9 0x9B
#define CS13_SW9 0x9C
#define CS14_SW9 0x9D
#define CS15_SW9 0x9E
#define CS16_SW9 0x9F
#define CS17_SW9 0xA0
#define CS18_SW9 0xA1
#define SW9_CS1 0x90
#define SW9_CS2 0x91
#define SW9_CS3 0x92
#define SW9_CS4 0x93
#define SW9_CS5 0x94
#define SW9_CS6 0x95
#define SW9_CS7 0x96
#define SW9_CS8 0x97
#define SW9_CS9 0x98
#define SW9_CS10 0x99
#define SW9_CS11 0x9A
#define SW9_CS12 0x9B
#define SW9_CS13 0x9C
#define SW9_CS14 0x9D
#define SW9_CS15 0x9E
#define SW9_CS16 0x9F
#define SW9_CS17 0xA0
#define SW9_CS18 0xA1
#define CS1_SW10 0xA2
#define CS2_SW10 0xA3
#define CS3_SW10 0xA4
#define CS4_SW10 0xA5
#define CS5_SW10 0xA6
#define CS6_SW10 0xA7
#define CS7_SW10 0xA8
#define CS8_SW10 0xA9
#define CS9_SW10 0xAA
#define CS10_SW10 0xAB
#define CS11_SW10 0xAC
#define CS12_SW10 0xAD
#define CS13_SW10 0xAE
#define CS14_SW10 0xAF
#define CS15_SW10 0xB0
#define CS16_SW10 0xB1
#define CS17_SW10 0xB2
#define CS18_SW10 0xB3
#define SW10_CS1 0xA2
#define SW10_CS2 0xA3
#define SW10_CS3 0xA4
#define SW10_CS4 0xA5
#define SW10_CS5 0xA6
#define SW10_CS6 0xA7
#define SW10_CS7 0xA8
#define SW10_CS8 0xA9
#define SW10_CS9 0xAA
#define SW10_CS10 0xAB
#define SW10_CS11 0xAC
#define SW10_CS12 0xAD
#define SW10_CS13 0xAE
#define SW10_CS14 0xAF
#define SW10_CS15 0xB0
#define SW10_CS16 0xB1
#define SW10_CS17 0xB2
#define SW10_CS18 0xB3
#define CS1_SW11 0xB4
#define CS2_SW11 0xB5
#define CS3_SW11 0xB6
#define CS4_SW11 0xB7
#define CS5_SW11 0xB8
#define CS6_SW11 0xB9
#define CS7_SW11 0xBA
#define CS8_SW11 0xBB
#define CS9_SW11 0xBC
#define CS10_SW11 0xBD
#define CS11_SW11 0xBE
#define CS12_SW11 0xBF
#define CS13_SW11 0xC0
#define CS14_SW11 0xC1
#define CS15_SW11 0xC2
#define CS16_SW11 0xC3
#define CS17_SW11 0xC4
#define CS18_SW11 0xC5
#define SW11_CS1 0xB4
#define SW11_CS2 0xB5
#define SW11_CS3 0xB6
#define SW11_CS4 0xB7
#define SW11_CS5 0xB8
#define SW11_CS6 0xB9
#define SW11_CS7 0xBA
#define SW11_CS8 0xBB
#define SW11_CS9 0xBC
#define SW11_CS10 0xBD
#define SW11_CS11 0xBE
#define SW11_CS12 0xBF
#define SW11_CS13 0xC0
#define SW11_CS14 0xC1
#define SW11_CS15 0xC2
#define SW11_CS16 0xC3
#define SW11_CS17 0xC4
#define SW11_CS18 0xC5

View File

@ -20,6 +20,7 @@
#include "is31fl3743a.h"
#include "i2c_master.h"
#include "gpio.h"
#include "wait.h"
#define IS31FL3743A_PWM_REGISTER_COUNT 198
@ -62,28 +63,62 @@
# define IS31FL3743A_SYNC_4 IS31FL3743A_SYNC_NONE
#endif
uint8_t g_pwm_buffer[IS31FL3743A_DRIVER_COUNT][IS31FL3743A_PWM_REGISTER_COUNT];
bool g_pwm_buffer_update_required[IS31FL3743A_DRIVER_COUNT] = {false};
bool g_scaling_registers_update_required[IS31FL3743A_DRIVER_COUNT] = {false};
const uint8_t i2c_addresses[IS31FL3743A_DRIVER_COUNT] = {
IS31FL3743A_I2C_ADDRESS_1,
#ifdef IS31FL3743A_I2C_ADDRESS_2
IS31FL3743A_I2C_ADDRESS_2,
# ifdef IS31FL3743A_I2C_ADDRESS_3
IS31FL3743A_I2C_ADDRESS_3,
# ifdef IS31FL3743A_I2C_ADDRESS_4
IS31FL3743A_I2C_ADDRESS_4,
# endif
# endif
#endif
};
uint8_t g_scaling_registers[IS31FL3743A_DRIVER_COUNT][IS31FL3743A_SCALING_REGISTER_COUNT];
const uint8_t driver_sync[IS31FL3743A_DRIVER_COUNT] = {
IS31FL3743A_SYNC_1,
#ifdef IS31FL3743A_I2C_ADDRESS_2
IS31FL3743A_SYNC_2,
# ifdef IS31FL3743A_I2C_ADDRESS_3
IS31FL3743A_SYNC_3,
# ifdef IS31FL3743A_I2C_ADDRESS_4
IS31FL3743A_SYNC_4,
# endif
# endif
#endif
};
void is31fl3743a_write_register(uint8_t addr, uint8_t reg, uint8_t data) {
typedef struct is31fl3743a_driver_t {
uint8_t pwm_buffer[IS31FL3743A_PWM_REGISTER_COUNT];
bool pwm_buffer_dirty;
uint8_t scaling_buffer[IS31FL3743A_SCALING_REGISTER_COUNT];
bool scaling_buffer_dirty;
} PACKED is31fl3743a_driver_t;
is31fl3743a_driver_t driver_buffers[IS31FL3743A_DRIVER_COUNT] = {{
.pwm_buffer = {0},
.pwm_buffer_dirty = false,
.scaling_buffer = {0},
.scaling_buffer_dirty = false,
}};
void is31fl3743a_write_register(uint8_t index, uint8_t reg, uint8_t data) {
#if IS31FL3743A_I2C_PERSISTENCE > 0
for (uint8_t i = 0; i < IS31FL3743A_I2C_PERSISTENCE; i++) {
if (i2c_write_register(addr << 1, reg, &data, 1, IS31FL3743A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3743A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, reg, &data, 1, IS31FL3743A_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3743A_I2C_TIMEOUT);
#endif
}
void is31fl3743a_select_page(uint8_t addr, uint8_t page) {
is31fl3743a_write_register(addr, IS31FL3743A_REG_COMMAND_WRITE_LOCK, IS31FL3743A_COMMAND_WRITE_LOCK_MAGIC);
is31fl3743a_write_register(addr, IS31FL3743A_REG_COMMAND, page);
void is31fl3743a_select_page(uint8_t index, uint8_t page) {
is31fl3743a_write_register(index, IS31FL3743A_REG_COMMAND_WRITE_LOCK, IS31FL3743A_COMMAND_WRITE_LOCK_MAGIC);
is31fl3743a_write_register(index, IS31FL3743A_REG_COMMAND, page);
}
void is31fl3743a_write_pwm_buffer(uint8_t addr, uint8_t index) {
void is31fl3743a_write_pwm_buffer(uint8_t index) {
// Assumes page 0 is already selected.
// Transmit PWM registers in 11 transfers of 18 bytes.
@ -91,10 +126,10 @@ void is31fl3743a_write_pwm_buffer(uint8_t addr, uint8_t index) {
for (uint8_t i = 0; i < IS31FL3743A_PWM_REGISTER_COUNT; i += 18) {
#if IS31FL3743A_I2C_PERSISTENCE > 0
for (uint8_t j = 0; j < IS31FL3743A_I2C_PERSISTENCE; j++) {
if (i2c_write_register(addr << 1, i + 1, g_pwm_buffer[index] + i, 18, IS31FL3743A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, i + 1, driver_buffers[index].pwm_buffer + i, 18, IS31FL3743A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, i + 1, g_pwm_buffer[index] + i, 18, IS31FL3743A_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, i + 1, driver_buffers[index].pwm_buffer + i, 18, IS31FL3743A_I2C_TIMEOUT);
#endif
}
}
@ -102,58 +137,51 @@ void is31fl3743a_write_pwm_buffer(uint8_t addr, uint8_t index) {
void is31fl3743a_init_drivers(void) {
i2c_init();
is31fl3743a_init(IS31FL3743A_I2C_ADDRESS_1, IS31FL3743A_SYNC_1);
#if defined(IS31FL3743A_I2C_ADDRESS_2)
is31fl3743a_init(IS31FL3743A_I2C_ADDRESS_2, IS31FL3743A_SYNC_2);
# if defined(IS31FL3743A_I2C_ADDRESS_3)
is31fl3743a_init(IS31FL3743A_I2C_ADDRESS_3, IS31FL3743A_SYNC_3);
# if defined(IS31FL3743A_I2C_ADDRESS_4)
is31fl3743a_init(IS31FL3743A_I2C_ADDRESS_4, IS31FL3743A_SYNC_4);
# endif
# endif
#if defined(IS31FL3743A_SDB_PIN)
gpio_set_pin_output(IS31FL3743A_SDB_PIN);
gpio_write_pin_high(IS31FL3743A_SDB_PIN);
#endif
for (uint8_t i = 0; i < IS31FL3743A_DRIVER_COUNT; i++) {
is31fl3743a_init(i);
}
for (int i = 0; i < IS31FL3743A_LED_COUNT; i++) {
is31fl3743a_set_scaling_register(i, 0xFF, 0xFF, 0xFF);
}
is31fl3743a_update_scaling_registers(IS31FL3743A_I2C_ADDRESS_1, 0);
#if defined(IS31FL3743A_I2C_ADDRESS_2)
is31fl3743a_update_scaling_registers(IS31FL3743A_I2C_ADDRESS_2, 1);
# if defined(IS31FL3743A_I2C_ADDRESS_3)
is31fl3743a_update_scaling_registers(IS31FL3743A_I2C_ADDRESS_3, 2);
# if defined(IS31FL3743A_I2C_ADDRESS_4)
is31fl3743a_update_scaling_registers(IS31FL3743A_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3743A_DRIVER_COUNT; i++) {
is31fl3743a_update_scaling_registers(i);
}
}
void is31fl3743a_init(uint8_t addr, uint8_t sync) {
void is31fl3743a_init(uint8_t index) {
// In order to avoid the LEDs being driven with garbage data
// in the LED driver's PWM registers, shutdown is enabled last.
// Set up the mode and other settings, clear the PWM registers,
// then disable software shutdown.
is31fl3743a_select_page(addr, IS31FL3743A_COMMAND_SCALING);
is31fl3743a_select_page(index, IS31FL3743A_COMMAND_SCALING);
// Turn off all LEDs.
for (uint8_t i = 0; i < IS31FL3743A_SCALING_REGISTER_COUNT; i++) {
is31fl3743a_write_register(addr, i + 1, 0x00);
is31fl3743a_write_register(index, i + 1, 0x00);
}
is31fl3743a_select_page(addr, IS31FL3743A_COMMAND_PWM);
is31fl3743a_select_page(index, IS31FL3743A_COMMAND_PWM);
for (uint8_t i = 0; i < IS31FL3743A_PWM_REGISTER_COUNT; i++) {
is31fl3743a_write_register(addr, i + 1, 0x00);
is31fl3743a_write_register(index, i + 1, 0x00);
}
is31fl3743a_select_page(addr, IS31FL3743A_COMMAND_FUNCTION);
is31fl3743a_select_page(index, IS31FL3743A_COMMAND_FUNCTION);
is31fl3743a_write_register(addr, IS31FL3743A_FUNCTION_REG_PULLDOWNUP, (IS31FL3743A_SW_PULLDOWN << 4) | IS31FL3743A_CS_PULLUP);
is31fl3743a_write_register(addr, IS31FL3743A_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3743A_GLOBAL_CURRENT);
is31fl3743a_write_register(addr, IS31FL3743A_FUNCTION_REG_SPREAD_SPECTRUM, (sync & 0b11) << 6);
is31fl3743a_write_register(addr, IS31FL3743A_FUNCTION_REG_CONFIGURATION, IS31FL3743A_CONFIGURATION);
uint8_t sync = driver_sync[index];
is31fl3743a_write_register(index, IS31FL3743A_FUNCTION_REG_PULLDOWNUP, (IS31FL3743A_SW_PULLDOWN << 4) | IS31FL3743A_CS_PULLUP);
is31fl3743a_write_register(index, IS31FL3743A_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3743A_GLOBAL_CURRENT);
is31fl3743a_write_register(index, IS31FL3743A_FUNCTION_REG_SPREAD_SPECTRUM, (sync & 0b11) << 6);
is31fl3743a_write_register(index, IS31FL3743A_FUNCTION_REG_CONFIGURATION, IS31FL3743A_CONFIGURATION);
// Wait 10ms to ensure the device has woken up.
wait_ms(10);
@ -165,14 +193,14 @@ void is31fl3743a_set_color(int index, uint8_t red, uint8_t green, uint8_t blue)
if (index >= 0 && index < IS31FL3743A_LED_COUNT) {
memcpy_P(&led, (&g_is31fl3743a_leds[index]), sizeof(led));
if (g_pwm_buffer[led.driver][led.r] == red && g_pwm_buffer[led.driver][led.g] == green && g_pwm_buffer[led.driver][led.b] == blue) {
if (driver_buffers[led.driver].pwm_buffer[led.r] == red && driver_buffers[led.driver].pwm_buffer[led.g] == green && driver_buffers[led.driver].pwm_buffer[led.b] == blue) {
return;
}
g_pwm_buffer_update_required[led.driver] = true;
g_pwm_buffer[led.driver][led.r] = red;
g_pwm_buffer[led.driver][led.g] = green;
g_pwm_buffer[led.driver][led.b] = blue;
driver_buffers[led.driver].pwm_buffer[led.r] = red;
driver_buffers[led.driver].pwm_buffer[led.g] = green;
driver_buffers[led.driver].pwm_buffer[led.b] = blue;
driver_buffers[led.driver].pwm_buffer_dirty = true;
}
}
@ -186,43 +214,36 @@ void is31fl3743a_set_scaling_register(uint8_t index, uint8_t red, uint8_t green,
is31fl3743a_led_t led;
memcpy_P(&led, (&g_is31fl3743a_leds[index]), sizeof(led));
g_scaling_registers[led.driver][led.r] = red;
g_scaling_registers[led.driver][led.g] = green;
g_scaling_registers[led.driver][led.b] = blue;
g_scaling_registers_update_required[led.driver] = true;
driver_buffers[led.driver].scaling_buffer[led.r] = red;
driver_buffers[led.driver].scaling_buffer[led.g] = green;
driver_buffers[led.driver].scaling_buffer[led.b] = blue;
driver_buffers[led.driver].scaling_buffer_dirty = true;
}
void is31fl3743a_update_pwm_buffers(uint8_t addr, uint8_t index) {
if (g_pwm_buffer_update_required[index]) {
is31fl3743a_select_page(addr, IS31FL3743A_COMMAND_PWM);
void is31fl3743a_update_pwm_buffers(uint8_t index) {
if (driver_buffers[index].pwm_buffer_dirty) {
is31fl3743a_select_page(index, IS31FL3743A_COMMAND_PWM);
is31fl3743a_write_pwm_buffer(addr, index);
is31fl3743a_write_pwm_buffer(index);
g_pwm_buffer_update_required[index] = false;
driver_buffers[index].pwm_buffer_dirty = false;
}
}
void is31fl3743a_update_scaling_registers(uint8_t addr, uint8_t index) {
if (g_scaling_registers_update_required[index]) {
is31fl3743a_select_page(addr, IS31FL3743A_COMMAND_SCALING);
void is31fl3743a_update_scaling_registers(uint8_t index) {
if (driver_buffers[index].scaling_buffer_dirty) {
is31fl3743a_select_page(index, IS31FL3743A_COMMAND_SCALING);
for (uint8_t i = 0; i < IS31FL3743A_SCALING_REGISTER_COUNT; i++) {
is31fl3743a_write_register(addr, i + 1, g_scaling_registers[index][i]);
is31fl3743a_write_register(index, i + 1, driver_buffers[index].scaling_buffer[i]);
}
g_scaling_registers_update_required[index] = false;
driver_buffers[index].scaling_buffer_dirty = false;
}
}
void is31fl3743a_flush(void) {
is31fl3743a_update_pwm_buffers(IS31FL3743A_I2C_ADDRESS_1, 0);
#if defined(IS31FL3743A_I2C_ADDRESS_2)
is31fl3743a_update_pwm_buffers(IS31FL3743A_I2C_ADDRESS_2, 1);
# if defined(IS31FL3743A_I2C_ADDRESS_3)
is31fl3743a_update_pwm_buffers(IS31FL3743A_I2C_ADDRESS_3, 2);
# if defined(IS31FL3743A_I2C_ADDRESS_4)
is31fl3743a_update_pwm_buffers(IS31FL3743A_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3743A_DRIVER_COUNT; i++) {
is31fl3743a_update_pwm_buffers(i);
}
}

View File

@ -84,17 +84,17 @@ typedef struct is31fl3743a_led_t {
extern const is31fl3743a_led_t PROGMEM g_is31fl3743a_leds[IS31FL3743A_LED_COUNT];
void is31fl3743a_init_drivers(void);
void is31fl3743a_init(uint8_t addr, uint8_t sync);
void is31fl3743a_write_register(uint8_t addr, uint8_t reg, uint8_t data);
void is31fl3743a_select_page(uint8_t addr, uint8_t page);
void is31fl3743a_init(uint8_t index);
void is31fl3743a_write_register(uint8_t index, uint8_t reg, uint8_t data);
void is31fl3743a_select_page(uint8_t index, uint8_t page);
void is31fl3743a_set_color(int index, uint8_t red, uint8_t green, uint8_t blue);
void is31fl3743a_set_color_all(uint8_t red, uint8_t green, uint8_t blue);
void is31fl3743a_set_scaling_register(uint8_t index, uint8_t red, uint8_t green, uint8_t blue);
void is31fl3743a_update_pwm_buffers(uint8_t addr, uint8_t index);
void is31fl3743a_update_scaling_registers(uint8_t addr, uint8_t index);
void is31fl3743a_update_pwm_buffers(uint8_t index);
void is31fl3743a_update_scaling_registers(uint8_t index);
void is31fl3743a_flush(void);
@ -120,211 +120,422 @@ void is31fl3743a_flush(void);
#define IS31FL3743A_SYNC_SLAVE 0b10
#define IS31FL3743A_SYNC_MASTER 0b11
#define CS1_SW1 0x00
#define CS2_SW1 0x01
#define CS3_SW1 0x02
#define CS4_SW1 0x03
#define CS5_SW1 0x04
#define CS6_SW1 0x05
#define CS7_SW1 0x06
#define CS8_SW1 0x07
#define CS9_SW1 0x08
#define CS10_SW1 0x09
#define CS11_SW1 0x0A
#define CS12_SW1 0x0B
#define CS13_SW1 0x0C
#define CS14_SW1 0x0D
#define CS15_SW1 0x0E
#define CS16_SW1 0x0F
#define CS17_SW1 0x10
#define CS18_SW1 0x11
#define SW1_CS1 0x00
#define SW1_CS2 0x01
#define SW1_CS3 0x02
#define SW1_CS4 0x03
#define SW1_CS5 0x04
#define SW1_CS6 0x05
#define SW1_CS7 0x06
#define SW1_CS8 0x07
#define SW1_CS9 0x08
#define SW1_CS10 0x09
#define SW1_CS11 0x0A
#define SW1_CS12 0x0B
#define SW1_CS13 0x0C
#define SW1_CS14 0x0D
#define SW1_CS15 0x0E
#define SW1_CS16 0x0F
#define SW1_CS17 0x10
#define SW1_CS18 0x11
#define CS1_SW2 0x12
#define CS2_SW2 0x13
#define CS3_SW2 0x14
#define CS4_SW2 0x15
#define CS5_SW2 0x16
#define CS6_SW2 0x17
#define CS7_SW2 0x18
#define CS8_SW2 0x19
#define CS9_SW2 0x1A
#define CS10_SW2 0x1B
#define CS11_SW2 0x1C
#define CS12_SW2 0x1D
#define CS13_SW2 0x1E
#define CS14_SW2 0x1F
#define CS15_SW2 0x20
#define CS16_SW2 0x21
#define CS17_SW2 0x22
#define CS18_SW2 0x23
#define SW2_CS1 0x12
#define SW2_CS2 0x13
#define SW2_CS3 0x14
#define SW2_CS4 0x15
#define SW2_CS5 0x16
#define SW2_CS6 0x17
#define SW2_CS7 0x18
#define SW2_CS8 0x19
#define SW2_CS9 0x1A
#define SW2_CS10 0x1B
#define SW2_CS11 0x1C
#define SW2_CS12 0x1D
#define SW2_CS13 0x1E
#define SW2_CS14 0x1F
#define SW2_CS15 0x20
#define SW2_CS16 0x21
#define SW2_CS17 0x22
#define SW2_CS18 0x23
#define CS1_SW3 0x24
#define CS2_SW3 0x25
#define CS3_SW3 0x26
#define CS4_SW3 0x27
#define CS5_SW3 0x28
#define CS6_SW3 0x29
#define CS7_SW3 0x2A
#define CS8_SW3 0x2B
#define CS9_SW3 0x2C
#define CS10_SW3 0x2D
#define CS11_SW3 0x2E
#define CS12_SW3 0x2F
#define CS13_SW3 0x30
#define CS14_SW3 0x31
#define CS15_SW3 0x32
#define CS16_SW3 0x33
#define CS17_SW3 0x34
#define CS18_SW3 0x35
#define SW3_CS1 0x24
#define SW3_CS2 0x25
#define SW3_CS3 0x26
#define SW3_CS4 0x27
#define SW3_CS5 0x28
#define SW3_CS6 0x29
#define SW3_CS7 0x2A
#define SW3_CS8 0x2B
#define SW3_CS9 0x2C
#define SW3_CS10 0x2D
#define SW3_CS11 0x2E
#define SW3_CS12 0x2F
#define SW3_CS13 0x30
#define SW3_CS14 0x31
#define SW3_CS15 0x32
#define SW3_CS16 0x33
#define SW3_CS17 0x34
#define SW3_CS18 0x35
#define CS1_SW4 0x36
#define CS2_SW4 0x37
#define CS3_SW4 0x38
#define CS4_SW4 0x39
#define CS5_SW4 0x3A
#define CS6_SW4 0x3B
#define CS7_SW4 0x3C
#define CS8_SW4 0x3D
#define CS9_SW4 0x3E
#define CS10_SW4 0x3F
#define CS11_SW4 0x40
#define CS12_SW4 0x41
#define CS13_SW4 0x42
#define CS14_SW4 0x43
#define CS15_SW4 0x44
#define CS16_SW4 0x45
#define CS17_SW4 0x46
#define CS18_SW4 0x47
#define SW4_CS1 0x36
#define SW4_CS2 0x37
#define SW4_CS3 0x38
#define SW4_CS4 0x39
#define SW4_CS5 0x3A
#define SW4_CS6 0x3B
#define SW4_CS7 0x3C
#define SW4_CS8 0x3D
#define SW4_CS9 0x3E
#define SW4_CS10 0x3F
#define SW4_CS11 0x40
#define SW4_CS12 0x41
#define SW4_CS13 0x42
#define SW4_CS14 0x43
#define SW4_CS15 0x44
#define SW4_CS16 0x45
#define SW4_CS17 0x46
#define SW4_CS18 0x47
#define CS1_SW5 0x48
#define CS2_SW5 0x49
#define CS3_SW5 0x4A
#define CS4_SW5 0x4B
#define CS5_SW5 0x4C
#define CS6_SW5 0x4D
#define CS7_SW5 0x4E
#define CS8_SW5 0x4F
#define CS9_SW5 0x50
#define CS10_SW5 0x51
#define CS11_SW5 0x52
#define CS12_SW5 0x53
#define CS13_SW5 0x54
#define CS14_SW5 0x55
#define CS15_SW5 0x56
#define CS16_SW5 0x57
#define CS17_SW5 0x58
#define CS18_SW5 0x59
#define SW5_CS1 0x48
#define SW5_CS2 0x49
#define SW5_CS3 0x4A
#define SW5_CS4 0x4B
#define SW5_CS5 0x4C
#define SW5_CS6 0x4D
#define SW5_CS7 0x4E
#define SW5_CS8 0x4F
#define SW5_CS9 0x50
#define SW5_CS10 0x51
#define SW5_CS11 0x52
#define SW5_CS12 0x53
#define SW5_CS13 0x54
#define SW5_CS14 0x55
#define SW5_CS15 0x56
#define SW5_CS16 0x57
#define SW5_CS17 0x58
#define SW5_CS18 0x59
#define CS1_SW6 0x5A
#define CS2_SW6 0x5B
#define CS3_SW6 0x5C
#define CS4_SW6 0x5D
#define CS5_SW6 0x5E
#define CS6_SW6 0x5F
#define CS7_SW6 0x60
#define CS8_SW6 0x61
#define CS9_SW6 0x62
#define CS10_SW6 0x63
#define CS11_SW6 0x64
#define CS12_SW6 0x65
#define CS13_SW6 0x66
#define CS14_SW6 0x67
#define CS15_SW6 0x68
#define CS16_SW6 0x69
#define CS17_SW6 0x6A
#define CS18_SW6 0x6B
#define SW6_CS1 0x5A
#define SW6_CS2 0x5B
#define SW6_CS3 0x5C
#define SW6_CS4 0x5D
#define SW6_CS5 0x5E
#define SW6_CS6 0x5F
#define SW6_CS7 0x60
#define SW6_CS8 0x61
#define SW6_CS9 0x62
#define SW6_CS10 0x63
#define SW6_CS11 0x64
#define SW6_CS12 0x65
#define SW6_CS13 0x66
#define SW6_CS14 0x67
#define SW6_CS15 0x68
#define SW6_CS16 0x69
#define SW6_CS17 0x6A
#define SW6_CS18 0x6B
#define CS1_SW7 0x6C
#define CS2_SW7 0x6D
#define CS3_SW7 0x6E
#define CS4_SW7 0x6F
#define CS5_SW7 0x70
#define CS6_SW7 0x71
#define CS7_SW7 0x72
#define CS8_SW7 0x73
#define CS9_SW7 0x74
#define CS10_SW7 0x75
#define CS11_SW7 0x76
#define CS12_SW7 0x77
#define CS13_SW7 0x78
#define CS14_SW7 0x79
#define CS15_SW7 0x7A
#define CS16_SW7 0x7B
#define CS17_SW7 0x7C
#define CS18_SW7 0x7D
#define SW7_CS1 0x6C
#define SW7_CS2 0x6D
#define SW7_CS3 0x6E
#define SW7_CS4 0x6F
#define SW7_CS5 0x70
#define SW7_CS6 0x71
#define SW7_CS7 0x72
#define SW7_CS8 0x73
#define SW7_CS9 0x74
#define SW7_CS10 0x75
#define SW7_CS11 0x76
#define SW7_CS12 0x77
#define SW7_CS13 0x78
#define SW7_CS14 0x79
#define SW7_CS15 0x7A
#define SW7_CS16 0x7B
#define SW7_CS17 0x7C
#define SW7_CS18 0x7D
#define CS1_SW8 0x7E
#define CS2_SW8 0x7F
#define CS3_SW8 0x80
#define CS4_SW8 0x81
#define CS5_SW8 0x82
#define CS6_SW8 0x83
#define CS7_SW8 0x84
#define CS8_SW8 0x85
#define CS9_SW8 0x86
#define CS10_SW8 0x87
#define CS11_SW8 0x88
#define CS12_SW8 0x89
#define CS13_SW8 0x8A
#define CS14_SW8 0x8B
#define CS15_SW8 0x8C
#define CS16_SW8 0x8D
#define CS17_SW8 0x8E
#define CS18_SW8 0x8F
#define SW8_CS1 0x7E
#define SW8_CS2 0x7F
#define SW8_CS3 0x80
#define SW8_CS4 0x81
#define SW8_CS5 0x82
#define SW8_CS6 0x83
#define SW8_CS7 0x84
#define SW8_CS8 0x85
#define SW8_CS9 0x86
#define SW8_CS10 0x87
#define SW8_CS11 0x88
#define SW8_CS12 0x89
#define SW8_CS13 0x8A
#define SW8_CS14 0x8B
#define SW8_CS15 0x8C
#define SW8_CS16 0x8D
#define SW8_CS17 0x8E
#define SW8_CS18 0x8F
#define CS1_SW9 0x90
#define CS2_SW9 0x91
#define CS3_SW9 0x92
#define CS4_SW9 0x93
#define CS5_SW9 0x94
#define CS6_SW9 0x95
#define CS7_SW9 0x96
#define CS8_SW9 0x97
#define CS9_SW9 0x98
#define CS10_SW9 0x99
#define CS11_SW9 0x9A
#define CS12_SW9 0x9B
#define CS13_SW9 0x9C
#define CS14_SW9 0x9D
#define CS15_SW9 0x9E
#define CS16_SW9 0x9F
#define CS17_SW9 0xA0
#define CS18_SW9 0xA1
#define SW9_CS1 0x90
#define SW9_CS2 0x91
#define SW9_CS3 0x92
#define SW9_CS4 0x93
#define SW9_CS5 0x94
#define SW9_CS6 0x95
#define SW9_CS7 0x96
#define SW9_CS8 0x97
#define SW9_CS9 0x98
#define SW9_CS10 0x99
#define SW9_CS11 0x9A
#define SW9_CS12 0x9B
#define SW9_CS13 0x9C
#define SW9_CS14 0x9D
#define SW9_CS15 0x9E
#define SW9_CS16 0x9F
#define SW9_CS17 0xA0
#define SW9_CS18 0xA1
#define CS1_SW10 0xA2
#define CS2_SW10 0xA3
#define CS3_SW10 0xA4
#define CS4_SW10 0xA5
#define CS5_SW10 0xA6
#define CS6_SW10 0xA7
#define CS7_SW10 0xA8
#define CS8_SW10 0xA9
#define CS9_SW10 0xAA
#define CS10_SW10 0xAB
#define CS11_SW10 0xAC
#define CS12_SW10 0xAD
#define CS13_SW10 0xAE
#define CS14_SW10 0xAF
#define CS15_SW10 0xB0
#define CS16_SW10 0xB1
#define CS17_SW10 0xB2
#define CS18_SW10 0xB3
#define SW10_CS1 0xA2
#define SW10_CS2 0xA3
#define SW10_CS3 0xA4
#define SW10_CS4 0xA5
#define SW10_CS5 0xA6
#define SW10_CS6 0xA7
#define SW10_CS7 0xA8
#define SW10_CS8 0xA9
#define SW10_CS9 0xAA
#define SW10_CS10 0xAB
#define SW10_CS11 0xAC
#define SW10_CS12 0xAD
#define SW10_CS13 0xAE
#define SW10_CS14 0xAF
#define SW10_CS15 0xB0
#define SW10_CS16 0xB1
#define SW10_CS17 0xB2
#define SW10_CS18 0xB3
#define CS1_SW11 0xB4
#define CS2_SW11 0xB5
#define CS3_SW11 0xB6
#define CS4_SW11 0xB7
#define CS5_SW11 0xB8
#define CS6_SW11 0xB9
#define CS7_SW11 0xBA
#define CS8_SW11 0xBB
#define CS9_SW11 0xBC
#define CS10_SW11 0xBD
#define CS11_SW11 0xBE
#define CS12_SW11 0xBF
#define CS13_SW11 0xC0
#define CS14_SW11 0xC1
#define CS15_SW11 0xC2
#define CS16_SW11 0xC3
#define CS17_SW11 0xC4
#define CS18_SW11 0xC5
#define SW11_CS1 0xB4
#define SW11_CS2 0xB5
#define SW11_CS3 0xB6
#define SW11_CS4 0xB7
#define SW11_CS5 0xB8
#define SW11_CS6 0xB9
#define SW11_CS7 0xBA
#define SW11_CS8 0xBB
#define SW11_CS9 0xBC
#define SW11_CS10 0xBD
#define SW11_CS11 0xBE
#define SW11_CS12 0xBF
#define SW11_CS13 0xC0
#define SW11_CS14 0xC1
#define SW11_CS15 0xC2
#define SW11_CS16 0xC3
#define SW11_CS17 0xC4
#define SW11_CS18 0xC5
// DEPRECATED - DO NOT USE
#define CS1_SW1 SW1_CS1
#define CS2_SW1 SW1_CS2
#define CS3_SW1 SW1_CS3
#define CS4_SW1 SW1_CS4
#define CS5_SW1 SW1_CS5
#define CS6_SW1 SW1_CS6
#define CS7_SW1 SW1_CS7
#define CS8_SW1 SW1_CS8
#define CS9_SW1 SW1_CS9
#define CS10_SW1 SW1_CS10
#define CS11_SW1 SW1_CS11
#define CS12_SW1 SW1_CS12
#define CS13_SW1 SW1_CS13
#define CS14_SW1 SW1_CS14
#define CS15_SW1 SW1_CS15
#define CS16_SW1 SW1_CS16
#define CS17_SW1 SW1_CS17
#define CS18_SW1 SW1_CS18
#define CS1_SW2 SW2_CS1
#define CS2_SW2 SW2_CS2
#define CS3_SW2 SW2_CS3
#define CS4_SW2 SW2_CS4
#define CS5_SW2 SW2_CS5
#define CS6_SW2 SW2_CS6
#define CS7_SW2 SW2_CS7
#define CS8_SW2 SW2_CS8
#define CS9_SW2 SW2_CS9
#define CS10_SW2 SW2_CS10
#define CS11_SW2 SW2_CS11
#define CS12_SW2 SW2_CS12
#define CS13_SW2 SW2_CS13
#define CS14_SW2 SW2_CS14
#define CS15_SW2 SW2_CS15
#define CS16_SW2 SW2_CS16
#define CS17_SW2 SW2_CS17
#define CS18_SW2 SW2_CS18
#define CS1_SW3 SW3_CS1
#define CS2_SW3 SW3_CS2
#define CS3_SW3 SW3_CS3
#define CS4_SW3 SW3_CS4
#define CS5_SW3 SW3_CS5
#define CS6_SW3 SW3_CS6
#define CS7_SW3 SW3_CS7
#define CS8_SW3 SW3_CS8
#define CS9_SW3 SW3_CS9
#define CS10_SW3 SW3_CS10
#define CS11_SW3 SW3_CS11
#define CS12_SW3 SW3_CS12
#define CS13_SW3 SW3_CS13
#define CS14_SW3 SW3_CS14
#define CS15_SW3 SW3_CS15
#define CS16_SW3 SW3_CS16
#define CS17_SW3 SW3_CS17
#define CS18_SW3 SW3_CS18
#define CS1_SW4 SW4_CS1
#define CS2_SW4 SW4_CS2
#define CS3_SW4 SW4_CS3
#define CS4_SW4 SW4_CS4
#define CS5_SW4 SW4_CS5
#define CS6_SW4 SW4_CS6
#define CS7_SW4 SW4_CS7
#define CS8_SW4 SW4_CS8
#define CS9_SW4 SW4_CS9
#define CS10_SW4 SW4_CS10
#define CS11_SW4 SW4_CS11
#define CS12_SW4 SW4_CS12
#define CS13_SW4 SW4_CS13
#define CS14_SW4 SW4_CS14
#define CS15_SW4 SW4_CS15
#define CS16_SW4 SW4_CS16
#define CS17_SW4 SW4_CS17
#define CS18_SW4 SW4_CS18
#define CS1_SW5 SW5_CS1
#define CS2_SW5 SW5_CS2
#define CS3_SW5 SW5_CS3
#define CS4_SW5 SW5_CS4
#define CS5_SW5 SW5_CS5
#define CS6_SW5 SW5_CS6
#define CS7_SW5 SW5_CS7
#define CS8_SW5 SW5_CS8
#define CS9_SW5 SW5_CS9
#define CS10_SW5 SW5_CS10
#define CS11_SW5 SW5_CS11
#define CS12_SW5 SW5_CS12
#define CS13_SW5 SW5_CS13
#define CS14_SW5 SW5_CS14
#define CS15_SW5 SW5_CS15
#define CS16_SW5 SW5_CS16
#define CS17_SW5 SW5_CS17
#define CS18_SW5 SW5_CS18
#define CS1_SW6 SW6_CS1
#define CS2_SW6 SW6_CS2
#define CS3_SW6 SW6_CS3
#define CS4_SW6 SW6_CS4
#define CS5_SW6 SW6_CS5
#define CS6_SW6 SW6_CS6
#define CS7_SW6 SW6_CS7
#define CS8_SW6 SW6_CS8
#define CS9_SW6 SW6_CS9
#define CS10_SW6 SW6_CS10
#define CS11_SW6 SW6_CS11
#define CS12_SW6 SW6_CS12
#define CS13_SW6 SW6_CS13
#define CS14_SW6 SW6_CS14
#define CS15_SW6 SW6_CS15
#define CS16_SW6 SW6_CS16
#define CS17_SW6 SW6_CS17
#define CS18_SW6 SW6_CS18
#define CS1_SW7 SW7_CS1
#define CS2_SW7 SW7_CS2
#define CS3_SW7 SW7_CS3
#define CS4_SW7 SW7_CS4
#define CS5_SW7 SW7_CS5
#define CS6_SW7 SW7_CS6
#define CS7_SW7 SW7_CS7
#define CS8_SW7 SW7_CS8
#define CS9_SW7 SW7_CS9
#define CS10_SW7 SW7_CS10
#define CS11_SW7 SW7_CS11
#define CS12_SW7 SW7_CS12
#define CS13_SW7 SW7_CS13
#define CS14_SW7 SW7_CS14
#define CS15_SW7 SW7_CS15
#define CS16_SW7 SW7_CS16
#define CS17_SW7 SW7_CS17
#define CS18_SW7 SW7_CS18
#define CS1_SW8 SW8_CS1
#define CS2_SW8 SW8_CS2
#define CS3_SW8 SW8_CS3
#define CS4_SW8 SW8_CS4
#define CS5_SW8 SW8_CS5
#define CS6_SW8 SW8_CS6
#define CS7_SW8 SW8_CS7
#define CS8_SW8 SW8_CS8
#define CS9_SW8 SW8_CS9
#define CS10_SW8 SW8_CS10
#define CS11_SW8 SW8_CS11
#define CS12_SW8 SW8_CS12
#define CS13_SW8 SW8_CS13
#define CS14_SW8 SW8_CS14
#define CS15_SW8 SW8_CS15
#define CS16_SW8 SW8_CS16
#define CS17_SW8 SW8_CS17
#define CS18_SW8 SW8_CS18
#define CS1_SW9 SW9_CS1
#define CS2_SW9 SW9_CS2
#define CS3_SW9 SW9_CS3
#define CS4_SW9 SW9_CS4
#define CS5_SW9 SW9_CS5
#define CS6_SW9 SW9_CS6
#define CS7_SW9 SW9_CS7
#define CS8_SW9 SW9_CS8
#define CS9_SW9 SW9_CS9
#define CS10_SW9 SW9_CS10
#define CS11_SW9 SW9_CS11
#define CS12_SW9 SW9_CS12
#define CS13_SW9 SW9_CS13
#define CS14_SW9 SW9_CS14
#define CS15_SW9 SW9_CS15
#define CS16_SW9 SW9_CS16
#define CS17_SW9 SW9_CS17
#define CS18_SW9 SW9_CS18
#define CS1_SW10 SW10_CS1
#define CS2_SW10 SW10_CS2
#define CS3_SW10 SW10_CS3
#define CS4_SW10 SW10_CS4
#define CS5_SW10 SW10_CS5
#define CS6_SW10 SW10_CS6
#define CS7_SW10 SW10_CS7
#define CS8_SW10 SW10_CS8
#define CS9_SW10 SW10_CS9
#define CS10_SW10 SW10_CS10
#define CS11_SW10 SW10_CS11
#define CS12_SW10 SW10_CS12
#define CS13_SW10 SW10_CS13
#define CS14_SW10 SW10_CS14
#define CS15_SW10 SW10_CS15
#define CS16_SW10 SW10_CS16
#define CS17_SW10 SW10_CS17
#define CS18_SW10 SW10_CS18
#define CS1_SW11 SW11_CS1
#define CS2_SW11 SW11_CS2
#define CS3_SW11 SW11_CS3
#define CS4_SW11 SW11_CS4
#define CS5_SW11 SW11_CS5
#define CS6_SW11 SW11_CS6
#define CS7_SW11 SW11_CS7
#define CS8_SW11 SW11_CS8
#define CS9_SW11 SW11_CS9
#define CS10_SW11 SW11_CS10
#define CS11_SW11 SW11_CS11
#define CS12_SW11 SW11_CS12
#define CS13_SW11 SW11_CS13
#define CS14_SW11 SW11_CS14
#define CS15_SW11 SW11_CS15
#define CS16_SW11 SW11_CS16
#define CS17_SW11 SW11_CS17
#define CS18_SW11 SW11_CS18

View File

@ -20,6 +20,7 @@
#include "is31fl3745-mono.h"
#include "i2c_master.h"
#include "gpio.h"
#include "wait.h"
#define IS31FL3745_PWM_REGISTER_COUNT 144
@ -62,28 +63,62 @@
# define IS31FL3745_SYNC_4 IS31FL3745_SYNC_NONE
#endif
uint8_t g_pwm_buffer[IS31FL3745_DRIVER_COUNT][IS31FL3745_PWM_REGISTER_COUNT];
bool g_pwm_buffer_update_required[IS31FL3745_DRIVER_COUNT] = {false};
bool g_scaling_registers_update_required[IS31FL3745_DRIVER_COUNT] = {false};
const uint8_t i2c_addresses[IS31FL3745_DRIVER_COUNT] = {
IS31FL3745_I2C_ADDRESS_1,
#ifdef IS31FL3745_I2C_ADDRESS_2
IS31FL3745_I2C_ADDRESS_2,
# ifdef IS31FL3745_I2C_ADDRESS_3
IS31FL3745_I2C_ADDRESS_3,
# ifdef IS31FL3745_I2C_ADDRESS_4
IS31FL3745_I2C_ADDRESS_4,
# endif
# endif
#endif
};
uint8_t g_scaling_registers[IS31FL3745_DRIVER_COUNT][IS31FL3745_SCALING_REGISTER_COUNT];
const uint8_t driver_sync[IS31FL3745_DRIVER_COUNT] = {
IS31FL3745_SYNC_1,
#ifdef IS31FL3745_I2C_ADDRESS_2
IS31FL3745_SYNC_2,
# ifdef IS31FL3745_I2C_ADDRESS_3
IS31FL3745_SYNC_3,
# ifdef IS31FL3745_I2C_ADDRESS_4
IS31FL3745_SYNC_4,
# endif
# endif
#endif
};
void is31fl3745_write_register(uint8_t addr, uint8_t reg, uint8_t data) {
typedef struct is31fl3745_driver_t {
uint8_t pwm_buffer[IS31FL3745_PWM_REGISTER_COUNT];
bool pwm_buffer_dirty;
uint8_t scaling_buffer[IS31FL3745_SCALING_REGISTER_COUNT];
bool scaling_buffer_dirty;
} PACKED is31fl3745_driver_t;
is31fl3745_driver_t driver_buffers[IS31FL3745_DRIVER_COUNT] = {{
.pwm_buffer = {0},
.pwm_buffer_dirty = false,
.scaling_buffer = {0},
.scaling_buffer_dirty = false,
}};
void is31fl3745_write_register(uint8_t index, uint8_t reg, uint8_t data) {
#if IS31FL3745_I2C_PERSISTENCE > 0
for (uint8_t i = 0; i < IS31FL3745_I2C_PERSISTENCE; i++) {
if (i2c_write_register(addr << 1, reg, &data, 1, IS31FL3745_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3745_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, reg, &data, 1, IS31FL3745_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3745_I2C_TIMEOUT);
#endif
}
void is31fl3745_select_page(uint8_t addr, uint8_t page) {
is31fl3745_write_register(addr, IS31FL3745_REG_COMMAND_WRITE_LOCK, IS31FL3745_COMMAND_WRITE_LOCK_MAGIC);
is31fl3745_write_register(addr, IS31FL3745_REG_COMMAND, page);
void is31fl3745_select_page(uint8_t index, uint8_t page) {
is31fl3745_write_register(index, IS31FL3745_REG_COMMAND_WRITE_LOCK, IS31FL3745_COMMAND_WRITE_LOCK_MAGIC);
is31fl3745_write_register(index, IS31FL3745_REG_COMMAND, page);
}
void is31fl3745_write_pwm_buffer(uint8_t addr, uint8_t index) {
void is31fl3745_write_pwm_buffer(uint8_t index) {
// Assumes page 0 is already selected.
// Transmit PWM registers in 8 transfers of 18 bytes.
@ -91,10 +126,10 @@ void is31fl3745_write_pwm_buffer(uint8_t addr, uint8_t index) {
for (uint8_t i = 0; i < IS31FL3745_PWM_REGISTER_COUNT; i += 18) {
#if IS31FL3745_I2C_PERSISTENCE > 0
for (uint8_t j = 0; j < IS31FL3745_I2C_PERSISTENCE; j++) {
if (i2c_write_register(addr << 1, i + 1, g_pwm_buffer[index] + i, 18, IS31FL3745_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, i + 1, driver_buffers[index].pwm_buffer + i, 18, IS31FL3745_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, i + 1, g_pwm_buffer[index] + i, 18, IS31FL3745_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, i + 1, driver_buffers[index].pwm_buffer + i, 18, IS31FL3745_I2C_TIMEOUT);
#endif
}
}
@ -102,58 +137,51 @@ void is31fl3745_write_pwm_buffer(uint8_t addr, uint8_t index) {
void is31fl3745_init_drivers(void) {
i2c_init();
is31fl3745_init(IS31FL3745_I2C_ADDRESS_1, IS31FL3745_SYNC_1);
#if defined(IS31FL3745_I2C_ADDRESS_2)
is31fl3745_init(IS31FL3745_I2C_ADDRESS_2, IS31FL3745_SYNC_2);
# if defined(IS31FL3745_I2C_ADDRESS_3)
is31fl3745_init(IS31FL3745_I2C_ADDRESS_3, IS31FL3745_SYNC_3);
# if defined(IS31FL3745_I2C_ADDRESS_4)
is31fl3745_init(IS31FL3745_I2C_ADDRESS_4, IS31FL3745_SYNC_4);
# endif
# endif
#if defined(IS31FL3745_SDB_PIN)
gpio_set_pin_output(IS31FL3745_SDB_PIN);
gpio_write_pin_high(IS31FL3745_SDB_PIN);
#endif
for (uint8_t i = 0; i < IS31FL3745_DRIVER_COUNT; i++) {
is31fl3745_init(i);
}
for (int i = 0; i < IS31FL3745_LED_COUNT; i++) {
is31fl3745_set_scaling_register(i, 0xFF);
}
is31fl3745_update_scaling_registers(IS31FL3745_I2C_ADDRESS_1, 0);
#if defined(IS31FL3745_I2C_ADDRESS_2)
is31fl3745_update_scaling_registers(IS31FL3745_I2C_ADDRESS_2, 1);
# if defined(IS31FL3745_I2C_ADDRESS_3)
is31fl3745_update_scaling_registers(IS31FL3745_I2C_ADDRESS_3, 2);
# if defined(IS31FL3745_I2C_ADDRESS_4)
is31fl3745_update_scaling_registers(IS31FL3745_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3745_DRIVER_COUNT; i++) {
is31fl3745_update_scaling_registers(i);
}
}
void is31fl3745_init(uint8_t addr, uint8_t sync) {
void is31fl3745_init(uint8_t index) {
// In order to avoid the LEDs being driven with garbage data
// in the LED driver's PWM registers, shutdown is enabled last.
// Set up the mode and other settings, clear the PWM registers,
// then disable software shutdown.
is31fl3745_select_page(addr, IS31FL3745_COMMAND_SCALING);
is31fl3745_select_page(index, IS31FL3745_COMMAND_SCALING);
// Turn off all LEDs.
for (uint8_t i = 0; i < IS31FL3745_SCALING_REGISTER_COUNT; i++) {
is31fl3745_write_register(addr, i + 1, 0x00);
is31fl3745_write_register(index, i + 1, 0x00);
}
is31fl3745_select_page(addr, IS31FL3745_COMMAND_PWM);
is31fl3745_select_page(index, IS31FL3745_COMMAND_PWM);
for (uint8_t i = 0; i < IS31FL3745_PWM_REGISTER_COUNT; i++) {
is31fl3745_write_register(addr, i + 1, 0x00);
is31fl3745_write_register(index, i + 1, 0x00);
}
is31fl3745_select_page(addr, IS31FL3745_COMMAND_FUNCTION);
is31fl3745_select_page(index, IS31FL3745_COMMAND_FUNCTION);
is31fl3745_write_register(addr, IS31FL3745_FUNCTION_REG_PULLDOWNUP, (IS31FL3745_SW_PULLDOWN << 4) | IS31FL3745_CS_PULLUP);
is31fl3745_write_register(addr, IS31FL3745_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3745_GLOBAL_CURRENT);
is31fl3745_write_register(addr, IS31FL3745_FUNCTION_REG_SPREAD_SPECTRUM, (sync & 0b11) << 6);
is31fl3745_write_register(addr, IS31FL3745_FUNCTION_REG_CONFIGURATION, IS31FL3745_CONFIGURATION);
uint8_t sync = driver_sync[index];
is31fl3745_write_register(index, IS31FL3745_FUNCTION_REG_PULLDOWNUP, (IS31FL3745_SW_PULLDOWN << 4) | IS31FL3745_CS_PULLUP);
is31fl3745_write_register(index, IS31FL3745_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3745_GLOBAL_CURRENT);
is31fl3745_write_register(index, IS31FL3745_FUNCTION_REG_SPREAD_SPECTRUM, (sync & 0b11) << 6);
is31fl3745_write_register(index, IS31FL3745_FUNCTION_REG_CONFIGURATION, IS31FL3745_CONFIGURATION);
// Wait 10ms to ensure the device has woken up.
wait_ms(10);
@ -165,12 +193,12 @@ void is31fl3745_set_value(int index, uint8_t value) {
if (index >= 0 && index < IS31FL3745_LED_COUNT) {
memcpy_P(&led, (&g_is31fl3745_leds[index]), sizeof(led));
if (g_pwm_buffer[led.driver][led.v] == value) {
if (driver_buffers[led.driver].pwm_buffer[led.v] == value) {
return;
}
g_pwm_buffer_update_required[led.driver] = true;
g_pwm_buffer[led.driver][led.v] = value;
driver_buffers[led.driver].pwm_buffer[led.v] = value;
driver_buffers[led.driver].pwm_buffer_dirty = true;
}
}
@ -184,41 +212,34 @@ void is31fl3745_set_scaling_register(uint8_t index, uint8_t value) {
is31fl3745_led_t led;
memcpy_P(&led, (&g_is31fl3745_leds[index]), sizeof(led));
g_scaling_registers[led.driver][led.v] = value;
g_scaling_registers_update_required[led.driver] = true;
driver_buffers[led.driver].scaling_buffer[led.v] = value;
driver_buffers[led.driver].scaling_buffer_dirty = true;
}
void is31fl3745_update_pwm_buffers(uint8_t addr, uint8_t index) {
if (g_pwm_buffer_update_required[index]) {
is31fl3745_select_page(addr, IS31FL3745_COMMAND_PWM);
void is31fl3745_update_pwm_buffers(uint8_t index) {
if (driver_buffers[index].pwm_buffer_dirty) {
is31fl3745_select_page(index, IS31FL3745_COMMAND_PWM);
is31fl3745_write_pwm_buffer(addr, index);
is31fl3745_write_pwm_buffer(index);
g_pwm_buffer_update_required[index] = false;
driver_buffers[index].pwm_buffer_dirty = false;
}
}
void is31fl3745_update_scaling_registers(uint8_t addr, uint8_t index) {
if (g_scaling_registers_update_required[index]) {
is31fl3745_select_page(addr, IS31FL3745_COMMAND_SCALING);
void is31fl3745_update_scaling_registers(uint8_t index) {
if (driver_buffers[index].scaling_buffer_dirty) {
is31fl3745_select_page(index, IS31FL3745_COMMAND_SCALING);
for (uint8_t i = 0; i < IS31FL3745_SCALING_REGISTER_COUNT; i++) {
is31fl3745_write_register(addr, i + 1, g_scaling_registers[index][i]);
is31fl3745_write_register(index, i + 1, driver_buffers[index].scaling_buffer[i]);
}
g_scaling_registers_update_required[index] = false;
driver_buffers[index].scaling_buffer_dirty = false;
}
}
void is31fl3745_flush(void) {
is31fl3745_update_pwm_buffers(IS31FL3745_I2C_ADDRESS_1, 0);
#if defined(IS31FL3745_I2C_ADDRESS_2)
is31fl3745_update_pwm_buffers(IS31FL3745_I2C_ADDRESS_2, 1);
# if defined(IS31FL3745_I2C_ADDRESS_3)
is31fl3745_update_pwm_buffers(IS31FL3745_I2C_ADDRESS_3, 2);
# if defined(IS31FL3745_I2C_ADDRESS_4)
is31fl3745_update_pwm_buffers(IS31FL3745_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3745_DRIVER_COUNT; i++) {
is31fl3745_update_pwm_buffers(i);
}
}

View File

@ -82,17 +82,17 @@ typedef struct is31fl3745_led_t {
extern const is31fl3745_led_t PROGMEM g_is31fl3745_leds[IS31FL3745_LED_COUNT];
void is31fl3745_init_drivers(void);
void is31fl3745_init(uint8_t addr, uint8_t sync);
void is31fl3745_write_register(uint8_t addr, uint8_t reg, uint8_t data);
void is31fl3745_select_page(uint8_t addr, uint8_t page);
void is31fl3745_init(uint8_t index);
void is31fl3745_write_register(uint8_t index, uint8_t reg, uint8_t data);
void is31fl3745_select_page(uint8_t index, uint8_t page);
void is31fl3745_set_value(int index, uint8_t value);
void is31fl3745_set_value_all(uint8_t value);
void is31fl3745_set_scaling_register(uint8_t index, uint8_t value);
void is31fl3745_update_pwm_buffers(uint8_t addr, uint8_t index);
void is31fl3745_update_scaling_registers(uint8_t addr, uint8_t index);
void is31fl3745_update_pwm_buffers(uint8_t index);
void is31fl3745_update_scaling_registers(uint8_t index);
void is31fl3745_flush(void);
@ -118,154 +118,154 @@ void is31fl3745_flush(void);
#define IS31FL3745_SYNC_SLAVE 0b10
#define IS31FL3745_SYNC_MASTER 0b11
#define CS1_SW1 0x00
#define CS2_SW1 0x01
#define CS3_SW1 0x02
#define CS4_SW1 0x03
#define CS5_SW1 0x04
#define CS6_SW1 0x05
#define CS7_SW1 0x06
#define CS8_SW1 0x07
#define CS9_SW1 0x08
#define CS10_SW1 0x09
#define CS11_SW1 0x0A
#define CS12_SW1 0x0B
#define CS13_SW1 0x0C
#define CS14_SW1 0x0D
#define CS15_SW1 0x0E
#define CS16_SW1 0x0F
#define CS17_SW1 0x10
#define CS18_SW1 0x11
#define SW1_CS1 0x00
#define SW1_CS2 0x01
#define SW1_CS3 0x02
#define SW1_CS4 0x03
#define SW1_CS5 0x04
#define SW1_CS6 0x05
#define SW1_CS7 0x06
#define SW1_CS8 0x07
#define SW1_CS9 0x08
#define SW1_CS10 0x09
#define SW1_CS11 0x0A
#define SW1_CS12 0x0B
#define SW1_CS13 0x0C
#define SW1_CS14 0x0D
#define SW1_CS15 0x0E
#define SW1_CS16 0x0F
#define SW1_CS17 0x10
#define SW1_CS18 0x11
#define CS1_SW2 0x12
#define CS2_SW2 0x13
#define CS3_SW2 0x14
#define CS4_SW2 0x15
#define CS5_SW2 0x16
#define CS6_SW2 0x17
#define CS7_SW2 0x18
#define CS8_SW2 0x19
#define CS9_SW2 0x1A
#define CS10_SW2 0x1B
#define CS11_SW2 0x1C
#define CS12_SW2 0x1D
#define CS13_SW2 0x1E
#define CS14_SW2 0x1F
#define CS15_SW2 0x20
#define CS16_SW2 0x21
#define CS17_SW2 0x22
#define CS18_SW2 0x23
#define SW2_CS1 0x12
#define SW2_CS2 0x13
#define SW2_CS3 0x14
#define SW2_CS4 0x15
#define SW2_CS5 0x16
#define SW2_CS6 0x17
#define SW2_CS7 0x18
#define SW2_CS8 0x19
#define SW2_CS9 0x1A
#define SW2_CS10 0x1B
#define SW2_CS11 0x1C
#define SW2_CS12 0x1D
#define SW2_CS13 0x1E
#define SW2_CS14 0x1F
#define SW2_CS15 0x20
#define SW2_CS16 0x21
#define SW2_CS17 0x22
#define SW2_CS18 0x23
#define CS1_SW3 0x24
#define CS2_SW3 0x25
#define CS3_SW3 0x26
#define CS4_SW3 0x27
#define CS5_SW3 0x28
#define CS6_SW3 0x29
#define CS7_SW3 0x2A
#define CS8_SW3 0x2B
#define CS9_SW3 0x2C
#define CS10_SW3 0x2D
#define CS11_SW3 0x2E
#define CS12_SW3 0x2F
#define CS13_SW3 0x30
#define CS14_SW3 0x31
#define CS15_SW3 0x32
#define CS16_SW3 0x33
#define CS17_SW3 0x34
#define CS18_SW3 0x35
#define SW3_CS1 0x24
#define SW3_CS2 0x25
#define SW3_CS3 0x26
#define SW3_CS4 0x27
#define SW3_CS5 0x28
#define SW3_CS6 0x29
#define SW3_CS7 0x2A
#define SW3_CS8 0x2B
#define SW3_CS9 0x2C
#define SW3_CS10 0x2D
#define SW3_CS11 0x2E
#define SW3_CS12 0x2F
#define SW3_CS13 0x30
#define SW3_CS14 0x31
#define SW3_CS15 0x32
#define SW3_CS16 0x33
#define SW3_CS17 0x34
#define SW3_CS18 0x35
#define CS1_SW4 0x36
#define CS2_SW4 0x37
#define CS3_SW4 0x38
#define CS4_SW4 0x39
#define CS5_SW4 0x3A
#define CS6_SW4 0x3B
#define CS7_SW4 0x3C
#define CS8_SW4 0x3D
#define CS9_SW4 0x3E
#define CS10_SW4 0x3F
#define CS11_SW4 0x40
#define CS12_SW4 0x41
#define CS13_SW4 0x42
#define CS14_SW4 0x43
#define CS15_SW4 0x44
#define CS16_SW4 0x45
#define CS17_SW4 0x46
#define CS18_SW4 0x47
#define SW4_CS1 0x36
#define SW4_CS2 0x37
#define SW4_CS3 0x38
#define SW4_CS4 0x39
#define SW4_CS5 0x3A
#define SW4_CS6 0x3B
#define SW4_CS7 0x3C
#define SW4_CS8 0x3D
#define SW4_CS9 0x3E
#define SW4_CS10 0x3F
#define SW4_CS11 0x40
#define SW4_CS12 0x41
#define SW4_CS13 0x42
#define SW4_CS14 0x43
#define SW4_CS15 0x44
#define SW4_CS16 0x45
#define SW4_CS17 0x46
#define SW4_CS18 0x47
#define CS1_SW5 0x48
#define CS2_SW5 0x49
#define CS3_SW5 0x4A
#define CS4_SW5 0x4B
#define CS5_SW5 0x4C
#define CS6_SW5 0x4D
#define CS7_SW5 0x4E
#define CS8_SW5 0x4F
#define CS9_SW5 0x50
#define CS10_SW5 0x51
#define CS11_SW5 0x52
#define CS12_SW5 0x53
#define CS13_SW5 0x54
#define CS14_SW5 0x55
#define CS15_SW5 0x56
#define CS16_SW5 0x57
#define CS17_SW5 0x58
#define CS18_SW5 0x59
#define SW5_CS1 0x48
#define SW5_CS2 0x49
#define SW5_CS3 0x4A
#define SW5_CS4 0x4B
#define SW5_CS5 0x4C
#define SW5_CS6 0x4D
#define SW5_CS7 0x4E
#define SW5_CS8 0x4F
#define SW5_CS9 0x50
#define SW5_CS10 0x51
#define SW5_CS11 0x52
#define SW5_CS12 0x53
#define SW5_CS13 0x54
#define SW5_CS14 0x55
#define SW5_CS15 0x56
#define SW5_CS16 0x57
#define SW5_CS17 0x58
#define SW5_CS18 0x59
#define CS1_SW6 0x5A
#define CS2_SW6 0x5B
#define CS3_SW6 0x5C
#define CS4_SW6 0x5D
#define CS5_SW6 0x5E
#define CS6_SW6 0x5F
#define CS7_SW6 0x60
#define CS8_SW6 0x61
#define CS9_SW6 0x62
#define CS10_SW6 0x63
#define CS11_SW6 0x64
#define CS12_SW6 0x65
#define CS13_SW6 0x66
#define CS14_SW6 0x67
#define CS15_SW6 0x68
#define CS16_SW6 0x69
#define CS17_SW6 0x6A
#define CS18_SW6 0x6B
#define SW6_CS1 0x5A
#define SW6_CS2 0x5B
#define SW6_CS3 0x5C
#define SW6_CS4 0x5D
#define SW6_CS5 0x5E
#define SW6_CS6 0x5F
#define SW6_CS7 0x60
#define SW6_CS8 0x61
#define SW6_CS9 0x62
#define SW6_CS10 0x63
#define SW6_CS11 0x64
#define SW6_CS12 0x65
#define SW6_CS13 0x66
#define SW6_CS14 0x67
#define SW6_CS15 0x68
#define SW6_CS16 0x69
#define SW6_CS17 0x6A
#define SW6_CS18 0x6B
#define CS1_SW7 0x6C
#define CS2_SW7 0x6D
#define CS3_SW7 0x6E
#define CS4_SW7 0x6F
#define CS5_SW7 0x70
#define CS6_SW7 0x71
#define CS7_SW7 0x72
#define CS8_SW7 0x73
#define CS9_SW7 0x74
#define CS10_SW7 0x75
#define CS11_SW7 0x76
#define CS12_SW7 0x77
#define CS13_SW7 0x78
#define CS14_SW7 0x79
#define CS15_SW7 0x7A
#define CS16_SW7 0x7B
#define CS17_SW7 0x7C
#define CS18_SW7 0x7D
#define SW7_CS1 0x6C
#define SW7_CS2 0x6D
#define SW7_CS3 0x6E
#define SW7_CS4 0x6F
#define SW7_CS5 0x70
#define SW7_CS6 0x71
#define SW7_CS7 0x72
#define SW7_CS8 0x73
#define SW7_CS9 0x74
#define SW7_CS10 0x75
#define SW7_CS11 0x76
#define SW7_CS12 0x77
#define SW7_CS13 0x78
#define SW7_CS14 0x79
#define SW7_CS15 0x7A
#define SW7_CS16 0x7B
#define SW7_CS17 0x7C
#define SW7_CS18 0x7D
#define CS1_SW8 0x7E
#define CS2_SW8 0x7F
#define CS3_SW8 0x80
#define CS4_SW8 0x81
#define CS5_SW8 0x82
#define CS6_SW8 0x83
#define CS7_SW8 0x84
#define CS8_SW8 0x85
#define CS9_SW8 0x86
#define CS10_SW8 0x87
#define CS11_SW8 0x88
#define CS12_SW8 0x89
#define CS13_SW8 0x8A
#define CS14_SW8 0x8B
#define CS15_SW8 0x8C
#define CS16_SW8 0x8D
#define CS17_SW8 0x8E
#define CS18_SW8 0x8F
#define SW8_CS1 0x7E
#define SW8_CS2 0x7F
#define SW8_CS3 0x80
#define SW8_CS4 0x81
#define SW8_CS5 0x82
#define SW8_CS6 0x83
#define SW8_CS7 0x84
#define SW8_CS8 0x85
#define SW8_CS9 0x86
#define SW8_CS10 0x87
#define SW8_CS11 0x88
#define SW8_CS12 0x89
#define SW8_CS13 0x8A
#define SW8_CS14 0x8B
#define SW8_CS15 0x8C
#define SW8_CS16 0x8D
#define SW8_CS17 0x8E
#define SW8_CS18 0x8F

View File

@ -20,6 +20,7 @@
#include "is31fl3745.h"
#include "i2c_master.h"
#include "gpio.h"
#include "wait.h"
#define IS31FL3745_PWM_REGISTER_COUNT 144
@ -62,28 +63,62 @@
# define IS31FL3745_SYNC_4 IS31FL3745_SYNC_NONE
#endif
uint8_t g_pwm_buffer[IS31FL3745_DRIVER_COUNT][IS31FL3745_PWM_REGISTER_COUNT];
bool g_pwm_buffer_update_required[IS31FL3745_DRIVER_COUNT] = {false};
bool g_scaling_registers_update_required[IS31FL3745_DRIVER_COUNT] = {false};
const uint8_t i2c_addresses[IS31FL3745_DRIVER_COUNT] = {
IS31FL3745_I2C_ADDRESS_1,
#ifdef IS31FL3745_I2C_ADDRESS_2
IS31FL3745_I2C_ADDRESS_2,
# ifdef IS31FL3745_I2C_ADDRESS_3
IS31FL3745_I2C_ADDRESS_3,
# ifdef IS31FL3745_I2C_ADDRESS_4
IS31FL3745_I2C_ADDRESS_4,
# endif
# endif
#endif
};
uint8_t g_scaling_registers[IS31FL3745_DRIVER_COUNT][IS31FL3745_SCALING_REGISTER_COUNT];
const uint8_t driver_sync[IS31FL3745_DRIVER_COUNT] = {
IS31FL3745_SYNC_1,
#ifdef IS31FL3745_I2C_ADDRESS_2
IS31FL3745_SYNC_2,
# ifdef IS31FL3745_I2C_ADDRESS_3
IS31FL3745_SYNC_3,
# ifdef IS31FL3745_I2C_ADDRESS_4
IS31FL3745_SYNC_4,
# endif
# endif
#endif
};
void is31fl3745_write_register(uint8_t addr, uint8_t reg, uint8_t data) {
typedef struct is31fl3745_driver_t {
uint8_t pwm_buffer[IS31FL3745_PWM_REGISTER_COUNT];
bool pwm_buffer_dirty;
uint8_t scaling_buffer[IS31FL3745_SCALING_REGISTER_COUNT];
bool scaling_buffer_dirty;
} PACKED is31fl3745_driver_t;
is31fl3745_driver_t driver_buffers[IS31FL3745_DRIVER_COUNT] = {{
.pwm_buffer = {0},
.pwm_buffer_dirty = false,
.scaling_buffer = {0},
.scaling_buffer_dirty = false,
}};
void is31fl3745_write_register(uint8_t index, uint8_t reg, uint8_t data) {
#if IS31FL3745_I2C_PERSISTENCE > 0
for (uint8_t i = 0; i < IS31FL3745_I2C_PERSISTENCE; i++) {
if (i2c_write_register(addr << 1, reg, &data, 1, IS31FL3745_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3745_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, reg, &data, 1, IS31FL3745_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3745_I2C_TIMEOUT);
#endif
}
void is31fl3745_select_page(uint8_t addr, uint8_t page) {
is31fl3745_write_register(addr, IS31FL3745_REG_COMMAND_WRITE_LOCK, IS31FL3745_COMMAND_WRITE_LOCK_MAGIC);
is31fl3745_write_register(addr, IS31FL3745_REG_COMMAND, page);
void is31fl3745_select_page(uint8_t index, uint8_t page) {
is31fl3745_write_register(index, IS31FL3745_REG_COMMAND_WRITE_LOCK, IS31FL3745_COMMAND_WRITE_LOCK_MAGIC);
is31fl3745_write_register(index, IS31FL3745_REG_COMMAND, page);
}
void is31fl3745_write_pwm_buffer(uint8_t addr, uint8_t index) {
void is31fl3745_write_pwm_buffer(uint8_t index) {
// Assumes page 0 is already selected.
// Transmit PWM registers in 8 transfers of 18 bytes.
@ -91,10 +126,10 @@ void is31fl3745_write_pwm_buffer(uint8_t addr, uint8_t index) {
for (uint8_t i = 0; i < IS31FL3745_PWM_REGISTER_COUNT; i += 18) {
#if IS31FL3745_I2C_PERSISTENCE > 0
for (uint8_t j = 0; j < IS31FL3745_I2C_PERSISTENCE; j++) {
if (i2c_write_register(addr << 1, i + 1, g_pwm_buffer[index] + i, 18, IS31FL3745_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, i + 1, driver_buffers[index].pwm_buffer + i, 18, IS31FL3745_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, i + 1, g_pwm_buffer[index] + i, 18, IS31FL3745_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, i + 1, driver_buffers[index].pwm_buffer + i, 18, IS31FL3745_I2C_TIMEOUT);
#endif
}
}
@ -102,58 +137,51 @@ void is31fl3745_write_pwm_buffer(uint8_t addr, uint8_t index) {
void is31fl3745_init_drivers(void) {
i2c_init();
is31fl3745_init(IS31FL3745_I2C_ADDRESS_1, IS31FL3745_SYNC_1);
#if defined(IS31FL3745_I2C_ADDRESS_2)
is31fl3745_init(IS31FL3745_I2C_ADDRESS_2, IS31FL3745_SYNC_2);
# if defined(IS31FL3745_I2C_ADDRESS_3)
is31fl3745_init(IS31FL3745_I2C_ADDRESS_3, IS31FL3745_SYNC_3);
# if defined(IS31FL3745_I2C_ADDRESS_4)
is31fl3745_init(IS31FL3745_I2C_ADDRESS_4, IS31FL3745_SYNC_4);
# endif
# endif
#if defined(IS31FL3745_SDB_PIN)
gpio_set_pin_output(IS31FL3745_SDB_PIN);
gpio_write_pin_high(IS31FL3745_SDB_PIN);
#endif
for (uint8_t i = 0; i < IS31FL3745_DRIVER_COUNT; i++) {
is31fl3745_init(i);
}
for (int i = 0; i < IS31FL3745_LED_COUNT; i++) {
is31fl3745_set_scaling_register(i, 0xFF, 0xFF, 0xFF);
}
is31fl3745_update_scaling_registers(IS31FL3745_I2C_ADDRESS_1, 0);
#if defined(IS31FL3745_I2C_ADDRESS_2)
is31fl3745_update_scaling_registers(IS31FL3745_I2C_ADDRESS_2, 1);
# if defined(IS31FL3745_I2C_ADDRESS_3)
is31fl3745_update_scaling_registers(IS31FL3745_I2C_ADDRESS_3, 2);
# if defined(IS31FL3745_I2C_ADDRESS_4)
is31fl3745_update_scaling_registers(IS31FL3745_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3745_DRIVER_COUNT; i++) {
is31fl3745_update_scaling_registers(i);
}
}
void is31fl3745_init(uint8_t addr, uint8_t sync) {
void is31fl3745_init(uint8_t index) {
// In order to avoid the LEDs being driven with garbage data
// in the LED driver's PWM registers, shutdown is enabled last.
// Set up the mode and other settings, clear the PWM registers,
// then disable software shutdown.
is31fl3745_select_page(addr, IS31FL3745_COMMAND_SCALING);
is31fl3745_select_page(index, IS31FL3745_COMMAND_SCALING);
// Turn off all LEDs.
for (uint8_t i = 0; i < IS31FL3745_SCALING_REGISTER_COUNT; i++) {
is31fl3745_write_register(addr, i + 1, 0x00);
is31fl3745_write_register(index, i + 1, 0x00);
}
is31fl3745_select_page(addr, IS31FL3745_COMMAND_PWM);
is31fl3745_select_page(index, IS31FL3745_COMMAND_PWM);
for (uint8_t i = 0; i < IS31FL3745_PWM_REGISTER_COUNT; i++) {
is31fl3745_write_register(addr, i + 1, 0x00);
is31fl3745_write_register(index, i + 1, 0x00);
}
is31fl3745_select_page(addr, IS31FL3745_COMMAND_FUNCTION);
is31fl3745_select_page(index, IS31FL3745_COMMAND_FUNCTION);
is31fl3745_write_register(addr, IS31FL3745_FUNCTION_REG_PULLDOWNUP, (IS31FL3745_SW_PULLDOWN << 4) | IS31FL3745_CS_PULLUP);
is31fl3745_write_register(addr, IS31FL3745_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3745_GLOBAL_CURRENT);
is31fl3745_write_register(addr, IS31FL3745_FUNCTION_REG_SPREAD_SPECTRUM, (sync & 0b11) << 6);
is31fl3745_write_register(addr, IS31FL3745_FUNCTION_REG_CONFIGURATION, IS31FL3745_CONFIGURATION);
uint8_t sync = driver_sync[index];
is31fl3745_write_register(index, IS31FL3745_FUNCTION_REG_PULLDOWNUP, (IS31FL3745_SW_PULLDOWN << 4) | IS31FL3745_CS_PULLUP);
is31fl3745_write_register(index, IS31FL3745_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3745_GLOBAL_CURRENT);
is31fl3745_write_register(index, IS31FL3745_FUNCTION_REG_SPREAD_SPECTRUM, (sync & 0b11) << 6);
is31fl3745_write_register(index, IS31FL3745_FUNCTION_REG_CONFIGURATION, IS31FL3745_CONFIGURATION);
// Wait 10ms to ensure the device has woken up.
wait_ms(10);
@ -165,14 +193,14 @@ void is31fl3745_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
if (index >= 0 && index < IS31FL3745_LED_COUNT) {
memcpy_P(&led, (&g_is31fl3745_leds[index]), sizeof(led));
if (g_pwm_buffer[led.driver][led.r] == red && g_pwm_buffer[led.driver][led.g] == green && g_pwm_buffer[led.driver][led.b] == blue) {
if (driver_buffers[led.driver].pwm_buffer[led.r] == red && driver_buffers[led.driver].pwm_buffer[led.g] == green && driver_buffers[led.driver].pwm_buffer[led.b] == blue) {
return;
}
g_pwm_buffer_update_required[led.driver] = true;
g_pwm_buffer[led.driver][led.r] = red;
g_pwm_buffer[led.driver][led.g] = green;
g_pwm_buffer[led.driver][led.b] = blue;
driver_buffers[led.driver].pwm_buffer[led.r] = red;
driver_buffers[led.driver].pwm_buffer[led.g] = green;
driver_buffers[led.driver].pwm_buffer[led.b] = blue;
driver_buffers[led.driver].pwm_buffer_dirty = true;
}
}
@ -186,43 +214,36 @@ void is31fl3745_set_scaling_register(uint8_t index, uint8_t red, uint8_t green,
is31fl3745_led_t led;
memcpy_P(&led, (&g_is31fl3745_leds[index]), sizeof(led));
g_scaling_registers[led.driver][led.r] = red;
g_scaling_registers[led.driver][led.g] = green;
g_scaling_registers[led.driver][led.b] = blue;
g_scaling_registers_update_required[led.driver] = true;
driver_buffers[led.driver].scaling_buffer[led.r] = red;
driver_buffers[led.driver].scaling_buffer[led.g] = green;
driver_buffers[led.driver].scaling_buffer[led.b] = blue;
driver_buffers[led.driver].scaling_buffer_dirty = true;
}
void is31fl3745_update_pwm_buffers(uint8_t addr, uint8_t index) {
if (g_pwm_buffer_update_required[index]) {
is31fl3745_select_page(addr, IS31FL3745_COMMAND_PWM);
void is31fl3745_update_pwm_buffers(uint8_t index) {
if (driver_buffers[index].pwm_buffer_dirty) {
is31fl3745_select_page(index, IS31FL3745_COMMAND_PWM);
is31fl3745_write_pwm_buffer(addr, index);
is31fl3745_write_pwm_buffer(index);
g_pwm_buffer_update_required[index] = false;
driver_buffers[index].pwm_buffer_dirty = false;
}
}
void is31fl3745_update_scaling_registers(uint8_t addr, uint8_t index) {
if (g_scaling_registers_update_required[index]) {
is31fl3745_select_page(addr, IS31FL3745_COMMAND_SCALING);
void is31fl3745_update_scaling_registers(uint8_t index) {
if (driver_buffers[index].scaling_buffer_dirty) {
is31fl3745_select_page(index, IS31FL3745_COMMAND_SCALING);
for (uint8_t i = 0; i < IS31FL3745_SCALING_REGISTER_COUNT; i++) {
is31fl3745_write_register(addr, i + 1, g_scaling_registers[index][i]);
is31fl3745_write_register(index, i + 1, driver_buffers[index].scaling_buffer[i]);
}
g_scaling_registers_update_required[index] = false;
driver_buffers[index].scaling_buffer_dirty = false;
}
}
void is31fl3745_flush(void) {
is31fl3745_update_pwm_buffers(IS31FL3745_I2C_ADDRESS_1, 0);
#if defined(IS31FL3745_I2C_ADDRESS_2)
is31fl3745_update_pwm_buffers(IS31FL3745_I2C_ADDRESS_2, 1);
# if defined(IS31FL3745_I2C_ADDRESS_3)
is31fl3745_update_pwm_buffers(IS31FL3745_I2C_ADDRESS_3, 2);
# if defined(IS31FL3745_I2C_ADDRESS_4)
is31fl3745_update_pwm_buffers(IS31FL3745_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3745_DRIVER_COUNT; i++) {
is31fl3745_update_pwm_buffers(i);
}
}

View File

@ -84,17 +84,17 @@ typedef struct is31fl3745_led_t {
extern const is31fl3745_led_t PROGMEM g_is31fl3745_leds[IS31FL3745_LED_COUNT];
void is31fl3745_init_drivers(void);
void is31fl3745_init(uint8_t addr, uint8_t sync);
void is31fl3745_write_register(uint8_t addr, uint8_t reg, uint8_t data);
void is31fl3745_select_page(uint8_t addr, uint8_t page);
void is31fl3745_init(uint8_t index);
void is31fl3745_write_register(uint8_t index, uint8_t reg, uint8_t data);
void is31fl3745_select_page(uint8_t index, uint8_t page);
void is31fl3745_set_color(int index, uint8_t red, uint8_t green, uint8_t blue);
void is31fl3745_set_color_all(uint8_t red, uint8_t green, uint8_t blue);
void is31fl3745_set_scaling_register(uint8_t index, uint8_t red, uint8_t green, uint8_t blue);
void is31fl3745_update_pwm_buffers(uint8_t addr, uint8_t index);
void is31fl3745_update_scaling_registers(uint8_t addr, uint8_t index);
void is31fl3745_update_pwm_buffers(uint8_t index);
void is31fl3745_update_scaling_registers(uint8_t index);
void is31fl3745_flush(void);
@ -120,154 +120,154 @@ void is31fl3745_flush(void);
#define IS31FL3745_SYNC_SLAVE 0b10
#define IS31FL3745_SYNC_MASTER 0b11
#define CS1_SW1 0x00
#define CS2_SW1 0x01
#define CS3_SW1 0x02
#define CS4_SW1 0x03
#define CS5_SW1 0x04
#define CS6_SW1 0x05
#define CS7_SW1 0x06
#define CS8_SW1 0x07
#define CS9_SW1 0x08
#define CS10_SW1 0x09
#define CS11_SW1 0x0A
#define CS12_SW1 0x0B
#define CS13_SW1 0x0C
#define CS14_SW1 0x0D
#define CS15_SW1 0x0E
#define CS16_SW1 0x0F
#define CS17_SW1 0x10
#define CS18_SW1 0x11
#define SW1_CS1 0x00
#define SW1_CS2 0x01
#define SW1_CS3 0x02
#define SW1_CS4 0x03
#define SW1_CS5 0x04
#define SW1_CS6 0x05
#define SW1_CS7 0x06
#define SW1_CS8 0x07
#define SW1_CS9 0x08
#define SW1_CS10 0x09
#define SW1_CS11 0x0A
#define SW1_CS12 0x0B
#define SW1_CS13 0x0C
#define SW1_CS14 0x0D
#define SW1_CS15 0x0E
#define SW1_CS16 0x0F
#define SW1_CS17 0x10
#define SW1_CS18 0x11
#define CS1_SW2 0x12
#define CS2_SW2 0x13
#define CS3_SW2 0x14
#define CS4_SW2 0x15
#define CS5_SW2 0x16
#define CS6_SW2 0x17
#define CS7_SW2 0x18
#define CS8_SW2 0x19
#define CS9_SW2 0x1A
#define CS10_SW2 0x1B
#define CS11_SW2 0x1C
#define CS12_SW2 0x1D
#define CS13_SW2 0x1E
#define CS14_SW2 0x1F
#define CS15_SW2 0x20
#define CS16_SW2 0x21
#define CS17_SW2 0x22
#define CS18_SW2 0x23
#define SW2_CS1 0x12
#define SW2_CS2 0x13
#define SW2_CS3 0x14
#define SW2_CS4 0x15
#define SW2_CS5 0x16
#define SW2_CS6 0x17
#define SW2_CS7 0x18
#define SW2_CS8 0x19
#define SW2_CS9 0x1A
#define SW2_CS10 0x1B
#define SW2_CS11 0x1C
#define SW2_CS12 0x1D
#define SW2_CS13 0x1E
#define SW2_CS14 0x1F
#define SW2_CS15 0x20
#define SW2_CS16 0x21
#define SW2_CS17 0x22
#define SW2_CS18 0x23
#define CS1_SW3 0x24
#define CS2_SW3 0x25
#define CS3_SW3 0x26
#define CS4_SW3 0x27
#define CS5_SW3 0x28
#define CS6_SW3 0x29
#define CS7_SW3 0x2A
#define CS8_SW3 0x2B
#define CS9_SW3 0x2C
#define CS10_SW3 0x2D
#define CS11_SW3 0x2E
#define CS12_SW3 0x2F
#define CS13_SW3 0x30
#define CS14_SW3 0x31
#define CS15_SW3 0x32
#define CS16_SW3 0x33
#define CS17_SW3 0x34
#define CS18_SW3 0x35
#define SW3_CS1 0x24
#define SW3_CS2 0x25
#define SW3_CS3 0x26
#define SW3_CS4 0x27
#define SW3_CS5 0x28
#define SW3_CS6 0x29
#define SW3_CS7 0x2A
#define SW3_CS8 0x2B
#define SW3_CS9 0x2C
#define SW3_CS10 0x2D
#define SW3_CS11 0x2E
#define SW3_CS12 0x2F
#define SW3_CS13 0x30
#define SW3_CS14 0x31
#define SW3_CS15 0x32
#define SW3_CS16 0x33
#define SW3_CS17 0x34
#define SW3_CS18 0x35
#define CS1_SW4 0x36
#define CS2_SW4 0x37
#define CS3_SW4 0x38
#define CS4_SW4 0x39
#define CS5_SW4 0x3A
#define CS6_SW4 0x3B
#define CS7_SW4 0x3C
#define CS8_SW4 0x3D
#define CS9_SW4 0x3E
#define CS10_SW4 0x3F
#define CS11_SW4 0x40
#define CS12_SW4 0x41
#define CS13_SW4 0x42
#define CS14_SW4 0x43
#define CS15_SW4 0x44
#define CS16_SW4 0x45
#define CS17_SW4 0x46
#define CS18_SW4 0x47
#define SW4_CS1 0x36
#define SW4_CS2 0x37
#define SW4_CS3 0x38
#define SW4_CS4 0x39
#define SW4_CS5 0x3A
#define SW4_CS6 0x3B
#define SW4_CS7 0x3C
#define SW4_CS8 0x3D
#define SW4_CS9 0x3E
#define SW4_CS10 0x3F
#define SW4_CS11 0x40
#define SW4_CS12 0x41
#define SW4_CS13 0x42
#define SW4_CS14 0x43
#define SW4_CS15 0x44
#define SW4_CS16 0x45
#define SW4_CS17 0x46
#define SW4_CS18 0x47
#define CS1_SW5 0x48
#define CS2_SW5 0x49
#define CS3_SW5 0x4A
#define CS4_SW5 0x4B
#define CS5_SW5 0x4C
#define CS6_SW5 0x4D
#define CS7_SW5 0x4E
#define CS8_SW5 0x4F
#define CS9_SW5 0x50
#define CS10_SW5 0x51
#define CS11_SW5 0x52
#define CS12_SW5 0x53
#define CS13_SW5 0x54
#define CS14_SW5 0x55
#define CS15_SW5 0x56
#define CS16_SW5 0x57
#define CS17_SW5 0x58
#define CS18_SW5 0x59
#define SW5_CS1 0x48
#define SW5_CS2 0x49
#define SW5_CS3 0x4A
#define SW5_CS4 0x4B
#define SW5_CS5 0x4C
#define SW5_CS6 0x4D
#define SW5_CS7 0x4E
#define SW5_CS8 0x4F
#define SW5_CS9 0x50
#define SW5_CS10 0x51
#define SW5_CS11 0x52
#define SW5_CS12 0x53
#define SW5_CS13 0x54
#define SW5_CS14 0x55
#define SW5_CS15 0x56
#define SW5_CS16 0x57
#define SW5_CS17 0x58
#define SW5_CS18 0x59
#define CS1_SW6 0x5A
#define CS2_SW6 0x5B
#define CS3_SW6 0x5C
#define CS4_SW6 0x5D
#define CS5_SW6 0x5E
#define CS6_SW6 0x5F
#define CS7_SW6 0x60
#define CS8_SW6 0x61
#define CS9_SW6 0x62
#define CS10_SW6 0x63
#define CS11_SW6 0x64
#define CS12_SW6 0x65
#define CS13_SW6 0x66
#define CS14_SW6 0x67
#define CS15_SW6 0x68
#define CS16_SW6 0x69
#define CS17_SW6 0x6A
#define CS18_SW6 0x6B
#define SW6_CS1 0x5A
#define SW6_CS2 0x5B
#define SW6_CS3 0x5C
#define SW6_CS4 0x5D
#define SW6_CS5 0x5E
#define SW6_CS6 0x5F
#define SW6_CS7 0x60
#define SW6_CS8 0x61
#define SW6_CS9 0x62
#define SW6_CS10 0x63
#define SW6_CS11 0x64
#define SW6_CS12 0x65
#define SW6_CS13 0x66
#define SW6_CS14 0x67
#define SW6_CS15 0x68
#define SW6_CS16 0x69
#define SW6_CS17 0x6A
#define SW6_CS18 0x6B
#define CS1_SW7 0x6C
#define CS2_SW7 0x6D
#define CS3_SW7 0x6E
#define CS4_SW7 0x6F
#define CS5_SW7 0x70
#define CS6_SW7 0x71
#define CS7_SW7 0x72
#define CS8_SW7 0x73
#define CS9_SW7 0x74
#define CS10_SW7 0x75
#define CS11_SW7 0x76
#define CS12_SW7 0x77
#define CS13_SW7 0x78
#define CS14_SW7 0x79
#define CS15_SW7 0x7A
#define CS16_SW7 0x7B
#define CS17_SW7 0x7C
#define CS18_SW7 0x7D
#define SW7_CS1 0x6C
#define SW7_CS2 0x6D
#define SW7_CS3 0x6E
#define SW7_CS4 0x6F
#define SW7_CS5 0x70
#define SW7_CS6 0x71
#define SW7_CS7 0x72
#define SW7_CS8 0x73
#define SW7_CS9 0x74
#define SW7_CS10 0x75
#define SW7_CS11 0x76
#define SW7_CS12 0x77
#define SW7_CS13 0x78
#define SW7_CS14 0x79
#define SW7_CS15 0x7A
#define SW7_CS16 0x7B
#define SW7_CS17 0x7C
#define SW7_CS18 0x7D
#define CS1_SW8 0x7E
#define CS2_SW8 0x7F
#define CS3_SW8 0x80
#define CS4_SW8 0x81
#define CS5_SW8 0x82
#define CS6_SW8 0x83
#define CS7_SW8 0x84
#define CS8_SW8 0x85
#define CS9_SW8 0x86
#define CS10_SW8 0x87
#define CS11_SW8 0x88
#define CS12_SW8 0x89
#define CS13_SW8 0x8A
#define CS14_SW8 0x8B
#define CS15_SW8 0x8C
#define CS16_SW8 0x8D
#define CS17_SW8 0x8E
#define CS18_SW8 0x8F
#define SW8_CS1 0x7E
#define SW8_CS2 0x7F
#define SW8_CS3 0x80
#define SW8_CS4 0x81
#define SW8_CS5 0x82
#define SW8_CS6 0x83
#define SW8_CS7 0x84
#define SW8_CS8 0x85
#define SW8_CS9 0x86
#define SW8_CS10 0x87
#define SW8_CS11 0x88
#define SW8_CS12 0x89
#define SW8_CS13 0x8A
#define SW8_CS14 0x8B
#define SW8_CS15 0x8C
#define SW8_CS16 0x8D
#define SW8_CS17 0x8E
#define SW8_CS18 0x8F

View File

@ -20,6 +20,7 @@
#include "is31fl3746a-mono.h"
#include "i2c_master.h"
#include "gpio.h"
#include "wait.h"
#define IS31FL3746A_PWM_REGISTER_COUNT 72
@ -53,28 +54,49 @@
# define IS31FL3746A_GLOBAL_CURRENT 0xFF
#endif
uint8_t g_pwm_buffer[IS31FL3746A_DRIVER_COUNT][IS31FL3746A_PWM_REGISTER_COUNT];
bool g_pwm_buffer_update_required[IS31FL3746A_DRIVER_COUNT] = {false};
bool g_scaling_registers_update_required[IS31FL3746A_DRIVER_COUNT] = {false};
const uint8_t i2c_addresses[IS31FL3746A_DRIVER_COUNT] = {
IS31FL3746A_I2C_ADDRESS_1,
#ifdef IS31FL3746A_I2C_ADDRESS_2
IS31FL3746A_I2C_ADDRESS_2,
# ifdef IS31FL3746A_I2C_ADDRESS_3
IS31FL3746A_I2C_ADDRESS_3,
# ifdef IS31FL3746A_I2C_ADDRESS_4
IS31FL3746A_I2C_ADDRESS_4,
# endif
# endif
#endif
};
uint8_t g_scaling_registers[IS31FL3746A_DRIVER_COUNT][IS31FL3746A_SCALING_REGISTER_COUNT];
typedef struct is31fl3746a_driver_t {
uint8_t pwm_buffer[IS31FL3746A_PWM_REGISTER_COUNT];
bool pwm_buffer_dirty;
uint8_t scaling_buffer[IS31FL3746A_SCALING_REGISTER_COUNT];
bool scaling_buffer_dirty;
} PACKED is31fl3746a_driver_t;
void is31fl3746a_write_register(uint8_t addr, uint8_t reg, uint8_t data) {
is31fl3746a_driver_t driver_buffers[IS31FL3746A_DRIVER_COUNT] = {{
.pwm_buffer = {0},
.pwm_buffer_dirty = false,
.scaling_buffer = {0},
.scaling_buffer_dirty = false,
}};
void is31fl3746a_write_register(uint8_t index, uint8_t reg, uint8_t data) {
#if IS31FL3746A_I2C_PERSISTENCE > 0
for (uint8_t i = 0; i < IS31FL3746A_I2C_PERSISTENCE; i++) {
if (i2c_write_register(addr << 1, reg, &data, 1, IS31FL3746A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3746A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, reg, &data, 1, IS31FL3746A_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3746A_I2C_TIMEOUT);
#endif
}
void is31fl3746a_select_page(uint8_t addr, uint8_t page) {
is31fl3746a_write_register(addr, IS31FL3746A_REG_COMMAND_WRITE_LOCK, IS31FL3746A_COMMAND_WRITE_LOCK_MAGIC);
is31fl3746a_write_register(addr, IS31FL3746A_REG_COMMAND, page);
void is31fl3746a_select_page(uint8_t index, uint8_t page) {
is31fl3746a_write_register(index, IS31FL3746A_REG_COMMAND_WRITE_LOCK, IS31FL3746A_COMMAND_WRITE_LOCK_MAGIC);
is31fl3746a_write_register(index, IS31FL3746A_REG_COMMAND, page);
}
void is31fl3746a_write_pwm_buffer(uint8_t addr, uint8_t index) {
void is31fl3746a_write_pwm_buffer(uint8_t index) {
// Assumes page 0 is already selected.
// Transmit PWM registers in 4 transfers of 18 bytes.
@ -82,10 +104,10 @@ void is31fl3746a_write_pwm_buffer(uint8_t addr, uint8_t index) {
for (uint8_t i = 0; i < IS31FL3746A_PWM_REGISTER_COUNT; i += 18) {
#if IS31FL3746A_I2C_PERSISTENCE > 0
for (uint8_t j = 0; j < IS31FL3746A_I2C_PERSISTENCE; j++) {
if (i2c_write_register(addr << 1, i + 1, g_pwm_buffer[index] + i, 18, IS31FL3746A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, i + 1, driver_buffers[index].pwm_buffer + i, 18, IS31FL3746A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, i + 1, g_pwm_buffer[index] + i, 18, IS31FL3746A_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, i + 1, driver_buffers[index].pwm_buffer + i, 18, IS31FL3746A_I2C_TIMEOUT);
#endif
}
}
@ -93,59 +115,50 @@ void is31fl3746a_write_pwm_buffer(uint8_t addr, uint8_t index) {
void is31fl3746a_init_drivers(void) {
i2c_init();
is31fl3746a_init(IS31FL3746A_I2C_ADDRESS_1);
#if defined(IS31FL3746A_I2C_ADDRESS_2)
is31fl3746a_init(IS31FL3746A_I2C_ADDRESS_2);
# if defined(IS31FL3746A_I2C_ADDRESS_3)
is31fl3746a_init(IS31FL3746A_I2C_ADDRESS_3);
# if defined(IS31FL3746A_I2C_ADDRESS_4)
is31fl3746a_init(IS31FL3746A_I2C_ADDRESS_4);
# endif
# endif
#if defined(IS31FL3746A_SDB_PIN)
gpio_set_pin_output(IS31FL3746A_SDB_PIN);
gpio_write_pin_high(IS31FL3746A_SDB_PIN);
#endif
for (uint8_t i = 0; i < IS31FL3746A_DRIVER_COUNT; i++) {
is31fl3746a_init(i);
}
for (int i = 0; i < IS31FL3746A_LED_COUNT; i++) {
is31fl3746a_set_scaling_register(i, 0xFF);
}
is31fl3746a_update_scaling_registers(IS31FL3746A_I2C_ADDRESS_1, 0);
#if defined(IS31FL3746A_I2C_ADDRESS_2)
is31fl3746a_update_scaling_registers(IS31FL3746A_I2C_ADDRESS_2, 1);
# if defined(IS31FL3746A_I2C_ADDRESS_3)
is31fl3746a_update_scaling_registers(IS31FL3746A_I2C_ADDRESS_3, 2);
# if defined(IS31FL3746A_I2C_ADDRESS_4)
is31fl3746a_update_scaling_registers(IS31FL3746A_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3746A_DRIVER_COUNT; i++) {
is31fl3746a_update_scaling_registers(i);
}
}
void is31fl3746a_init(uint8_t addr) {
void is31fl3746a_init(uint8_t index) {
// In order to avoid the LEDs being driven with garbage data
// in the LED driver's PWM registers, shutdown is enabled last.
// Set up the mode and other settings, clear the PWM registers,
// then disable software shutdown.
is31fl3746a_select_page(addr, IS31FL3746A_COMMAND_SCALING);
is31fl3746a_select_page(index, IS31FL3746A_COMMAND_SCALING);
// Turn off all LEDs.
for (uint8_t i = 0; i < IS31FL3746A_SCALING_REGISTER_COUNT; i++) {
is31fl3746a_write_register(addr, i + 1, 0x00);
is31fl3746a_write_register(index, i + 1, 0x00);
}
is31fl3746a_select_page(addr, IS31FL3746A_COMMAND_PWM);
is31fl3746a_select_page(index, IS31FL3746A_COMMAND_PWM);
for (uint8_t i = 0; i < IS31FL3746A_PWM_REGISTER_COUNT; i++) {
is31fl3746a_write_register(addr, i + 1, 0x00);
is31fl3746a_write_register(index, i + 1, 0x00);
}
is31fl3746a_select_page(addr, IS31FL3746A_COMMAND_FUNCTION);
is31fl3746a_select_page(index, IS31FL3746A_COMMAND_FUNCTION);
is31fl3746a_write_register(addr, IS31FL3746A_FUNCTION_REG_PULLDOWNUP, (IS31FL3746A_SW_PULLDOWN << 4) | IS31FL3746A_CS_PULLUP);
is31fl3746a_write_register(addr, IS31FL3746A_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3746A_GLOBAL_CURRENT);
is31fl3746a_write_register(addr, IS31FL3746A_FUNCTION_REG_PWM_ENABLE, 0x01);
is31fl3746a_write_register(addr, IS31FL3746A_FUNCTION_REG_PWM_FREQUENCY, IS31FL3746A_PWM_FREQUENCY);
is31fl3746a_write_register(addr, IS31FL3746A_FUNCTION_REG_CONFIGURATION, IS31FL3746A_CONFIGURATION);
is31fl3746a_write_register(index, IS31FL3746A_FUNCTION_REG_PULLDOWNUP, (IS31FL3746A_SW_PULLDOWN << 4) | IS31FL3746A_CS_PULLUP);
is31fl3746a_write_register(index, IS31FL3746A_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3746A_GLOBAL_CURRENT);
is31fl3746a_write_register(index, IS31FL3746A_FUNCTION_REG_PWM_ENABLE, 0x01);
is31fl3746a_write_register(index, IS31FL3746A_FUNCTION_REG_PWM_FREQUENCY, IS31FL3746A_PWM_FREQUENCY);
is31fl3746a_write_register(index, IS31FL3746A_FUNCTION_REG_CONFIGURATION, IS31FL3746A_CONFIGURATION);
// Wait 10ms to ensure the device has woken up.
wait_ms(10);
@ -157,12 +170,12 @@ void is31fl3746a_set_value(int index, uint8_t value) {
if (index >= 0 && index < IS31FL3746A_LED_COUNT) {
memcpy_P(&led, (&g_is31fl3746a_leds[index]), sizeof(led));
if (g_pwm_buffer[led.driver][led.v] == value) {
if (driver_buffers[led.driver].pwm_buffer[led.v] == value) {
return;
}
g_pwm_buffer_update_required[led.driver] = true;
g_pwm_buffer[led.driver][led.v] = value;
driver_buffers[led.driver].pwm_buffer[led.v] = value;
driver_buffers[led.driver].pwm_buffer_dirty = true;
}
}
@ -176,41 +189,34 @@ void is31fl3746a_set_scaling_register(uint8_t index, uint8_t value) {
is31fl3746a_led_t led;
memcpy_P(&led, (&g_is31fl3746a_leds[index]), sizeof(led));
g_scaling_registers[led.driver][led.v] = value;
g_scaling_registers_update_required[led.driver] = true;
driver_buffers[led.driver].scaling_buffer[led.v] = value;
driver_buffers[led.driver].scaling_buffer_dirty = true;
}
void is31fl3746a_update_pwm_buffers(uint8_t addr, uint8_t index) {
if (g_pwm_buffer_update_required[index]) {
is31fl3746a_select_page(addr, IS31FL3746A_COMMAND_PWM);
void is31fl3746a_update_pwm_buffers(uint8_t index) {
if (driver_buffers[index].pwm_buffer_dirty) {
is31fl3746a_select_page(index, IS31FL3746A_COMMAND_PWM);
is31fl3746a_write_pwm_buffer(addr, index);
is31fl3746a_write_pwm_buffer(index);
g_pwm_buffer_update_required[index] = false;
driver_buffers[index].pwm_buffer_dirty = false;
}
}
void is31fl3746a_update_scaling_registers(uint8_t addr, uint8_t index) {
if (g_scaling_registers_update_required[index]) {
is31fl3746a_select_page(addr, IS31FL3746A_COMMAND_SCALING);
void is31fl3746a_update_scaling_registers(uint8_t index) {
if (driver_buffers[index].scaling_buffer_dirty) {
is31fl3746a_select_page(index, IS31FL3746A_COMMAND_SCALING);
for (uint8_t i = 0; i < IS31FL3746A_SCALING_REGISTER_COUNT; i++) {
is31fl3746a_write_register(addr, i + 1, g_scaling_registers[index][i]);
is31fl3746a_write_register(index, i + 1, driver_buffers[index].scaling_buffer[i]);
}
g_scaling_registers_update_required[index] = false;
driver_buffers[index].scaling_buffer_dirty = false;
}
}
void is31fl3746a_flush(void) {
is31fl3746a_update_pwm_buffers(IS31FL3746A_I2C_ADDRESS_1, 0);
#if defined(IS31FL3746A_I2C_ADDRESS_2)
is31fl3746a_update_pwm_buffers(IS31FL3746A_I2C_ADDRESS_2, 1);
# if defined(IS31FL3746A_I2C_ADDRESS_3)
is31fl3746a_update_pwm_buffers(IS31FL3746A_I2C_ADDRESS_3, 2);
# if defined(IS31FL3746A_I2C_ADDRESS_4)
is31fl3746a_update_pwm_buffers(IS31FL3746A_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3746A_DRIVER_COUNT; i++) {
is31fl3746a_update_pwm_buffers(i);
}
}

View File

@ -84,17 +84,17 @@ typedef struct is31fl3746a_led_t {
extern const is31fl3746a_led_t PROGMEM g_is31fl3746a_leds[IS31FL3746A_LED_COUNT];
void is31fl3746a_init_drivers(void);
void is31fl3746a_init(uint8_t addr);
void is31fl3746a_write_register(uint8_t addr, uint8_t reg, uint8_t data);
void is31fl3746a_select_page(uint8_t addr, uint8_t page);
void is31fl3746a_init(uint8_t index);
void is31fl3746a_write_register(uint8_t index, uint8_t reg, uint8_t data);
void is31fl3746a_select_page(uint8_t index, uint8_t page);
void is31fl3746a_set_value(int index, uint8_t value);
void is31fl3746a_set_value_all(uint8_t value);
void is31fl3746a_set_scaling_register(uint8_t index, uint8_t value);
void is31fl3746a_update_pwm_buffers(uint8_t addr, uint8_t index);
void is31fl3746a_update_scaling_registers(uint8_t addr, uint8_t index);
void is31fl3746a_update_pwm_buffers(uint8_t index);
void is31fl3746a_update_scaling_registers(uint8_t index);
void is31fl3746a_flush(void);
@ -124,78 +124,78 @@ void is31fl3746a_flush(void);
#define IS31FL3746A_PWM_FREQUENCY_906_HZ 0b101
#define IS31FL3746A_PWM_FREQUENCY_453_HZ 0b110
#define CS1_SW1 0x00
#define CS2_SW1 0x01
#define CS3_SW1 0x02
#define CS4_SW1 0x03
#define CS5_SW1 0x04
#define CS6_SW1 0x05
#define CS7_SW1 0x06
#define CS8_SW1 0x07
#define CS9_SW1 0x08
#define CS10_SW1 0x09
#define CS11_SW1 0x0A
#define CS12_SW1 0x0B
#define CS13_SW1 0x0C
#define CS14_SW1 0x0D
#define CS15_SW1 0x0E
#define CS16_SW1 0x0F
#define CS17_SW1 0x10
#define CS18_SW1 0x11
#define SW1_CS1 0x00
#define SW1_CS2 0x01
#define SW1_CS3 0x02
#define SW1_CS4 0x03
#define SW1_CS5 0x04
#define SW1_CS6 0x05
#define SW1_CS7 0x06
#define SW1_CS8 0x07
#define SW1_CS9 0x08
#define SW1_CS10 0x09
#define SW1_CS11 0x0A
#define SW1_CS12 0x0B
#define SW1_CS13 0x0C
#define SW1_CS14 0x0D
#define SW1_CS15 0x0E
#define SW1_CS16 0x0F
#define SW1_CS17 0x10
#define SW1_CS18 0x11
#define CS1_SW2 0x12
#define CS2_SW2 0x13
#define CS3_SW2 0x14
#define CS4_SW2 0x15
#define CS5_SW2 0x16
#define CS6_SW2 0x17
#define CS7_SW2 0x18
#define CS8_SW2 0x19
#define CS9_SW2 0x1A
#define CS10_SW2 0x1B
#define CS11_SW2 0x1C
#define CS12_SW2 0x1D
#define CS13_SW2 0x1E
#define CS14_SW2 0x1F
#define CS15_SW2 0x20
#define CS16_SW2 0x21
#define CS17_SW2 0x22
#define CS18_SW2 0x23
#define SW2_CS1 0x12
#define SW2_CS2 0x13
#define SW2_CS3 0x14
#define SW2_CS4 0x15
#define SW2_CS5 0x16
#define SW2_CS6 0x17
#define SW2_CS7 0x18
#define SW2_CS8 0x19
#define SW2_CS9 0x1A
#define SW2_CS10 0x1B
#define SW2_CS11 0x1C
#define SW2_CS12 0x1D
#define SW2_CS13 0x1E
#define SW2_CS14 0x1F
#define SW2_CS15 0x20
#define SW2_CS16 0x21
#define SW2_CS17 0x22
#define SW2_CS18 0x23
#define CS1_SW3 0x24
#define CS2_SW3 0x25
#define CS3_SW3 0x26
#define CS4_SW3 0x27
#define CS5_SW3 0x28
#define CS6_SW3 0x29
#define CS7_SW3 0x2A
#define CS8_SW3 0x2B
#define CS9_SW3 0x2C
#define CS10_SW3 0x2D
#define CS11_SW3 0x2E
#define CS12_SW3 0x2F
#define CS13_SW3 0x30
#define CS14_SW3 0x31
#define CS15_SW3 0x32
#define CS16_SW3 0x33
#define CS17_SW3 0x34
#define CS18_SW3 0x35
#define SW3_CS1 0x24
#define SW3_CS2 0x25
#define SW3_CS3 0x26
#define SW3_CS4 0x27
#define SW3_CS5 0x28
#define SW3_CS6 0x29
#define SW3_CS7 0x2A
#define SW3_CS8 0x2B
#define SW3_CS9 0x2C
#define SW3_CS10 0x2D
#define SW3_CS11 0x2E
#define SW3_CS12 0x2F
#define SW3_CS13 0x30
#define SW3_CS14 0x31
#define SW3_CS15 0x32
#define SW3_CS16 0x33
#define SW3_CS17 0x34
#define SW3_CS18 0x35
#define CS1_SW4 0x36
#define CS2_SW4 0x37
#define CS3_SW4 0x38
#define CS4_SW4 0x39
#define CS5_SW4 0x3A
#define CS6_SW4 0x3B
#define CS7_SW4 0x3C
#define CS8_SW4 0x3D
#define CS9_SW4 0x3E
#define CS10_SW4 0x3F
#define CS11_SW4 0x40
#define CS12_SW4 0x41
#define CS13_SW4 0x42
#define CS14_SW4 0x43
#define CS15_SW4 0x44
#define CS16_SW4 0x45
#define CS17_SW4 0x46
#define CS18_SW4 0x47
#define SW4_CS1 0x36
#define SW4_CS2 0x37
#define SW4_CS3 0x38
#define SW4_CS4 0x39
#define SW4_CS5 0x3A
#define SW4_CS6 0x3B
#define SW4_CS7 0x3C
#define SW4_CS8 0x3D
#define SW4_CS9 0x3E
#define SW4_CS10 0x3F
#define SW4_CS11 0x40
#define SW4_CS12 0x41
#define SW4_CS13 0x42
#define SW4_CS14 0x43
#define SW4_CS15 0x44
#define SW4_CS16 0x45
#define SW4_CS17 0x46
#define SW4_CS18 0x47

View File

@ -20,6 +20,7 @@
#include "is31fl3746a.h"
#include "i2c_master.h"
#include "gpio.h"
#include "wait.h"
#define IS31FL3746A_PWM_REGISTER_COUNT 72
@ -53,28 +54,49 @@
# define IS31FL3746A_GLOBAL_CURRENT 0xFF
#endif
uint8_t g_pwm_buffer[IS31FL3746A_DRIVER_COUNT][IS31FL3746A_PWM_REGISTER_COUNT];
bool g_pwm_buffer_update_required[IS31FL3746A_DRIVER_COUNT] = {false};
bool g_scaling_registers_update_required[IS31FL3746A_DRIVER_COUNT] = {false};
const uint8_t i2c_addresses[IS31FL3746A_DRIVER_COUNT] = {
IS31FL3746A_I2C_ADDRESS_1,
#ifdef IS31FL3746A_I2C_ADDRESS_2
IS31FL3746A_I2C_ADDRESS_2,
# ifdef IS31FL3746A_I2C_ADDRESS_3
IS31FL3746A_I2C_ADDRESS_3,
# ifdef IS31FL3746A_I2C_ADDRESS_4
IS31FL3746A_I2C_ADDRESS_4,
# endif
# endif
#endif
};
uint8_t g_scaling_registers[IS31FL3746A_DRIVER_COUNT][IS31FL3746A_SCALING_REGISTER_COUNT];
typedef struct is31fl3746a_driver_t {
uint8_t pwm_buffer[IS31FL3746A_PWM_REGISTER_COUNT];
bool pwm_buffer_dirty;
uint8_t scaling_buffer[IS31FL3746A_SCALING_REGISTER_COUNT];
bool scaling_buffer_dirty;
} PACKED is31fl3746a_driver_t;
void is31fl3746a_write_register(uint8_t addr, uint8_t reg, uint8_t data) {
is31fl3746a_driver_t driver_buffers[IS31FL3746A_DRIVER_COUNT] = {{
.pwm_buffer = {0},
.pwm_buffer_dirty = false,
.scaling_buffer = {0},
.scaling_buffer_dirty = false,
}};
void is31fl3746a_write_register(uint8_t index, uint8_t reg, uint8_t data) {
#if IS31FL3746A_I2C_PERSISTENCE > 0
for (uint8_t i = 0; i < IS31FL3746A_I2C_PERSISTENCE; i++) {
if (i2c_write_register(addr << 1, reg, &data, 1, IS31FL3746A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3746A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, reg, &data, 1, IS31FL3746A_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, IS31FL3746A_I2C_TIMEOUT);
#endif
}
void is31fl3746a_select_page(uint8_t addr, uint8_t page) {
is31fl3746a_write_register(addr, IS31FL3746A_REG_COMMAND_WRITE_LOCK, IS31FL3746A_COMMAND_WRITE_LOCK_MAGIC);
is31fl3746a_write_register(addr, IS31FL3746A_REG_COMMAND, page);
void is31fl3746a_select_page(uint8_t index, uint8_t page) {
is31fl3746a_write_register(index, IS31FL3746A_REG_COMMAND_WRITE_LOCK, IS31FL3746A_COMMAND_WRITE_LOCK_MAGIC);
is31fl3746a_write_register(index, IS31FL3746A_REG_COMMAND, page);
}
void is31fl3746a_write_pwm_buffer(uint8_t addr, uint8_t index) {
void is31fl3746a_write_pwm_buffer(uint8_t index) {
// Assumes page 0 is already selected.
// Transmit PWM registers in 4 transfers of 18 bytes.
@ -82,10 +104,10 @@ void is31fl3746a_write_pwm_buffer(uint8_t addr, uint8_t index) {
for (uint8_t i = 0; i < IS31FL3746A_PWM_REGISTER_COUNT; i += 18) {
#if IS31FL3746A_I2C_PERSISTENCE > 0
for (uint8_t j = 0; j < IS31FL3746A_I2C_PERSISTENCE; j++) {
if (i2c_write_register(addr << 1, i + 1, g_pwm_buffer[index] + i, 18, IS31FL3746A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, i + 1, driver_buffers[index].pwm_buffer + i, 18, IS31FL3746A_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, i + 1, g_pwm_buffer[index] + i, 18, IS31FL3746A_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, i + 1, driver_buffers[index].pwm_buffer + i, 18, IS31FL3746A_I2C_TIMEOUT);
#endif
}
}
@ -93,59 +115,50 @@ void is31fl3746a_write_pwm_buffer(uint8_t addr, uint8_t index) {
void is31fl3746a_init_drivers(void) {
i2c_init();
is31fl3746a_init(IS31FL3746A_I2C_ADDRESS_1);
#if defined(IS31FL3746A_I2C_ADDRESS_2)
is31fl3746a_init(IS31FL3746A_I2C_ADDRESS_2);
# if defined(IS31FL3746A_I2C_ADDRESS_3)
is31fl3746a_init(IS31FL3746A_I2C_ADDRESS_3);
# if defined(IS31FL3746A_I2C_ADDRESS_4)
is31fl3746a_init(IS31FL3746A_I2C_ADDRESS_4);
# endif
# endif
#if defined(IS31FL3746A_SDB_PIN)
gpio_set_pin_output(IS31FL3746A_SDB_PIN);
gpio_write_pin_high(IS31FL3746A_SDB_PIN);
#endif
for (uint8_t i = 0; i < IS31FL3746A_DRIVER_COUNT; i++) {
is31fl3746a_init(i);
}
for (int i = 0; i < IS31FL3746A_LED_COUNT; i++) {
is31fl3746a_set_scaling_register(i, 0xFF, 0xFF, 0xFF);
}
is31fl3746a_update_scaling_registers(IS31FL3746A_I2C_ADDRESS_1, 0);
#if defined(IS31FL3746A_I2C_ADDRESS_2)
is31fl3746a_update_scaling_registers(IS31FL3746A_I2C_ADDRESS_2, 1);
# if defined(IS31FL3746A_I2C_ADDRESS_3)
is31fl3746a_update_scaling_registers(IS31FL3746A_I2C_ADDRESS_3, 2);
# if defined(IS31FL3746A_I2C_ADDRESS_4)
is31fl3746a_update_scaling_registers(IS31FL3746A_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3746A_DRIVER_COUNT; i++) {
is31fl3746a_update_scaling_registers(i);
}
}
void is31fl3746a_init(uint8_t addr) {
void is31fl3746a_init(uint8_t index) {
// In order to avoid the LEDs being driven with garbage data
// in the LED driver's PWM registers, shutdown is enabled last.
// Set up the mode and other settings, clear the PWM registers,
// then disable software shutdown.
is31fl3746a_select_page(addr, IS31FL3746A_COMMAND_SCALING);
is31fl3746a_select_page(index, IS31FL3746A_COMMAND_SCALING);
// Turn off all LEDs.
for (uint8_t i = 0; i < IS31FL3746A_SCALING_REGISTER_COUNT; i++) {
is31fl3746a_write_register(addr, i + 1, 0x00);
is31fl3746a_write_register(index, i + 1, 0x00);
}
is31fl3746a_select_page(addr, IS31FL3746A_COMMAND_PWM);
is31fl3746a_select_page(index, IS31FL3746A_COMMAND_PWM);
for (uint8_t i = 0; i < IS31FL3746A_PWM_REGISTER_COUNT; i++) {
is31fl3746a_write_register(addr, i + 1, 0x00);
is31fl3746a_write_register(index, i + 1, 0x00);
}
is31fl3746a_select_page(addr, IS31FL3746A_COMMAND_FUNCTION);
is31fl3746a_select_page(index, IS31FL3746A_COMMAND_FUNCTION);
is31fl3746a_write_register(addr, IS31FL3746A_FUNCTION_REG_PULLDOWNUP, (IS31FL3746A_SW_PULLDOWN << 4) | IS31FL3746A_CS_PULLUP);
is31fl3746a_write_register(addr, IS31FL3746A_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3746A_GLOBAL_CURRENT);
is31fl3746a_write_register(addr, IS31FL3746A_FUNCTION_REG_PWM_ENABLE, 0x01);
is31fl3746a_write_register(addr, IS31FL3746A_FUNCTION_REG_PWM_FREQUENCY, IS31FL3746A_PWM_FREQUENCY);
is31fl3746a_write_register(addr, IS31FL3746A_FUNCTION_REG_CONFIGURATION, IS31FL3746A_CONFIGURATION);
is31fl3746a_write_register(index, IS31FL3746A_FUNCTION_REG_PULLDOWNUP, (IS31FL3746A_SW_PULLDOWN << 4) | IS31FL3746A_CS_PULLUP);
is31fl3746a_write_register(index, IS31FL3746A_FUNCTION_REG_GLOBAL_CURRENT, IS31FL3746A_GLOBAL_CURRENT);
is31fl3746a_write_register(index, IS31FL3746A_FUNCTION_REG_PWM_ENABLE, 0x01);
is31fl3746a_write_register(index, IS31FL3746A_FUNCTION_REG_PWM_FREQUENCY, IS31FL3746A_PWM_FREQUENCY);
is31fl3746a_write_register(index, IS31FL3746A_FUNCTION_REG_CONFIGURATION, IS31FL3746A_CONFIGURATION);
// Wait 10ms to ensure the device has woken up.
wait_ms(10);
@ -157,14 +170,14 @@ void is31fl3746a_set_color(int index, uint8_t red, uint8_t green, uint8_t blue)
if (index >= 0 && index < IS31FL3746A_LED_COUNT) {
memcpy_P(&led, (&g_is31fl3746a_leds[index]), sizeof(led));
if (g_pwm_buffer[led.driver][led.r] == red && g_pwm_buffer[led.driver][led.g] == green && g_pwm_buffer[led.driver][led.b] == blue) {
if (driver_buffers[led.driver].pwm_buffer[led.r] == red && driver_buffers[led.driver].pwm_buffer[led.g] == green && driver_buffers[led.driver].pwm_buffer[led.b] == blue) {
return;
}
g_pwm_buffer_update_required[led.driver] = true;
g_pwm_buffer[led.driver][led.r] = red;
g_pwm_buffer[led.driver][led.g] = green;
g_pwm_buffer[led.driver][led.b] = blue;
driver_buffers[led.driver].pwm_buffer[led.r] = red;
driver_buffers[led.driver].pwm_buffer[led.g] = green;
driver_buffers[led.driver].pwm_buffer[led.b] = blue;
driver_buffers[led.driver].pwm_buffer_dirty = true;
}
}
@ -178,43 +191,36 @@ void is31fl3746a_set_scaling_register(uint8_t index, uint8_t red, uint8_t green,
is31fl3746a_led_t led;
memcpy_P(&led, (&g_is31fl3746a_leds[index]), sizeof(led));
g_scaling_registers[led.driver][led.r] = red;
g_scaling_registers[led.driver][led.g] = green;
g_scaling_registers[led.driver][led.b] = blue;
g_scaling_registers_update_required[led.driver] = true;
driver_buffers[led.driver].scaling_buffer[led.r] = red;
driver_buffers[led.driver].scaling_buffer[led.g] = green;
driver_buffers[led.driver].scaling_buffer[led.b] = blue;
driver_buffers[led.driver].scaling_buffer_dirty = true;
}
void is31fl3746a_update_pwm_buffers(uint8_t addr, uint8_t index) {
if (g_pwm_buffer_update_required[index]) {
is31fl3746a_select_page(addr, IS31FL3746A_COMMAND_PWM);
void is31fl3746a_update_pwm_buffers(uint8_t index) {
if (driver_buffers[index].pwm_buffer_dirty) {
is31fl3746a_select_page(index, IS31FL3746A_COMMAND_PWM);
is31fl3746a_write_pwm_buffer(addr, index);
is31fl3746a_write_pwm_buffer(index);
g_pwm_buffer_update_required[index] = false;
driver_buffers[index].pwm_buffer_dirty = false;
}
}
void is31fl3746a_update_scaling_registers(uint8_t addr, uint8_t index) {
if (g_scaling_registers_update_required[index]) {
is31fl3746a_select_page(addr, IS31FL3746A_COMMAND_SCALING);
void is31fl3746a_update_scaling_registers(uint8_t index) {
if (driver_buffers[index].scaling_buffer_dirty) {
is31fl3746a_select_page(index, IS31FL3746A_COMMAND_SCALING);
for (uint8_t i = 0; i < IS31FL3746A_SCALING_REGISTER_COUNT; i++) {
is31fl3746a_write_register(addr, i + 1, g_scaling_registers[index][i]);
is31fl3746a_write_register(index, i + 1, driver_buffers[index].scaling_buffer[i]);
}
g_scaling_registers_update_required[index] = false;
driver_buffers[index].scaling_buffer_dirty = false;
}
}
void is31fl3746a_flush(void) {
is31fl3746a_update_pwm_buffers(IS31FL3746A_I2C_ADDRESS_1, 0);
#if defined(IS31FL3746A_I2C_ADDRESS_2)
is31fl3746a_update_pwm_buffers(IS31FL3746A_I2C_ADDRESS_2, 1);
# if defined(IS31FL3746A_I2C_ADDRESS_3)
is31fl3746a_update_pwm_buffers(IS31FL3746A_I2C_ADDRESS_3, 2);
# if defined(IS31FL3746A_I2C_ADDRESS_4)
is31fl3746a_update_pwm_buffers(IS31FL3746A_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < IS31FL3746A_DRIVER_COUNT; i++) {
is31fl3746a_update_pwm_buffers(i);
}
}

View File

@ -86,17 +86,17 @@ typedef struct is31fl3746a_led_t {
extern const is31fl3746a_led_t PROGMEM g_is31fl3746a_leds[IS31FL3746A_LED_COUNT];
void is31fl3746a_init_drivers(void);
void is31fl3746a_init(uint8_t addr);
void is31fl3746a_write_register(uint8_t addr, uint8_t reg, uint8_t data);
void is31fl3746a_select_page(uint8_t addr, uint8_t page);
void is31fl3746a_init(uint8_t index);
void is31fl3746a_write_register(uint8_t index, uint8_t reg, uint8_t data);
void is31fl3746a_select_page(uint8_t index, uint8_t page);
void is31fl3746a_set_color(int index, uint8_t red, uint8_t green, uint8_t blue);
void is31fl3746a_set_color_all(uint8_t red, uint8_t green, uint8_t blue);
void is31fl3746a_set_scaling_register(uint8_t index, uint8_t red, uint8_t green, uint8_t blue);
void is31fl3746a_update_pwm_buffers(uint8_t addr, uint8_t index);
void is31fl3746a_update_scaling_registers(uint8_t addr, uint8_t index);
void is31fl3746a_update_pwm_buffers(uint8_t index);
void is31fl3746a_update_scaling_registers(uint8_t index);
void is31fl3746a_flush(void);
@ -126,78 +126,78 @@ void is31fl3746a_flush(void);
#define IS31FL3746A_PWM_FREQUENCY_906_HZ 0b101
#define IS31FL3746A_PWM_FREQUENCY_453_HZ 0b110
#define CS1_SW1 0x00
#define CS2_SW1 0x01
#define CS3_SW1 0x02
#define CS4_SW1 0x03
#define CS5_SW1 0x04
#define CS6_SW1 0x05
#define CS7_SW1 0x06
#define CS8_SW1 0x07
#define CS9_SW1 0x08
#define CS10_SW1 0x09
#define CS11_SW1 0x0A
#define CS12_SW1 0x0B
#define CS13_SW1 0x0C
#define CS14_SW1 0x0D
#define CS15_SW1 0x0E
#define CS16_SW1 0x0F
#define CS17_SW1 0x10
#define CS18_SW1 0x11
#define SW1_CS1 0x00
#define SW1_CS2 0x01
#define SW1_CS3 0x02
#define SW1_CS4 0x03
#define SW1_CS5 0x04
#define SW1_CS6 0x05
#define SW1_CS7 0x06
#define SW1_CS8 0x07
#define SW1_CS9 0x08
#define SW1_CS10 0x09
#define SW1_CS11 0x0A
#define SW1_CS12 0x0B
#define SW1_CS13 0x0C
#define SW1_CS14 0x0D
#define SW1_CS15 0x0E
#define SW1_CS16 0x0F
#define SW1_CS17 0x10
#define SW1_CS18 0x11
#define CS1_SW2 0x12
#define CS2_SW2 0x13
#define CS3_SW2 0x14
#define CS4_SW2 0x15
#define CS5_SW2 0x16
#define CS6_SW2 0x17
#define CS7_SW2 0x18
#define CS8_SW2 0x19
#define CS9_SW2 0x1A
#define CS10_SW2 0x1B
#define CS11_SW2 0x1C
#define CS12_SW2 0x1D
#define CS13_SW2 0x1E
#define CS14_SW2 0x1F
#define CS15_SW2 0x20
#define CS16_SW2 0x21
#define CS17_SW2 0x22
#define CS18_SW2 0x23
#define SW2_CS1 0x12
#define SW2_CS2 0x13
#define SW2_CS3 0x14
#define SW2_CS4 0x15
#define SW2_CS5 0x16
#define SW2_CS6 0x17
#define SW2_CS7 0x18
#define SW2_CS8 0x19
#define SW2_CS9 0x1A
#define SW2_CS10 0x1B
#define SW2_CS11 0x1C
#define SW2_CS12 0x1D
#define SW2_CS13 0x1E
#define SW2_CS14 0x1F
#define SW2_CS15 0x20
#define SW2_CS16 0x21
#define SW2_CS17 0x22
#define SW2_CS18 0x23
#define CS1_SW3 0x24
#define CS2_SW3 0x25
#define CS3_SW3 0x26
#define CS4_SW3 0x27
#define CS5_SW3 0x28
#define CS6_SW3 0x29
#define CS7_SW3 0x2A
#define CS8_SW3 0x2B
#define CS9_SW3 0x2C
#define CS10_SW3 0x2D
#define CS11_SW3 0x2E
#define CS12_SW3 0x2F
#define CS13_SW3 0x30
#define CS14_SW3 0x31
#define CS15_SW3 0x32
#define CS16_SW3 0x33
#define CS17_SW3 0x34
#define CS18_SW3 0x35
#define SW3_CS1 0x24
#define SW3_CS2 0x25
#define SW3_CS3 0x26
#define SW3_CS4 0x27
#define SW3_CS5 0x28
#define SW3_CS6 0x29
#define SW3_CS7 0x2A
#define SW3_CS8 0x2B
#define SW3_CS9 0x2C
#define SW3_CS10 0x2D
#define SW3_CS11 0x2E
#define SW3_CS12 0x2F
#define SW3_CS13 0x30
#define SW3_CS14 0x31
#define SW3_CS15 0x32
#define SW3_CS16 0x33
#define SW3_CS17 0x34
#define SW3_CS18 0x35
#define CS1_SW4 0x36
#define CS2_SW4 0x37
#define CS3_SW4 0x38
#define CS4_SW4 0x39
#define CS5_SW4 0x3A
#define CS6_SW4 0x3B
#define CS7_SW4 0x3C
#define CS8_SW4 0x3D
#define CS9_SW4 0x3E
#define CS10_SW4 0x3F
#define CS11_SW4 0x40
#define CS12_SW4 0x41
#define CS13_SW4 0x42
#define CS14_SW4 0x43
#define CS15_SW4 0x44
#define CS16_SW4 0x45
#define CS17_SW4 0x46
#define CS18_SW4 0x47
#define SW4_CS1 0x36
#define SW4_CS2 0x37
#define SW4_CS3 0x38
#define SW4_CS4 0x39
#define SW4_CS5 0x3A
#define SW4_CS6 0x3B
#define SW4_CS7 0x3C
#define SW4_CS8 0x3D
#define SW4_CS9 0x3E
#define SW4_CS10 0x3F
#define SW4_CS11 0x40
#define SW4_CS12 0x41
#define SW4_CS13 0x42
#define SW4_CS14 0x43
#define SW4_CS15 0x44
#define SW4_CS16 0x45
#define SW4_CS17 0x46
#define SW4_CS18 0x47

View File

@ -16,6 +16,7 @@
#include "snled27351-mono.h"
#include "i2c_master.h"
#include "gpio.h"
#define SNLED27351_PWM_REGISTER_COUNT 192
#define SNLED27351_LED_CONTROL_REGISTER_COUNT 24
@ -37,33 +38,54 @@
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }
#endif
const uint8_t i2c_addresses[SNLED27351_DRIVER_COUNT] = {
SNLED27351_I2C_ADDRESS_1,
#ifdef SNLED27351_I2C_ADDRESS_2
SNLED27351_I2C_ADDRESS_2,
# ifdef SNLED27351_I2C_ADDRESS_3
SNLED27351_I2C_ADDRESS_3,
# ifdef SNLED27351_I2C_ADDRESS_4
SNLED27351_I2C_ADDRESS_4,
# endif
# endif
#endif
};
// These buffers match the SNLED27351 PWM registers.
// The control buffers match the PG0 LED On/Off registers.
// Storing them like this is optimal for I2C transfers to the registers.
// We could optimize this and take out the unused registers from these
// buffers and the transfers in snled27351_write_pwm_buffer() but it's
// probably not worth the extra complexity.
uint8_t g_pwm_buffer[SNLED27351_DRIVER_COUNT][SNLED27351_PWM_REGISTER_COUNT];
bool g_pwm_buffer_update_required[SNLED27351_DRIVER_COUNT] = {false};
typedef struct snled27351_driver_t {
uint8_t pwm_buffer[SNLED27351_PWM_REGISTER_COUNT];
bool pwm_buffer_dirty;
uint8_t led_control_buffer[SNLED27351_LED_CONTROL_REGISTER_COUNT];
bool led_control_buffer_dirty;
} PACKED snled27351_driver_t;
uint8_t g_led_control_registers[SNLED27351_DRIVER_COUNT][SNLED27351_LED_CONTROL_REGISTER_COUNT] = {0};
bool g_led_control_registers_update_required[SNLED27351_DRIVER_COUNT] = {false};
snled27351_driver_t driver_buffers[SNLED27351_DRIVER_COUNT] = {{
.pwm_buffer = {0},
.pwm_buffer_dirty = false,
.led_control_buffer = {0},
.led_control_buffer_dirty = false,
}};
void snled27351_write_register(uint8_t addr, uint8_t reg, uint8_t data) {
void snled27351_write_register(uint8_t index, uint8_t reg, uint8_t data) {
#if SNLED27351_I2C_PERSISTENCE > 0
for (uint8_t i = 0; i < SNLED27351_I2C_PERSISTENCE; i++) {
if (i2c_write_register(addr << 1, reg, &data, 1, SNLED27351_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, SNLED27351_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, reg, &data, 1, SNLED27351_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, SNLED27351_I2C_TIMEOUT);
#endif
}
void snled27351_select_page(uint8_t addr, uint8_t page) {
snled27351_write_register(addr, SNLED27351_REG_COMMAND, page);
void snled27351_select_page(uint8_t index, uint8_t page) {
snled27351_write_register(index, SNLED27351_REG_COMMAND, page);
}
void snled27351_write_pwm_buffer(uint8_t addr, uint8_t index) {
void snled27351_write_pwm_buffer(uint8_t index) {
// Assumes PG1 is already selected.
// Transmit PWM registers in 12 transfers of 16 bytes.
@ -71,10 +93,10 @@ void snled27351_write_pwm_buffer(uint8_t addr, uint8_t index) {
for (uint8_t i = 0; i < SNLED27351_PWM_REGISTER_COUNT; i += 16) {
#if SNLED27351_I2C_PERSISTENCE > 0
for (uint8_t j = 0; j < SNLED27351_I2C_PERSISTENCE; j++) {
if (i2c_write_register(addr << 1, i, g_pwm_buffer[index] + i, 16, SNLED27351_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, SNLED27351_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, i, g_pwm_buffer[index] + i, 16, SNLED27351_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, SNLED27351_I2C_TIMEOUT);
#endif
}
}
@ -82,78 +104,69 @@ void snled27351_write_pwm_buffer(uint8_t addr, uint8_t index) {
void snled27351_init_drivers(void) {
i2c_init();
snled27351_init(SNLED27351_I2C_ADDRESS_1);
#if defined(SNLED27351_I2C_ADDRESS_2)
snled27351_init(SNLED27351_I2C_ADDRESS_2);
# if defined(SNLED27351_I2C_ADDRESS_3)
snled27351_init(SNLED27351_I2C_ADDRESS_3);
# if defined(SNLED27351_I2C_ADDRESS_4)
snled27351_init(SNLED27351_I2C_ADDRESS_4);
# endif
# endif
#if defined(SNLED27351_SDB_PIN)
gpio_set_pin_output(SNLED27351_SDB_PIN);
gpio_write_pin_high(SNLED27351_SDB_PIN);
#endif
for (uint8_t i = 0; i < SNLED27351_DRIVER_COUNT; i++) {
snled27351_init(i);
}
for (int i = 0; i < SNLED27351_LED_COUNT; i++) {
snled27351_set_led_control_register(i, true);
}
snled27351_update_led_control_registers(SNLED27351_I2C_ADDRESS_1, 0);
#if defined(SNLED27351_I2C_ADDRESS_2)
snled27351_update_led_control_registers(SNLED27351_I2C_ADDRESS_2, 1);
# if defined(SNLED27351_I2C_ADDRESS_3)
snled27351_update_led_control_registers(SNLED27351_I2C_ADDRESS_3, 2);
# if defined(SNLED27351_I2C_ADDRESS_4)
snled27351_update_led_control_registers(SNLED27351_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < SNLED27351_DRIVER_COUNT; i++) {
snled27351_update_led_control_registers(i);
}
}
void snled27351_init(uint8_t addr) {
snled27351_select_page(addr, SNLED27351_COMMAND_FUNCTION);
void snled27351_init(uint8_t index) {
snled27351_select_page(index, SNLED27351_COMMAND_FUNCTION);
// Setting LED driver to shutdown mode
snled27351_write_register(addr, SNLED27351_FUNCTION_REG_SOFTWARE_SHUTDOWN, SNLED27351_SOFTWARE_SHUTDOWN_SSD_SHUTDOWN);
snled27351_write_register(index, SNLED27351_FUNCTION_REG_SOFTWARE_SHUTDOWN, SNLED27351_SOFTWARE_SHUTDOWN_SSD_SHUTDOWN);
// Setting internal channel pulldown/pullup
snled27351_write_register(addr, SNLED27351_FUNCTION_REG_PULLDOWNUP, SNLED27351_PULLDOWNUP_ALL_ENABLED);
snled27351_write_register(index, SNLED27351_FUNCTION_REG_PULLDOWNUP, SNLED27351_PULLDOWNUP_ALL_ENABLED);
// Select number of scan phase
snled27351_write_register(addr, SNLED27351_FUNCTION_REG_SCAN_PHASE, SNLED27351_PHASE_CHANNEL);
snled27351_write_register(index, SNLED27351_FUNCTION_REG_SCAN_PHASE, SNLED27351_PHASE_CHANNEL);
// Setting PWM Delay Phase
snled27351_write_register(addr, SNLED27351_FUNCTION_REG_SLEW_RATE_CONTROL_MODE_1, SNLED27351_SLEW_RATE_CONTROL_MODE_1_PDP_ENABLE);
snled27351_write_register(index, SNLED27351_FUNCTION_REG_SLEW_RATE_CONTROL_MODE_1, SNLED27351_SLEW_RATE_CONTROL_MODE_1_PDP_ENABLE);
// Setting Driving/Sinking Channel Slew Rate
snled27351_write_register(addr, SNLED27351_FUNCTION_REG_SLEW_RATE_CONTROL_MODE_2, SNLED27351_SLEW_RATE_CONTROL_MODE_2_DSL_ENABLE | SNLED27351_SLEW_RATE_CONTROL_MODE_2_SSL_ENABLE);
snled27351_write_register(index, SNLED27351_FUNCTION_REG_SLEW_RATE_CONTROL_MODE_2, SNLED27351_SLEW_RATE_CONTROL_MODE_2_DSL_ENABLE | SNLED27351_SLEW_RATE_CONTROL_MODE_2_SSL_ENABLE);
// Setting Iref
snled27351_write_register(addr, SNLED27351_FUNCTION_REG_SOFTWARE_SLEEP, 0);
snled27351_write_register(index, SNLED27351_FUNCTION_REG_SOFTWARE_SLEEP, 0);
snled27351_select_page(addr, SNLED27351_COMMAND_LED_CONTROL);
snled27351_select_page(index, SNLED27351_COMMAND_LED_CONTROL);
for (int i = 0; i < SNLED27351_LED_CONTROL_ON_OFF_LENGTH; i++) {
snled27351_write_register(addr, i, 0x00);
snled27351_write_register(index, i, 0x00);
}
snled27351_select_page(addr, SNLED27351_COMMAND_PWM);
snled27351_select_page(index, SNLED27351_COMMAND_PWM);
for (int i = 0; i < SNLED27351_LED_CURRENT_TUNE_LENGTH; i++) {
snled27351_write_register(addr, i, 0x00);
snled27351_write_register(index, i, 0x00);
}
snled27351_select_page(addr, SNLED27351_COMMAND_CURRENT_TUNE);
snled27351_select_page(index, SNLED27351_COMMAND_CURRENT_TUNE);
uint8_t current_tune_reg_list[SNLED27351_LED_CURRENT_TUNE_LENGTH] = SNLED27351_CURRENT_TUNE;
for (int i = 0; i < SNLED27351_LED_CURRENT_TUNE_LENGTH; i++) {
snled27351_write_register(addr, i, current_tune_reg_list[i]);
snled27351_write_register(index, i, current_tune_reg_list[i]);
}
snled27351_select_page(addr, SNLED27351_COMMAND_LED_CONTROL);
snled27351_select_page(index, SNLED27351_COMMAND_LED_CONTROL);
for (int i = 0; i < SNLED27351_LED_CONTROL_ON_OFF_LENGTH; i++) {
snled27351_write_register(addr, i, 0xFF);
snled27351_write_register(index, i, 0xFF);
}
snled27351_select_page(addr, SNLED27351_COMMAND_FUNCTION);
snled27351_select_page(index, SNLED27351_COMMAND_FUNCTION);
// Setting LED driver to normal mode
snled27351_write_register(addr, SNLED27351_FUNCTION_REG_SOFTWARE_SHUTDOWN, SNLED27351_SOFTWARE_SHUTDOWN_SSD_NORMAL);
snled27351_write_register(index, SNLED27351_FUNCTION_REG_SOFTWARE_SHUTDOWN, SNLED27351_SOFTWARE_SHUTDOWN_SSD_NORMAL);
}
void snled27351_set_value(int index, uint8_t value) {
@ -161,12 +174,12 @@ void snled27351_set_value(int index, uint8_t value) {
if (index >= 0 && index < SNLED27351_LED_COUNT) {
memcpy_P(&led, (&g_snled27351_leds[index]), sizeof(led));
if (g_pwm_buffer[led.driver][led.v] == value) {
if (driver_buffers[led.driver].pwm_buffer[led.v] == value) {
return;
}
g_pwm_buffer[led.driver][led.v] = value;
g_pwm_buffer_update_required[led.driver] = true;
driver_buffers[led.driver].pwm_buffer[led.v] = value;
driver_buffers[led.driver].pwm_buffer_dirty = true;
}
}
@ -184,61 +197,54 @@ void snled27351_set_led_control_register(uint8_t index, bool value) {
uint8_t bit_value = led.v % 8;
if (value) {
g_led_control_registers[led.driver][control_register] |= (1 << bit_value);
driver_buffers[led.driver].led_control_buffer[control_register] |= (1 << bit_value);
} else {
g_led_control_registers[led.driver][control_register] &= ~(1 << bit_value);
driver_buffers[led.driver].led_control_buffer[control_register] &= ~(1 << bit_value);
}
g_led_control_registers_update_required[led.driver] = true;
driver_buffers[led.driver].led_control_buffer_dirty = true;
}
void snled27351_update_pwm_buffers(uint8_t addr, uint8_t index) {
if (g_pwm_buffer_update_required[index]) {
snled27351_select_page(addr, SNLED27351_COMMAND_PWM);
void snled27351_update_pwm_buffers(uint8_t index) {
if (driver_buffers[index].pwm_buffer_dirty) {
snled27351_select_page(index, SNLED27351_COMMAND_PWM);
snled27351_write_pwm_buffer(addr, index);
snled27351_write_pwm_buffer(index);
g_pwm_buffer_update_required[index] = false;
driver_buffers[index].pwm_buffer_dirty = false;
}
}
void snled27351_update_led_control_registers(uint8_t addr, uint8_t index) {
if (g_led_control_registers_update_required[index]) {
snled27351_select_page(addr, SNLED27351_COMMAND_LED_CONTROL);
void snled27351_update_led_control_registers(uint8_t index) {
if (driver_buffers[index].led_control_buffer_dirty) {
snled27351_select_page(index, SNLED27351_COMMAND_LED_CONTROL);
for (uint8_t i = 0; i < SNLED27351_LED_CONTROL_REGISTER_COUNT; i++) {
snled27351_write_register(addr, i, g_led_control_registers[index][i]);
snled27351_write_register(index, i, driver_buffers[index].led_control_buffer[i]);
}
g_led_control_registers_update_required[index] = false;
driver_buffers[index].led_control_buffer_dirty = false;
}
}
void snled27351_flush(void) {
snled27351_update_pwm_buffers(SNLED27351_I2C_ADDRESS_1, 0);
#if defined(SNLED27351_I2C_ADDRESS_2)
snled27351_update_pwm_buffers(SNLED27351_I2C_ADDRESS_2, 1);
# if defined(SNLED27351_I2C_ADDRESS_3)
snled27351_update_pwm_buffers(SNLED27351_I2C_ADDRESS_3, 2);
# if defined(SNLED27351_I2C_ADDRESS_4)
snled27351_update_pwm_buffers(SNLED27351_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < SNLED27351_DRIVER_COUNT; i++) {
snled27351_update_pwm_buffers(i);
}
}
void snled27351_sw_return_normal(uint8_t addr) {
snled27351_select_page(addr, SNLED27351_COMMAND_FUNCTION);
void snled27351_sw_return_normal(uint8_t index) {
snled27351_select_page(index, SNLED27351_COMMAND_FUNCTION);
// Setting LED driver to normal mode
snled27351_write_register(addr, SNLED27351_FUNCTION_REG_SOFTWARE_SHUTDOWN, SNLED27351_SOFTWARE_SHUTDOWN_SSD_NORMAL);
snled27351_write_register(index, SNLED27351_FUNCTION_REG_SOFTWARE_SHUTDOWN, SNLED27351_SOFTWARE_SHUTDOWN_SSD_NORMAL);
}
void snled27351_sw_shutdown(uint8_t addr) {
snled27351_select_page(addr, SNLED27351_COMMAND_FUNCTION);
void snled27351_sw_shutdown(uint8_t index) {
snled27351_select_page(index, SNLED27351_COMMAND_FUNCTION);
// Setting LED driver to shutdown mode
snled27351_write_register(addr, SNLED27351_FUNCTION_REG_SOFTWARE_SHUTDOWN, SNLED27351_SOFTWARE_SHUTDOWN_SSD_SHUTDOWN);
snled27351_write_register(index, SNLED27351_FUNCTION_REG_SOFTWARE_SHUTDOWN, SNLED27351_SOFTWARE_SHUTDOWN_SSD_SHUTDOWN);
// Write SW Sleep Register
snled27351_write_register(addr, SNLED27351_FUNCTION_REG_SOFTWARE_SLEEP, SNLED27351_SOFTWARE_SLEEP_ENABLE);
snled27351_write_register(index, SNLED27351_FUNCTION_REG_SOFTWARE_SLEEP, SNLED27351_SOFTWARE_SLEEP_ENABLE);
}

View File

@ -154,9 +154,9 @@ typedef struct snled27351_led_t {
extern const snled27351_led_t PROGMEM g_snled27351_leds[SNLED27351_LED_COUNT];
void snled27351_init_drivers(void);
void snled27351_init(uint8_t addr);
void snled27351_select_page(uint8_t addr, uint8_t page);
void snled27351_write_register(uint8_t addr, uint8_t reg, uint8_t data);
void snled27351_init(uint8_t index);
void snled27351_select_page(uint8_t index, uint8_t page);
void snled27351_write_register(uint8_t index, uint8_t reg, uint8_t data);
void snled27351_set_value(int index, uint8_t value);
void snled27351_set_value_all(uint8_t value);
@ -167,214 +167,420 @@ void snled27351_set_led_control_register(uint8_t index, bool value);
// (eg. from a timer interrupt).
// Call this while idle (in between matrix scans).
// If the buffer is dirty, it will update the driver with the buffer.
void snled27351_update_pwm_buffers(uint8_t addr, uint8_t index);
void snled27351_update_led_control_registers(uint8_t addr, uint8_t index);
void snled27351_update_pwm_buffers(uint8_t index);
void snled27351_update_led_control_registers(uint8_t index);
void snled27351_flush(void);
void snled27351_sw_return_normal(uint8_t addr);
void snled27351_sw_shutdown(uint8_t addr);
void snled27351_sw_return_normal(uint8_t index);
void snled27351_sw_shutdown(uint8_t index);
#define A_1 0x00
#define A_2 0x01
#define A_3 0x02
#define A_4 0x03
#define A_5 0x04
#define A_6 0x05
#define A_7 0x06
#define A_8 0x07
#define A_9 0x08
#define A_10 0x09
#define A_11 0x0A
#define A_12 0x0B
#define A_13 0x0C
#define A_14 0x0D
#define A_15 0x0E
#define A_16 0x0F
#define CB1_CA1 0x00
#define CB1_CA2 0x01
#define CB1_CA3 0x02
#define CB1_CA4 0x03
#define CB1_CA5 0x04
#define CB1_CA6 0x05
#define CB1_CA7 0x06
#define CB1_CA8 0x07
#define CB1_CA9 0x08
#define CB1_CA10 0x09
#define CB1_CA11 0x0A
#define CB1_CA12 0x0B
#define CB1_CA13 0x0C
#define CB1_CA14 0x0D
#define CB1_CA15 0x0E
#define CB1_CA16 0x0F
#define B_1 0x10
#define B_2 0x11
#define B_3 0x12
#define B_4 0x13
#define B_5 0x14
#define B_6 0x15
#define B_7 0x16
#define B_8 0x17
#define B_9 0x18
#define B_10 0x19
#define B_11 0x1A
#define B_12 0x1B
#define B_13 0x1C
#define B_14 0x1D
#define B_15 0x1E
#define B_16 0x1F
#define CB2_CA1 0x10
#define CB2_CA2 0x11
#define CB2_CA3 0x12
#define CB2_CA4 0x13
#define CB2_CA5 0x14
#define CB2_CA6 0x15
#define CB2_CA7 0x16
#define CB2_CA8 0x17
#define CB2_CA9 0x18
#define CB2_CA10 0x19
#define CB2_CA11 0x1A
#define CB2_CA12 0x1B
#define CB2_CA13 0x1C
#define CB2_CA14 0x1D
#define CB2_CA15 0x1E
#define CB2_CA16 0x1F
#define C_1 0x20
#define C_2 0x21
#define C_3 0x22
#define C_4 0x23
#define C_5 0x24
#define C_6 0x25
#define C_7 0x26
#define C_8 0x27
#define C_9 0x28
#define C_10 0x29
#define C_11 0x2A
#define C_12 0x2B
#define C_13 0x2C
#define C_14 0x2D
#define C_15 0x2E
#define C_16 0x2F
#define CB3_CA1 0x20
#define CB3_CA2 0x21
#define CB3_CA3 0x22
#define CB3_CA4 0x23
#define CB3_CA5 0x24
#define CB3_CA6 0x25
#define CB3_CA7 0x26
#define CB3_CA8 0x27
#define CB3_CA9 0x28
#define CB3_CA10 0x29
#define CB3_CA11 0x2A
#define CB3_CA12 0x2B
#define CB3_CA13 0x2C
#define CB3_CA14 0x2D
#define CB3_CA15 0x2E
#define CB3_CA16 0x2F
#define D_1 0x30
#define D_2 0x31
#define D_3 0x32
#define D_4 0x33
#define D_5 0x34
#define D_6 0x35
#define D_7 0x36
#define D_8 0x37
#define D_9 0x38
#define D_10 0x39
#define D_11 0x3A
#define D_12 0x3B
#define D_13 0x3C
#define D_14 0x3D
#define D_15 0x3E
#define D_16 0x3F
#define CB4_CA1 0x30
#define CB4_CA2 0x31
#define CB4_CA3 0x32
#define CB4_CA4 0x33
#define CB4_CA5 0x34
#define CB4_CA6 0x35
#define CB4_CA7 0x36
#define CB4_CA8 0x37
#define CB4_CA9 0x38
#define CB4_CA10 0x39
#define CB4_CA11 0x3A
#define CB4_CA12 0x3B
#define CB4_CA13 0x3C
#define CB4_CA14 0x3D
#define CB4_CA15 0x3E
#define CB4_CA16 0x3F
#define E_1 0x40
#define E_2 0x41
#define E_3 0x42
#define E_4 0x43
#define E_5 0x44
#define E_6 0x45
#define E_7 0x46
#define E_8 0x47
#define E_9 0x48
#define E_10 0x49
#define E_11 0x4A
#define E_12 0x4B
#define E_13 0x4C
#define E_14 0x4D
#define E_15 0x4E
#define E_16 0x4F
#define CB5_CA1 0x40
#define CB5_CA2 0x41
#define CB5_CA3 0x42
#define CB5_CA4 0x43
#define CB5_CA5 0x44
#define CB5_CA6 0x45
#define CB5_CA7 0x46
#define CB5_CA8 0x47
#define CB5_CA9 0x48
#define CB5_CA10 0x49
#define CB5_CA11 0x4A
#define CB5_CA12 0x4B
#define CB5_CA13 0x4C
#define CB5_CA14 0x4D
#define CB5_CA15 0x4E
#define CB5_CA16 0x4F
#define F_1 0x50
#define F_2 0x51
#define F_3 0x52
#define F_4 0x53
#define F_5 0x54
#define F_6 0x55
#define F_7 0x56
#define F_8 0x57
#define F_9 0x58
#define F_10 0x59
#define F_11 0x5A
#define F_12 0x5B
#define F_13 0x5C
#define F_14 0x5D
#define F_15 0x5E
#define F_16 0x5F
#define CB6_CA1 0x50
#define CB6_CA2 0x51
#define CB6_CA3 0x52
#define CB6_CA4 0x53
#define CB6_CA5 0x54
#define CB6_CA6 0x55
#define CB6_CA7 0x56
#define CB6_CA8 0x57
#define CB6_CA9 0x58
#define CB6_CA10 0x59
#define CB6_CA11 0x5A
#define CB6_CA12 0x5B
#define CB6_CA13 0x5C
#define CB6_CA14 0x5D
#define CB6_CA15 0x5E
#define CB6_CA16 0x5F
#define G_1 0x60
#define G_2 0x61
#define G_3 0x62
#define G_4 0x63
#define G_5 0x64
#define G_6 0x65
#define G_7 0x66
#define G_8 0x67
#define G_9 0x68
#define G_10 0x69
#define G_11 0x6A
#define G_12 0x6B
#define G_13 0x6C
#define G_14 0x6D
#define G_15 0x6E
#define G_16 0x6F
#define CB7_CA1 0x60
#define CB7_CA2 0x61
#define CB7_CA3 0x62
#define CB7_CA4 0x63
#define CB7_CA5 0x64
#define CB7_CA6 0x65
#define CB7_CA7 0x66
#define CB7_CA8 0x67
#define CB7_CA9 0x68
#define CB7_CA10 0x69
#define CB7_CA11 0x6A
#define CB7_CA12 0x6B
#define CB7_CA13 0x6C
#define CB7_CA14 0x6D
#define CB7_CA15 0x6E
#define CB7_CA16 0x6F
#define H_1 0x70
#define H_2 0x71
#define H_3 0x72
#define H_4 0x73
#define H_5 0x74
#define H_6 0x75
#define H_7 0x76
#define H_8 0x77
#define H_9 0x78
#define H_10 0x79
#define H_11 0x7A
#define H_12 0x7B
#define H_13 0x7C
#define H_14 0x7D
#define H_15 0x7E
#define H_16 0x7F
#define CB8_CA1 0x70
#define CB8_CA2 0x71
#define CB8_CA3 0x72
#define CB8_CA4 0x73
#define CB8_CA5 0x74
#define CB8_CA6 0x75
#define CB8_CA7 0x76
#define CB8_CA8 0x77
#define CB8_CA9 0x78
#define CB8_CA10 0x79
#define CB8_CA11 0x7A
#define CB8_CA12 0x7B
#define CB8_CA13 0x7C
#define CB8_CA14 0x7D
#define CB8_CA15 0x7E
#define CB8_CA16 0x7F
#define I_1 0x80
#define I_2 0x81
#define I_3 0x82
#define I_4 0x83
#define I_5 0x84
#define I_6 0x85
#define I_7 0x86
#define I_8 0x87
#define I_9 0x88
#define I_10 0x89
#define I_11 0x8A
#define I_12 0x8B
#define I_13 0x8C
#define I_14 0x8D
#define I_15 0x8E
#define I_16 0x8F
#define CB9_CA1 0x80
#define CB9_CA2 0x81
#define CB9_CA3 0x82
#define CB9_CA4 0x83
#define CB9_CA5 0x84
#define CB9_CA6 0x85
#define CB9_CA7 0x86
#define CB9_CA8 0x87
#define CB9_CA9 0x88
#define CB9_CA10 0x89
#define CB9_CA11 0x8A
#define CB9_CA12 0x8B
#define CB9_CA13 0x8C
#define CB9_CA14 0x8D
#define CB9_CA15 0x8E
#define CB9_CA16 0x8F
#define J_1 0x90
#define J_2 0x91
#define J_3 0x92
#define J_4 0x93
#define J_5 0x94
#define J_6 0x95
#define J_7 0x96
#define J_8 0x97
#define J_9 0x98
#define J_10 0x99
#define J_11 0x9A
#define J_12 0x9B
#define J_13 0x9C
#define J_14 0x9D
#define J_15 0x9E
#define J_16 0x9F
#define CB10_CA1 0x90
#define CB10_CA2 0x91
#define CB10_CA3 0x92
#define CB10_CA4 0x93
#define CB10_CA5 0x94
#define CB10_CA6 0x95
#define CB10_CA7 0x96
#define CB10_CA8 0x97
#define CB10_CA9 0x98
#define CB10_CA10 0x99
#define CB10_CA11 0x9A
#define CB10_CA12 0x9B
#define CB10_CA13 0x9C
#define CB10_CA14 0x9D
#define CB10_CA15 0x9E
#define CB10_CA16 0x9F
#define K_1 0xA0
#define K_2 0xA1
#define K_3 0xA2
#define K_4 0xA3
#define K_5 0xA4
#define K_6 0xA5
#define K_7 0xA6
#define K_8 0xA7
#define K_9 0xA8
#define K_10 0xA9
#define K_11 0xAA
#define K_12 0xAB
#define K_13 0xAC
#define K_14 0xAD
#define K_15 0xAE
#define K_16 0xAF
#define CB11_CA1 0xA0
#define CB11_CA2 0xA1
#define CB11_CA3 0xA2
#define CB11_CA4 0xA3
#define CB11_CA5 0xA4
#define CB11_CA6 0xA5
#define CB11_CA7 0xA6
#define CB11_CA8 0xA7
#define CB11_CA9 0xA8
#define CB11_CA10 0xA9
#define CB11_CA11 0xAA
#define CB11_CA12 0xAB
#define CB11_CA13 0xAC
#define CB11_CA14 0xAD
#define CB11_CA15 0xAE
#define CB11_CA16 0xAF
#define L_1 0xB0
#define L_2 0xB1
#define L_3 0xB2
#define L_4 0xB3
#define L_5 0xB4
#define L_6 0xB5
#define L_7 0xB6
#define L_8 0xB7
#define L_9 0xB8
#define L_10 0xB9
#define L_11 0xBA
#define L_12 0xBB
#define L_13 0xBC
#define L_14 0xBD
#define L_15 0xBE
#define L_16 0xBF
#define CB12_CA1 0xB0
#define CB12_CA2 0xB1
#define CB12_CA3 0xB2
#define CB12_CA4 0xB3
#define CB12_CA5 0xB4
#define CB12_CA6 0xB5
#define CB12_CA7 0xB6
#define CB12_CA8 0xB7
#define CB12_CA9 0xB8
#define CB12_CA10 0xB9
#define CB12_CA11 0xBA
#define CB12_CA12 0xBB
#define CB12_CA13 0xBC
#define CB12_CA14 0xBD
#define CB12_CA15 0xBE
#define CB12_CA16 0xBF
// DEPRECATED - DO NOT USE
#define A_1 CB1_CA1
#define A_2 CB1_CA2
#define A_3 CB1_CA3
#define A_4 CB1_CA4
#define A_5 CB1_CA5
#define A_6 CB1_CA6
#define A_7 CB1_CA7
#define A_8 CB1_CA8
#define A_9 CB1_CA9
#define A_10 CB1_CA10
#define A_11 CB1_CA11
#define A_12 CB1_CA12
#define A_13 CB1_CA13
#define A_14 CB1_CA14
#define A_15 CB1_CA15
#define A_16 CB1_CA16
#define B_1 CB2_CA1
#define B_2 CB2_CA2
#define B_3 CB2_CA3
#define B_4 CB2_CA4
#define B_5 CB2_CA5
#define B_6 CB2_CA6
#define B_7 CB2_CA7
#define B_8 CB2_CA8
#define B_9 CB2_CA9
#define B_10 CB2_CA10
#define B_11 CB2_CA11
#define B_12 CB2_CA12
#define B_13 CB2_CA13
#define B_14 CB2_CA14
#define B_15 CB2_CA15
#define B_16 CB2_CA16
#define C_1 CB3_CA1
#define C_2 CB3_CA2
#define C_3 CB3_CA3
#define C_4 CB3_CA4
#define C_5 CB3_CA5
#define C_6 CB3_CA6
#define C_7 CB3_CA7
#define C_8 CB3_CA8
#define C_9 CB3_CA9
#define C_10 CB3_CA10
#define C_11 CB3_CA11
#define C_12 CB3_CA12
#define C_13 CB3_CA13
#define C_14 CB3_CA14
#define C_15 CB3_CA15
#define C_16 CB3_CA16
#define D_1 CB4_CA1
#define D_2 CB4_CA2
#define D_3 CB4_CA3
#define D_4 CB4_CA4
#define D_5 CB4_CA5
#define D_6 CB4_CA6
#define D_7 CB4_CA7
#define D_8 CB4_CA8
#define D_9 CB4_CA9
#define D_10 CB4_CA10
#define D_11 CB4_CA11
#define D_12 CB4_CA12
#define D_13 CB4_CA13
#define D_14 CB4_CA14
#define D_15 CB4_CA15
#define D_16 CB4_CA16
#define E_1 CB5_CA1
#define E_2 CB5_CA2
#define E_3 CB5_CA3
#define E_4 CB5_CA4
#define E_5 CB5_CA5
#define E_6 CB5_CA6
#define E_7 CB5_CA7
#define E_8 CB5_CA8
#define E_9 CB5_CA9
#define E_10 CB5_CA10
#define E_11 CB5_CA11
#define E_12 CB5_CA12
#define E_13 CB5_CA13
#define E_14 CB5_CA14
#define E_15 CB5_CA15
#define E_16 CB5_CA16
#define F_1 CB6_CA1
#define F_2 CB6_CA2
#define F_3 CB6_CA3
#define F_4 CB6_CA4
#define F_5 CB6_CA5
#define F_6 CB6_CA6
#define F_7 CB6_CA7
#define F_8 CB6_CA8
#define F_9 CB6_CA9
#define F_10 CB6_CA10
#define F_11 CB6_CA11
#define F_12 CB6_CA12
#define F_13 CB6_CA13
#define F_14 CB6_CA14
#define F_15 CB6_CA15
#define F_16 CB6_CA16
#define G_1 CB7_CA1
#define G_2 CB7_CA2
#define G_3 CB7_CA3
#define G_4 CB7_CA4
#define G_5 CB7_CA5
#define G_6 CB7_CA6
#define G_7 CB7_CA7
#define G_8 CB7_CA8
#define G_9 CB7_CA9
#define G_10 CB7_CA10
#define G_11 CB7_CA11
#define G_12 CB7_CA12
#define G_13 CB7_CA13
#define G_14 CB7_CA14
#define G_15 CB7_CA15
#define G_16 CB7_CA16
#define H_1 CB8_CA1
#define H_2 CB8_CA2
#define H_3 CB8_CA3
#define H_4 CB8_CA4
#define H_5 CB8_CA5
#define H_6 CB8_CA6
#define H_7 CB8_CA7
#define H_8 CB8_CA8
#define H_9 CB8_CA9
#define H_10 CB8_CA10
#define H_11 CB8_CA11
#define H_12 CB8_CA12
#define H_13 CB8_CA13
#define H_14 CB8_CA14
#define H_15 CB8_CA15
#define H_16 CB8_CA16
#define I_1 CB9_CA1
#define I_2 CB9_CA2
#define I_3 CB9_CA3
#define I_4 CB9_CA4
#define I_5 CB9_CA5
#define I_6 CB9_CA6
#define I_7 CB9_CA7
#define I_8 CB9_CA8
#define I_9 CB9_CA9
#define I_10 CB9_CA10
#define I_11 CB9_CA11
#define I_12 CB9_CA12
#define I_13 CB9_CA13
#define I_14 CB9_CA14
#define I_15 CB9_CA15
#define I_16 CB9_CA16
#define J_1 CB10_CA1
#define J_2 CB10_CA2
#define J_3 CB10_CA3
#define J_4 CB10_CA4
#define J_5 CB10_CA5
#define J_6 CB10_CA6
#define J_7 CB10_CA7
#define J_8 CB10_CA8
#define J_9 CB10_CA9
#define J_10 CB10_CA10
#define J_11 CB10_CA11
#define J_12 CB10_CA12
#define J_13 CB10_CA13
#define J_14 CB10_CA14
#define J_15 CB10_CA15
#define J_16 CB10_CA16
#define K_1 CB11_CA1
#define K_2 CB11_CA2
#define K_3 CB11_CA3
#define K_4 CB11_CA4
#define K_5 CB11_CA5
#define K_6 CB11_CA6
#define K_7 CB11_CA7
#define K_8 CB11_CA8
#define K_9 CB11_CA9
#define K_10 CB11_CA10
#define K_11 CB11_CA11
#define K_12 CB11_CA12
#define K_13 CB11_CA13
#define K_14 CB11_CA14
#define K_15 CB11_CA15
#define K_16 CB11_CA16
#define L_1 CB12_CA1
#define L_2 CB12_CA2
#define L_3 CB12_CA3
#define L_4 CB12_CA4
#define L_5 CB12_CA5
#define L_6 CB12_CA6
#define L_7 CB12_CA7
#define L_8 CB12_CA8
#define L_9 CB12_CA9
#define L_10 CB12_CA10
#define L_11 CB12_CA11
#define L_12 CB12_CA12
#define L_13 CB12_CA13
#define L_14 CB12_CA14
#define L_15 CB12_CA15
#define L_16 CB12_CA16

View File

@ -16,6 +16,7 @@
#include "snled27351.h"
#include "i2c_master.h"
#include "gpio.h"
#define SNLED27351_PWM_REGISTER_COUNT 192
#define SNLED27351_LED_CONTROL_REGISTER_COUNT 24
@ -37,33 +38,54 @@
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }
#endif
const uint8_t i2c_addresses[SNLED27351_DRIVER_COUNT] = {
SNLED27351_I2C_ADDRESS_1,
#ifdef SNLED27351_I2C_ADDRESS_2
SNLED27351_I2C_ADDRESS_2,
# ifdef SNLED27351_I2C_ADDRESS_3
SNLED27351_I2C_ADDRESS_3,
# ifdef SNLED27351_I2C_ADDRESS_4
SNLED27351_I2C_ADDRESS_4,
# endif
# endif
#endif
};
// These buffers match the SNLED27351 PWM registers.
// The control buffers match the PG0 LED On/Off registers.
// Storing them like this is optimal for I2C transfers to the registers.
// We could optimize this and take out the unused registers from these
// buffers and the transfers in snled27351_write_pwm_buffer() but it's
// probably not worth the extra complexity.
uint8_t g_pwm_buffer[SNLED27351_DRIVER_COUNT][SNLED27351_PWM_REGISTER_COUNT];
bool g_pwm_buffer_update_required[SNLED27351_DRIVER_COUNT] = {false};
typedef struct snled27351_driver_t {
uint8_t pwm_buffer[SNLED27351_PWM_REGISTER_COUNT];
bool pwm_buffer_dirty;
uint8_t led_control_buffer[SNLED27351_LED_CONTROL_REGISTER_COUNT];
bool led_control_buffer_dirty;
} PACKED snled27351_driver_t;
uint8_t g_led_control_registers[SNLED27351_DRIVER_COUNT][SNLED27351_LED_CONTROL_REGISTER_COUNT] = {0};
bool g_led_control_registers_update_required[SNLED27351_DRIVER_COUNT] = {false};
snled27351_driver_t driver_buffers[SNLED27351_DRIVER_COUNT] = {{
.pwm_buffer = {0},
.pwm_buffer_dirty = false,
.led_control_buffer = {0},
.led_control_buffer_dirty = false,
}};
void snled27351_write_register(uint8_t addr, uint8_t reg, uint8_t data) {
void snled27351_write_register(uint8_t index, uint8_t reg, uint8_t data) {
#if SNLED27351_I2C_PERSISTENCE > 0
for (uint8_t i = 0; i < SNLED27351_I2C_PERSISTENCE; i++) {
if (i2c_write_register(addr << 1, reg, &data, 1, SNLED27351_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, SNLED27351_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, reg, &data, 1, SNLED27351_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, reg, &data, 1, SNLED27351_I2C_TIMEOUT);
#endif
}
void snled27351_select_page(uint8_t addr, uint8_t page) {
snled27351_write_register(addr, SNLED27351_REG_COMMAND, page);
void snled27351_select_page(uint8_t index, uint8_t page) {
snled27351_write_register(index, SNLED27351_REG_COMMAND, page);
}
void snled27351_write_pwm_buffer(uint8_t addr, uint8_t index) {
void snled27351_write_pwm_buffer(uint8_t index) {
// Assumes PG1 is already selected.
// Transmit PWM registers in 12 transfers of 16 bytes.
@ -71,10 +93,10 @@ void snled27351_write_pwm_buffer(uint8_t addr, uint8_t index) {
for (uint8_t i = 0; i < SNLED27351_PWM_REGISTER_COUNT; i += 16) {
#if SNLED27351_I2C_PERSISTENCE > 0
for (uint8_t j = 0; j < SNLED27351_I2C_PERSISTENCE; j++) {
if (i2c_write_register(addr << 1, i, g_pwm_buffer[index] + i, 16, SNLED27351_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
if (i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, SNLED27351_I2C_TIMEOUT) == I2C_STATUS_SUCCESS) break;
}
#else
i2c_write_register(addr << 1, i, g_pwm_buffer[index] + i, 16, SNLED27351_I2C_TIMEOUT);
i2c_write_register(i2c_addresses[index] << 1, i, driver_buffers[index].pwm_buffer + i, 16, SNLED27351_I2C_TIMEOUT);
#endif
}
}
@ -82,78 +104,69 @@ void snled27351_write_pwm_buffer(uint8_t addr, uint8_t index) {
void snled27351_init_drivers(void) {
i2c_init();
snled27351_init(SNLED27351_I2C_ADDRESS_1);
#if defined(SNLED27351_I2C_ADDRESS_2)
snled27351_init(SNLED27351_I2C_ADDRESS_2);
# if defined(SNLED27351_I2C_ADDRESS_3)
snled27351_init(SNLED27351_I2C_ADDRESS_3);
# if defined(SNLED27351_I2C_ADDRESS_4)
snled27351_init(SNLED27351_I2C_ADDRESS_4);
# endif
# endif
#if defined(SNLED27351_SDB_PIN)
gpio_set_pin_output(SNLED27351_SDB_PIN);
gpio_write_pin_high(SNLED27351_SDB_PIN);
#endif
for (uint8_t i = 0; i < SNLED27351_DRIVER_COUNT; i++) {
snled27351_init(i);
}
for (int i = 0; i < SNLED27351_LED_COUNT; i++) {
snled27351_set_led_control_register(i, true, true, true);
}
snled27351_update_led_control_registers(SNLED27351_I2C_ADDRESS_1, 0);
#if defined(SNLED27351_I2C_ADDRESS_2)
snled27351_update_led_control_registers(SNLED27351_I2C_ADDRESS_2, 1);
# if defined(SNLED27351_I2C_ADDRESS_3)
snled27351_update_led_control_registers(SNLED27351_I2C_ADDRESS_3, 2);
# if defined(SNLED27351_I2C_ADDRESS_4)
snled27351_update_led_control_registers(SNLED27351_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < SNLED27351_DRIVER_COUNT; i++) {
snled27351_update_led_control_registers(i);
}
}
void snled27351_init(uint8_t addr) {
snled27351_select_page(addr, SNLED27351_COMMAND_FUNCTION);
void snled27351_init(uint8_t index) {
snled27351_select_page(index, SNLED27351_COMMAND_FUNCTION);
// Setting LED driver to shutdown mode
snled27351_write_register(addr, SNLED27351_FUNCTION_REG_SOFTWARE_SHUTDOWN, SNLED27351_SOFTWARE_SHUTDOWN_SSD_SHUTDOWN);
snled27351_write_register(index, SNLED27351_FUNCTION_REG_SOFTWARE_SHUTDOWN, SNLED27351_SOFTWARE_SHUTDOWN_SSD_SHUTDOWN);
// Setting internal channel pulldown/pullup
snled27351_write_register(addr, SNLED27351_FUNCTION_REG_PULLDOWNUP, SNLED27351_PULLDOWNUP_ALL_ENABLED);
snled27351_write_register(index, SNLED27351_FUNCTION_REG_PULLDOWNUP, SNLED27351_PULLDOWNUP_ALL_ENABLED);
// Select number of scan phase
snled27351_write_register(addr, SNLED27351_FUNCTION_REG_SCAN_PHASE, SNLED27351_PHASE_CHANNEL);
snled27351_write_register(index, SNLED27351_FUNCTION_REG_SCAN_PHASE, SNLED27351_PHASE_CHANNEL);
// Setting PWM Delay Phase
snled27351_write_register(addr, SNLED27351_FUNCTION_REG_SLEW_RATE_CONTROL_MODE_1, SNLED27351_SLEW_RATE_CONTROL_MODE_1_PDP_ENABLE);
snled27351_write_register(index, SNLED27351_FUNCTION_REG_SLEW_RATE_CONTROL_MODE_1, SNLED27351_SLEW_RATE_CONTROL_MODE_1_PDP_ENABLE);
// Setting Driving/Sinking Channel Slew Rate
snled27351_write_register(addr, SNLED27351_FUNCTION_REG_SLEW_RATE_CONTROL_MODE_2, SNLED27351_SLEW_RATE_CONTROL_MODE_2_DSL_ENABLE | SNLED27351_SLEW_RATE_CONTROL_MODE_2_SSL_ENABLE);
snled27351_write_register(index, SNLED27351_FUNCTION_REG_SLEW_RATE_CONTROL_MODE_2, SNLED27351_SLEW_RATE_CONTROL_MODE_2_DSL_ENABLE | SNLED27351_SLEW_RATE_CONTROL_MODE_2_SSL_ENABLE);
// Setting Iref
snled27351_write_register(addr, SNLED27351_FUNCTION_REG_SOFTWARE_SLEEP, 0);
snled27351_write_register(index, SNLED27351_FUNCTION_REG_SOFTWARE_SLEEP, 0);
snled27351_select_page(addr, SNLED27351_COMMAND_LED_CONTROL);
snled27351_select_page(index, SNLED27351_COMMAND_LED_CONTROL);
for (int i = 0; i < SNLED27351_LED_CONTROL_ON_OFF_LENGTH; i++) {
snled27351_write_register(addr, i, 0x00);
snled27351_write_register(index, i, 0x00);
}
snled27351_select_page(addr, SNLED27351_COMMAND_PWM);
snled27351_select_page(index, SNLED27351_COMMAND_PWM);
for (int i = 0; i < SNLED27351_LED_CURRENT_TUNE_LENGTH; i++) {
snled27351_write_register(addr, i, 0x00);
snled27351_write_register(index, i, 0x00);
}
snled27351_select_page(addr, SNLED27351_COMMAND_CURRENT_TUNE);
snled27351_select_page(index, SNLED27351_COMMAND_CURRENT_TUNE);
uint8_t current_tune_reg_list[SNLED27351_LED_CURRENT_TUNE_LENGTH] = SNLED27351_CURRENT_TUNE;
for (int i = 0; i < SNLED27351_LED_CURRENT_TUNE_LENGTH; i++) {
snled27351_write_register(addr, i, current_tune_reg_list[i]);
snled27351_write_register(index, i, current_tune_reg_list[i]);
}
snled27351_select_page(addr, SNLED27351_COMMAND_LED_CONTROL);
snled27351_select_page(index, SNLED27351_COMMAND_LED_CONTROL);
for (int i = 0; i < SNLED27351_LED_CONTROL_ON_OFF_LENGTH; i++) {
snled27351_write_register(addr, i, 0xFF);
snled27351_write_register(index, i, 0xFF);
}
snled27351_select_page(addr, SNLED27351_COMMAND_FUNCTION);
snled27351_select_page(index, SNLED27351_COMMAND_FUNCTION);
// Setting LED driver to normal mode
snled27351_write_register(addr, SNLED27351_FUNCTION_REG_SOFTWARE_SHUTDOWN, SNLED27351_SOFTWARE_SHUTDOWN_SSD_NORMAL);
snled27351_write_register(index, SNLED27351_FUNCTION_REG_SOFTWARE_SHUTDOWN, SNLED27351_SOFTWARE_SHUTDOWN_SSD_NORMAL);
}
void snled27351_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
@ -161,14 +174,14 @@ void snled27351_set_color(int index, uint8_t red, uint8_t green, uint8_t blue) {
if (index >= 0 && index < SNLED27351_LED_COUNT) {
memcpy_P(&led, (&g_snled27351_leds[index]), sizeof(led));
if (g_pwm_buffer[led.driver][led.r] == red && g_pwm_buffer[led.driver][led.g] == green && g_pwm_buffer[led.driver][led.b] == blue) {
if (driver_buffers[led.driver].pwm_buffer[led.r] == red && driver_buffers[led.driver].pwm_buffer[led.g] == green && driver_buffers[led.driver].pwm_buffer[led.b] == blue) {
return;
}
g_pwm_buffer[led.driver][led.r] = red;
g_pwm_buffer[led.driver][led.g] = green;
g_pwm_buffer[led.driver][led.b] = blue;
g_pwm_buffer_update_required[led.driver] = true;
driver_buffers[led.driver].pwm_buffer[led.r] = red;
driver_buffers[led.driver].pwm_buffer[led.g] = green;
driver_buffers[led.driver].pwm_buffer[led.b] = blue;
driver_buffers[led.driver].pwm_buffer_dirty = true;
}
}
@ -190,71 +203,64 @@ void snled27351_set_led_control_register(uint8_t index, bool red, bool green, bo
uint8_t bit_b = led.b % 8;
if (red) {
g_led_control_registers[led.driver][control_register_r] |= (1 << bit_r);
driver_buffers[led.driver].led_control_buffer[control_register_r] |= (1 << bit_r);
} else {
g_led_control_registers[led.driver][control_register_r] &= ~(1 << bit_r);
driver_buffers[led.driver].led_control_buffer[control_register_r] &= ~(1 << bit_r);
}
if (green) {
g_led_control_registers[led.driver][control_register_g] |= (1 << bit_g);
driver_buffers[led.driver].led_control_buffer[control_register_g] |= (1 << bit_g);
} else {
g_led_control_registers[led.driver][control_register_g] &= ~(1 << bit_g);
driver_buffers[led.driver].led_control_buffer[control_register_g] &= ~(1 << bit_g);
}
if (blue) {
g_led_control_registers[led.driver][control_register_b] |= (1 << bit_b);
driver_buffers[led.driver].led_control_buffer[control_register_b] |= (1 << bit_b);
} else {
g_led_control_registers[led.driver][control_register_b] &= ~(1 << bit_b);
driver_buffers[led.driver].led_control_buffer[control_register_b] &= ~(1 << bit_b);
}
g_led_control_registers_update_required[led.driver] = true;
driver_buffers[led.driver].led_control_buffer_dirty = true;
}
void snled27351_update_pwm_buffers(uint8_t addr, uint8_t index) {
if (g_pwm_buffer_update_required[index]) {
snled27351_select_page(addr, SNLED27351_COMMAND_PWM);
void snled27351_update_pwm_buffers(uint8_t index) {
if (driver_buffers[index].pwm_buffer_dirty) {
snled27351_select_page(index, SNLED27351_COMMAND_PWM);
snled27351_write_pwm_buffer(addr, index);
snled27351_write_pwm_buffer(index);
g_pwm_buffer_update_required[index] = false;
driver_buffers[index].pwm_buffer_dirty = false;
}
}
void snled27351_update_led_control_registers(uint8_t addr, uint8_t index) {
if (g_led_control_registers_update_required[index]) {
snled27351_select_page(addr, SNLED27351_COMMAND_LED_CONTROL);
void snled27351_update_led_control_registers(uint8_t index) {
if (driver_buffers[index].led_control_buffer_dirty) {
snled27351_select_page(index, SNLED27351_COMMAND_LED_CONTROL);
for (uint8_t i = 0; i < SNLED27351_LED_CONTROL_REGISTER_COUNT; i++) {
snled27351_write_register(addr, i, g_led_control_registers[index][i]);
snled27351_write_register(index, i, driver_buffers[index].led_control_buffer[i]);
}
g_led_control_registers_update_required[index] = false;
driver_buffers[index].led_control_buffer_dirty = false;
}
}
void snled27351_flush(void) {
snled27351_update_pwm_buffers(SNLED27351_I2C_ADDRESS_1, 0);
#if defined(SNLED27351_I2C_ADDRESS_2)
snled27351_update_pwm_buffers(SNLED27351_I2C_ADDRESS_2, 1);
# if defined(SNLED27351_I2C_ADDRESS_3)
snled27351_update_pwm_buffers(SNLED27351_I2C_ADDRESS_3, 2);
# if defined(SNLED27351_I2C_ADDRESS_4)
snled27351_update_pwm_buffers(SNLED27351_I2C_ADDRESS_4, 3);
# endif
# endif
#endif
for (uint8_t i = 0; i < SNLED27351_DRIVER_COUNT; i++) {
snled27351_update_pwm_buffers(i);
}
}
void snled27351_sw_return_normal(uint8_t addr) {
snled27351_select_page(addr, SNLED27351_COMMAND_FUNCTION);
void snled27351_sw_return_normal(uint8_t index) {
snled27351_select_page(index, SNLED27351_COMMAND_FUNCTION);
// Setting LED driver to normal mode
snled27351_write_register(addr, SNLED27351_FUNCTION_REG_SOFTWARE_SHUTDOWN, SNLED27351_SOFTWARE_SHUTDOWN_SSD_NORMAL);
snled27351_write_register(index, SNLED27351_FUNCTION_REG_SOFTWARE_SHUTDOWN, SNLED27351_SOFTWARE_SHUTDOWN_SSD_NORMAL);
}
void snled27351_sw_shutdown(uint8_t addr) {
snled27351_select_page(addr, SNLED27351_COMMAND_FUNCTION);
void snled27351_sw_shutdown(uint8_t index) {
snled27351_select_page(index, SNLED27351_COMMAND_FUNCTION);
// Setting LED driver to shutdown mode
snled27351_write_register(addr, SNLED27351_FUNCTION_REG_SOFTWARE_SHUTDOWN, SNLED27351_SOFTWARE_SHUTDOWN_SSD_SHUTDOWN);
snled27351_write_register(index, SNLED27351_FUNCTION_REG_SOFTWARE_SHUTDOWN, SNLED27351_SOFTWARE_SHUTDOWN_SSD_SHUTDOWN);
// Write SW Sleep Register
snled27351_write_register(addr, SNLED27351_FUNCTION_REG_SOFTWARE_SLEEP, SNLED27351_SOFTWARE_SLEEP_ENABLE);
snled27351_write_register(index, SNLED27351_FUNCTION_REG_SOFTWARE_SLEEP, SNLED27351_SOFTWARE_SLEEP_ENABLE);
}

View File

@ -168,9 +168,9 @@ typedef struct snled27351_led_t {
extern const snled27351_led_t PROGMEM g_snled27351_leds[SNLED27351_LED_COUNT];
void snled27351_init_drivers(void);
void snled27351_init(uint8_t addr);
void snled27351_select_page(uint8_t addr, uint8_t page);
void snled27351_write_register(uint8_t addr, uint8_t reg, uint8_t data);
void snled27351_init(uint8_t index);
void snled27351_select_page(uint8_t index, uint8_t page);
void snled27351_write_register(uint8_t index, uint8_t reg, uint8_t data);
void snled27351_set_color(int index, uint8_t red, uint8_t green, uint8_t blue);
void snled27351_set_color_all(uint8_t red, uint8_t green, uint8_t blue);
@ -181,214 +181,420 @@ void snled27351_set_led_control_register(uint8_t index, bool red, bool green, bo
// (eg. from a timer interrupt).
// Call this while idle (in between matrix scans).
// If the buffer is dirty, it will update the driver with the buffer.
void snled27351_update_pwm_buffers(uint8_t addr, uint8_t index);
void snled27351_update_led_control_registers(uint8_t addr, uint8_t index);
void snled27351_update_pwm_buffers(uint8_t index);
void snled27351_update_led_control_registers(uint8_t index);
void snled27351_flush(void);
void snled27351_sw_return_normal(uint8_t addr);
void snled27351_sw_shutdown(uint8_t addr);
void snled27351_sw_return_normal(uint8_t index);
void snled27351_sw_shutdown(uint8_t index);
#define A_1 0x00
#define A_2 0x01
#define A_3 0x02
#define A_4 0x03
#define A_5 0x04
#define A_6 0x05
#define A_7 0x06
#define A_8 0x07
#define A_9 0x08
#define A_10 0x09
#define A_11 0x0A
#define A_12 0x0B
#define A_13 0x0C
#define A_14 0x0D
#define A_15 0x0E
#define A_16 0x0F
#define CB1_CA1 0x00
#define CB1_CA2 0x01
#define CB1_CA3 0x02
#define CB1_CA4 0x03
#define CB1_CA5 0x04
#define CB1_CA6 0x05
#define CB1_CA7 0x06
#define CB1_CA8 0x07
#define CB1_CA9 0x08
#define CB1_CA10 0x09
#define CB1_CA11 0x0A
#define CB1_CA12 0x0B
#define CB1_CA13 0x0C
#define CB1_CA14 0x0D
#define CB1_CA15 0x0E
#define CB1_CA16 0x0F
#define B_1 0x10
#define B_2 0x11
#define B_3 0x12
#define B_4 0x13
#define B_5 0x14
#define B_6 0x15
#define B_7 0x16
#define B_8 0x17
#define B_9 0x18
#define B_10 0x19
#define B_11 0x1A
#define B_12 0x1B
#define B_13 0x1C
#define B_14 0x1D
#define B_15 0x1E
#define B_16 0x1F
#define CB2_CA1 0x10
#define CB2_CA2 0x11
#define CB2_CA3 0x12
#define CB2_CA4 0x13
#define CB2_CA5 0x14
#define CB2_CA6 0x15
#define CB2_CA7 0x16
#define CB2_CA8 0x17
#define CB2_CA9 0x18
#define CB2_CA10 0x19
#define CB2_CA11 0x1A
#define CB2_CA12 0x1B
#define CB2_CA13 0x1C
#define CB2_CA14 0x1D
#define CB2_CA15 0x1E
#define CB2_CA16 0x1F
#define C_1 0x20
#define C_2 0x21
#define C_3 0x22
#define C_4 0x23
#define C_5 0x24
#define C_6 0x25
#define C_7 0x26
#define C_8 0x27
#define C_9 0x28
#define C_10 0x29
#define C_11 0x2A
#define C_12 0x2B
#define C_13 0x2C
#define C_14 0x2D
#define C_15 0x2E
#define C_16 0x2F
#define CB3_CA1 0x20
#define CB3_CA2 0x21
#define CB3_CA3 0x22
#define CB3_CA4 0x23
#define CB3_CA5 0x24
#define CB3_CA6 0x25
#define CB3_CA7 0x26
#define CB3_CA8 0x27
#define CB3_CA9 0x28
#define CB3_CA10 0x29
#define CB3_CA11 0x2A
#define CB3_CA12 0x2B
#define CB3_CA13 0x2C
#define CB3_CA14 0x2D
#define CB3_CA15 0x2E
#define CB3_CA16 0x2F
#define D_1 0x30
#define D_2 0x31
#define D_3 0x32
#define D_4 0x33
#define D_5 0x34
#define D_6 0x35
#define D_7 0x36
#define D_8 0x37
#define D_9 0x38
#define D_10 0x39
#define D_11 0x3A
#define D_12 0x3B
#define D_13 0x3C
#define D_14 0x3D
#define D_15 0x3E
#define D_16 0x3F
#define CB4_CA1 0x30
#define CB4_CA2 0x31
#define CB4_CA3 0x32
#define CB4_CA4 0x33
#define CB4_CA5 0x34
#define CB4_CA6 0x35
#define CB4_CA7 0x36
#define CB4_CA8 0x37
#define CB4_CA9 0x38
#define CB4_CA10 0x39
#define CB4_CA11 0x3A
#define CB4_CA12 0x3B
#define CB4_CA13 0x3C
#define CB4_CA14 0x3D
#define CB4_CA15 0x3E
#define CB4_CA16 0x3F
#define E_1 0x40
#define E_2 0x41
#define E_3 0x42
#define E_4 0x43
#define E_5 0x44
#define E_6 0x45
#define E_7 0x46
#define E_8 0x47
#define E_9 0x48
#define E_10 0x49
#define E_11 0x4A
#define E_12 0x4B
#define E_13 0x4C
#define E_14 0x4D
#define E_15 0x4E
#define E_16 0x4F
#define CB5_CA1 0x40
#define CB5_CA2 0x41
#define CB5_CA3 0x42
#define CB5_CA4 0x43
#define CB5_CA5 0x44
#define CB5_CA6 0x45
#define CB5_CA7 0x46
#define CB5_CA8 0x47
#define CB5_CA9 0x48
#define CB5_CA10 0x49
#define CB5_CA11 0x4A
#define CB5_CA12 0x4B
#define CB5_CA13 0x4C
#define CB5_CA14 0x4D
#define CB5_CA15 0x4E
#define CB5_CA16 0x4F
#define F_1 0x50
#define F_2 0x51
#define F_3 0x52
#define F_4 0x53
#define F_5 0x54
#define F_6 0x55
#define F_7 0x56
#define F_8 0x57
#define F_9 0x58
#define F_10 0x59
#define F_11 0x5A
#define F_12 0x5B
#define F_13 0x5C
#define F_14 0x5D
#define F_15 0x5E
#define F_16 0x5F
#define CB6_CA1 0x50
#define CB6_CA2 0x51
#define CB6_CA3 0x52
#define CB6_CA4 0x53
#define CB6_CA5 0x54
#define CB6_CA6 0x55
#define CB6_CA7 0x56
#define CB6_CA8 0x57
#define CB6_CA9 0x58
#define CB6_CA10 0x59
#define CB6_CA11 0x5A
#define CB6_CA12 0x5B
#define CB6_CA13 0x5C
#define CB6_CA14 0x5D
#define CB6_CA15 0x5E
#define CB6_CA16 0x5F
#define G_1 0x60
#define G_2 0x61
#define G_3 0x62
#define G_4 0x63
#define G_5 0x64
#define G_6 0x65
#define G_7 0x66
#define G_8 0x67
#define G_9 0x68
#define G_10 0x69
#define G_11 0x6A
#define G_12 0x6B
#define G_13 0x6C
#define G_14 0x6D
#define G_15 0x6E
#define G_16 0x6F
#define CB7_CA1 0x60
#define CB7_CA2 0x61
#define CB7_CA3 0x62
#define CB7_CA4 0x63
#define CB7_CA5 0x64
#define CB7_CA6 0x65
#define CB7_CA7 0x66
#define CB7_CA8 0x67
#define CB7_CA9 0x68
#define CB7_CA10 0x69
#define CB7_CA11 0x6A
#define CB7_CA12 0x6B
#define CB7_CA13 0x6C
#define CB7_CA14 0x6D
#define CB7_CA15 0x6E
#define CB7_CA16 0x6F
#define H_1 0x70
#define H_2 0x71
#define H_3 0x72
#define H_4 0x73
#define H_5 0x74
#define H_6 0x75
#define H_7 0x76
#define H_8 0x77
#define H_9 0x78
#define H_10 0x79
#define H_11 0x7A
#define H_12 0x7B
#define H_13 0x7C
#define H_14 0x7D
#define H_15 0x7E
#define H_16 0x7F
#define CB8_CA1 0x70
#define CB8_CA2 0x71
#define CB8_CA3 0x72
#define CB8_CA4 0x73
#define CB8_CA5 0x74
#define CB8_CA6 0x75
#define CB8_CA7 0x76
#define CB8_CA8 0x77
#define CB8_CA9 0x78
#define CB8_CA10 0x79
#define CB8_CA11 0x7A
#define CB8_CA12 0x7B
#define CB8_CA13 0x7C
#define CB8_CA14 0x7D
#define CB8_CA15 0x7E
#define CB8_CA16 0x7F
#define I_1 0x80
#define I_2 0x81
#define I_3 0x82
#define I_4 0x83
#define I_5 0x84
#define I_6 0x85
#define I_7 0x86
#define I_8 0x87
#define I_9 0x88
#define I_10 0x89
#define I_11 0x8A
#define I_12 0x8B
#define I_13 0x8C
#define I_14 0x8D
#define I_15 0x8E
#define I_16 0x8F
#define CB9_CA1 0x80
#define CB9_CA2 0x81
#define CB9_CA3 0x82
#define CB9_CA4 0x83
#define CB9_CA5 0x84
#define CB9_CA6 0x85
#define CB9_CA7 0x86
#define CB9_CA8 0x87
#define CB9_CA9 0x88
#define CB9_CA10 0x89
#define CB9_CA11 0x8A
#define CB9_CA12 0x8B
#define CB9_CA13 0x8C
#define CB9_CA14 0x8D
#define CB9_CA15 0x8E
#define CB9_CA16 0x8F
#define J_1 0x90
#define J_2 0x91
#define J_3 0x92
#define J_4 0x93
#define J_5 0x94
#define J_6 0x95
#define J_7 0x96
#define J_8 0x97
#define J_9 0x98
#define J_10 0x99
#define J_11 0x9A
#define J_12 0x9B
#define J_13 0x9C
#define J_14 0x9D
#define J_15 0x9E
#define J_16 0x9F
#define CB10_CA1 0x90
#define CB10_CA2 0x91
#define CB10_CA3 0x92
#define CB10_CA4 0x93
#define CB10_CA5 0x94
#define CB10_CA6 0x95
#define CB10_CA7 0x96
#define CB10_CA8 0x97
#define CB10_CA9 0x98
#define CB10_CA10 0x99
#define CB10_CA11 0x9A
#define CB10_CA12 0x9B
#define CB10_CA13 0x9C
#define CB10_CA14 0x9D
#define CB10_CA15 0x9E
#define CB10_CA16 0x9F
#define K_1 0xA0
#define K_2 0xA1
#define K_3 0xA2
#define K_4 0xA3
#define K_5 0xA4
#define K_6 0xA5
#define K_7 0xA6
#define K_8 0xA7
#define K_9 0xA8
#define K_10 0xA9
#define K_11 0xAA
#define K_12 0xAB
#define K_13 0xAC
#define K_14 0xAD
#define K_15 0xAE
#define K_16 0xAF
#define CB11_CA1 0xA0
#define CB11_CA2 0xA1
#define CB11_CA3 0xA2
#define CB11_CA4 0xA3
#define CB11_CA5 0xA4
#define CB11_CA6 0xA5
#define CB11_CA7 0xA6
#define CB11_CA8 0xA7
#define CB11_CA9 0xA8
#define CB11_CA10 0xA9
#define CB11_CA11 0xAA
#define CB11_CA12 0xAB
#define CB11_CA13 0xAC
#define CB11_CA14 0xAD
#define CB11_CA15 0xAE
#define CB11_CA16 0xAF
#define L_1 0xB0
#define L_2 0xB1
#define L_3 0xB2
#define L_4 0xB3
#define L_5 0xB4
#define L_6 0xB5
#define L_7 0xB6
#define L_8 0xB7
#define L_9 0xB8
#define L_10 0xB9
#define L_11 0xBA
#define L_12 0xBB
#define L_13 0xBC
#define L_14 0xBD
#define L_15 0xBE
#define L_16 0xBF
#define CB12_CA1 0xB0
#define CB12_CA2 0xB1
#define CB12_CA3 0xB2
#define CB12_CA4 0xB3
#define CB12_CA5 0xB4
#define CB12_CA6 0xB5
#define CB12_CA7 0xB6
#define CB12_CA8 0xB7
#define CB12_CA9 0xB8
#define CB12_CA10 0xB9
#define CB12_CA11 0xBA
#define CB12_CA12 0xBB
#define CB12_CA13 0xBC
#define CB12_CA14 0xBD
#define CB12_CA15 0xBE
#define CB12_CA16 0xBF
// DEPRECATED - DO NOT USE
#define A_1 CB1_CA1
#define A_2 CB1_CA2
#define A_3 CB1_CA3
#define A_4 CB1_CA4
#define A_5 CB1_CA5
#define A_6 CB1_CA6
#define A_7 CB1_CA7
#define A_8 CB1_CA8
#define A_9 CB1_CA9
#define A_10 CB1_CA10
#define A_11 CB1_CA11
#define A_12 CB1_CA12
#define A_13 CB1_CA13
#define A_14 CB1_CA14
#define A_15 CB1_CA15
#define A_16 CB1_CA16
#define B_1 CB2_CA1
#define B_2 CB2_CA2
#define B_3 CB2_CA3
#define B_4 CB2_CA4
#define B_5 CB2_CA5
#define B_6 CB2_CA6
#define B_7 CB2_CA7
#define B_8 CB2_CA8
#define B_9 CB2_CA9
#define B_10 CB2_CA10
#define B_11 CB2_CA11
#define B_12 CB2_CA12
#define B_13 CB2_CA13
#define B_14 CB2_CA14
#define B_15 CB2_CA15
#define B_16 CB2_CA16
#define C_1 CB3_CA1
#define C_2 CB3_CA2
#define C_3 CB3_CA3
#define C_4 CB3_CA4
#define C_5 CB3_CA5
#define C_6 CB3_CA6
#define C_7 CB3_CA7
#define C_8 CB3_CA8
#define C_9 CB3_CA9
#define C_10 CB3_CA10
#define C_11 CB3_CA11
#define C_12 CB3_CA12
#define C_13 CB3_CA13
#define C_14 CB3_CA14
#define C_15 CB3_CA15
#define C_16 CB3_CA16
#define D_1 CB4_CA1
#define D_2 CB4_CA2
#define D_3 CB4_CA3
#define D_4 CB4_CA4
#define D_5 CB4_CA5
#define D_6 CB4_CA6
#define D_7 CB4_CA7
#define D_8 CB4_CA8
#define D_9 CB4_CA9
#define D_10 CB4_CA10
#define D_11 CB4_CA11
#define D_12 CB4_CA12
#define D_13 CB4_CA13
#define D_14 CB4_CA14
#define D_15 CB4_CA15
#define D_16 CB4_CA16
#define E_1 CB5_CA1
#define E_2 CB5_CA2
#define E_3 CB5_CA3
#define E_4 CB5_CA4
#define E_5 CB5_CA5
#define E_6 CB5_CA6
#define E_7 CB5_CA7
#define E_8 CB5_CA8
#define E_9 CB5_CA9
#define E_10 CB5_CA10
#define E_11 CB5_CA11
#define E_12 CB5_CA12
#define E_13 CB5_CA13
#define E_14 CB5_CA14
#define E_15 CB5_CA15
#define E_16 CB5_CA16
#define F_1 CB6_CA1
#define F_2 CB6_CA2
#define F_3 CB6_CA3
#define F_4 CB6_CA4
#define F_5 CB6_CA5
#define F_6 CB6_CA6
#define F_7 CB6_CA7
#define F_8 CB6_CA8
#define F_9 CB6_CA9
#define F_10 CB6_CA10
#define F_11 CB6_CA11
#define F_12 CB6_CA12
#define F_13 CB6_CA13
#define F_14 CB6_CA14
#define F_15 CB6_CA15
#define F_16 CB6_CA16
#define G_1 CB7_CA1
#define G_2 CB7_CA2
#define G_3 CB7_CA3
#define G_4 CB7_CA4
#define G_5 CB7_CA5
#define G_6 CB7_CA6
#define G_7 CB7_CA7
#define G_8 CB7_CA8
#define G_9 CB7_CA9
#define G_10 CB7_CA10
#define G_11 CB7_CA11
#define G_12 CB7_CA12
#define G_13 CB7_CA13
#define G_14 CB7_CA14
#define G_15 CB7_CA15
#define G_16 CB7_CA16
#define H_1 CB8_CA1
#define H_2 CB8_CA2
#define H_3 CB8_CA3
#define H_4 CB8_CA4
#define H_5 CB8_CA5
#define H_6 CB8_CA6
#define H_7 CB8_CA7
#define H_8 CB8_CA8
#define H_9 CB8_CA9
#define H_10 CB8_CA10
#define H_11 CB8_CA11
#define H_12 CB8_CA12
#define H_13 CB8_CA13
#define H_14 CB8_CA14
#define H_15 CB8_CA15
#define H_16 CB8_CA16
#define I_1 CB9_CA1
#define I_2 CB9_CA2
#define I_3 CB9_CA3
#define I_4 CB9_CA4
#define I_5 CB9_CA5
#define I_6 CB9_CA6
#define I_7 CB9_CA7
#define I_8 CB9_CA8
#define I_9 CB9_CA9
#define I_10 CB9_CA10
#define I_11 CB9_CA11
#define I_12 CB9_CA12
#define I_13 CB9_CA13
#define I_14 CB9_CA14
#define I_15 CB9_CA15
#define I_16 CB9_CA16
#define J_1 CB10_CA1
#define J_2 CB10_CA2
#define J_3 CB10_CA3
#define J_4 CB10_CA4
#define J_5 CB10_CA5
#define J_6 CB10_CA6
#define J_7 CB10_CA7
#define J_8 CB10_CA8
#define J_9 CB10_CA9
#define J_10 CB10_CA10
#define J_11 CB10_CA11
#define J_12 CB10_CA12
#define J_13 CB10_CA13
#define J_14 CB10_CA14
#define J_15 CB10_CA15
#define J_16 CB10_CA16
#define K_1 CB11_CA1
#define K_2 CB11_CA2
#define K_3 CB11_CA3
#define K_4 CB11_CA4
#define K_5 CB11_CA5
#define K_6 CB11_CA6
#define K_7 CB11_CA7
#define K_8 CB11_CA8
#define K_9 CB11_CA9
#define K_10 CB11_CA10
#define K_11 CB11_CA11
#define K_12 CB11_CA12
#define K_13 CB11_CA13
#define K_14 CB11_CA14
#define K_15 CB11_CA15
#define K_16 CB11_CA16
#define L_1 CB12_CA1
#define L_2 CB12_CA2
#define L_3 CB12_CA3
#define L_4 CB12_CA4
#define L_5 CB12_CA5
#define L_6 CB12_CA6
#define L_7 CB12_CA7
#define L_8 CB12_CA8
#define L_9 CB12_CA9
#define L_10 CB12_CA10
#define L_11 CB12_CA11
#define L_12 CB12_CA12
#define L_13 CB12_CA13
#define L_14 CB12_CA14
#define L_15 CB12_CA15
#define L_16 CB12_CA16

View File

@ -192,7 +192,7 @@ __attribute__((weak)) bool oled_send_cmd(const uint8_t *data, uint16_t size) {
return false;
}
// Command Mode
writePinLow(OLED_DC_PIN);
gpio_write_pin_low(OLED_DC_PIN);
// Send the commands
if (spi_transmit(&data[1], size - 1) != SPI_STATUS_SUCCESS) {
spi_stop();
@ -215,7 +215,7 @@ __attribute__((weak)) bool oled_send_cmd_P(const uint8_t *data, uint16_t size) {
}
spi_status_t status = SPI_STATUS_SUCCESS;
// Command Mode
writePinLow(OLED_DC_PIN);
gpio_write_pin_low(OLED_DC_PIN);
// Send the commands
for (uint16_t i = 1; i < size && status >= 0; i++) {
status = spi_write(pgm_read_byte((const char *)&data[i]));
@ -239,7 +239,7 @@ __attribute__((weak)) bool oled_send_data(const uint8_t *data, uint16_t size) {
return false;
}
// Data Mode
writePinHigh(OLED_DC_PIN);
gpio_write_pin_high(OLED_DC_PIN);
// Send the commands
if (spi_transmit(data, size) != SPI_STATUS_SUCCESS) {
spi_stop();
@ -256,17 +256,17 @@ __attribute__((weak)) bool oled_send_data(const uint8_t *data, uint16_t size) {
__attribute__((weak)) void oled_driver_init(void) {
#if defined(OLED_TRANSPORT_SPI)
spi_init();
setPinOutput(OLED_CS_PIN);
writePinHigh(OLED_CS_PIN);
gpio_set_pin_output(OLED_CS_PIN);
gpio_write_pin_high(OLED_CS_PIN);
setPinOutput(OLED_DC_PIN);
writePinLow(OLED_DC_PIN);
gpio_set_pin_output(OLED_DC_PIN);
gpio_write_pin_low(OLED_DC_PIN);
# ifdef OLED_RST_PIN
/* Reset device */
setPinOutput(OLED_RST_PIN);
writePinLow(OLED_RST_PIN);
gpio_set_pin_output(OLED_RST_PIN);
gpio_write_pin_low(OLED_RST_PIN);
wait_ms(20);
writePinHigh(OLED_RST_PIN);
gpio_write_pin_high(OLED_RST_PIN);
wait_ms(20);
# endif
#elif defined(OLED_TRANSPORT_I2C)

View File

@ -17,8 +17,8 @@ bool qp_comms_spi_init(painter_device_t device) {
spi_init();
// Set up CS as output high
setPinOutput(comms_config->chip_select_pin);
writePinHigh(comms_config->chip_select_pin);
gpio_set_pin_output(comms_config->chip_select_pin);
gpio_write_pin_high(comms_config->chip_select_pin);
return true;
}
@ -49,7 +49,7 @@ void qp_comms_spi_stop(painter_device_t device) {
painter_driver_t * driver = (painter_driver_t *)device;
qp_comms_spi_config_t *comms_config = (qp_comms_spi_config_t *)driver->comms_config;
spi_stop();
writePinHigh(comms_config->chip_select_pin);
gpio_write_pin_high(comms_config->chip_select_pin);
}
const painter_comms_vtable_t spi_comms_vtable = {
@ -74,16 +74,16 @@ bool qp_comms_spi_dc_reset_init(painter_device_t device) {
// Set up D/C as output low, if specified
if (comms_config->dc_pin != NO_PIN) {
setPinOutput(comms_config->dc_pin);
writePinLow(comms_config->dc_pin);
gpio_set_pin_output(comms_config->dc_pin);
gpio_write_pin_low(comms_config->dc_pin);
}
// Set up RST as output, if specified, performing a reset in the process
if (comms_config->reset_pin != NO_PIN) {
setPinOutput(comms_config->reset_pin);
writePinLow(comms_config->reset_pin);
gpio_set_pin_output(comms_config->reset_pin);
gpio_write_pin_low(comms_config->reset_pin);
wait_ms(20);
writePinHigh(comms_config->reset_pin);
gpio_write_pin_high(comms_config->reset_pin);
wait_ms(20);
}
@ -93,14 +93,14 @@ bool qp_comms_spi_dc_reset_init(painter_device_t device) {
uint32_t qp_comms_spi_dc_reset_send_data(painter_device_t device, const void *data, uint32_t byte_count) {
painter_driver_t * driver = (painter_driver_t *)device;
qp_comms_spi_dc_reset_config_t *comms_config = (qp_comms_spi_dc_reset_config_t *)driver->comms_config;
writePinHigh(comms_config->dc_pin);
gpio_write_pin_high(comms_config->dc_pin);
return qp_comms_spi_send_data(device, data, byte_count);
}
void qp_comms_spi_dc_reset_send_command(painter_device_t device, uint8_t cmd) {
painter_driver_t * driver = (painter_driver_t *)device;
qp_comms_spi_dc_reset_config_t *comms_config = (qp_comms_spi_dc_reset_config_t *)driver->comms_config;
writePinLow(comms_config->dc_pin);
gpio_write_pin_low(comms_config->dc_pin);
spi_write(cmd);
}

View File

@ -38,6 +38,7 @@ typedef struct qp_comms_spi_dc_reset_config_t {
bool command_params_uses_command_pin; // keep D/C held low when sending command sequences for data bytes
} qp_comms_spi_dc_reset_config_t;
bool qp_comms_spi_dc_reset_init(painter_device_t device);
void qp_comms_spi_dc_reset_send_command(painter_device_t device, uint8_t cmd);
uint32_t qp_comms_spi_dc_reset_send_data(painter_device_t device, const void* data, uint32_t byte_count);
void qp_comms_spi_dc_reset_bulk_command_sequence(painter_device_t device, const uint8_t* sequence, size_t sequence_len);

View File

@ -0,0 +1,298 @@
// Copyright 2021 Nick Brassel (@tzarc)
// Copyright 2023 Pablo Martinez (@elpekenin) <elpekenin@elpekenin.dev>
// SPDX-License-Identifier: GPL-2.0-or-later
#include "qp_internal.h"
#include "qp_comms.h"
#include "qp_ili9486.h"
#include "qp_ili9xxx_opcodes.h"
#include "qp_tft_panel.h"
#ifdef QUANTUM_PAINTER_ILI9486_SPI_ENABLE
# include "spi_master.h"
# include <qp_comms_spi.h>
#endif // QUANTUM_PAINTER_ILI9486_SPI_ENABLE
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Common
// Driver storage
tft_panel_dc_reset_painter_device_t ili9486_drivers[ILI9486_NUM_DEVICES] = {0};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Initialization
bool qp_ili9486_init(painter_device_t device, painter_rotation_t rotation) {
// clang-format off
const uint8_t ili9486_init_sequence[] = {
// Command, Delay, N, Data[N]
ILI9XXX_CMD_RESET, 120, 0,
ILI9XXX_SET_PIX_FMT, 0, 1, 0x55,
ILI9XXX_SET_PGAMMA, 0, 15, 0x0F, 0x1F, 0x1C, 0x0C, 0x0F, 0x08, 0x48, 0x98, 0x37, 0x0A, 0x13, 0x04, 0x11, 0x0D, 0x00,
ILI9XXX_SET_NGAMMA, 0, 15, 0x0F, 0x32, 0x2E, 0x0B, 0x0D, 0x05, 0x47, 0x75, 0x37, 0x06, 0x10, 0x03, 0x24, 0x20, 0x00,
ILI9XXX_SET_POWER_CTL_1, 0, 2, 0x0D, 0x0D,
ILI9XXX_SET_POWER_CTL_2, 0, 2, 0x43, 0x00,
ILI9XXX_SET_POWER_CTL_3, 0, 1, 0x00,
ILI9XXX_SET_VCOM_CTL_1, 0, 4, 0x00, 0x48, 0x00, 0x48,
ILI9XXX_SET_INVERSION_CTL, 0, 1, 0x02,
};
// clang-format on
qp_comms_bulk_command_sequence(device, ili9486_init_sequence, sizeof(ili9486_init_sequence));
// Configure the rotation (i.e. the ordering and direction of memory writes in GRAM)
const uint8_t madctl[] = {
[QP_ROTATION_0] = ILI9XXX_MADCTL_BGR,
[QP_ROTATION_90] = ILI9XXX_MADCTL_BGR | ILI9XXX_MADCTL_MV,
[QP_ROTATION_180] = ILI9XXX_MADCTL_BGR,
[QP_ROTATION_270] = ILI9XXX_MADCTL_BGR | ILI9XXX_MADCTL_MV,
};
const uint8_t functl[] = {
[QP_ROTATION_0] = 0x42,
[QP_ROTATION_90] = 0x62,
[QP_ROTATION_180] = 0x22,
[QP_ROTATION_270] = 0x02,
};
// clang-format off
uint8_t rotation_sequence[] = {
// Command, Delay, N, Data[N]
ILI9XXX_SET_MEM_ACS_CTL, 0, 1, madctl[rotation],
ILI9XXX_SET_FUNCTION_CTL, 0, 2, 0x00, functl[rotation],
ILI9XXX_CMD_SLEEP_OFF, 5, 0,
ILI9XXX_CMD_DISPLAY_ON, 5, 0,
};
// clang-format on
qp_comms_bulk_command_sequence(device, rotation_sequence, sizeof(rotation_sequence));
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Driver vtable
// waveshare variant needs some tweaks due to shift registers
static void qp_comms_spi_dc_reset_send_command_odd_cs_pulse(painter_device_t device, uint8_t cmd) {
painter_driver_t * driver = (painter_driver_t *)device;
qp_comms_spi_dc_reset_config_t *comms_config = (qp_comms_spi_dc_reset_config_t *)driver->comms_config;
writePinLow(comms_config->spi_config.chip_select_pin);
qp_comms_spi_dc_reset_send_command(device, cmd);
writePinHigh(comms_config->spi_config.chip_select_pin);
}
static uint32_t qp_comms_spi_send_data_odd_cs_pulse(painter_device_t device, const void *data, uint32_t byte_count) {
painter_driver_t * driver = (painter_driver_t *)device;
qp_comms_spi_dc_reset_config_t *comms_config = (qp_comms_spi_dc_reset_config_t *)driver->comms_config;
uint32_t bytes_remaining = byte_count;
const uint8_t *p = (const uint8_t *)data;
uint32_t max_msg_length = 1024;
writePinHigh(comms_config->dc_pin);
while (bytes_remaining > 0) {
uint32_t bytes_this_loop = QP_MIN(bytes_remaining, max_msg_length);
bool odd_bytes = bytes_this_loop & 1;
// send data
writePinLow(comms_config->spi_config.chip_select_pin);
spi_transmit(p, bytes_this_loop);
p += bytes_this_loop;
// extra CS toggle, for alignment
if (odd_bytes) {
writePinHigh(comms_config->spi_config.chip_select_pin);
writePinLow(comms_config->spi_config.chip_select_pin);
}
bytes_remaining -= bytes_this_loop;
}
return byte_count - bytes_remaining;
}
static uint32_t qp_ili9486_send_data_toggling(painter_device_t device, const uint8_t *data, uint32_t byte_count) {
painter_driver_t * driver = (painter_driver_t *)device;
qp_comms_spi_dc_reset_config_t *comms_config = (qp_comms_spi_dc_reset_config_t *)driver->comms_config;
uint32_t ret;
for (uint8_t j = 0; j < byte_count; ++j) {
writePinLow(comms_config->spi_config.chip_select_pin);
ret = qp_comms_spi_dc_reset_send_data(device, &data[j], 1);
writePinHigh(comms_config->spi_config.chip_select_pin);
}
return ret;
}
static void qp_comms_spi_send_command_sequence_odd_cs_pulse(painter_device_t device, const uint8_t *sequence, size_t sequence_len) {
for (size_t i = 0; i < sequence_len;) {
uint8_t command = sequence[i];
uint8_t delay = sequence[i + 1];
uint8_t num_bytes = sequence[i + 2];
qp_comms_spi_dc_reset_send_command_odd_cs_pulse(device, command);
if (num_bytes > 0) {
qp_ili9486_send_data_toggling(device, &sequence[i + 3], num_bytes);
}
if (delay > 0) {
wait_ms(delay);
}
i += (3 + num_bytes);
}
}
static bool qp_ili9486_viewport(painter_device_t device, uint16_t left, uint16_t top, uint16_t right, uint16_t bottom) {
painter_driver_t * driver = (painter_driver_t *)device;
tft_panel_dc_reset_painter_driver_vtable_t *vtable = (tft_panel_dc_reset_painter_driver_vtable_t *)driver->driver_vtable;
// Fix up the drawing location if required
left += driver->offset_x;
right += driver->offset_x;
top += driver->offset_y;
bottom += driver->offset_y;
// Check if we need to manually swap the window coordinates based on whether or not we're in a sideways rotation
if (vtable->swap_window_coords && (driver->rotation == QP_ROTATION_90 || driver->rotation == QP_ROTATION_270)) {
uint16_t temp;
temp = left;
left = top;
top = temp;
temp = right;
right = bottom;
bottom = temp;
}
// Set up the x-window
uint8_t xbuf[4] = {left >> 8, left & 0xFF, right >> 8, right & 0xFF};
qp_comms_spi_dc_reset_send_command_odd_cs_pulse(device, vtable->opcodes.set_column_address);
qp_ili9486_send_data_toggling(device, xbuf, 4);
// Set up the y-window
uint8_t ybuf[4] = {top >> 8, top & 0xFF, bottom >> 8, bottom & 0xFF};
qp_comms_spi_dc_reset_send_command_odd_cs_pulse(device, vtable->opcodes.set_row_address);
qp_ili9486_send_data_toggling(device, ybuf, 4);
// Lock in the window
qp_comms_spi_dc_reset_send_command_odd_cs_pulse(device, vtable->opcodes.enable_writes);
return true;
}
// Regular
const tft_panel_dc_reset_painter_driver_vtable_t ili9486_driver_vtable = {
.base =
{
.init = qp_ili9486_init,
.power = qp_tft_panel_power,
.clear = qp_tft_panel_clear,
.flush = qp_tft_panel_flush,
.pixdata = qp_tft_panel_pixdata,
.viewport = qp_tft_panel_viewport,
.palette_convert = qp_tft_panel_palette_convert_rgb565_swapped,
.append_pixels = qp_tft_panel_append_pixels_rgb565,
.append_pixdata = qp_tft_panel_append_pixdata,
},
.num_window_bytes = 2,
.swap_window_coords = false,
.opcodes =
{
.display_on = ILI9XXX_CMD_DISPLAY_ON,
.display_off = ILI9XXX_CMD_DISPLAY_OFF,
.set_column_address = ILI9XXX_SET_COL_ADDR,
.set_row_address = ILI9XXX_SET_PAGE_ADDR,
.enable_writes = ILI9XXX_SET_MEM,
},
};
// Waveshare tweaks
const tft_panel_dc_reset_painter_driver_vtable_t ili9486_waveshare_driver_vtable = {
.base =
{
.init = qp_ili9486_init,
.power = qp_tft_panel_power,
.clear = qp_tft_panel_clear,
.flush = qp_tft_panel_flush,
.pixdata = qp_tft_panel_pixdata,
.viewport = qp_ili9486_viewport,
.palette_convert = qp_tft_panel_palette_convert_rgb565_swapped,
.append_pixels = qp_tft_panel_append_pixels_rgb565,
.append_pixdata = qp_tft_panel_append_pixdata,
},
.num_window_bytes = 2,
.swap_window_coords = false,
.opcodes =
{
.display_on = ILI9XXX_CMD_DISPLAY_ON,
.display_off = ILI9XXX_CMD_DISPLAY_OFF,
.set_column_address = ILI9XXX_SET_COL_ADDR,
.set_row_address = ILI9XXX_SET_PAGE_ADDR,
.enable_writes = ILI9XXX_SET_MEM,
},
};
static const painter_comms_with_command_vtable_t spi_comms_odd_cs_pulse_vtable = {
.base =
{
.comms_init = qp_comms_spi_dc_reset_init,
.comms_start = qp_comms_spi_start,
.comms_send = qp_comms_spi_send_data_odd_cs_pulse,
.comms_stop = qp_comms_spi_stop,
},
.send_command = qp_comms_spi_dc_reset_send_command_odd_cs_pulse,
.bulk_command_sequence = qp_comms_spi_send_command_sequence_odd_cs_pulse,
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// SPI
#ifdef QUANTUM_PAINTER_ILI9486_SPI_ENABLE
// Factory function for creating a handle to the ILI9486 device
painter_device_t qp_ili9486_make_spi_device(uint16_t panel_width, uint16_t panel_height, pin_t chip_select_pin, pin_t dc_pin, pin_t reset_pin, uint16_t spi_divisor, int spi_mode) {
for (uint32_t i = 0; i < ILI9486_NUM_DEVICES; ++i) {
tft_panel_dc_reset_painter_device_t *driver = &ili9486_drivers[i];
if (!driver->base.driver_vtable) {
driver->base.driver_vtable = (const painter_driver_vtable_t *)&ili9486_driver_vtable;
driver->base.comms_vtable = (const painter_comms_vtable_t *)&spi_comms_with_dc_vtable;
driver->base.native_bits_per_pixel = 16; // RGB565
driver->base.panel_width = panel_width;
driver->base.panel_height = panel_height;
driver->base.rotation = QP_ROTATION_0;
driver->base.offset_x = 0;
driver->base.offset_y = 0;
// SPI and other pin configuration
driver->base.comms_config = &driver->spi_dc_reset_config;
driver->spi_dc_reset_config.spi_config.chip_select_pin = chip_select_pin;
driver->spi_dc_reset_config.spi_config.divisor = spi_divisor;
driver->spi_dc_reset_config.spi_config.lsb_first = false;
driver->spi_dc_reset_config.spi_config.mode = spi_mode;
driver->spi_dc_reset_config.dc_pin = dc_pin;
driver->spi_dc_reset_config.reset_pin = reset_pin;
if (!qp_internal_register_device((painter_device_t)driver)) {
memset(driver, 0, sizeof(tft_panel_dc_reset_painter_device_t));
return NULL;
}
return (painter_device_t)driver;
}
}
return NULL;
}
painter_device_t qp_ili9486_make_spi_waveshare_device(uint16_t panel_width, uint16_t panel_height, pin_t chip_select_pin, pin_t dc_pin, pin_t reset_pin, uint16_t spi_divisor, int spi_mode) {
painter_device_t device = qp_ili9486_make_spi_device(panel_width, panel_height, chip_select_pin, dc_pin, reset_pin, spi_divisor, spi_mode);
if (device) {
tft_panel_dc_reset_painter_device_t *driver = (tft_panel_dc_reset_painter_device_t *)device;
driver->base.driver_vtable = (const painter_driver_vtable_t *)&ili9486_waveshare_driver_vtable;
driver->base.comms_vtable = (const painter_comms_vtable_t *)&spi_comms_odd_cs_pulse_vtable;
}
return device;
}
#endif // QUANTUM_PAINTER_ILI9486_SPI_ENABLE
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -0,0 +1,52 @@
// Copyright 2021 Nick Brassel (@tzarc)
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "gpio.h"
#include "qp_internal.h"
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Quantum Painter ILI9486 configurables (add to your keyboard's config.h)
#ifndef ILI9486_NUM_DEVICES
/**
* @def This controls the maximum number of ILI9486 devices that Quantum Painter can communicate with at any one time.
* Increasing this number allows for multiple displays to be used.
*/
# define ILI9486_NUM_DEVICES 1
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Quantum Painter ILI9486 device factories
#ifdef QUANTUM_PAINTER_ILI9486_SPI_ENABLE
/**
* Factory method for an ILI9486 SPI LCD device.
*
* @param panel_width[in] the width of the display panel
* @param panel_height[in] the height of the display panel
* @param chip_select_pin[in] the GPIO pin used for SPI chip select
* @param dc_pin[in] the GPIO pin used for D/C control
* @param reset_pin[in] the GPIO pin used for RST
* @param spi_divisor[in] the SPI divisor to use when communicating with the display
* @param spi_mode[in] the SPI mode to use when communicating with the display
* @return the device handle used with all drawing routines in Quantum Painter
*/
painter_device_t qp_ili9486_make_spi_device(uint16_t panel_width, uint16_t panel_height, pin_t chip_select_pin, pin_t dc_pin, pin_t reset_pin, uint16_t spi_divisor, int spi_mode);
/**
* Factory method for an ILI9486 SPI LCD device.
*
* @param panel_width[in] the width of the display panel
* @param panel_height[in] the height of the display panel
* @param chip_select_pin[in] the GPIO pin used for SPI chip select
* @param dc_pin[in] the GPIO pin used for D/C control
* @param reset_pin[in] the GPIO pin used for RST
* @param spi_divisor[in] the SPI divisor to use when communicating with the display
* @param spi_mode[in] the SPI mode to use when communicating with the display
* @return the device handle used with all drawing routines in Quantum Painter
*/
painter_device_t qp_ili9486_make_spi_waveshare_device(uint16_t panel_width, uint16_t panel_height, pin_t chip_select_pin, pin_t dc_pin, pin_t reset_pin, uint16_t spi_divisor, int spi_mode);
#endif // QUANTUM_PAINTER_ILI9486_SPI_ENABLE

View File

@ -70,6 +70,7 @@
#define ILI9XXX_SET_LIGHT_CTL_8 0xBF // Set backlight ctl 8
#define ILI9XXX_SET_POWER_CTL_1 0xC0 // Set power ctl 1
#define ILI9XXX_SET_POWER_CTL_2 0xC1 // Set power ctl 2
#define ILI9XXX_SET_POWER_CTL_3 0xC2 // Set power ctl 3
#define ILI9XXX_SET_VCOM_CTL_1 0xC5 // Set VCOM ctl 1
#define ILI9XXX_SET_VCOM_CTL_2 0xC7 // Set VCOM ctl 2
#define ILI9XXX_POWER_CTL_A 0xCB // Set power control A

View File

@ -47,9 +47,9 @@
void adns5050_init(void) {
// Initialize the ADNS serial pins.
setPinOutput(ADNS5050_SCLK_PIN);
setPinOutput(ADNS5050_SDIO_PIN);
setPinOutput(ADNS5050_CS_PIN);
gpio_set_pin_output(ADNS5050_SCLK_PIN);
gpio_set_pin_output(ADNS5050_SDIO_PIN);
gpio_set_pin_output(ADNS5050_CS_PIN);
// reboot the adns.
// if the adns hasn't initialized yet, this is harmless.
@ -69,30 +69,30 @@ void adns5050_init(void) {
// Just as with the serial protocol, this is used by the slave to send a
// synchronization signal to the master.
void adns5050_sync(void) {
writePinLow(ADNS5050_CS_PIN);
gpio_write_pin_low(ADNS5050_CS_PIN);
wait_us(1);
writePinHigh(ADNS5050_CS_PIN);
gpio_write_pin_high(ADNS5050_CS_PIN);
}
void adns5050_cs_select(void) {
writePinLow(ADNS5050_CS_PIN);
gpio_write_pin_low(ADNS5050_CS_PIN);
}
void adns5050_cs_deselect(void) {
writePinHigh(ADNS5050_CS_PIN);
gpio_write_pin_high(ADNS5050_CS_PIN);
}
uint8_t adns5050_serial_read(void) {
setPinInput(ADNS5050_SDIO_PIN);
gpio_set_pin_input(ADNS5050_SDIO_PIN);
uint8_t byte = 0;
for (uint8_t i = 0; i < 8; ++i) {
writePinLow(ADNS5050_SCLK_PIN);
gpio_write_pin_low(ADNS5050_SCLK_PIN);
wait_us(1);
byte = (byte << 1) | readPin(ADNS5050_SDIO_PIN);
byte = (byte << 1) | gpio_read_pin(ADNS5050_SDIO_PIN);
writePinHigh(ADNS5050_SCLK_PIN);
gpio_write_pin_high(ADNS5050_SCLK_PIN);
wait_us(1);
}
@ -100,19 +100,19 @@ uint8_t adns5050_serial_read(void) {
}
void adns5050_serial_write(uint8_t data) {
setPinOutput(ADNS5050_SDIO_PIN);
gpio_set_pin_output(ADNS5050_SDIO_PIN);
for (int8_t b = 7; b >= 0; b--) {
writePinLow(ADNS5050_SCLK_PIN);
gpio_write_pin_low(ADNS5050_SCLK_PIN);
if (data & (1 << b))
writePinHigh(ADNS5050_SDIO_PIN);
gpio_write_pin_high(ADNS5050_SDIO_PIN);
else
writePinLow(ADNS5050_SDIO_PIN);
gpio_write_pin_low(ADNS5050_SDIO_PIN);
wait_us(2);
writePinHigh(ADNS5050_SCLK_PIN);
gpio_write_pin_high(ADNS5050_SCLK_PIN);
}
// tSWR. See page 15 of the ADNS spec sheet.

View File

@ -100,7 +100,7 @@ uint8_t adns9800_read(uint8_t reg_addr) {
}
void adns9800_init(void) {
setPinOutput(ADNS9800_CS_PIN);
gpio_set_pin_output(ADNS9800_CS_PIN);
spi_init();

View File

@ -122,14 +122,17 @@ report_analog_joystick_t analog_joystick_read(void) {
report.y = axisToMouseComponent(ANALOG_JOYSTICK_Y_AXIS_PIN, yOrigin, maxCursorSpeed, 1);
}
#ifdef ANALOG_JOYSTICK_CLICK_PIN
report.button = !readPin(ANALOG_JOYSTICK_CLICK_PIN);
report.button = !gpio_read_pin(ANALOG_JOYSTICK_CLICK_PIN);
#endif
return report;
}
void analog_joystick_init(void) {
gpio_set_pin_input_high(ANALOG_JOYSTICK_X_AXIS_PIN);
gpio_set_pin_input_high(ANALOG_JOYSTICK_Y_AXIS_PIN);
#ifdef ANALOG_JOYSTICK_CLICK_PIN
setPinInputHigh(ANALOG_JOYSTICK_CLICK_PIN);
gpio_set_pin_input_high(ANALOG_JOYSTICK_CLICK_PIN);
#endif
// Account for drift
xOrigin = analogReadPin(ANALOG_JOYSTICK_X_AXIS_PIN);

View File

@ -51,8 +51,8 @@ uint8_t paw3204_read_reg(uint8_t reg_addr);
void paw3204_write_reg(uint8_t reg_addr, uint8_t data);
void paw3204_init(void) {
setPinOutput(PAW3204_SCLK_PIN); // setclockpin to output
setPinInputHigh(PAW3204_SDIO_PIN); // set datapin input high
gpio_set_pin_output(PAW3204_SCLK_PIN); // setclockpin to output
gpio_set_pin_input_high(PAW3204_SDIO_PIN); // set datapin input high
paw3204_write_reg(REG_SETUP, 0x86); // reset sensor and set 1600cpi
wait_us(5);
@ -64,16 +64,16 @@ void paw3204_init(void) {
}
uint8_t paw3204_serial_read(void) {
setPinInput(PAW3204_SDIO_PIN);
gpio_set_pin_input(PAW3204_SDIO_PIN);
uint8_t byte = 0;
for (uint8_t i = 0; i < 8; ++i) {
writePinLow(PAW3204_SCLK_PIN);
gpio_write_pin_low(PAW3204_SCLK_PIN);
wait_us(1);
byte = (byte << 1) | readPin(PAW3204_SDIO_PIN);
byte = (byte << 1) | gpio_read_pin(PAW3204_SDIO_PIN);
writePinHigh(PAW3204_SCLK_PIN);
gpio_write_pin_high(PAW3204_SCLK_PIN);
wait_us(1);
}
@ -81,17 +81,17 @@ uint8_t paw3204_serial_read(void) {
}
void paw3204_serial_write(uint8_t data) {
writePinLow(PAW3204_SDIO_PIN);
setPinOutput(PAW3204_SDIO_PIN);
gpio_write_pin_low(PAW3204_SDIO_PIN);
gpio_set_pin_output(PAW3204_SDIO_PIN);
for (int8_t b = 7; b >= 0; b--) {
writePinLow(PAW3204_SCLK_PIN);
gpio_write_pin_low(PAW3204_SCLK_PIN);
if (data & (1 << b)) {
writePinHigh(PAW3204_SDIO_PIN);
gpio_write_pin_high(PAW3204_SDIO_PIN);
} else {
writePinLow(PAW3204_SDIO_PIN);
gpio_write_pin_low(PAW3204_SDIO_PIN);
}
writePinHigh(PAW3204_SCLK_PIN);
gpio_write_pin_high(PAW3204_SCLK_PIN);
}
wait_us(4);

View File

@ -24,9 +24,9 @@
void pmw3320_init(void) {
// Initialize sensor serial pins.
setPinOutput(PMW3320_SCLK_PIN);
setPinOutput(PMW3320_SDIO_PIN);
setPinOutput(PMW3320_CS_PIN);
gpio_set_pin_output(PMW3320_SCLK_PIN);
gpio_set_pin_output(PMW3320_SDIO_PIN);
gpio_set_pin_output(PMW3320_CS_PIN);
// reboot the sensor.
pmw3320_write_reg(REG_Power_Up_Reset, 0x5a);
@ -54,30 +54,30 @@ void pmw3320_init(void) {
// Just as with the serial protocol, this is used by the slave to send a
// synchronization signal to the master.
void pmw3320_sync(void) {
writePinLow(PMW3320_CS_PIN);
gpio_write_pin_low(PMW3320_CS_PIN);
wait_us(1);
writePinHigh(PMW3320_CS_PIN);
gpio_write_pin_high(PMW3320_CS_PIN);
}
void pmw3320_cs_select(void) {
writePinLow(PMW3320_CS_PIN);
gpio_write_pin_low(PMW3320_CS_PIN);
}
void pmw3320_cs_deselect(void) {
writePinHigh(PMW3320_CS_PIN);
gpio_write_pin_high(PMW3320_CS_PIN);
}
uint8_t pmw3320_serial_read(void) {
setPinInput(PMW3320_SDIO_PIN);
gpio_set_pin_input(PMW3320_SDIO_PIN);
uint8_t byte = 0;
for (uint8_t i = 0; i < 8; ++i) {
writePinLow(PMW3320_SCLK_PIN);
gpio_write_pin_low(PMW3320_SCLK_PIN);
wait_us(1);
byte = (byte << 1) | readPin(PMW3320_SDIO_PIN);
byte = (byte << 1) | gpio_read_pin(PMW3320_SDIO_PIN);
writePinHigh(PMW3320_SCLK_PIN);
gpio_write_pin_high(PMW3320_SCLK_PIN);
wait_us(1);
}
@ -85,19 +85,19 @@ uint8_t pmw3320_serial_read(void) {
}
void pmw3320_serial_write(uint8_t data) {
setPinOutput(PMW3320_SDIO_PIN);
gpio_set_pin_output(PMW3320_SDIO_PIN);
for (int8_t b = 7; b >= 0; b--) {
writePinLow(PMW3320_SCLK_PIN);
gpio_write_pin_low(PMW3320_SCLK_PIN);
if (data & (1 << b))
writePinHigh(PMW3320_SDIO_PIN);
gpio_write_pin_high(PMW3320_SDIO_PIN);
else
writePinLow(PMW3320_SDIO_PIN);
gpio_write_pin_low(PMW3320_SDIO_PIN);
wait_us(2);
writePinHigh(PMW3320_SCLK_PIN);
gpio_write_pin_high(PMW3320_SCLK_PIN);
}
// This was taken from ADNS5050 driver.

View File

@ -15,6 +15,9 @@ uint16_t pmw33xx_get_cpi(uint8_t sensor) {
}
uint8_t cpival = pmw33xx_read(sensor, REG_Config1);
// In some cases (100, 900, 1700, 2500), reading the CPI corrupts the firmware and the sensor stops responding.
// To avoid this, we write the value back to the sensor, which seems to prevent the corruption.
pmw33xx_write(sensor, REG_Config1, cpival);
return (uint16_t)((cpival + 1) & 0xFF) * PMW33XX_CPI_STEP;
}

View File

@ -346,10 +346,10 @@ static void USB2422_write_block(void) {
void USB2422_init(void) {
#ifdef USB2422_RESET_PIN
setPinOutput(USB2422_RESET_PIN);
gpio_set_pin_output(USB2422_RESET_PIN);
#endif
#ifdef USB2422_ACTIVE_PIN
setPinInput(USB2422_ACTIVE_PIN);
gpio_set_pin_input(USB2422_ACTIVE_PIN);
#endif
i2c_init(); // IC2 clk must be high at USB2422 reset release time to signal SMB configuration
@ -387,15 +387,15 @@ void USB2422_configure(void) {
void USB2422_reset(void) {
#ifdef USB2422_RESET_PIN
writePinLow(USB2422_RESET_PIN);
gpio_write_pin_low(USB2422_RESET_PIN);
wait_us(2);
writePinHigh(USB2422_RESET_PIN);
gpio_write_pin_high(USB2422_RESET_PIN);
#endif
}
bool USB2422_active(void) {
#ifdef USB2422_ACTIVE_PIN
return readPin(USB2422_ACTIVE_PIN);
return gpio_read_pin(USB2422_ACTIVE_PIN);
#else
return 1;
#endif

View File

@ -16,15 +16,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
// clang-format off
/* default setup after eeprom reset */
#define RGBLIGHT_DEFAULT_MODE RGBLIGHT_EFFECT_BREATHING + 2
#define RGBLIGHT_DEFAULT_HUE 152
#define RGBLIGHT_DEFAULT_SAT 232
#define RGBLIGHT_DEFAULT_VAR 255
#define RGBLIGHT_DEFAULT_SPD 2
// clang-format on
/* default setup after eeprom reset */
#define RGBLIGHT_DEFAULT_MODE RGBLIGHT_EFFECT_BREATHING + 2
/* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */
#define LOCKING_SUPPORT_ENABLE

View File

@ -40,6 +40,11 @@
"rgb_test": true,
"alternating": true,
"twinkle": true
},
"default": {
"hue": 152,
"sat": 232,
"speed": 2
}
},
"ws2812": {

View File

@ -38,7 +38,6 @@
"rgb_matrix": {
"animations": {
"breathing": true,
"solid_color": true,
"band_sat": true,
"band_spiral_val": true,
"cycle_all": true,

Some files were not shown because too many files have changed in this diff Show More