From 1f41b761a914ee535375faca3e08a75747f37eb8 Mon Sep 17 00:00:00 2001
From: Rahix <rahix@rahix.de>
Date: Thu, 13 Jun 2019 22:33:51 +0200
Subject: [PATCH] feat(api-demo): More code

---
 hw-tests/api-demo/api.h          | 20 +++++++++-----------
 hw-tests/api-demo/genapi.py      | 16 ++++++++++++----
 hw-tests/api-demo/main.c         |  1 +
 hw-tests/api-demo/meson.build    | 16 ++++++++++++++++
 hw-tests/api-demo/test-payload.c | 24 ++++++++++++++++++------
 meson.build                      |  2 ++
 6 files changed, 58 insertions(+), 21 deletions(-)

diff --git a/hw-tests/api-demo/api.h b/hw-tests/api-demo/api.h
index 7283a779e..797a3a33e 100644
--- a/hw-tests/api-demo/api.h
+++ b/hw-tests/api-demo/api.h
@@ -1,23 +1,21 @@
 #ifndef _API_H
 #define _API_H
+#include <stdint.h>
 
 #ifndef API
 #  define API(id, def) def
 #endif
 
-#define API_FOO 0x35c3
-API(API_FOO, void foo(short x, int y, char z, int w));
-
-#define API_BAR 0xc0ffee
-API(API_BAR, void bar(char*astr));
+#define API_BUZZER 0x35c3
+API(API_BUZZER, void api_set_buzzer(uint8_t state));
 
 typedef struct {
-    int foo;
-    int bar;
-    int baz;
-} qux_t;
+    uint8_t red;
+    uint8_t green;
+    uint8_t blue;
+} led_color_t;
 
-#define API_QUX 0xCCC
-API(API_QUX, void qux(qux_t q));
+#define API_LED 0xc0ffee
+API(API_LED, void api_set_led(uint8_t led, led_color_t color));
 
 #endif /* _API_H */
diff --git a/hw-tests/api-demo/genapi.py b/hw-tests/api-demo/genapi.py
index 3316875ef..3aacbdc02 100644
--- a/hw-tests/api-demo/genapi.py
+++ b/hw-tests/api-demo/genapi.py
@@ -50,7 +50,12 @@ def main():
         f_client = cx.enter_context(open(args.client, "w"))
         f_server = cx.enter_context(open(args.server, "w"))
 
-        print('#include "{}"\n'.format(args.header))
+        print('#include "{}"\n'.format(
+            os.path.basename(args.header)
+        ), file=f_client)
+        print('#include "{}"\n'.format(
+            os.path.basename(args.header)
+        ), file=f_server)
 
         for match in matcher.finditer(source):
             api_id = match.group("id")
@@ -81,7 +86,8 @@ def main():
                     cdecl=api_decl,
                     cargs=api_args,
                     total_size=" + ".join(api_args_sizes),
-                )
+                ),
+                file=f_client,
             )
 
             for i, (arg, ty) in enumerate(zip(api_args_names, api_args_types)):
@@ -90,7 +96,8 @@ def main():
                         type=ty,
                         offset=" + ".join(api_args_sizes[:i]) if i > 0 else "0",
                         arg=arg,
-                    )
+                    ),
+                    file=f_client,
                 )
 
             print(
@@ -103,7 +110,8 @@ def main():
 }}
 """.format(
                     id=api_id
-                )
+                ),
+                file=f_client,
             )
 
 
diff --git a/hw-tests/api-demo/main.c b/hw-tests/api-demo/main.c
index a78e9ea66..d3e4fe6b3 100644
--- a/hw-tests/api-demo/main.c
+++ b/hw-tests/api-demo/main.c
@@ -4,6 +4,7 @@
 
 #include "card10.h"
 #include "tmr_utils.h"
+#include "api.h"
 
 void Core1_Start(void) {
     //MXC_GCR->gp0 = (uint32_t)(&__isr_vector_core1);
diff --git a/hw-tests/api-demo/meson.build b/hw-tests/api-demo/meson.build
index a86ba8c8f..2440a8ad3 100644
--- a/hw-tests/api-demo/meson.build
+++ b/hw-tests/api-demo/meson.build
@@ -1,8 +1,23 @@
+api_stubs = custom_target(
+  'api_*.c',
+  input: 'api.h',
+  output: ['api_client.c', 'api_server.c'],
+  command: [
+    python3,
+    meson.current_source_dir() + '/genapi.py',
+    '-H', '@INPUT0@',
+    '-c', '@OUTPUT0@', '-s', '@OUTPUT1@',
+  ],
+  depend_files: 'genapi.py',
+)
+
+
 name = 'api-demo-core0'
 
 executable(
   name + '.elf',
   'main.c',
+  api_stubs[0],
   dependencies: [libcard10, max32665_startup_core0],
   link_whole: [max32665_startup_core0_lib, board_card10_lib],
   link_args: [
@@ -15,6 +30,7 @@ name = 'api-demo-core1'
 executable(
   name + '.elf',
   'test-payload.c',
+  api_stubs[1],
   dependencies: [libcard10, max32665_startup_core1],
   link_whole: [max32665_startup_core1_lib, board_card10_lib],
   link_args: [
diff --git a/hw-tests/api-demo/test-payload.c b/hw-tests/api-demo/test-payload.c
index 989c611a0..f0aa0fe09 100644
--- a/hw-tests/api-demo/test-payload.c
+++ b/hw-tests/api-demo/test-payload.c
@@ -1,9 +1,27 @@
 #include "board.h"
 #include "gpio.h"
 #include "mxc_delay.h"
+#include "api.h"
 
 static const gpio_cfg_t motor_pin = {PORT_0, PIN_8, GPIO_FUNC_OUT, GPIO_PAD_NONE};
 
+void api_set_buzzer(uint8_t state)
+{
+	if (state) {
+		printf("API: Turning motor ON!\n");
+		GPIO_OutSet(&motor_pin);
+	} else {
+		printf("API: Turning motor OFF!\n");
+		GPIO_OutClr(&motor_pin);
+	}
+}
+
+void api_set_led(uint8_t led, led_color_t color)
+{
+	printf("API: Changing color of led %d.\n", led);
+	leds_set(led, color.red, color.green, color.blue);
+}
+
 int main(void)
 {
 	// Enable rxev on core1
@@ -11,11 +29,5 @@ int main(void)
 	for (int i = 0; 1; i++) {
 		__asm volatile("wfe");
 		printf("core1: Hello! %d\n", i);
-
-#if 0
-		GPIO_OutSet(&motor_pin);
-		mxc_delay(30000);
-		GPIO_OutClr(&motor_pin);
-#endif
 	}
 }
diff --git a/meson.build b/meson.build
index e01f491ce..0cb5a984f 100644
--- a/meson.build
+++ b/meson.build
@@ -27,5 +27,7 @@ add_global_link_arguments(
   language: 'c',
 )
 
+python3 = import('python').find_installation('python3')
+
 subdir('lib/')
 subdir('hw-tests/')
-- 
GitLab