Segment Painting Feature
Project Overview: This module is applicable to Wi-Fi + BLE protocol RGBIC string light devices, supporting segment selection and independent control of LED beads for precise lighting effect customization.
🖼️ Preview:
The segment painting feature allows users to select specific LED bead segments through a visual interface and independently control the color of selected segments. This feature supports both color light and white light modes, enabling full segment control, single segment control, and segment painting operations. It is an important tool for users to personalize lighting effects.
Feature List
- LED Bead Visualization: Displays all LED beads in grid format, supports click selection
- Select All/Deselect All: One-click selection or deselection of all LED beads
- Segment Selection: Supports clicking single or multiple LED beads for selection
- Segment Painting Control: Independent color control for selected LED bead segments
- Full Segment Painting Control: Unified color control for all LED beads
- Real-time Preview: Real-time update of light strip color display during slider adjustment
- Data Synchronization: LED bead color data syncs to cloud storage and device
Module Implementation Details
Implementation Overview
The segment painting feature uses Redux to manage selection state and sends data through the paint_colour_1 DP point. Core implementation approach:
- State Management: Uses Redux's
commonSliceto manage selected LED bead list (checkedIdList) and select all state (stripCheckAll) - Data Flow: User selection → Update Redux state → Color adjustment → Build DP data → Send to device
- Painting Modes: Supports paint bucket mode (full segment) and single point painting mode (segment)
- Data Persistence: LED bead color data saved to cloud storage, supports cross-session recovery
Core Components
src/components/light-strip/index.tsx: Light strip visualization component, responsible for LED bead display and selection interactionsrc/components/light/index.tsx: Main light control component, integrates segment painting and color control logicsrc/redux/modules/commonSlice.ts: Redux state management, stores selected LED bead list and select all statesrc/hooks/useDrawToolDataList.ts: Painting tool data management Hook, handles LED bead data updatessrc/devices/protocols/paintColour1.ts:paint_colour_1DP point encoder/decodersrc/hooks/useStorageData.ts: Cloud storage data management Hook
Component Responsibilities:
- LightStrip Component: Responsible for LED bead visualization rendering and click interaction, maintains UI feedback for selection state
- Light Component: Integrates color control and segment painting logic, handles color changes and DP sending
- commonSlice: Centrally manages selection state, provides state update methods
- useDrawToolDataList Hook: Encapsulates LED bead data update logic, handles both full segment and segment modes
Data Flow
sequenceDiagram
participant User as User
participant LightStrip as Light Strip Component
participant Redux as Redux Store
participant Light as Light Component
participant Hook as Data Hook
participant Protocol as Protocol Parser
participant Device as Device
participant Cloud as Cloud Storage
User->>LightStrip: Click to select LED bead
LightStrip->>Redux: Update selected list
Redux-->>LightStrip: Update UI state
User->>Light: Adjust color
Light->>Hook: Call update function
Hook->>Hook: Build LED bead data array
Hook->>Protocol: Encode DP data
Protocol->>Device: Send paint_colour_1
Device-->>User: Display effect
Hook->>Cloud: Save LED bead color dataMermaid Flowcharts
Main Feature Flow:
flowchart TD
A[Enter light control page] --> B[Load LED bead data]
B --> C[Display light strip visualization]
C --> D{User action}
D -->|Click LED bead| E[Update selection state]
D -->|Click select all| F[Toggle select all state]
D -->|Adjust color| G[Get selected LED beads]
E --> H[Update Redux state]
F --> H
G --> I{Is all selected}
I -->|Yes| J[Full segment painting mode]
I -->|No| K[Segment painting mode]
J --> L[Build full segment DP data]
K --> M[Build segment DP data]
L --> N[Encode DP data]
M --> N
N --> O[Send to device]
O --> P[Update cloud storage]
P --> Q[Real-time preview effect]Data Flow Diagram:
sequenceDiagram
participant User as User Action
participant UI as UI Component
participant State as Redux State
participant Logic as Business Logic
participant DP as DP Protocol
participant Device as Device
participant Storage as Cloud Storage
User->>UI: Select LED bead
UI->>State: dispatch(updateCheckedIdList)
State-->>UI: Update selection state
User->>UI: Adjust color
UI->>Logic: Get selected LED bead list
Logic->>Logic: Build color data array
Logic->>DP: Encode paint_colour_1 data
DP->>Device: Send DP command
Device-->>User: Display lighting effect
Logic->>Storage: Save LED bead color dataComponent Interaction Diagram:
graph TB
A[Light Component] -->|Use| B[LightStrip Component]
A -->|Use| C[ColorLight Component]
A -->|Use| D[WhiteLight Component]
B -->|dispatch| E[Redux Store]
C -->|Call| F[useDrawToolDataList]
D -->|Call| F
F -->|Use| G[useStorageData]
F -->|Encode| H[paintColour1 Protocol]
H -->|Send| I[Device]
G -->|Save| J[Cloud Storage]State Transition Diagram:
stateDiagram-v2
[*] --> InitialState: Page load
InitialState --> SelectLED: User click
SelectLED --> AdjustColor: Selection complete
AdjustColor --> SliderAdjust: Start sliding
SliderAdjust --> RealtimePreview: Color change
RealtimePreview --> ReleaseConfirm: Stop sliding
ReleaseConfirm --> SendDP: Build data
SendDP --> UpdateStorage: Success callback
UpdateStorage --> SelectLED: Continue operation
UpdateStorage --> [*]: CompleteKey Code Snippets
LED Bead Selection Handling:
Select All/Deselect All:
Segment Painting Data Update:
Color Light Segment Control:
Data Structures
Selection State Data Structure (Redux):
paint_colour_1 DP Data Structure:
LED Bead Color Data Array:
Painting Mode Enum:
API Calls
DP Send Interface:
- DP Point:
paint_colour_1 - Call Method:
structuredActions.paint_colour_1.set(data, options) - Data Format: Hexadecimal string encoded by
SmearFormater - Call Timing:
- Full segment control: User adjusts color when select all state is active
- Segment control: User adjusts color when there are selected LED beads
- Slider adjustment: Only send on release, only update UI during sliding
Cloud Storage Interface:
- Storage Key:
lightStripDataKey(constant definition) - Call Method:
useSetStorageData(lightStripDataKey) - Data Format: RGB color string array
- Call Timing: After DP send success, update cloud storage data
Notes
Implementation Challenges
-
Selection State Synchronization:
- Challenge: Need to synchronize selection state across multiple components, ensure UI and data consistency
- Solution: Use Redux for centralized state management, implement state sharing and updates through
useSelectoranddispatch
-
Real-time Preview Performance:
- Challenge: Need to update large number of LED bead colors in real-time during slider adjustment, may cause performance issues
- Solution: Use throttle to limit update frequency, only update UI during sliding, send DP on release
-
Data Alignment:
- Challenge: LED bead count may change, need to ensure data array length matches device LED bead count
- Solution: Check data length in
useCutDrawDiySceneSet, pad with default colors when insufficient, truncate when exceeded
-
Select All State Determination:
- Challenge: Need to accurately determine whether all LED beads are selected
- Solution: Compare selected set size with total LED bead count, use
useEffectto monitor changes and update select all state
Common Issues
-
Issue: Color adjustment has no effect after selecting LED beads
- Cause: No LED beads selected and select all mode not enabled
- Solution: Check
checkedSet.sizeandstripCheckAllstate, prompt user to select LED beads first
-
Issue: Color update delay during slider adjustment
- Cause: Throttle time set too long or DP sending too frequent
- Solution: Adjust throttle time (recommended 300ms), only update UI during sliding, send DP on release
-
Issue: Data mismatch after device LED bead count changes
- Cause: Cloud storage data length doesn't match device LED bead count
- Solution: Automatically align data during loading, pad or truncate data array
-
Issue: Select all state display incorrect
- Cause: Select all state not synchronized in time after selection state update
- Solution: Use
useEffectto monitor selected list changes, automatically update select all state
Performance Optimization
-
Throttle Processing:
- Color adjustment uses throttle to avoid frequent updates
- Use throttle to update UI during sliding, reduce render count
-
Data Caching:
- Use
useRefto cache previous LED bead data, avoid repeated updates - Use
useMemoto cache calculation results, reduce repeated calculations
- Use
-
Conditional Rendering:
- Dynamically adjust rendering hierarchy based on LED bead count, reduce unnecessary DOM nodes
- Use
useMemoto cache LED bead element list
-
State Update Optimization:
- Batch update states, reduce Redux dispatch count
- Use
useCallbackto cache event handler functions
Edge Cases
-
LED Bead Count is 0:
- Handling: Component returns empty view, no rendering
- Code:
if (!lightNum) return <View />;
-
No LED Beads Selected:
- Handling: Prompt user to select LED beads first, don't execute color update
- Code: Check
checkedSet.size === 0 && !isSectionAll
-
Device LED Bead Count Changes:
- Handling: Automatically align data length, pad or truncate
- Code: Handle in
useCutDrawDiySceneSet
-
DP Send Failed:
- Handling: Show error prompt, don't update cloud storage data
- Code: Handle error in
failcallback
-
Multiple Operations Simultaneously:
- Handling: Use
isPutDpIngRegflag to prevent repeated sending - Code: Set timeout recovery mechanism (1 second)
- Handling: Use