Skip to content

Commit

Permalink
* Create constants for dirModes
Browse files Browse the repository at this point in the history
* Update README
  • Loading branch information
Malte Muench committed Jul 29, 2024
1 parent 5326f29 commit c96eeae
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ Here they are:
| `pixelbin2ascii` | This mode was designed to address colorized pixels. MQTT-wise you can insert a string like "<0-255> #RRGGBB" which will be converted to 4 byte on the CAN-BUS the first byte will be the number of the LED 0-255 and bytes 1, 2, 3 are the color of red, green and blue. |
| `16bool2ascii` | Interprets two bytes can-wise and publishes them as 16 boolean values to mqtt |
| `{i}[u]int{b}2ascii` | `i` is the amount of instances of numbers in the CAN-Frame/the MQTT-message. Valid instance amounts are currently 1,2,4 and 8. Although other combinations are possible. Might be added in the future, if the need arises. `b` is the size of each number in bits. Supported values are 8,16,32 and 64. You can use either one unsigned or signed integers. This are all possible combinations: `int82ascii`, `2int82ascii`, `4int82ascii`, `8int82ascii`, `int162ascii`, `2int162ascii`, `4int162ascii`, `int322ascii`, `2int322ascii`, `int642ascii`, `uint82ascii`, `2uint82ascii`, `4uint82ascii`, `8uint82ascii`, `uint162ascii`, `2uint162ascii`, `4uint162ascii`, `uint322ascii`, `2uint322ascii`, `uint642ascii` |
|


## Unidirectional Mode
Expand Down
4 changes: 2 additions & 2 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var (
debugLog bool
canInterface, mqttConnection, configFile string
version = "dev"
dirMode = 0 // directional modes: 0=bidirectional 1=can2mqtt only 2=mqtt2can only [-d]
dirMode = BIDIRECTIONAL // directional modes: 0=bidirectional 1=can2mqtt only 2=mqtt2can only [-d]
wg sync.WaitGroup
)

Expand All @@ -34,7 +34,7 @@ func main() {
flag.IntVar(&dirMode, "d", 0, "direction mode\n0: bidirectional (default)\n1: can2mqtt only\n2: mqtt2can only")
flag.Parse()

if dirMode < 0 || dirMode > 2 {
if dirMode < BIDIRECTIONAL || dirMode > MQTT2CAN_ONLY {
slog.Error("got invalid value for -d. Valid values are 0 (bidirectional), 1 (can2mqtt only) or 2 (mqtt2can only)", "d", dirMode)
}

Expand Down
4 changes: 2 additions & 2 deletions src/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func handleCAN(cf can.Frame) {
slog.Debug("received CANFrame", "id", cf.ID, "len", cf.Length, "data", cf.Data)
// Only do conversions when necessary
if dirMode != 2 {
if dirMode != MQTT2CAN_ONLY {
mqttPayload, err := pairFromID[cf.ID].convertMode.ToMqtt(cf)
if err != nil {
slog.Warn("conversion to MQTT message unsuccessful", "convertmode", pairFromID[cf.ID].convertMode, "error", err)
Expand All @@ -33,7 +33,7 @@ func handleCAN(cf can.Frame) {
func handleMQTT(_ MQTT.Client, msg MQTT.Message) {
slog.Debug("received message", "topic", msg.Topic(), "payload", msg.Payload())

if dirMode != 1 {
if dirMode != CAN2MQTT_ONLY {
cf, err := pairFromTopic[msg.Topic()].convertMode.ToCan(msg.Payload())
if err != nil {
slog.Warn("conversion to CAN-Frame unsuccessful", "convertmode", pairFromTopic[msg.Topic()].convertMode, "error", err)
Expand Down
9 changes: 8 additions & 1 deletion src/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,11 @@ type can2mqtt struct {
canId uint32
convertMode ConvertMode
mqttTopic string
}
}

// Valid values for "dirMode"
const (
BIDIRECTIONAL = iota
CAN2MQTT_ONLY
MQTT2CAN_ONLY
)

0 comments on commit c96eeae

Please sign in to comment.