Skip to content

Instantly share code, notes, and snippets.

@hejfelix
Created March 20, 2020 06:35
Show Gist options
  • Save hejfelix/b00f11fdbe70904574f18c5c7142e203 to your computer and use it in GitHub Desktop.
Save hejfelix/b00f11fdbe70904574f18c5c7142e203 to your computer and use it in GitHub Desktop.
module Lwp exposing (..)
import Binary exposing (fromIntegers, toDecimal)
import Bitwise exposing (and)
type alias Byte =
Int
type SystemType
= LEGO_WeDo_2_0
| LEGO_Duplo
| LEGO_System_1
| LEGO_System_2
| UNKNOWN Byte
toByte : SystemType -> Byte
toByte sysType =
case sysType of
LEGO_WeDo_2_0 ->
0
LEGO_Duplo ->
1
LEGO_System_1 ->
2
LEGO_System_2 ->
3
UNKNOWN byte ->
byte
maskType : Byte -> Byte
maskType byte =
byte
|> and (toDecimal <| fromIntegers [ 1, 1, 1, 0, 0, 0, 0, 0 ])
|> Bitwise.shiftRightBy 5
fromMasked : Byte -> SystemType
fromMasked masked =
case masked of
0 ->
LEGO_WeDo_2_0
1 ->
LEGO_Duplo
2 ->
LEGO_System_1
3 ->
LEGO_System_2
_ ->
UNKNOWN masked
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment