From ca0cc3956da7611933ae25e57746b006decea639 Mon Sep 17 00:00:00 2001
From: smlng <s@mlng.net>
Date: Mon, 12 Jun 2017 22:36:02 +0200
Subject: [PATCH] saul: add support for grove ledbar

---
 drivers/grove_ledbar/grove_ledbar_saul.c      | 38 ++++++++++
 .../include/grove_ledbar_params.h             | 11 +++
 sys/auto_init/auto_init.c                     |  4 ++
 sys/auto_init/saul/auto_init_grove_ledbar.c   | 69 +++++++++++++++++++
 4 files changed, 122 insertions(+)
 create mode 100644 drivers/grove_ledbar/grove_ledbar_saul.c
 create mode 100644 sys/auto_init/saul/auto_init_grove_ledbar.c

diff --git a/drivers/grove_ledbar/grove_ledbar_saul.c b/drivers/grove_ledbar/grove_ledbar_saul.c
new file mode 100644
index 0000000000..11a2eb03a2
--- /dev/null
+++ b/drivers/grove_ledbar/grove_ledbar_saul.c
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2017 HAW Hamburg
+ *
+ * This file is subject to the terms and conditions of the GNU Lesser
+ * General Public License v2.1. See the file LICENSE in the top level
+ * directory for more details.
+ */
+
+/**
+ * @ingroup     drivers_grove_ledbar
+ * @{
+ *
+ * @file
+ * @brief       Grove LED bar adaption to SAUL
+ *
+ * @author      Sebastian Meiling <s@mlng.net>
+ *
+ * @}
+ */
+
+#include <string.h>
+#include <stdio.h>
+
+#include "saul.h"
+#include "grove_ledbar.h"
+
+static int set_ledbar(const void *dev, phydat_t *res)
+{
+    uint8_t lvl = (uint8_t)res->val[0];
+    grove_ledbar_set((grove_ledbar_t *)dev, lvl);
+    return 1;
+}
+
+const saul_driver_t grove_ledbar_saul_driver = {
+    .read = saul_notsup,
+    .write = set_ledbar,
+    .type = SAUL_ACT_LED_RGB,
+};
diff --git a/drivers/grove_ledbar/include/grove_ledbar_params.h b/drivers/grove_ledbar/include/grove_ledbar_params.h
index ac3c13677b..f9089595c0 100644
--- a/drivers/grove_ledbar/include/grove_ledbar_params.h
+++ b/drivers/grove_ledbar/include/grove_ledbar_params.h
@@ -20,6 +20,7 @@
 
 #include "board.h"
 #include "periph/gpio.h"
+#include "saul_reg.h"
 
 #include "grove_ledbar.h"
 #include "my9221.h"
@@ -72,6 +73,16 @@ static const grove_ledbar_params_t grove_ledbar_params[] =
 #endif
 };
 
+/**
+ * @brief   Additional meta information to keep in the SAUL registry
+ */
+static const saul_reg_info_t grove_ledbar_saul_info[] =
+{
+    {
+        .name = "Grove LED bar"
+    }
+};
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/sys/auto_init/auto_init.c b/sys/auto_init/auto_init.c
index 21f392cbe7..7eb0635afc 100644
--- a/sys/auto_init/auto_init.c
+++ b/sys/auto_init/auto_init.c
@@ -291,6 +291,10 @@ void auto_init(void)
     extern void auto_init_mpl3115a2(void);
     auto_init_mpl3115a2();
 #endif
+#ifdef MODULE_GROVE_LEDBAR
+    extern void auto_init_grove_ledbar(void);
+    auto_init_grove_ledbar();
+#endif
 #ifdef MODULE_SI70XX
     extern void auto_init_si70xx(void);
     auto_init_si70xx();
diff --git a/sys/auto_init/saul/auto_init_grove_ledbar.c b/sys/auto_init/saul/auto_init_grove_ledbar.c
new file mode 100644
index 0000000000..6c64497038
--- /dev/null
+++ b/sys/auto_init/saul/auto_init_grove_ledbar.c
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2017 HAW Hamburg
+ *
+ * This file is subject to the terms and conditions of the GNU Lesser
+ * General Public License v2.1. See the file LICENSE in the top level
+ * directory for more details.
+ *
+ */
+
+/**
+ * @ingroup     auto_init_saul
+ * @{
+ *
+ * @file
+ * @brief       Auto initialization of Grove LED bar
+ *
+ * @author      Sebastian Meiling <s@mlng.net>
+ *
+ * @}
+ */
+
+#ifdef MODULE_GROVE_LEDBAR
+
+#include "log.h"
+#include "saul_reg.h"
+
+#include "grove_ledbar.h"
+#include "grove_ledbar_params.h"
+
+/**
+ * @brief   Define the number of configured sensors
+ */
+#define GROVE_LEDBAR_NUM     (sizeof(grove_ledbar_params) / sizeof(grove_ledbar_params[0]))
+
+/**
+ * @brief   Allocate memory for the device descriptors
+ */
+static grove_ledbar_t grove_ledbar_devs[GROVE_LEDBAR_NUM];
+
+/**
+ * @brief   Memory for the SAUL registry entries
+ */
+static saul_reg_t saul_entries[GROVE_LEDBAR_NUM];
+
+/**
+ * @brief   Reference the driver struct
+ */
+extern const saul_driver_t grove_ledbar_saul_driver;
+
+void auto_init_grove_ledbar(void)
+{
+    for (unsigned i = 0; i < GROVE_LEDBAR_NUM; i++) {
+        LOG_DEBUG("[auto_init_saul] initializing Grove LED bar #%u: ", i);
+
+        if (my9221_init(&grove_ledbar_devs[i], &grove_ledbar_params[i]) != MY9221_OK) {
+            LOG_ERROR("ERROR\n");
+            continue;
+        }
+        LOG_DEBUG("SUCCESS\n");
+        saul_entries[i].dev = &(grove_ledbar_devs[i]);
+        saul_entries[i].name = grove_ledbar_saul_info[i].name;
+        saul_entries[i].driver = &grove_ledbar_saul_driver;
+        saul_reg_add(&(saul_entries[i]));
+    }
+}
+
+#else
+typedef int dont_be_pedantic;
+#endif /* MODULE_GROVE_LEDBAR */
-- 
GitLab