rid.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package main
  2. import "time"
  3. import "log"
  4. import "fmt"
  5. import "os"
  6. import "os/signal"
  7. import "syscall"
  8. import "github.com/stianeikeland/go-rpio/v4"
  9. import "github.com/hypebeast/go-osc/osc"
  10. const (
  11. // TX series codes
  12. AMP_Input_CD = 0x020 // ATEM
  13. AMP_Input_MD = 0x030
  14. AMP_Input_Tape = 0x070
  15. AMP_Input_DVD = 0x120
  16. AMP_Input_CDR = 0x130 // Assigned to tape by holding button for 3 secs
  17. AMP_Input_HDD = 0x170 // Assigned to video or tape by holding button for 3 secs
  18. AMP_Input_Video = 0x1a0
  19. AMP_PowerOn_OrMask = 0x0f
  20. // These work only when video input is selected
  21. AMP_Vol_up = 0x1a2
  22. AMP_Vol_down = 0x1a3
  23. AMP_Vol_mute = 0x1a4
  24. AMP_Vol_unmute = 0x1a5
  25. AMP_Off = 0x1ae
  26. AMP_On = 0x1af
  27. // Dimmer
  28. AMP_DimmerHi = 0x2b0
  29. AMP_DimmerMid = 0x2b1
  30. AMP_DimmerLo = 0x2b2
  31. // Test modes
  32. Test_mode = 0x421
  33. )
  34. type IR struct {
  35. pin rpio.Pin
  36. }
  37. func NewIR() *IR {
  38. if err := rpio.Open(); err != nil {
  39. log.Fatal(err)
  40. }
  41. //defer rpio.Close()
  42. pin := rpio.Pin(26)
  43. pin.Output() // Output mode
  44. return &IR{pin}
  45. }
  46. // Encode a bit of IR information
  47. func (ir *IR) sendHiLo(hi time.Duration, lo time.Duration) {
  48. ir.pin.High()
  49. time.Sleep(hi * time.Millisecond)
  50. ir.pin.Low()
  51. time.Sleep(lo * time.Millisecond)
  52. }
  53. func (ir *IR) sendCode(code int) {
  54. // Send header
  55. ir.sendHiLo(3, 1)
  56. // Send 12 bits of command
  57. for mask := 0x800; mask != 0; mask >>= 1 {
  58. if code&mask != 0 {
  59. ir.sendHiLo(1, 2) // 1
  60. } else {
  61. ir.sendHiLo(1, 1) // 0
  62. }
  63. }
  64. // Send end gap
  65. ir.sendHiLo(1, 21)
  66. fmt.Printf("Sent code: %03x\n", code)
  67. }
  68. func getOSCInt(msg *osc.Message) int32 {
  69. for _, arg := range msg.Arguments {
  70. switch arg.(type) {
  71. case int32:
  72. return arg.(int32)
  73. }
  74. }
  75. return 0
  76. }
  77. func main() {
  78. ir := NewIR()
  79. // Setup and spawn the OSC server
  80. addr := "0.0.0.0:9137"
  81. d := osc.NewStandardDispatcher()
  82. d.AddMsgHandler("/8bus/amp/input/tape", func(msg *osc.Message) { ir.sendCode(AMP_Input_Tape) })
  83. d.AddMsgHandler("/8bus/amp/input/md", func(msg *osc.Message) { ir.sendCode(AMP_Input_MD) })
  84. d.AddMsgHandler("/8bus/amp/input/cd", func(msg *osc.Message) { ir.sendCode(AMP_Input_CD) })
  85. d.AddMsgHandler("/8bus/amp/input/cdr", func(msg *osc.Message) { ir.sendCode(AMP_Input_CDR) })
  86. d.AddMsgHandler("/8bus/amp/input/dvd", func(msg *osc.Message) { ir.sendCode(AMP_Input_DVD) })
  87. d.AddMsgHandler("/8bus/amp/input/hdd", func(msg *osc.Message) { ir.sendCode(AMP_Input_HDD) })
  88. d.AddMsgHandler("/8bus/amp/input/video", func(msg *osc.Message) { ir.sendCode(AMP_Input_Video) })
  89. d.AddMsgHandler("/8bus/amp/vol/up", func(msg *osc.Message) { ir.sendCode(AMP_Vol_up) })
  90. d.AddMsgHandler("/8bus/amp/vol/down", func(msg *osc.Message) { ir.sendCode(AMP_Vol_down) })
  91. d.AddMsgHandler("/8bus/amp/vol/mute", func(msg *osc.Message) { ir.sendCode(AMP_Vol_mute) })
  92. d.AddMsgHandler("/8bus/amp/vol/unmute", func(msg *osc.Message) { ir.sendCode(AMP_Vol_unmute) })
  93. d.AddMsgHandler("/8bus/amp/power/on", func(msg *osc.Message) { ir.sendCode(AMP_On) })
  94. d.AddMsgHandler("/8bus/amp/power/off", func(msg *osc.Message) { ir.sendCode(AMP_Off) })
  95. d.AddMsgHandler("/8bus/amp/code", func(msg *osc.Message) { ir.sendCode(int(getOSCInt(msg))) })
  96. // Set up and start OSC server
  97. go func() {
  98. server := &osc.Server{Addr: addr, Dispatcher: d}
  99. if err := server.ListenAndServe(); err != nil {
  100. log.Fatal("error: ", err.Error())
  101. }
  102. }()
  103. // Wait for signal
  104. ch := make(chan os.Signal, 1)
  105. signal.Notify(ch, syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT)
  106. d.AddMsgHandler("/8bus/amp/halt", func(msg *osc.Message) { ch <- syscall.SIGINT })
  107. <-ch
  108. fmt.Println("sent")
  109. }