## Advanced - Biorhythm

### Function Introduction
- Biorhythm: Bring the concept of natural light indoors through the device: Set a day to N moments (1≤N≤8), and set the corresponding brightness and color temperature. The device simulates the sun and automatically changes gradually according to these N moments in a day.
- Tuya Human Lighting (Advanced Function): You can choose to locate the destination with precise latitude and longitude. After clicking Save Address, the longitude and latitude biorhythm function is turned on. After the function is turned on, the smart lighting device will automatically adjust its color temperature and brightness according to the time change to simulate the natural lighting conditions in the location area. The natural light environment can not only help balance the visual perception of the human eye, but also regulate hormone levels and biological clocks, thereby positively affecting the psychological and emotional health of the human body.
**Currently only a single device has this function, and the group is blocked. The number of channels only supports two and five channels**
#### Biorhythm default mode
- The default mode displays 4 nodes, WIFI protocol: 6 nodes are actually sent by dp, and sigMesh protocol: 4 nodes are sent.
- The color and time of the sent data are not adjustable. Click the color wheel to gray it out. The default is the display function.
<Image src='/images/extended/lamp-rhythm-normal.png' style={{ width: '300px'}} />

#### Biorhythm Natural Mode
- The natural mode displays 3 nodes. When using the WIFI protocol: 5 nodes are actually sent. When using the sigMesh protocol: 3 nodes are sent.
When entering [Natural Mode] for the first time, the default value is the same as [Default Mode]. There is a prompt above the color ring to select a city. Click to jump to the map page to select longitude and latitude. If the positioning function is not turned on on the phone, you need to be reminded to turn on the positioning function.
When entering the map, if the GPS is not turned on, the default location is Beijing; if the GPS is turned on, the default location is the longitude and latitude of the phone's location.
As long as the longitude and latitude have been selected, regardless of whether it is saved, the longitude and latitude selected last time will be remembered when entering [Natural Mode] next time.
After selecting the longitude and latitude, the interface will obtain the three time nodes of sunrise, noon, and sunset according to the longitude and latitude, and supplement the remaining two hidden nodes based on these three nodes and send them down.
<Image src='/images/extended/lamp-rhythm-natural.png' style={{ width: '300px'}} />

#### Biorhythm Custom Mode
- The custom mode displays 4 nodes. When using the WIFI protocol: 6 nodes are actually sent down by dp, and 4 nodes are sent down by the sigMesh protocol.
- Click the color circle below the time to customize the time and color temperature.
<Image src='/images/extended/lamp-rhythm-custom.png' style={{ width: '300px'}} />

### DP protocol

#### Default data:
```html
0. Hidden nodes:
hour: 6,
minute: 0,
hue: 0,
saturation: 0,
value: 0,
brightness: 0,
temperature: 100,
1. Wake-up/sunrise
hour: 6,
minute: 30,
hue: 0,
saturation: 0,
value: 0,
brightness: 100,
temperature: 50,
2. Noon
hour: 11,
minute: 30,
hue: 0,
saturation: 0,
value: 0,
brightness: 100,
temperature: 100,
3. Sunset
hour: 17,
minute: 0,
hue: 0,
saturation: 0,
value: 0,
brightness: 70,
temperature: 50,
4. Hidden nodes
hour: 20,
minute: 30,
hue: 0,
saturation: 0,
value: 0,
brightness: 70,
temperature: 50,
5. Fall asleep
hour: 21,
minute: 0,
hue: 0,
saturation: 0,
value: 0,
brightness: 0,
temperature: 0,
Node 0 is half an hour earlier than Node 1 (get up)
Node 4 is half an hour earlier than Node 5 (fall asleep)
When the protocol is Wi-Fi, the hidden node needs to be sent, otherwise it is not used.
```

#### Specific protocol
- Version 0x00

|Version number |Task switch |Mode selection|Date setting|Number of nodes|Node 1| …….|Node N|
| ----|---- | ------- |---|---- |---|---- |---|
|1byte:0x00| 1byte| 1byte| 1byte| 1byte| 9byte| |9byte|

- Version 0x01

|Version number |Task switch| Mode selection|Date setting |Parameter mode |Number of nodes |Node 1| ……. |Node N|
| ----|---- | ------- |---|---- |---|---- |---|----|
|1byte:0x01 |1byte |1byte |1byte |1byte: 0x01: Only BT 0x02: Only HSV 0x03: HSVBT |1byte| 5-9byte 5byte/7byte/9byte | 5 - 9byte|

- Field description

|Field||Serial number |Length (byte)| Data representation|Description|
| ----|---|---- | ------- |---|---- |
|Version number ||1 |1| 0x00: Initial version|
|Task switch|| 2| 1| 0x00: Task closed 0x01: Task open|
|Mode selection ||3| 1| 0x00: Gradual change between two nodes 0x0F: Gradual change starts after reaching the node time, and the gradient completion time is the value of this field. The minimum allowed value is 15. For example: 0x0F means that the gradient is completed 15 minutes after the node time arrives|
|Date setting||4| 1 |Analyze the specific byte position, take effect on a certain day, the corresponding position is 1, see the fifth description below|
|Number of nodes||5| 1| 0x01: 1 node. The allowed range is 1~8|
|Node parameter|Node switch| |1| The node switch priority is lower than the task switch. The node switch status is determined only when the task switch is turned on. 0x00: Node disabled, not effective 0x01: Node enabled, effective|
|| Hour| |1| 0x10: hex code, 16 points, the allowed range is 0~23. |
|| Minute| |1| 0x10: hex code, 16 minutes, the allowed range is 0~59|
|| Hue| |2| 0x03, 0x10: hex code. Big endian format, the first byte represents hundreds, the second byte represents tens, and the units. The actual value represents 316. The allowed range is 0~360. |
|| Saturation percentage| | 1| 0x10: hex code, 16%; range allowed value 0~100|
|| Brightness percentage| |1| 0x10: hex code, 16%, range allowed value 1~100|
|| Brightness percentage| |1| 0x10: hex code, 16%, range allowed value 1~100|
|| Color temperature percentage| |1| 0x10: hex code, 16%. Range allowed value 0~100|

- The current biorhythm cannot select a date, the default is all 1
- If all 0, it means single mode, which only takes effect once; otherwise, it is a cyclic mode, judging whether the corresponding bit is set to 1, and position 1 means it takes effect on the same day. For example, 0x42 means that the task takes effect on Saturday and Monday (note that the corresponding task switch must be turned on)

|Reserved| Saturday| Friday| Thursday| Wednesday| Tuesday| Monday| Sunday|
| ----|---- | ------- |---|---- |---|---- |---|
|bit7 |bit6 |bit5 |bit4 |bit3 |bit2 |bit1 |bit0|

#### Biorhythm interface
- [Interface details](/en/miniapp/develop/ray/api/light/light-rhythms)

### How to develop
- Monitor, report and send DP according to UI interaction
  - [Specific monitoring and sending examples](/en/miniapp/solution-panel/ability#device-function-point-management)

- Component reference example
```ts
import RhythmCircle from '@ray-js/lamp-rhythm-circle';

export default function Home() {
  // Each time node 8> Number of nodes> 2
  const [rhythmNode, setRhythmNode] = useState([
    {
      activeColor: '#CE8040', // Gradient color corresponding to the node
      icon: res.icon1_colour, // Icon displayed corresponding to the node
      time: 390, // Time data corresponding to the node in minutes
      valid: true, // Whether the node can be dragged
    },
    {
      activeColor: '#CEECFE',
      icon: res.icon2_colour,
      time: 690,
      valid: true,
    },
    {
      activeColor: '#B3ABA8',
      icon: res.icon3_colour,
      time: 1020,
      valid: true,
    },
    {
      activeColor: '#1E272C',
      icon: res.icon4_colour,
      time: 1260,
      valid: true,
    },
  ]);

  const handleChange = v => {
    // When sliding, all current node data and the currently dragged node index will be returned
    const { value, activeIndex } = v;
    setRhythmNode(value);
  };

  const handleRelease = v => {};
  return (
    <View className={styles.view}>
      <DemoBlock title="Basic Usage">
        <Text className={styles.demoBlockTitleText}>Biological Rhythm</Text>
        // For detailed documents, see https://developer.tuya.com/material/library_oHEKLjj0/component?code=ComponentsTyLamp&subCode=LampRhythmCircle
        <RhythmsCircle
          // Outer circle radius
          innerRadius={107.5}
          // inner circle background
          radius={147.5}
          // minimum time interval of nodes
          timeOffset={30}
          // node data
          data={rhythmNode}
          // event triggered by move
          onChange={handleChange}
          // event triggered by end release
          onRelease={handleRelease}
        />
      </DemoBlock>
    </View>
  );
}
```

### Notes
- If Wi-Fi and zigbee devices are turned on when biorhythm is enabled, biorhythm needs to be downloaded again to make it effective

### Related materials
- [Function page]: 
  - None
- [Component]:
  1. [Biorhythm color ring](/en/miniapp/solution-ai/ability/lamp-solution/lamp/ability-set/component#general-biorhythm-color-circle)
- [Tutorial]: 
  - None
- [SDK]: 
  - None
- [API]:
  1. [Biorhythm interface capabilities](/en/miniapp/develop/ray/api/light/light-rhythms)