init - display test

new idf project (with  idf.py create-project)
add 2 components for encoder and display:
    https://github.com/DavidAntliff/esp32-rotary-encoder
    https://github.com/UncleRus/esp-idf-lib/tree/master/components/max7219
add code for testing a 7 segment display
This commit is contained in:
jonny_ji7
2022-08-06 09:26:27 +02:00
commit 307cad4a9a
21 changed files with 5819 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
---
components:
- name: max7219
description: Driver for 8-Digit LED display drivers, MAX7219/MAX7221
group: led
groups: []
code_owners: UncleRus
depends:
- driver
- log
thread_safe: yes
targets:
- name: esp32
- name: esp32s2
- name: esp32c3
licenses:
- name: BSD-3
copyrights:
- author:
name: UncleRus
year: 2019

View File

@@ -0,0 +1,5 @@
idf_component_register(
SRCS max7219.c
INCLUDE_DIRS .
REQUIRES driver log
)

View File

@@ -0,0 +1,26 @@
Copyright (c) 2017, 2018 Ruslan V. Uss <unclerus@gmail.com>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of itscontributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@@ -0,0 +1,2 @@
COMPONENT_ADD_INCLUDEDIRS = .
COMPONENT_DEPENDS = driver log

View File

@@ -0,0 +1,263 @@
/*
* Copyright (c) 2019 Ruslan V. Uss <unclerus@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of itscontributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @file max7219.c
*
* ESP-IDF driver for MAX7219/MAX7221
* Serially Interfaced, 8-Digit LED Display Drivers
*
* Ported from esp-open-rtos
*
* Copyright (c) 2017 Ruslan V. Uss <unclerus@gmail.com>
*
* BSD Licensed as described in the file LICENSE
*/
#include "max7219.h"
#include <string.h>
#include <esp_log.h>
#include "max7219_priv.h"
static const char *TAG = "max7219";
#define ALL_CHIPS 0xff
#define ALL_DIGITS 8
#define REG_DIGIT_0 (1 << 8)
#define REG_DECODE_MODE (9 << 8)
#define REG_INTENSITY (10 << 8)
#define REG_SCAN_LIMIT (11 << 8)
#define REG_SHUTDOWN (12 << 8)
#define REG_DISPLAY_TEST (15 << 8)
#define VAL_CLEAR_BCD 0x0f
#define VAL_CLEAR_NORMAL 0x00
#define CHECK(x) do { esp_err_t __; if ((__ = x) != ESP_OK) return __; } while (0)
#define CHECK_ARG(VAL) do { if (!(VAL)) return ESP_ERR_INVALID_ARG; } while (0)
static inline uint16_t shuffle(uint16_t val)
{
return (val >> 8) | (val << 8);
}
static esp_err_t send(max7219_t *dev, uint8_t chip, uint16_t value)
{
uint16_t buf[MAX7219_MAX_CASCADE_SIZE] = { 0 };
if (chip == ALL_CHIPS)
{
for (uint8_t i = 0; i < dev->cascade_size; i++)
buf[i] = shuffle(value);
}
else buf[chip] = shuffle(value);
spi_transaction_t t;
memset(&t, 0, sizeof(t));
t.length = dev->cascade_size * 16;
t.tx_buffer = buf;
return spi_device_transmit(dev->spi_dev, &t);
}
inline static uint8_t get_char(max7219_t *dev, char c)
{
if (dev->bcd)
{
if (c >= '0' && c <= '9')
return c - '0';
switch (c)
{
case '-':
return 0x0a;
case 'E':
case 'e':
return 0x0b;
case 'H':
case 'h':
return 0x0c;
case 'L':
case 'l':
return 0x0d;
case 'P':
case 'p':
return 0x0e;
}
return VAL_CLEAR_BCD;
}
return font_7seg[(c - 0x20) & 0x7f];
}
///////////////////////////////////////////////////////////////////////////////
esp_err_t max7219_init_desc(max7219_t *dev, spi_host_device_t host, uint32_t clock_speed_hz, gpio_num_t cs_pin)
{
CHECK_ARG(dev);
memset(&dev->spi_cfg, 0, sizeof(dev->spi_cfg));
dev->spi_cfg.spics_io_num = cs_pin;
dev->spi_cfg.clock_speed_hz = clock_speed_hz;
dev->spi_cfg.mode = 0;
dev->spi_cfg.queue_size = 1;
dev->spi_cfg.flags = SPI_DEVICE_NO_DUMMY;
return spi_bus_add_device(host, &dev->spi_cfg, &dev->spi_dev);
}
esp_err_t max7219_free_desc(max7219_t *dev)
{
CHECK_ARG(dev);
return spi_bus_remove_device(dev->spi_dev);
}
esp_err_t max7219_init(max7219_t *dev)
{
CHECK_ARG(dev);
if (!dev->cascade_size || dev->cascade_size > MAX7219_MAX_CASCADE_SIZE)
{
ESP_LOGE(TAG, "Invalid cascade size %d", dev->cascade_size);
return ESP_ERR_INVALID_ARG;
}
uint8_t max_digits = dev->cascade_size * ALL_DIGITS;
if (dev->digits > max_digits)
{
ESP_LOGE(TAG, "Invalid digits count %d, max %d", dev->digits, max_digits);
return ESP_ERR_INVALID_ARG;
}
if (!dev->digits)
dev->digits = max_digits;
// Shutdown all chips
CHECK(max7219_set_shutdown_mode(dev, true));
// Disable test
CHECK(send(dev, ALL_CHIPS, REG_DISPLAY_TEST));
// Set max scan limit
CHECK(send(dev, ALL_CHIPS, REG_SCAN_LIMIT | (ALL_DIGITS - 1)));
// Set normal decode mode & clear display
CHECK(max7219_set_decode_mode(dev, false));
// Set minimal brightness
CHECK(max7219_set_brightness(dev, 0));
// Wake up
CHECK(max7219_set_shutdown_mode(dev, false));
return ESP_OK;
}
esp_err_t max7219_set_decode_mode(max7219_t *dev, bool bcd)
{
CHECK_ARG(dev);
dev->bcd = bcd;
CHECK(send(dev, ALL_CHIPS, REG_DECODE_MODE | (bcd ? 0xff : 0)));
CHECK(max7219_clear(dev));
return ESP_OK;
}
esp_err_t max7219_set_brightness(max7219_t *dev, uint8_t value)
{
CHECK_ARG(dev);
CHECK_ARG(value <= MAX7219_MAX_BRIGHTNESS);
CHECK(send(dev, ALL_CHIPS, REG_INTENSITY | value));
return ESP_OK;
}
esp_err_t max7219_set_shutdown_mode(max7219_t *dev, bool shutdown)
{
CHECK_ARG(dev);
CHECK(send(dev, ALL_CHIPS, REG_SHUTDOWN | !shutdown));
return ESP_OK;
}
esp_err_t max7219_set_digit(max7219_t *dev, uint8_t digit, uint8_t val)
{
CHECK_ARG(dev);
if (digit >= dev->digits)
{
ESP_LOGE(TAG, "Invalid digit: %d", digit);
return ESP_ERR_INVALID_ARG;
}
if (dev->mirrored)
digit = dev->digits - digit - 1;
uint8_t c = digit / ALL_DIGITS;
uint8_t d = digit % ALL_DIGITS;
ESP_LOGV(TAG, "Chip %d, digit %d val 0x%02x", c, d, val);
CHECK(send(dev, c, (REG_DIGIT_0 + ((uint16_t)d << 8)) | val));
return ESP_OK;
}
esp_err_t max7219_clear(max7219_t *dev)
{
CHECK_ARG(dev);
uint8_t val = dev->bcd ? VAL_CLEAR_BCD : VAL_CLEAR_NORMAL;
for (uint8_t i = 0; i < ALL_DIGITS; i++)
CHECK(send(dev, ALL_CHIPS, (REG_DIGIT_0 + ((uint16_t)i << 8)) | val));
return ESP_OK;
}
esp_err_t max7219_draw_text_7seg(max7219_t *dev, uint8_t pos, const char *s)
{
CHECK_ARG(dev && s);
while (s && pos < dev->digits)
{
uint8_t c = get_char(dev, *s);
if (*(s + 1) == '.')
{
c |= 0x80;
s++;
}
CHECK(max7219_set_digit(dev, pos, c));
pos++;
s++;
}
return ESP_OK;
}
esp_err_t max7219_draw_image_8x8(max7219_t *dev, uint8_t pos, const void *image)
{
CHECK_ARG(dev && image);
for (uint8_t i = pos, offs = 0; i < dev->digits && offs < 8; i++, offs++)
max7219_set_digit(dev, i, *((uint8_t *)image + offs));
return ESP_OK;
}

View File

@@ -0,0 +1,174 @@
/*
* Copyright (c) 2019 Ruslan V. Uss <unclerus@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of itscontributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @file max7219.h
* @defgroup max7219 max7219
* @{
*
* ESP-IDF driver for MAX7219/MAX7221
* Serially Interfaced, 8-Digit LED Display Drivers
*
* Ported from esp-open-rtos
*
* Copyright (c) 2017 Ruslan V. Uss <unclerus@gmail.com>
*
* BSD Licensed as described in the file LICENSE
*/
#ifndef __MAX7219_H__
#define __MAX7219_H__
#include <stdint.h>
#include <stdbool.h>
#include <driver/spi_master.h>
#include <driver/gpio.h> // add by nopnop2002
#include <esp_err.h>
#ifdef __cplusplus
extern "C" {
#endif
#define MAX7219_MAX_CLOCK_SPEED_HZ (10000000) // 10 MHz
#define MAX7219_MAX_CASCADE_SIZE 8
#define MAX7219_MAX_BRIGHTNESS 15
/**
* Display descriptor
*/
typedef struct
{
spi_device_interface_config_t spi_cfg;
spi_device_handle_t spi_dev;
uint8_t digits; //!< Accessible digits in 7seg. Up to cascade_size * 8
uint8_t cascade_size; //!< Up to `MAX7219_MAX_CASCADE_SIZE` MAX721xx cascaded
bool mirrored; //!< true for horizontally mirrored displays
bool bcd;
} max7219_t;
/**
* @brief Initialize device descriptor
*
* @param dev Device descriptor
* @param host SPI host
* @param clock_speed_hz SPI clock speed, Hz
* @param cs_pin CS GPIO number
* @return `ESP_OK` on success
*/
esp_err_t max7219_init_desc(max7219_t *dev, spi_host_device_t host, uint32_t clock_speed_hz, gpio_num_t cs_pin);
/**
* @brief Free device descriptor
*
* @param dev Device descriptor
* @return `ESP_OK` on success
*/
esp_err_t max7219_free_desc(max7219_t *dev);
/**
* @brief Initialize display
*
* Switch it to normal operation from shutdown mode,
* set scan limit to the max and clear
*
* @param dev Display descriptor
* @return `ESP_OK` on success
*/
esp_err_t max7219_init(max7219_t *dev);
/**
* @brief Set decode mode and clear display
*
* @param dev Display descriptor
* @param bcd true to set BCD decode mode, false to normal
* @return `ESP_OK` on success
*/
esp_err_t max7219_set_decode_mode(max7219_t *dev, bool bcd);
/**
* @brief Set display brightness
*
* @param dev Display descriptor
* @param value Brightness value, 0..MAX7219_MAX_BRIGHTNESS
* @return `ESP_OK` on success
*/
esp_err_t max7219_set_brightness(max7219_t *dev, uint8_t value);
/**
* @brief Shutdown display or set it to normal mode
*
* @param dev Display descriptor
* @param shutdown Shutdown display if true
* @return `ESP_OK` on success
*/
esp_err_t max7219_set_shutdown_mode(max7219_t *dev, bool shutdown);
/**
* @brief Write data to display digit
*
* @param dev Display descriptor
* @param digit Digit index, 0..dev->digits - 1
* @param val Data
* @return `ESP_OK` on success
*/
esp_err_t max7219_set_digit(max7219_t *dev, uint8_t digit, uint8_t val);
/**
* @brief Clear display
*
* @param dev Display descriptor
* @return `ESP_OK` on success
*/
esp_err_t max7219_clear(max7219_t *dev);
/**
* @brief Draw text on 7-segment display
*
* @param dev Display descriptor
* @param pos Start digit
* @param s Text
* @return `ESP_OK` on success
*/
esp_err_t max7219_draw_text_7seg(max7219_t *dev, uint8_t pos, const char *s);
/**
* @brief Draw 64-bit image on 8x8 matrix
*
* @param dev Display descriptor
* @param pos Start digit
* @param image 64-bit buffer with image data
* @return `ESP_OK` on success
*/
esp_err_t max7219_draw_image_8x8(max7219_t *dev, uint8_t pos, const void *image);
#ifdef __cplusplus
}
#endif
/**@}*/
#endif /* __MAX7219_H__ */

View File

@@ -0,0 +1,67 @@
/*
* Copyright (c) 2019 Ruslan V. Uss <unclerus@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of itscontributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @file max7219_priv.h
*
* ESP-IDF driver for MAX7219/MAX7221
* Serially Interfaced, 8-Digit LED Display Drivers
*
* Ported from esp-open-rtos
*
* Copyright (c) 2017, 2018 Ruslan V. Uss <unclerus@gmail.com>
*
* BSD Licensed as described in the file LICENSE
*/
#ifndef __MAX7219_PRIV_H__
#define __MAX7219_PRIV_H__
static const uint8_t font_7seg[] = {
/* ' ' ! " # $ % & ' ( ) */
0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x00, 0x02, 0x4e, 0x78,
/* * + , - . / 0 1 2 3 */
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x7e, 0x30, 0x6d, 0x79,
/* 4 5 6 7 8 9 : ; < = */
0x33, 0x5b, 0x5f, 0x70, 0x7f, 0x7b, 0x00, 0x00, 0x0d, 0x09,
/* > ? @ A B C D E F G */
0x19, 0x65, 0x00, 0x77, 0x1f, 0x4e, 0x3d, 0x4f, 0x47, 0x5e,
/* H I J K L M N O P Q */
0x37, 0x06, 0x38, 0x57, 0x0e, 0x76, 0x15, 0x1d, 0x67, 0x73,
/* R S T U V W X Y Z [ */
0x05, 0x5b, 0x0f, 0x1c, 0x3e, 0x2a, 0x49, 0x3b, 0x6d, 0x4e,
/* \ ] ^ _ ` a b c d e */
0x00, 0x78, 0x00, 0x08, 0x02, 0x77, 0x1f, 0x4e, 0x3d, 0x4f,
/* f g h i j k l m n o */
0x47, 0x5e, 0x37, 0x06, 0x38, 0x57, 0x0e, 0x76, 0x15, 0x1d,
/* p q r s t u v w x y */
0x67, 0x73, 0x05, 0x5b, 0x0f, 0x1c, 0x3e, 0x2a, 0x49, 0x3b,
/* z { | } ~ */
0x6d, 0x4e, 0x06, 0x78, 0x00
};
#endif /* __MAX7219_PRIV_H__ */