@@ -550,6 +550,7 @@ func main() {
550550 var lastDrumTime time.Time
551551 currentStep := 0
552552
553+ ticker := time .Tick (1 * time .Millisecond )
553554 for {
554555 // ジョイスティック X 軸処理
555556 {
@@ -604,6 +605,7 @@ func main() {
604605 }
605606 prevRotaryButton = currentRotaryButton
606607
608+ <- ticker
607609 // ドラムパターン再生処理
608610 if state .DrumPlaying && state .DrumPatternIndex >= 0 && state .DrumPatternIndex < len (drumPatterns ) {
609611 currentPattern := drumPatterns [state .DrumPatternIndex ]
@@ -653,7 +655,6 @@ func main() {
653655 }
654656
655657 state .Keys [i ] = true
656- time .Sleep (1 * time .Millisecond )
657658
658659 case on2off :
659660 m .NoteOff (cable , channel , note , velocity )
@@ -664,23 +665,21 @@ func main() {
664665 // 音名をクリア
665666 state .ActiveNotes [i ] = ""
666667 state .Keys [i ] = false
667- time .Sleep (1 * time .Millisecond )
668668 }
669669 }
670670
671- // LED に色を反映
672- ws .WriteRaw (colors )
673-
674671 // redraw は毎フレームではなく、一定間隔にする(例: 100ms)
675672 now := time .Now ()
676- if lastRedrawTime .IsZero () || now .Sub (lastRedrawTime ) >= 100 * time .Millisecond {
677- // 画面を更新
678- redraw (state )
679- lastRedrawTime = now
673+ if true {
674+ if lastRedrawTime .IsZero () || now .Sub (lastRedrawTime ) >= 100 * time .Millisecond {
675+ // LED に色を反映
676+ ws .WriteRaw (colors )
677+
678+ // 画面を更新
679+ redraw (state )
680+ lastRedrawTime = now
681+ }
680682 }
681-
682- // 軽い sleep(1ms)を入れるとCPU負荷が安定します
683- time .Sleep (1 * time .Millisecond )
684683 }
685684}
686685
@@ -796,49 +795,48 @@ const (
796795 on2off2
797796 on2off3
798797 on2off4
799- on2offX
798+ on2off5
800799)
801800
802801func getKeys (colPins , rowPins []machine.Pin ) []state {
802+ colPins [0 ].Configure (machine.PinConfig {Mode : machine .PinOutput })
803803 colPins [0 ].High ()
804- colPins [1 ].Low ()
805- colPins [2 ].Low ()
806- colPins [3 ].Low ()
807- time .Sleep (1 * time .Millisecond )
808804
809805 States [0 ] = updateState (States [0 ], rowPins [0 ].Get ())
810806 States [1 ] = updateState (States [1 ], rowPins [1 ].Get ())
811807 States [2 ] = updateState (States [2 ], rowPins [2 ].Get ())
812808
813809 colPins [0 ].Low ()
810+ colPins [0 ].Configure (machine.PinConfig {Mode : machine .PinInputPulldown })
811+
812+ colPins [1 ].Configure (machine.PinConfig {Mode : machine .PinOutput })
814813 colPins [1 ].High ()
815- colPins [2 ].Low ()
816- colPins [3 ].Low ()
817- time .Sleep (1 * time .Millisecond )
818814
819815 States [3 ] = updateState (States [3 ], rowPins [0 ].Get ())
820816 States [4 ] = updateState (States [4 ], rowPins [1 ].Get ())
821817 States [5 ] = updateState (States [5 ], rowPins [2 ].Get ())
822818
823- colPins [0 ].Low ()
824819 colPins [1 ].Low ()
820+ colPins [1 ].Configure (machine.PinConfig {Mode : machine .PinInputPulldown })
821+
822+ colPins [2 ].Configure (machine.PinConfig {Mode : machine .PinOutput })
825823 colPins [2 ].High ()
826- colPins [3 ].Low ()
827- time .Sleep (1 * time .Millisecond )
828824
829825 States [6 ] = updateState (States [6 ], rowPins [0 ].Get ())
830826 States [7 ] = updateState (States [7 ], rowPins [1 ].Get ())
831827 States [8 ] = updateState (States [8 ], rowPins [2 ].Get ())
832828
833- colPins [0 ].Low ()
834- colPins [1 ].Low ()
835829 colPins [2 ].Low ()
830+ colPins [2 ].Configure (machine.PinConfig {Mode : machine .PinInputPulldown })
831+
832+ colPins [3 ].Configure (machine.PinConfig {Mode : machine .PinOutput })
836833 colPins [3 ].High ()
837- time .Sleep (1 * time .Millisecond )
838834
839835 States [9 ] = updateState (States [9 ], rowPins [0 ].Get ())
840836 States [10 ] = updateState (States [10 ], rowPins [1 ].Get ())
841837 States [11 ] = updateState (States [11 ], rowPins [2 ].Get ())
838+ colPins [3 ].Low ()
839+ colPins [3 ].Configure (machine.PinConfig {Mode : machine .PinInputPulldown })
842840
843841 return States [:]
844842}
@@ -851,28 +849,37 @@ func updateState(s state, btn bool) state {
851849 ret = off2on
852850 }
853851 case off2on :
854- ret = off2on2
855- case off2on2 :
856- ret = off2on3
857- case off2on3 :
858- ret = off2on4
859- case off2on4 :
860- ret = off2onX
861- case off2onX :
862852 ret = on
863853 case on :
864854 if ! btn {
865- ret = on2off
855+ ret = on2off2
866856 }
867- case on2off :
868- ret = on2off2
869857 case on2off2 :
870- ret = on2off3
858+ if btn {
859+ ret = on
860+ } else {
861+ ret = on2off3
862+ }
871863 case on2off3 :
872864 ret = on2off4
865+ if btn {
866+ ret = on
867+ } else {
868+ ret = on2off4
869+ }
873870 case on2off4 :
874- ret = on2offX
875- case on2offX :
871+ if btn {
872+ ret = on
873+ } else {
874+ ret = on2off5
875+ }
876+ case on2off5 :
877+ if btn {
878+ ret = on
879+ } else {
880+ ret = on2off
881+ }
882+ case on2off :
876883 ret = off
877884 }
878885 return ret
0 commit comments