packages feed

arduino-copilot-1.4.0: examples/eepromrange/demo.hs

{-# LANGUAGE RebindableSyntax #-}

import Copilot.Arduino.Uno
import qualified Copilot.Arduino.Library.EEPROMex as EEPROM
import qualified Copilot.Arduino.Library.Serial as Serial
import System.Posix.Env

-- Change this to use a smaller part of the EEPROM.
sz :: Word16
sz = sizeOfEEPROM

main :: IO ()
main = do
	z <- getEnvDefault "ZERO" ""
	arduino $ case z of
		"y" -> mainZero
		_ -> mainReal

-- Zero out the EEPROM, in preparation for mainReal.
mainZero :: Sketch ()
mainZero = do
	EEPROM.maxAllowedWrites sz

	range <- EEPROM.allocRange sz :: Sketch (EEPROM.Range Word8)
	range =: EEPROM.sweepRange 0 (constant (0 :: Word8))
	
	Serial.baud 9600
	Serial.device =: [ Serial.str "EEPROM zeroing started" ]
		@: firstIteration

mainReal :: Sketch ()
mainReal = do
	EEPROM.maxAllowedWrites (sz * 10)
	range <- EEPROM.allocRange sz :: Sketch (EEPROM.Range ADC)
	v <- input' a1 ([10, 20..] :: [ADC])
	range =: EEPROM.sweepRange 0 v @: frequency 3
	led =: frequency 3
	Serial.device =: [ Serial.show v, Serial.char '\n']
	delay =: MilliSeconds (constant 10000)
	Serial.baud 9600