Add CGo typecasts everywhere

This commit is contained in:
Gregor Riepl 2021-01-31 19:35:58 +01:00
parent 8a1cfbb064
commit 4447e89c5e

View file

@ -1,12 +1,14 @@
package uncanny package uncanny
/* /*
// include our message format definitions // Include our message format definitions
#cgo CPPFLAGS: -I${SRCDIR}/.. #cgo CPPFLAGS: -I${SRCDIR}/..
#include <stdint.h>
#include <messages.h> #include <messages.h>
// since we can't use macros in Go directly, let's add some wrappers // Since we can't use C macros in Go directly, let's add some wrappers.
// to avoid conflicts, we'll simply lowercase the symbol names // WARNING: CGo type translation can be very wonky, so make sure to always
// cast to the expected type!
const uint16_t can_msg_feedback_status = CAN_MSG_FEEDBACK_STATUS; const uint16_t can_msg_feedback_status = CAN_MSG_FEEDBACK_STATUS;
const size_t can_msg_feedback_status_field_reset_sw = CAN_MSG_FEEDBACK_STATUS_FIELD_RESET_SW; const size_t can_msg_feedback_status_field_reset_sw = CAN_MSG_FEEDBACK_STATUS_FIELD_RESET_SW;
@ -128,54 +130,54 @@ func DecodeFeedbackMessage(frame can.Frame) (FeedbackMessage, error) {
if frame.Length != 2 { if frame.Length != 2 {
return ret, InvalidMessageLength return ret, InvalidMessageLength
} }
ret.EndD = frame.Data[C.can_msg_feedback_status_field_end_d] & C.can_msg_feedback_status_bit_end_d != 0 ret.EndD = frame.Data[int(C.can_msg_feedback_status_field_end_d)] & uint8(C.can_msg_feedback_status_bit_end_d) != 0
ret.EndE = frame.Data[C.can_msg_feedback_status_field_end_e] & C.can_msg_feedback_status_bit_end_e != 0 ret.EndE = frame.Data[int(C.can_msg_feedback_status_field_end_e)] & uint8(C.can_msg_feedback_status_bit_end_e) != 0
ret.EndF = frame.Data[C.can_msg_feedback_status_field_end_f] & C.can_msg_feedback_status_bit_end_f != 0 ret.EndF = frame.Data[int(C.can_msg_feedback_status_field_end_f)] & uint8(C.can_msg_feedback_status_bit_end_f) != 0
ret.EndG = frame.Data[C.can_msg_feedback_status_field_end_g] & C.can_msg_feedback_status_bit_end_g != 0 ret.EndG = frame.Data[int(C.can_msg_feedback_status_field_end_g)] & uint8(C.can_msg_feedback_status_bit_end_g) != 0
ret.EndH = frame.Data[C.can_msg_feedback_status_field_end_h] & C.can_msg_feedback_status_bit_end_h != 0 ret.EndH = frame.Data[int(C.can_msg_feedback_status_field_end_h)] & uint8(C.can_msg_feedback_status_bit_end_h) != 0
ret.EmptyD = frame.Data[C.can_msg_feedback_status_field_empty_d] & C.can_msg_feedback_status_bit_empty_d != 0 ret.EmptyD = frame.Data[int(C.can_msg_feedback_status_field_empty_d)] & uint8(C.can_msg_feedback_status_bit_empty_d) != 0
ret.EmptyE = frame.Data[C.can_msg_feedback_status_field_empty_e] & C.can_msg_feedback_status_bit_empty_e != 0 ret.EmptyE = frame.Data[int(C.can_msg_feedback_status_field_empty_e)] & uint8(C.can_msg_feedback_status_bit_empty_e) != 0
ret.EmptyF = frame.Data[C.can_msg_feedback_status_field_empty_f] & C.can_msg_feedback_status_bit_empty_f != 0 ret.EmptyF = frame.Data[int(C.can_msg_feedback_status_field_empty_f)] & uint8(C.can_msg_feedback_status_bit_empty_f) != 0
ret.EmptyG = frame.Data[C.can_msg_feedback_status_field_empty_g] & C.can_msg_feedback_status_bit_empty_g != 0 ret.EmptyG = frame.Data[int(C.can_msg_feedback_status_field_empty_g)] & uint8(C.can_msg_feedback_status_bit_empty_g) != 0
ret.EmptyH = frame.Data[C.can_msg_feedback_status_field_empty_h] & C.can_msg_feedback_status_bit_empty_h != 0 ret.EmptyH = frame.Data[int(C.can_msg_feedback_status_field_empty_h)] & uint8(C.can_msg_feedback_status_bit_empty_h) != 0
ret.ResetSw = frame.Data[C.can_msg_feedback_status_field_reset_sw] & C.can_msg_feedback_status_bit_reset_sw != 0 ret.ResetSw = frame.Data[int(C.can_msg_feedback_status_field_reset_sw)] & uint8(C.can_msg_feedback_status_bit_reset_sw) != 0
return ret, nil return ret, nil
} }
func (f FeedbackMessage) Encode() can.Frame { func (f FeedbackMessage) Encode() can.Frame {
data := [8]uint8{} data := [8]uint8{}
if f.EndD { if f.EndD {
data[C.can_msg_feedback_status_field_end_d] |= C.can_msg_feedback_status_bit_end_d data[int(C.can_msg_feedback_status_field_end_d)] |= uint8(C.can_msg_feedback_status_bit_end_d)
} }
if f.EndE { if f.EndE {
data[C.can_msg_feedback_status_field_end_e] |= C.can_msg_feedback_status_bit_end_e data[int(C.can_msg_feedback_status_field_end_e)] |= uint8(C.can_msg_feedback_status_bit_end_e)
} }
if f.EndF { if f.EndF {
data[C.can_msg_feedback_status_field_end_f] |= C.can_msg_feedback_status_bit_end_f data[int(C.can_msg_feedback_status_field_end_f)] |= uint8(C.can_msg_feedback_status_bit_end_f)
} }
if f.EndG { if f.EndG {
data[C.can_msg_feedback_status_field_end_g] |= C.can_msg_feedback_status_bit_end_g data[int(C.can_msg_feedback_status_field_end_g)] |= uint8(C.can_msg_feedback_status_bit_end_g)
} }
if f.EndH { if f.EndH {
data[C.can_msg_feedback_status_field_end_h] |= C.can_msg_feedback_status_bit_end_h data[int(C.can_msg_feedback_status_field_end_h)] |= uint8(C.can_msg_feedback_status_bit_end_h)
} }
if f.EmptyD { if f.EmptyD {
data[C.can_msg_feedback_status_field_empty_d] |= C.can_msg_feedback_status_bit_empty_d data[int(C.can_msg_feedback_status_field_empty_d)] |= uint8(C.can_msg_feedback_status_bit_empty_d)
} }
if f.EmptyE { if f.EmptyE {
data[C.can_msg_feedback_status_field_empty_e] |= C.can_msg_feedback_status_bit_empty_e data[int(C.can_msg_feedback_status_field_empty_e)] |= uint8(C.can_msg_feedback_status_bit_empty_e)
} }
if f.EmptyF { if f.EmptyF {
data[C.can_msg_feedback_status_field_empty_f] |= C.can_msg_feedback_status_bit_empty_f data[int(C.can_msg_feedback_status_field_empty_f)] |= uint8(C.can_msg_feedback_status_bit_empty_f)
} }
if f.EmptyG { if f.EmptyG {
data[C.can_msg_feedback_status_field_empty_g] |= C.can_msg_feedback_status_bit_empty_g data[int(C.can_msg_feedback_status_field_empty_g)] |= uint8(C.can_msg_feedback_status_bit_empty_g)
} }
if f.EmptyH { if f.EmptyH {
data[C.can_msg_feedback_status_field_empty_h] |= C.can_msg_feedback_status_bit_empty_h data[int(C.can_msg_feedback_status_field_empty_h)] |= uint8(C.can_msg_feedback_status_bit_empty_h)
} }
if f.ResetSw { if f.ResetSw {
data[C.can_msg_feedback_status_field_reset_sw] |= C.can_msg_feedback_status_bit_reset_sw data[int(C.can_msg_feedback_status_field_reset_sw)] |= uint8(C.can_msg_feedback_status_bit_reset_sw)
} }
return can.Frame{ return can.Frame{
ID: uint32(C.can_msg_feedback_status), ID: uint32(C.can_msg_feedback_status),
@ -232,17 +234,17 @@ func DecodeDispenseMessage(frame can.Frame) (DispenseMessage, error) {
return ret, InvalidMessageLength return ret, InvalidMessageLength
} }
switch frame.Data[0] { switch frame.Data[0] {
case C.can_msg_power_dispense_off: case uint8(C.can_msg_power_dispense_off):
ret.Slot = DispenseSlotOff ret.Slot = DispenseSlotOff
case C.can_msg_power_dispense_slot1: case uint8(C.can_msg_power_dispense_slot1):
ret.Slot = 0 ret.Slot = 0
case C.can_msg_power_dispense_slot2: case uint8(C.can_msg_power_dispense_slot2):
ret.Slot = 1 ret.Slot = 1
case C.can_msg_power_dispense_slot3: case uint8(C.can_msg_power_dispense_slot3):
ret.Slot = 2 ret.Slot = 2
case C.can_msg_power_dispense_slot4: case uint8(C.can_msg_power_dispense_slot4):
ret.Slot = 3 ret.Slot = 3
case C.can_msg_power_dispense_slot5: case uint8(C.can_msg_power_dispense_slot5):
ret.Slot = 4 ret.Slot = 4
} }
return ret, nil return ret, nil
@ -252,17 +254,17 @@ func (f DispenseMessage) Encode() can.Frame {
data := [8]uint8{} data := [8]uint8{}
switch f.Slot { switch f.Slot {
case DispenseSlotOff: case DispenseSlotOff:
data[0] = C.can_msg_power_dispense_off data[0] = uint8(C.can_msg_power_dispense_off)
case 0: case 0:
data[0] = C.can_msg_power_dispense_slot1 data[0] = uint8(C.can_msg_power_dispense_slot1)
case 1: case 1:
data[0] = C.can_msg_power_dispense_slot2 data[0] = uint8(C.can_msg_power_dispense_slot2)
case 2: case 2:
data[0] = C.can_msg_power_dispense_slot3 data[0] = uint8(C.can_msg_power_dispense_slot3)
case 3: case 3:
data[0] = C.can_msg_power_dispense_slot4 data[0] = uint8(C.can_msg_power_dispense_slot4)
case 4: case 4:
data[0] = C.can_msg_power_dispense_slot5 data[0] = uint8(C.can_msg_power_dispense_slot5)
} }
return can.Frame{ return can.Frame{
ID: uint32(C.can_msg_power_dispense), ID: uint32(C.can_msg_power_dispense),
@ -284,7 +286,7 @@ func DecodeAutoMessage(frame can.Frame) (AutoMessage, error) {
return ret, InvalidMessageLength return ret, InvalidMessageLength
} }
// we accept all non-null values as "enable" // we accept all non-null values as "enable"
if frame.Data[0] != C.can_msg_auto_status_disable { if frame.Data[0] != uint8(C.can_msg_auto_status_disable) {
ret.AutoUpdate = true ret.AutoUpdate = true
} }
return ret, nil return ret, nil
@ -293,9 +295,9 @@ func DecodeAutoMessage(frame can.Frame) (AutoMessage, error) {
func (f AutoMessage) Encode() can.Frame { func (f AutoMessage) Encode() can.Frame {
data := [8]uint8{} data := [8]uint8{}
if f.AutoUpdate { if f.AutoUpdate {
data[0] = C.can_msg_auto_status_enable data[0] = uint8(C.can_msg_auto_status_enable)
} else { } else {
data[0] = C.can_msg_auto_status_disable data[0] = uint8(C.can_msg_auto_status_disable)
} }
return can.Frame{ return can.Frame{
ID: uint32(C.can_msg_auto_status), ID: uint32(C.can_msg_auto_status),