You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.3 KiB

  1. package nanoleaf
  2. import (
  3. "encoding/binary"
  4. "time"
  5. )
  6. type panel struct {
  7. ID uint16
  8. ColorRGBA [4]byte
  9. TransitionAt time.Time
  10. O int
  11. X int
  12. Y int
  13. ShapeType int
  14. Stale bool
  15. SlowUpdates int
  16. TicksUntilSlowUpdate int
  17. }
  18. func (p *panel) message() (message [8]byte) {
  19. transitionTime := p.TransitionAt.Sub(time.Now()).Round(time.Millisecond * 100)
  20. if transitionTime > maxTransitionTime {
  21. transitionTime = maxTransitionTime
  22. } else if transitionTime < 0 {
  23. transitionTime = 0
  24. }
  25. binary.BigEndian.PutUint16(message[0:], p.ID)
  26. copy(message[2:], p.ColorRGBA[:])
  27. binary.BigEndian.PutUint16(message[6:], uint16(transitionTime/(time.Millisecond*100)))
  28. return
  29. }
  30. func (p *panel) update(colorRGBA [4]byte, transitionAt time.Time) {
  31. if p.ColorRGBA != colorRGBA {
  32. p.ColorRGBA = colorRGBA
  33. p.Stale = true
  34. p.SlowUpdates = 3
  35. p.TicksUntilSlowUpdate = 10
  36. }
  37. p.TransitionAt = transitionAt
  38. }
  39. type panelUpdate []byte
  40. func (u *panelUpdate) Add(message [8]byte) {
  41. if len(*u) < 2 {
  42. *u = make([]byte, 2, 10)
  43. }
  44. binary.BigEndian.PutUint16(*u, binary.BigEndian.Uint16(*u)+1)
  45. *u = append(*u, message[:]...)
  46. }
  47. func (u *panelUpdate) Len() int {
  48. if len(*u) < 2 {
  49. return 0
  50. }
  51. return int(binary.BigEndian.Uint16(*u))
  52. }
  53. const maxTransitionTime = time.Minute * 109