Skip to content

Commit dc4e259

Browse files
xingrzkartben
authored andcommitted
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 <hi@xingrz.me>
1 parent 5be38c6 commit dc4e259

File tree

7 files changed

+205
-0
lines changed

7 files changed

+205
-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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
.. zephyr:code-sample:: auxdisplay_digits
2+
:name: Auxiliary digits display
3+
:relevant-api: auxdisplay_interface
4+
5+
Output increasing numbers to an auxiliary display.
6+
7+
Overview
8+
********
9+
10+
This sample demonstrates the use of the
11+
:ref:`auxiliary display driver <auxdisplay_api>` for digit-based displays, such
12+
as 7-segment displays.
13+
14+
Building and Running
15+
********************
16+
17+
Note that this sample requires a board with a 7-segment display setup. You can
18+
build your own setup by fly-wiring a 7-segment display to any board you have.
19+
20+
A sample overlay is provided for the ``native_sim`` target. See the overlay file
21+
:zephyr_file:`samples/drivers/auxdisplay_digits/boards/native_sim.overlay` for a
22+
demonstration.
23+
24+
.. zephyr-app-commands::
25+
:zephyr-app: samples/drivers/auxdisplay_digits
26+
:host-os: unix
27+
:board: native_sim
28+
:goals: build
29+
:compact:
30+
31+
If successful, the display first lights up all segments (e.g., 8.8.8. on a
32+
3-digit display), blinks once, sequentially lights up each digit from left to
33+
right, and then counts up from 0 to the maximum number that can be displayed.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_GPIO=y
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2024-2025 Chen Xingyu <hi@xingrz.me>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
&gpio0 {
8+
ngpios = <11>;
9+
};
10+
11+
/ {
12+
auxdisplay_0: auxdisplay {
13+
compatible = "gpio-7-segment";
14+
status = "okay";
15+
columns = <3>;
16+
rows = <1>;
17+
segment-gpios = <&gpio0 0 0>, /* A */
18+
<&gpio0 1 0>, /* B */
19+
<&gpio0 2 0>, /* C */
20+
<&gpio0 3 0>, /* D */
21+
<&gpio0 4 0>, /* E */
22+
<&gpio0 5 0>, /* F */
23+
<&gpio0 6 0>, /* G */
24+
<&gpio0 7 0>; /* DP */
25+
digit-gpios = <&gpio0 8 0>, /* DIG1 */
26+
<&gpio0 9 0>, /* DIG2 */
27+
<&gpio0 10 0>; /* DIG3 */
28+
refresh-period-ms = <1>;
29+
};
30+
};
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+
platform_allow:
8+
- native_sim
9+
integration_platforms:
10+
- native_sim
11+
build_only: true
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright (c) 2023 Jamie McCrae
3+
* Copyright (c) 2024-2025 Chen Xingyu <hi@xingrz.me>
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
#include <math.h>
9+
#include <stdio.h>
10+
#include <string.h>
11+
#include <zephyr/kernel.h>
12+
#include <zephyr/device.h>
13+
#include <zephyr/drivers/auxdisplay.h>
14+
#include <zephyr/logging/log.h>
15+
16+
LOG_MODULE_REGISTER(auxdisplay_sample, LOG_LEVEL_DBG);
17+
18+
#define AUXDISPLAY_NODE DT_NODELABEL(auxdisplay_0)
19+
#define AUXDISPLAY_DIGIT_COUNT DT_PROP_LEN(AUXDISPLAY_NODE, digit_gpios)
20+
21+
static const struct device *const dev = DEVICE_DT_GET(AUXDISPLAY_NODE);
22+
static uint8_t data[AUXDISPLAY_DIGIT_COUNT * 2];
23+
24+
int main(void)
25+
{
26+
int rc;
27+
int i, len;
28+
29+
if (!device_is_ready(dev)) {
30+
LOG_ERR("Auxdisplay device is not ready.");
31+
return -ENODEV;
32+
}
33+
34+
/* Light up all segments */
35+
for (i = 0; i < AUXDISPLAY_DIGIT_COUNT; i++) {
36+
data[i * 2] = '8';
37+
data[i * 2 + 1] = '.';
38+
}
39+
rc = auxdisplay_write(dev, data, strlen(data));
40+
if (rc != 0) {
41+
LOG_ERR("Failed to write data: %d", rc);
42+
return rc;
43+
}
44+
45+
k_msleep(500);
46+
47+
/* Blinks it once */
48+
49+
rc = auxdisplay_display_off(dev);
50+
if (rc != 0) {
51+
LOG_ERR("Failed to turn display off: %d", rc);
52+
return rc;
53+
}
54+
55+
k_msleep(500);
56+
57+
rc = auxdisplay_display_on(dev);
58+
if (rc != 0) {
59+
LOG_ERR("Failed to turn display on: %d", rc);
60+
return rc;
61+
}
62+
63+
k_msleep(500);
64+
65+
/* Clear the display */
66+
67+
rc = auxdisplay_clear(dev);
68+
if (rc != 0) {
69+
LOG_ERR("Failed to clear display: %d", rc);
70+
return rc;
71+
}
72+
73+
k_msleep(500);
74+
75+
/* Test cursor movement by filling each digit with a number */
76+
77+
for (i = 0; i < AUXDISPLAY_DIGIT_COUNT; i++) {
78+
snprintf(data, sizeof(data), "%d", i);
79+
rc = auxdisplay_write(dev, data, strlen(data));
80+
if (rc != 0) {
81+
LOG_ERR("Failed to write data: %d", rc);
82+
return rc;
83+
}
84+
85+
k_msleep(500);
86+
}
87+
88+
k_msleep(500);
89+
90+
/* Count from 0 up to fill all digits */
91+
92+
for (i = 0;; i = (i + 1) % (int)pow(10, AUXDISPLAY_DIGIT_COUNT)) {
93+
snprintk(data, sizeof(data), "%d", i);
94+
len = strlen(data);
95+
96+
rc = auxdisplay_clear(dev);
97+
if (rc != 0) {
98+
LOG_ERR("Failed to clear display: %d", rc);
99+
return rc;
100+
}
101+
102+
/* Right-align the number */
103+
rc = auxdisplay_cursor_position_set(dev, AUXDISPLAY_POSITION_ABSOLUTE,
104+
AUXDISPLAY_DIGIT_COUNT - len, 0);
105+
if (rc != 0) {
106+
LOG_ERR("Failed to set cursor position: %d", rc);
107+
return rc;
108+
}
109+
110+
rc = auxdisplay_write(dev, data, len);
111+
if (rc != 0) {
112+
LOG_ERR("Failed to write data: %d", rc);
113+
return rc;
114+
}
115+
116+
k_msleep(100);
117+
}
118+
119+
return 0;
120+
}

0 commit comments

Comments
 (0)