Last active
April 24, 2023 15:19
-
-
Save ivanperez-keera/81fe2c314bb925e2a75e1cb36f3fb4a7 to your computer and use it in GitHub Desktop.
Read multiple values from arduino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE DataKinds #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
{-# LANGUAGE MultiParamTypeClasses #-} | |
{-# LANGUAGE RebindableSyntax #-} | |
{-# LANGUAGE ScopedTypeVariables #-} | |
-- This module declares an orphan instance on purpose. This warning does not | |
-- need to be disabled if/when this code is moved into the arduino library. | |
{-# OPTIONS_GHC -Wno-orphans #-} | |
module ArduinoSerialArray where | |
-- External imports | |
import Data.Proxy | |
import GHC.TypeNats | |
import qualified Prelude | |
-- Internal imports (arduino) | |
import Copilot.Arduino hiding ( show ) | |
import Copilot.Arduino.Internals | |
import qualified Copilot.Arduino.Library.Serial as Serial | |
import qualified Copilot.Arduino.Library.Serial.Device as Serial | |
instance forall n . KnownNat n | |
=> Input Arduino Serial.SerialDevice (Array n Int8) where | |
input' (Serial.SerialDevice (Serial.SerialDeviceName devname)) interpretvalues | |
= mkInput s | |
where | |
s = InputSource | |
{ defineVar = mkCChunk | |
[ CLine $ "int " | |
<> varname | |
<> "[" <> Prelude.show (natVal (Proxy :: Proxy n )) | |
<> "];" | |
] | |
, setupInput = [] | |
, inputPinmode = mempty | |
, readInput = mkCChunk $ | |
map | |
(\n -> CLine $ varname | |
<> "[" <> Prelude.show n <> "]" | |
<> " = " <> devname <> ".read();") | |
[0 .. natVal (Proxy :: Proxy n) - 1] | |
, inputStream = extern varname interpretvalues' | |
} | |
varname = "input_" <> devname | |
interpretvalues' | |
| null interpretvalues = Nothing | |
| otherwise = Just interpretvalues |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE DataKinds #-} | |
{-# LANGUAGE RebindableSyntax #-} | |
{-# LANGUAGE ScopedTypeVariables #-} | |
module Examples.SerialPort.Demo where | |
import qualified Copilot.Arduino.Library.Serial as Serial | |
import qualified Copilot.Arduino.Library.Serial.Device as Serial | |
import Copilot.Arduino.Uno | |
import ArduinoSerialArray | |
main :: IO () | |
main = arduino $ do | |
Serial.baud 9600 | |
-- Light the led whenever the user types on the serial port and the | |
-- last two keys entered are the same. | |
(userinput :: Stream (Array 2 Int8)) <- input Serial.device | |
led =: sameVal (userinput .!! 0) (userinput .!! 1) | |
delay =: MilliSeconds (constant 100) | |
-- | True if both behaviors contain a value, and they are the same. | |
sameVal :: Behavior Int8 -> Behavior Int8 -> Behavior Bool | |
sameVal userinput0 userinput1 = | |
userinput0 /= constant Serial.noInput && userinput0 == userinput1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This example demonstrates how to read multiple values from the Serial port in arduino. | |
It's not tested. It's for demonstration only. | |
Most of the code is copy and paste from the arduino-copilot library created by Joey Hess. Please follow the license determined by him. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment