From 15e6bace9e8b0f8bc518dc813735c0f9eb354c07 Mon Sep 17 00:00:00 2001
From: Hans-Peter Deifel <hpd@hpdeifel.de>
Date: Tue, 18 Oct 2016 14:08:55 +0200
Subject: [PATCH] Generate IdleHook from Haskell

This will allow to trigger interrupts there.
---
 files/mockup_prelude.cc | 6 ------
 src/CFG/C.hs            | 8 ++++++++
 test/CFG/CSpec.hs       | 4 ++++
 3 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/files/mockup_prelude.cc b/files/mockup_prelude.cc
index 5a865c0..b8fe886 100644
--- a/files/mockup_prelude.cc
+++ b/files/mockup_prelude.cc
@@ -118,9 +118,3 @@ int _decisionMaker(int i) {
 
 	return res;
 }
-
-void PreIdleHook() {
-	_print_os_state("PreIdleHook", __LINE__);
-        ShutdownMachine();
-}
-
diff --git a/src/CFG/C.hs b/src/CFG/C.hs
index 3e4b981..5b5d6f9 100644
--- a/src/CFG/C.hs
+++ b/src/CFG/C.hs
@@ -6,6 +6,7 @@ module CFG.C
        , generateDecl
        , compileToCAll
        , declareExtern
+       , preIdleHook
        ) where
 
 import           Control.Comonad
@@ -26,6 +27,7 @@ compileToCAll funs = include
                   <> generateDecls funs
                   <> "\n"
                   <> T.unlines (map compileToC funs)
+                  <> preIdleHook
 
 include :: Text
 include = "#include <mockup_prelude.cc>\n\n"
@@ -66,6 +68,12 @@ declareExtern funs = mconcat $
     callName (Call name _) = Just name
     callName _             = Nothing
 
+preIdleHook :: Text
+preIdleHook = "void PreIdleHook() {\n"
+           <> "  _print_os_state(\"PreIdleHook\", __LINE__);\n"
+           <> "  ShutdownMachine();\n"
+           <> "}\n"
+
 instrsToC :: [Instr] -> Text
 instrsToC = (<> "\n") . mconcat . intersperse "\n" . map instrToC
 
diff --git a/test/CFG/CSpec.hs b/test/CFG/CSpec.hs
index faa57bd..047fa1c 100644
--- a/test/CFG/CSpec.hs
+++ b/test/CFG/CSpec.hs
@@ -141,6 +141,7 @@ compileToCAllTest = describe "compileToCAll" $ do
                  <> example1FunctionCDecl <> example2FunctionCDecl <> "\n"
                  <> example1FunctionC <> "\n"
                  <> example2FunctionC <> "\n"
+                 <> preIdleHook
                  )
 
   it "compiles two tasks" $
@@ -151,6 +152,7 @@ compileToCAllTest = describe "compileToCAll" $ do
                  <> example1TaskCDecl <> example2TaskCDecl <> "\n"
                  <> example1TaskC <> "\n"
                  <> example2TaskC <> "\n"
+                 <> preIdleHook
                  )
 
   it "compiles one task and one function" $
@@ -161,6 +163,7 @@ compileToCAllTest = describe "compileToCAll" $ do
                  <> example1TaskCDecl <> example2FunctionCDecl <> "\n"
                  <> example1TaskC <> "\n"
                  <> example2FunctionC <> "\n"
+                 <> preIdleHook
                  )
 
   it "compiles one tasks and one ISR" $
@@ -171,6 +174,7 @@ compileToCAllTest = describe "compileToCAll" $ do
                  <> example1TaskCDecl <> "\n"
                  <> example1TaskC <> "\n"
                  <> example2ISRC <> "\n"
+                 <> preIdleHook
                  )
 
 
-- 
GitLab