mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-20 12:29:03 +00:00
machine/usb/hid/joystick: Allow more hat switches
This commit is contained in:
@@ -67,6 +67,10 @@ func (c Definitions) Descriptor() []byte {
|
|||||||
|
|
||||||
func (c Definitions) NewState() State {
|
func (c Definitions) NewState() State {
|
||||||
bufSize := 1
|
bufSize := 1
|
||||||
|
hatSwitches := make([]HatDirection, c.HatSwitchCnt)
|
||||||
|
for i := range hatSwitches {
|
||||||
|
hatSwitches[i] = HatCenter
|
||||||
|
}
|
||||||
axises := make([]*AxisValue, 0, len(c.AxisDefs))
|
axises := make([]*AxisValue, 0, len(c.AxisDefs))
|
||||||
for _, v := range c.AxisDefs {
|
for _, v := range c.AxisDefs {
|
||||||
|
|
||||||
@@ -77,16 +81,14 @@ func (c Definitions) NewState() State {
|
|||||||
}
|
}
|
||||||
btnSize := (c.ButtonCnt + 7) / 8
|
btnSize := (c.ButtonCnt + 7) / 8
|
||||||
bufSize += btnSize
|
bufSize += btnSize
|
||||||
if c.HatSwitchCnt > 0 {
|
bufSize += (len(hatSwitches) + 1) / 2
|
||||||
bufSize++
|
|
||||||
}
|
|
||||||
bufSize += len(axises) * 2
|
bufSize += len(axises) * 2
|
||||||
initBuf := make([]byte, bufSize)
|
initBuf := make([]byte, bufSize)
|
||||||
initBuf[0] = c.ReportID
|
initBuf[0] = c.ReportID
|
||||||
return State{
|
return State{
|
||||||
buf: initBuf,
|
buf: initBuf,
|
||||||
Buttons: make([]byte, btnSize),
|
Buttons: make([]byte, btnSize),
|
||||||
HatSwitches: make([]HatDirection, c.HatSwitchCnt),
|
HatSwitches: hatSwitches,
|
||||||
Axises: axises,
|
Axises: axises,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -103,7 +105,11 @@ func (s State) MarshalBinary() ([]byte, error) {
|
|||||||
s.buf = append(s.buf, s.Buttons...)
|
s.buf = append(s.buf, s.Buttons...)
|
||||||
if len(s.HatSwitches) > 0 {
|
if len(s.HatSwitches) > 0 {
|
||||||
hat := byte(0)
|
hat := byte(0)
|
||||||
for _, v := range s.HatSwitches {
|
for i, v := range s.HatSwitches {
|
||||||
|
if i != 0 && i%2 == 0 {
|
||||||
|
s.buf = append(s.buf, hat)
|
||||||
|
hat = 0
|
||||||
|
}
|
||||||
hat <<= 4
|
hat <<= 4
|
||||||
hat |= byte(v & 0xf)
|
hat |= byte(v & 0xf)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user