From 75843d7e163f97fd42f96661863c1de664b08cc8 Mon Sep 17 00:00:00 2001
From: Guo Wenxue <guowenxue@gmail.com>
Date: Sun, 07 Jul 2024 19:28:29 +0800
Subject: [PATCH] Update modules led source code to support other pins

---
 modules/w25qflash.c |   95 +++++----------
 modules/at24c.c     |   94 +++++----------
 modules/sht20.c     |   95 +++++----------
 modules/pwm.c       |    4 
 modules/leds.c      |   15 ++
 5 files changed, 118 insertions(+), 185 deletions(-)

diff --git a/modules/at24c.c b/modules/at24c.c
index aaa05e6..02c9503 100644
--- a/modules/at24c.c
+++ b/modules/at24c.c
@@ -109,7 +109,7 @@
 void i2c_term(i2c_t *i2c);
 
 static inline void msleep(unsigned long ms);
-void dump_buf(const char *prompt, char *buf, size_t len);
+void dump_buf(const char *prompt, char *buffer, size_t length);
 
 static inline void banner(const char *progname)
 {
@@ -570,74 +570,46 @@
     nanosleep(&cSleep, 0);
 }
 
-#define LINELEN 81
-#define CHARS_PER_LINE 16
-static char *print_char =
-"                "
-"                "
-" !\"#$%&'()*+,-./"
-"0123456789:;<=>?"
-"@ABCDEFGHIJKLMNO"
-"PQRSTUVWXYZ[\\]^_"
-"`abcdefghijklmno"
-"pqrstuvwxyz{|}~ "
-"                "
-"                "
-" ???????????????"
-"????????????????"
-"????????????????"
-"????????????????"
-"????????????????"
-"????????????????";
-
-void dump_buf(const char *prompt, char *buf, size_t len)
+void dump_buf(const char *prompt, char *buffer, size_t length)
 {
-    int rc;
-    int idx;
-    char prn[LINELEN];
-    char lit[CHARS_PER_LINE + 2];
-    char hc[4];
-    short line_done = 1;
+    size_t i, j;
 
-    if( prompt )
-        printf("%s", prompt);
-
-    rc = len;
-    idx = 0;
-    lit[CHARS_PER_LINE] = '\0';
-
-    while (rc > 0)
+    if (prompt)
     {
-        if (line_done)
-            snprintf(prn, LINELEN, "%08X: ", idx);
-
-        do
-        {
-            unsigned char c = buf[idx];
-            snprintf(hc, 4, "%02X ", c);
-            strncat(prn, hc, LINELEN);
-
-            lit[idx % CHARS_PER_LINE] = print_char[c];
-        }
-        while (--rc > 0 && (++idx % CHARS_PER_LINE != 0));
-
-        line_done = (idx % CHARS_PER_LINE) == 0;
-        if (line_done)
-        {
-            printf("%s  %s\r\n", prn, lit);
-        }
+        printf("%s\n", prompt);
     }
 
-    if (!line_done)
+    for (i = 0; i < length; i += 16)
     {
-        int ldx = idx % CHARS_PER_LINE;
-        lit[ldx++] = print_char[(int)buf[idx]];
-        lit[ldx] = '\0';
+        printf("%08zx: ", i);
 
-        while ((++idx % CHARS_PER_LINE) != 0)
-            strncat(prn, "   ", sizeof(prn)-strlen(prn));
+        for (j = 0; j < 16; j++)
+        {
+            if (i + j < length)
+            {
+                printf("%02x ", buffer[i + j]);
+            }
+            else
+            {
+                printf("   ");
+            }
+        }
 
-        printf("%s  %s\r\n", prn, lit);
+        printf(" ");
 
+        for (j = 0; j < 16; j++)
+        {
+            if (i + j < length)
+            {
+                unsigned char c = buffer[i + j];
+                printf("%c", (c >= 32 && c <= 126) ? c : '.');
+            }
+            else
+            {
+                printf(" ");
+            }
+        }
+
+        printf("\n");
     }
 }
diff --git a/modules/leds.c b/modules/leds.c
index 6f68356..8c3c7b0 100644
--- a/modules/leds.c
+++ b/modules/leds.c
@@ -13,6 +13,12 @@
  *                   B        <----->      #Pin37(BCM GPIO26)
  *                  GND       <----->      GND
  *
+ *               RGB Led Module           Raspberry Pi Board
+ *                   R        <----->      #Pin36(BCM GPIO16)
+ *                   G        <----->      #Pin38(BCM GPIO20)
+ *                   B        <----->      #Pin40(BCM GPIO21)
+ *                  GND       <----->      GND
+ *
  * System install:
  *                  sudo apt install -y libgpiod-dev gpiod
  *
@@ -54,11 +60,20 @@
     struct gpiod_line  *line;  /* libgpiod line */
 } led_info_t;
 
+#define CONFIG_PIN_333537
+//#define CONFIG_PIN_363840
+
 static led_info_t    leds_info[LEDCNT] =
 {
+#ifdef CONFIG_PIN_333537
     {"red",   13, 1, NULL },
     {"green", 19, 1, NULL },
     {"blue",  26, 1, NULL },
+#elif (defined CONFIG_PIN_363840)
+    {"red",   16, 1, NULL },
+    {"green", 20, 1, NULL },
+    {"blue",  21, 1, NULL },
+#endif
 };
 
 /* Three LEDs API context */
diff --git a/modules/pwm.c b/modules/pwm.c
index 6c8f5d8..542971b 100644
--- a/modules/pwm.c
+++ b/modules/pwm.c
@@ -195,7 +195,7 @@
 }
 
 /* configure PWM $channel */
-int config_pwm(int channel, int freq, int duty) 
+int config_pwm(int channel, int freq, int duty)
 {
 	int           fd;
 	char          buf[32];
@@ -240,7 +240,7 @@
 	if( (rv=export_pwm(channel, 1)) )
 	{
 		printf("export PWM channel[%d] failed, rv=%d\n", channel, rv);
-		return rv; 
+		return rv;
 	}
 
 	if( (rv=config_pwm(channel, freq, duty)) )
diff --git a/modules/sht20.c b/modules/sht20.c
index 477adc6..f549369 100644
--- a/modules/sht20.c
+++ b/modules/sht20.c
@@ -73,7 +73,7 @@
 
 static inline void msleep(unsigned long ms);
 void print_buf(const char *prompt, uint8_t *buf, int size);
-void dump_buf(const char *prompt, char *buf, size_t len);
+void dump_buf(const char *prompt, char *buffer, size_t length);
 
 static inline void banner(const char *progname)
 {
@@ -469,74 +469,47 @@
     return ;
 }
 
-#define LINELEN 81
-#define CHARS_PER_LINE 16
-static char *print_char =
-"                "
-"                "
-" !\"#$%&'()*+,-./"
-"0123456789:;<=>?"
-"@ABCDEFGHIJKLMNO"
-"PQRSTUVWXYZ[\\]^_"
-"`abcdefghijklmno"
-"pqrstuvwxyz{|}~ "
-"                "
-"                "
-" ???????????????"
-"????????????????"
-"????????????????"
-"????????????????"
-"????????????????"
-"????????????????";
-
-void dump_buf(const char *prompt, char *buf, size_t len)
+void dump_buf(const char *prompt, char *buffer, size_t length)
 {
-    int rc;
-    int idx;
-    char prn[LINELEN];
-    char lit[CHARS_PER_LINE + 2];
-    char hc[4];
-    short line_done = 1;
+    size_t i, j;
 
-    if( prompt )
-        printf("%s", prompt);
-
-    rc = len;
-    idx = 0;
-    lit[CHARS_PER_LINE] = '\0';
-
-    while (rc > 0)
+    if (prompt)
     {
-        if (line_done)
-            snprintf(prn, LINELEN, "%08X: ", idx);
-
-        do
-        {
-            unsigned char c = buf[idx];
-            snprintf(hc, 4, "%02X ", c);
-            strncat(prn, hc, LINELEN);
-
-            lit[idx % CHARS_PER_LINE] = print_char[c];
-        }
-        while (--rc > 0 && (++idx % CHARS_PER_LINE != 0));
-
-        line_done = (idx % CHARS_PER_LINE) == 0;
-        if (line_done)
-        {
-            printf("%s  %s\r\n", prn, lit);
-        }
+        printf("%s\n", prompt);
     }
 
-    if (!line_done)
+    for (i = 0; i < length; i += 16)
     {
-        int ldx = idx % CHARS_PER_LINE;
-        lit[ldx++] = print_char[(int)buf[idx]];
-        lit[ldx] = '\0';
+        printf("%08zx: ", i);
 
-        while ((++idx % CHARS_PER_LINE) != 0)
-            strncat(prn, "   ", sizeof(prn)-strlen(prn));
+        for (j = 0; j < 16; j++)
+        {
+            if (i + j < length)
+            {
+                printf("%02x ", buffer[i + j]);
+            }
+            else
+            {
+                printf("   ");
+            }
+        }
 
-        printf("%s  %s\r\n", prn, lit);
+        printf(" ");
 
+        for (j = 0; j < 16; j++)
+        {
+            if (i + j < length)
+            {
+                unsigned char c = buffer[i + j];
+                printf("%c", (c >= 32 && c <= 126) ? c : '.');
+            }
+            else
+            {
+                printf(" ");
+            }
+        }
+
+        printf("\n");
     }
 }
+
diff --git a/modules/w25qflash.c b/modules/w25qflash.c
index 449a9c7..db20abd 100644
--- a/modules/w25qflash.c
+++ b/modules/w25qflash.c
@@ -55,7 +55,7 @@
  *|   Entry Functions     |
  *+-----------------------+*/
 
-void dump_buf(const char *prompt, char *buf, size_t len);
+void dump_buf(const char *prompt, char *buffer, size_t length);
 
 int main (int argc, char **argv)
 {
@@ -781,74 +781,47 @@
     return ;
 }
 
-#define LINELEN 81
-#define CHARS_PER_LINE 16
-static char *print_char =
-"                "
-"                "
-" !\"#$%&'()*+,-./"
-"0123456789:;<=>?"
-"@ABCDEFGHIJKLMNO"
-"PQRSTUVWXYZ[\\]^_"
-"`abcdefghijklmno"
-"pqrstuvwxyz{|}~ "
-"                "
-"                "
-" ???????????????"
-"????????????????"
-"????????????????"
-"????????????????"
-"????????????????"
-"????????????????";
-
-void dump_buf(const char *prompt, char *buf, size_t len)
+void dump_buf(const char *prompt, char *buffer, size_t length)
 {
-    int rc;
-    int idx;
-    char prn[LINELEN];
-    char lit[CHARS_PER_LINE + 2];
-    char hc[4];
-    short line_done = 1;
+    size_t i, j;
 
-    if( prompt )
-        printf("%s", prompt);
-
-    rc = len;
-    idx = 0;
-    lit[CHARS_PER_LINE] = '\0';
-
-    while (rc > 0)
+    if (prompt)
     {
-        if (line_done)
-            snprintf(prn, LINELEN, "%08X: ", idx);
-
-        do
-        {
-            unsigned char c = buf[idx];
-            snprintf(hc, 4, "%02X ", c);
-            strncat(prn, hc, LINELEN);
-
-            lit[idx % CHARS_PER_LINE] = print_char[c];
-        }
-        while (--rc > 0 && (++idx % CHARS_PER_LINE != 0));
-
-        line_done = (idx % CHARS_PER_LINE) == 0;
-        if (line_done)
-        {
-            printf("%s  %s\r\n", prn, lit);
-        }
+        printf("%s\n", prompt);
     }
 
-    if (!line_done)
+    for (i = 0; i < length; i += 16)
     {
-        int ldx = idx % CHARS_PER_LINE;
-        lit[ldx++] = print_char[(int)buf[idx]];
-        lit[ldx] = '\0';
+        printf("%08zx: ", i);
 
-        while ((++idx % CHARS_PER_LINE) != 0)
-            strncat(prn, "   ", sizeof(prn)-strlen(prn));
+        for (j = 0; j < 16; j++)
+        {
+            if (i + j < length)
+            {
+                printf("%02x ", buffer[i + j]);
+            }
+            else
+            {
+                printf("   ");
+            }
+        }
 
-        printf("%s  %s\r\n", prn, lit);
+        printf(" ");
 
+        for (j = 0; j < 16; j++)
+        {
+            if (i + j < length)
+            {
+                unsigned char c = buffer[i + j];
+                printf("%c", (c >= 32 && c <= 126) ? c : '.');
+            }
+            else
+            {
+                printf(" ");
+            }
+        }
+
+        printf("\n");
     }
 }
+

--
Gitblit v1.9.1