Skip to content

Commit 9a37357

Browse files
committed
samples: drivers: Add auxdisplay_digits sample
This commit introduces a new sample for the Auxiliary display driver. The sample demonstrates counting from 0 to 1000, with an interval of 100ms between each increment. This sample primarily serves to demonstrate the capabilities of segment displays. Signed-off-by: Chen Xingyu <[email protected]>
1 parent f06548b commit 9a37357

File tree

5 files changed

+166
-0
lines changed

5 files changed

+166
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr HINTS $ENV{ZEPHYR_BASE})
6+
project(auxdisplay_digits)
7+
8+
target_sources(app PRIVATE src/main.c)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2024 Chen Xingyu <[email protected]>
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include <zephyr/dt-bindings/gpio/gpio.h>
7+
8+
/ {
9+
auxdisplay_0: digi-display {
10+
compatible = "gpio-7-segment";
11+
columns = <3>;
12+
rows = <1>;
13+
segment-gpios = <&gpio0 5 GPIO_ACTIVE_LOW>, /* A */
14+
<&gpio0 9 GPIO_ACTIVE_LOW>, /* B */
15+
<&gpio0 3 GPIO_ACTIVE_LOW>, /* C */
16+
<&gpio0 1 GPIO_ACTIVE_LOW>, /* D */
17+
<&gpio0 10 GPIO_ACTIVE_LOW>, /* E */
18+
<&gpio0 6 GPIO_ACTIVE_LOW>, /* F */
19+
<&gpio0 2 GPIO_ACTIVE_LOW>, /* G */
20+
<&gpio0 0 GPIO_ACTIVE_LOW>; /* DP */
21+
digit-gpios = <&gpio0 4 GPIO_ACTIVE_HIGH>, /* DIG1 */
22+
<&gpio0 7 GPIO_ACTIVE_HIGH>, /* DIG2 */
23+
<&gpio0 8 GPIO_ACTIVE_HIGH>; /* DIG3 */
24+
refresh-period-ms = <1>;
25+
};
26+
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CONFIG_AUXDISPLAY=y
2+
CONFIG_LOG=y
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
sample:
2+
description: Demonstration of auxdisplay driver for segment displays
3+
name: Auxiliary (segment) display sample
4+
tests:
5+
sample.drivers.auxdisplay_digits:
6+
tags: auxdisplay
7+
harness_config:
8+
fixture: fixture_auxdisplay
9+
platform_allow: esp32c3_devkitm
10+
integration_platforms:
11+
- esp32c3_devkitm
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Copyright (c) 2023 Jamie McCrae
3+
* Copyright (c) 2024 Chen Xingyu <[email protected]>
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
#include <math.h>
9+
#include <string.h>
10+
#include <zephyr/kernel.h>
11+
#include <zephyr/device.h>
12+
#include <zephyr/drivers/auxdisplay.h>
13+
#include <zephyr/logging/log.h>
14+
15+
LOG_MODULE_REGISTER(auxdisplay_sample, LOG_LEVEL_DBG);
16+
17+
#define AUXDISPLAY_NODE DT_NODELABEL(auxdisplay_0)
18+
#define AUXDISPLAY_DIGIT_COUNT DT_PROP_LEN(AUXDISPLAY_NODE, digit_gpios)
19+
20+
static const struct device *const dev = DEVICE_DT_GET(AUXDISPLAY_NODE);
21+
static uint8_t data[AUXDISPLAY_DIGIT_COUNT * 2];
22+
23+
int main(void)
24+
{
25+
int rc;
26+
int i, len;
27+
28+
if (!device_is_ready(dev)) {
29+
LOG_ERR("Auxdisplay device is not ready.");
30+
return -ENODEV;
31+
}
32+
33+
/* Light up all segments */
34+
for (i = 0; i < AUXDISPLAY_DIGIT_COUNT; i++) {
35+
data[i * 2] = '8';
36+
data[i * 2 + 1] = '.';
37+
}
38+
rc = auxdisplay_write(dev, data, strlen(data));
39+
if (rc != 0) {
40+
LOG_ERR("Failed to write data: %d", rc);
41+
return rc;
42+
}
43+
44+
k_msleep(500);
45+
46+
/* Blinks it once */
47+
48+
rc = auxdisplay_display_off(dev);
49+
if (rc != 0) {
50+
LOG_ERR("Failed to turn display off: %d", rc);
51+
return rc;
52+
}
53+
54+
k_msleep(500);
55+
56+
rc = auxdisplay_display_on(dev);
57+
if (rc != 0) {
58+
LOG_ERR("Failed to turn display on: %d", rc);
59+
return rc;
60+
}
61+
62+
k_msleep(500);
63+
64+
/* Clear the display */
65+
66+
rc = auxdisplay_clear(dev);
67+
if (rc != 0) {
68+
LOG_ERR("Failed to clear display: %d", rc);
69+
return rc;
70+
}
71+
72+
k_msleep(500);
73+
74+
/* Test cursor movement by filling each digit with a number */
75+
76+
for (i = 0; i < AUXDISPLAY_DIGIT_COUNT; i++) {
77+
snprintf(data, sizeof(data), "%d", i);
78+
rc = auxdisplay_write(dev, data, strlen(data));
79+
if (rc != 0) {
80+
LOG_ERR("Failed to write data: %d", rc);
81+
return rc;
82+
}
83+
84+
k_msleep(500);
85+
}
86+
87+
k_msleep(500);
88+
89+
/* Count from 0 up to fill all digits */
90+
91+
for (i = 0;; i = (i + 1) % (int)pow(10, AUXDISPLAY_DIGIT_COUNT)) {
92+
snprintk(data, sizeof(data), "%d", i);
93+
len = strlen(data);
94+
95+
rc = auxdisplay_clear(dev);
96+
if (rc != 0) {
97+
LOG_ERR("Failed to clear display: %d", rc);
98+
return rc;
99+
}
100+
101+
/* Right-align the number */
102+
rc = auxdisplay_cursor_position_set(dev, AUXDISPLAY_POSITION_ABSOLUTE,
103+
AUXDISPLAY_DIGIT_COUNT - len, 0);
104+
if (rc != 0) {
105+
LOG_ERR("Failed to set cursor position: %d", rc);
106+
return rc;
107+
}
108+
109+
rc = auxdisplay_write(dev, data, len);
110+
if (rc != 0) {
111+
LOG_ERR("Failed to write data: %d", rc);
112+
return rc;
113+
}
114+
115+
k_msleep(100);
116+
}
117+
118+
return 0;
119+
}

0 commit comments

Comments
 (0)