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.

74 lines
1.7 KiB

2 years ago
2 years ago
2 years ago
  1. package device
  2. import (
  3. "git.aiterp.net/lucifer3/server/internal/gentools"
  4. "math/bits"
  5. )
  6. type SupportFlags uint32
  7. const (
  8. SFlagPower SupportFlags = 1 << iota
  9. SFlagIntensity
  10. SFlagColor
  11. SFlagTemperature
  12. SFlagSensorButtons
  13. SFlagSensorTemperature
  14. SFlagSensorLightLevel
  15. SFlagSensorPresence
  16. )
  17. var supportFlags = []SupportFlags{SFlagPower, SFlagIntensity, SFlagColor, SFlagTemperature, SFlagSensorButtons, SFlagSensorTemperature, SFlagSensorLightLevel, SFlagSensorPresence}
  18. func (f SupportFlags) String() string {
  19. return gentools.FlagString(f, supportFlags, []string{"P", "I", "C", "T", "SB", "ST", "SLL", "SP"})
  20. }
  21. func (f SupportFlags) HasAny(d SupportFlags) bool {
  22. return (f & d) != 0
  23. }
  24. func (f SupportFlags) HasAll(d SupportFlags) bool {
  25. return bits.OnesCount32(uint32(f&d)) == bits.OnesCount32(uint32(d))
  26. }
  27. // ColorFlag is primarily to detect warm-white lights, as XY/RGB/HS/HSK can convert without trouble.
  28. type ColorFlag uint32
  29. const (
  30. CFlagXY ColorFlag = 1 << iota
  31. CFlagRGB
  32. CFlagHS
  33. CFlagHSK
  34. CFlagKelvin
  35. )
  36. var colorFlags = []ColorFlag{CFlagXY, CFlagRGB, CFlagHS, CFlagHSK, CFlagKelvin}
  37. func (f ColorFlag) String() string {
  38. return gentools.FlagString(f, colorFlags, []string{"XY", "RGB", "HS", "HSK", "K"})
  39. }
  40. func (f ColorFlag) IsColor() bool {
  41. return f.HasAny(CFlagXY | CFlagRGB | CFlagHS | CFlagHSK)
  42. }
  43. func (f ColorFlag) IsWarmWhite() bool {
  44. return f&CFlagKelvin == CFlagKelvin
  45. }
  46. func (f ColorFlag) IsColorOnly() bool {
  47. return f.IsColor() && !f.IsWarmWhite()
  48. }
  49. func (f ColorFlag) IsWarmWhiteOnly() bool {
  50. return f.IsWarmWhite() && !f.IsColor()
  51. }
  52. func (f ColorFlag) HasAny(d ColorFlag) bool {
  53. return (f & d) != 0
  54. }
  55. func (f ColorFlag) HasAll(d ColorFlag) bool {
  56. return bits.OnesCount32(uint32(f&d)) == bits.OnesCount32(uint32(d))
  57. }