浏览代码

add jpfont_2_lcd

Satoshi Yoneda 3 周之前
父节点
当前提交
972006f390

+ 2 - 0
jpfont_2_lcd/.gitignore

@@ -0,0 +1,2 @@
+build
+!.vscode/*

+ 22 - 0
jpfont_2_lcd/.vscode/c_cpp_properties.json

@@ -0,0 +1,22 @@
+{
+    "configurations": [
+        {
+            "name": "Pico",
+            "includePath": [
+                "${workspaceFolder}/**",
+                "${userHome}/.pico-sdk/sdk/2.2.0/**"
+            ],
+            "forcedInclude": [
+                "${userHome}/.pico-sdk/sdk/2.2.0/src/common/pico_base_headers/include/pico.h",
+                "${workspaceFolder}/build/generated/pico_base/pico/config_autogen.h"
+            ],
+            "defines": [],
+            "compilerPath": "${userHome}/.pico-sdk/toolchain/14_2_Rel1/bin/arm-none-eabi-gcc.exe",
+            "compileCommands": "${workspaceFolder}/build/compile_commands.json",
+            "cStandard": "c17",
+            "cppStandard": "c++14",
+            "intelliSenseMode": "linux-gcc-arm"
+        }
+    ],
+    "version": 4
+}

+ 15 - 0
jpfont_2_lcd/.vscode/cmake-kits.json

@@ -0,0 +1,15 @@
+[
+    {
+        "name": "Pico",
+        "compilers": {
+            "C": "${command:raspberry-pi-pico.getCompilerPath}",
+            "CXX": "${command:raspberry-pi-pico.getCxxCompilerPath}"
+        },
+        "environmentVariables": {
+            "PATH": "${command:raspberry-pi-pico.getEnvPath};${env:PATH}"
+        },
+        "cmakeSettings": {
+            "Python3_EXECUTABLE": "${command:raspberry-pi-pico.getPythonPath}"
+        }
+    }
+]

+ 9 - 0
jpfont_2_lcd/.vscode/extensions.json

@@ -0,0 +1,9 @@
+{
+    "recommendations": [
+        "marus25.cortex-debug",
+        "ms-vscode.cpptools",
+        "ms-vscode.cpptools-extension-pack",
+        "ms-vscode.vscode-serial-monitor",
+        "raspberry-pi.raspberry-pi-pico"
+    ]
+}

+ 50 - 0
jpfont_2_lcd/.vscode/launch.json

@@ -0,0 +1,50 @@
+{
+    "version": "0.2.0",
+    "configurations": [
+        {
+            "name": "Pico Debug (Cortex-Debug)",
+            "cwd": "${userHome}/.pico-sdk/openocd/0.12.0+dev/scripts",
+            "executable": "${command:raspberry-pi-pico.launchTargetPath}",
+            "request": "launch",
+            "type": "cortex-debug",
+            "servertype": "openocd",
+            "serverpath": "${userHome}/.pico-sdk/openocd/0.12.0+dev/openocd.exe",
+            "gdbPath": "${command:raspberry-pi-pico.getGDBPath}",
+            "device": "${command:raspberry-pi-pico.getChipUppercase}",
+            "configFiles": [
+                "interface/cmsis-dap.cfg",
+                "target/${command:raspberry-pi-pico.getTarget}.cfg"
+            ],
+            "svdFile": "${userHome}/.pico-sdk/sdk/2.2.0/src/${command:raspberry-pi-pico.getChip}/hardware_regs/${command:raspberry-pi-pico.getChipUppercase}.svd",
+            "runToEntryPoint": "main",
+            // Fix for no_flash binaries, where monitor reset halt doesn't do what is expected
+            // Also works fine for flash binaries
+            "overrideLaunchCommands": [
+                "monitor reset init",
+                "load \"${command:raspberry-pi-pico.launchTargetPath}\""
+            ],
+            "openOCDLaunchCommands": [
+                "adapter speed 5000"
+            ]
+        },
+        {
+            "name": "Pico Debug (Cortex-Debug with external OpenOCD)",
+            "cwd": "${workspaceRoot}",
+            "executable": "${command:raspberry-pi-pico.launchTargetPath}",
+            "request": "launch",
+            "type": "cortex-debug",
+            "servertype": "external",
+            "gdbTarget": "localhost:3333",
+            "gdbPath": "${command:raspberry-pi-pico.getGDBPath}",
+            "device": "${command:raspberry-pi-pico.getChipUppercase}",
+            "svdFile": "${userHome}/.pico-sdk/sdk/2.2.0/src/${command:raspberry-pi-pico.getChip}/hardware_regs/${command:raspberry-pi-pico.getChipUppercase}.svd",
+            "runToEntryPoint": "main",
+            // Fix for no_flash binaries, where monitor reset halt doesn't do what is expected
+            // Also works fine for flash binaries
+            "overrideLaunchCommands": [
+                "monitor reset init",
+                "load \"${command:raspberry-pi-pico.launchTargetPath}\""
+            ]
+        },
+    ]
+}

+ 40 - 0
jpfont_2_lcd/.vscode/settings.json

@@ -0,0 +1,40 @@
+{
+    "cmake.showSystemKits": false,
+    "cmake.options.statusBarVisibility": "hidden",
+    "cmake.options.advanced": {
+        "build": {
+            "statusBarVisibility": "hidden"
+        },
+        "launch": {
+            "statusBarVisibility": "hidden"
+        },
+        "debug": {
+            "statusBarVisibility": "hidden"
+        }
+    },
+    "cmake.configureOnEdit": false,
+    "cmake.automaticReconfigure": false,
+    "cmake.configureOnOpen": false,
+    "cmake.generator": "Ninja",
+    "cmake.cmakePath": "${userHome}/.pico-sdk/cmake/v3.31.5/bin/cmake",
+    "C_Cpp.debugShortcut": false,
+    "terminal.integrated.env.windows": {
+        "PICO_SDK_PATH": "${env:USERPROFILE}/.pico-sdk/sdk/2.2.0",
+        "PICO_TOOLCHAIN_PATH": "${env:USERPROFILE}/.pico-sdk/toolchain/14_2_Rel1",
+        "Path": "${env:USERPROFILE}/.pico-sdk/toolchain/14_2_Rel1/bin;${env:USERPROFILE}/.pico-sdk/picotool/2.2.0/picotool;${env:USERPROFILE}/.pico-sdk/cmake/v3.31.5/bin;${env:USERPROFILE}/.pico-sdk/ninja/v1.12.1;${env:PATH}"
+    },
+    "terminal.integrated.env.osx": {
+        "PICO_SDK_PATH": "${env:HOME}/.pico-sdk/sdk/2.2.0",
+        "PICO_TOOLCHAIN_PATH": "${env:HOME}/.pico-sdk/toolchain/14_2_Rel1",
+        "PATH": "${env:HOME}/.pico-sdk/toolchain/14_2_Rel1/bin:${env:HOME}/.pico-sdk/picotool/2.2.0/picotool:${env:HOME}/.pico-sdk/cmake/v3.31.5/bin:${env:HOME}/.pico-sdk/ninja/v1.12.1:${env:PATH}"
+    },
+    "terminal.integrated.env.linux": {
+        "PICO_SDK_PATH": "${env:HOME}/.pico-sdk/sdk/2.2.0",
+        "PICO_TOOLCHAIN_PATH": "${env:HOME}/.pico-sdk/toolchain/14_2_Rel1",
+        "PATH": "${env:HOME}/.pico-sdk/toolchain/14_2_Rel1/bin:${env:HOME}/.pico-sdk/picotool/2.2.0/picotool:${env:HOME}/.pico-sdk/cmake/v3.31.5/bin:${env:HOME}/.pico-sdk/ninja/v1.12.1:${env:PATH}"
+    },
+    "raspberry-pi-pico.cmakeAutoConfigure": true,
+    "raspberry-pi-pico.useCmakeTools": false,
+    "raspberry-pi-pico.cmakePath": "${HOME}/.pico-sdk/cmake/v3.31.5/bin/cmake",
+    "raspberry-pi-pico.ninjaPath": "${HOME}/.pico-sdk/ninja/v1.12.1/ninja"
+}

+ 102 - 0
jpfont_2_lcd/.vscode/tasks.json

@@ -0,0 +1,102 @@
+{
+    "version": "2.0.0",
+    "tasks": [
+        {
+            "label": "Compile Project",
+            "type": "process",
+            "isBuildCommand": true,
+            "command": "${userHome}/.pico-sdk/ninja/v1.12.1/ninja",
+            "args": ["-C", "${workspaceFolder}/build"],
+            "group": "build",
+            "presentation": {
+                "reveal": "always",
+                "panel": "dedicated"
+            },
+            "problemMatcher": "$gcc",
+            "windows": {
+                "command": "${env:USERPROFILE}/.pico-sdk/ninja/v1.12.1/ninja.exe"
+            }
+        },
+        {
+            "label": "Run Project",
+            "type": "process",
+            "command": "${env:HOME}/.pico-sdk/picotool/2.2.0/picotool/picotool",
+            "args": [
+                "load",
+                "${command:raspberry-pi-pico.launchTargetPath}",
+                "-fx"
+            ],
+            "presentation": {
+                "reveal": "always",
+                "panel": "dedicated"
+            },
+            "problemMatcher": [],
+            "windows": {
+                "command": "${env:USERPROFILE}/.pico-sdk/picotool/2.2.0/picotool/picotool.exe"
+            }
+        },
+        {
+            "label": "Flash",
+            "type": "process",
+            "command": "${userHome}/.pico-sdk/openocd/0.12.0+dev/openocd.exe",
+            "args": [
+                "-s",
+                "${userHome}/.pico-sdk/openocd/0.12.0+dev/scripts",
+                "-f",
+                "interface/cmsis-dap.cfg",
+                "-f",
+                "target/${command:raspberry-pi-pico.getTarget}.cfg",
+                "-c",
+                "adapter speed 5000; program \"${command:raspberry-pi-pico.launchTargetPath}\" verify reset exit"
+            ],
+            "problemMatcher": [],
+            "windows": {
+                "command": "${env:USERPROFILE}/.pico-sdk/openocd/0.12.0+dev/openocd.exe",
+            }
+        },
+        {
+            "label": "Rescue Reset",
+            "type": "process",
+            "command": "${userHome}/.pico-sdk/openocd/0.12.0+dev/openocd.exe",
+            "args": [
+                "-s",
+                "${userHome}/.pico-sdk/openocd/0.12.0+dev/scripts",
+                "-f",
+                "interface/cmsis-dap.cfg",
+                "-f",
+                "target/${command:raspberry-pi-pico.getChip}-rescue.cfg",
+                "-c",
+                "adapter speed 5000; reset halt; exit"
+            ],
+            "problemMatcher": [],
+            "windows": {
+                "command": "${env:USERPROFILE}/.pico-sdk/openocd/0.12.0+dev/openocd.exe",
+            }
+        },
+        {
+            "label": "Risc-V Reset (RP2350)",
+            "type": "process",
+            "command": "${userHome}/.pico-sdk/openocd/0.12.0+dev/openocd.exe",
+            "args": [
+                "-s",
+                "${userHome}/.pico-sdk/openocd/0.12.0+dev/scripts",
+                "-c",
+                "set USE_CORE { rv0 rv1 cm0 cm1 }",
+                "-f",
+                "interface/cmsis-dap.cfg",
+                "-f",
+                "target/rp2350.cfg",
+                "-c",
+                "adapter speed 5000; init;",
+                "-c",
+                "write_memory 0x40120158 8 { 0x3 }; echo [format \"Info : ARCHSEL 0x%02x\" [read_memory 0x40120158 8 1]];",
+                "-c",
+                "reset halt; targets rp2350.rv0; echo [format \"Info : ARCHSEL_STATUS 0x%02x\" [read_memory 0x4012015C 8 1]]; exit"
+            ],
+            "problemMatcher": [],
+            "windows": {
+                "command": "${env:USERPROFILE}/.pico-sdk/openocd/0.12.0+dev/openocd.exe",
+            }
+        }
+    ]
+}

+ 67 - 0
jpfont_2_lcd/CMakeLists.txt

@@ -0,0 +1,67 @@
+# Generated Cmake Pico project file
+
+cmake_minimum_required(VERSION 3.13)
+
+set(CMAKE_C_STANDARD 11)
+set(CMAKE_CXX_STANDARD 17)
+set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
+
+# Initialise pico_sdk from installed location
+# (note this can come from environment, CMake cache etc)
+
+# == DO NOT EDIT THE FOLLOWING LINES for the Raspberry Pi Pico VS Code Extension to work ==
+if(WIN32)
+    set(USERHOME $ENV{USERPROFILE})
+else()
+    set(USERHOME $ENV{HOME})
+endif()
+set(sdkVersion 2.2.0)
+set(toolchainVersion 14_2_Rel1)
+set(picotoolVersion 2.2.0)
+set(picoVscode ${USERHOME}/.pico-sdk/cmake/pico-vscode.cmake)
+if (EXISTS ${picoVscode})
+    include(${picoVscode})
+endif()
+# ====================================================================================
+set(PICO_BOARD pico2 CACHE STRING "Board type")
+
+# Pull in Raspberry Pi Pico SDK (must be before project)
+include(pico_sdk_import.cmake)
+
+project(jpfont_2_lcd C CXX ASM)
+
+# Initialise the Raspberry Pi Pico SDK
+pico_sdk_init()
+
+# Add executable. Default name is the project name, version 0.1
+
+add_executable(jpfont_2_lcd
+    jpfont_2_lcd.cpp
+    pfnfont.cpp
+    )
+
+pico_set_program_name(jpfont_2_lcd "jpfont_2_lcd")
+pico_set_program_version(jpfont_2_lcd "0.1")
+
+# Modify the below lines to enable/disable output over UART/USB
+pico_enable_stdio_uart(jpfont_2_lcd 1)
+pico_enable_stdio_usb(jpfont_2_lcd 0)
+
+# Add the standard library to the build
+target_link_libraries(jpfont_2_lcd
+        pico_stdlib)
+
+# Add the standard include files to the build
+target_include_directories(jpfont_2_lcd PRIVATE
+        ${CMAKE_CURRENT_LIST_DIR}
+)
+
+# Add any user requested libraries
+target_link_libraries(jpfont_2_lcd 
+        hardware_spi
+        hardware_dma
+        hardware_timer
+        )
+
+pico_add_extra_outputs(jpfont_2_lcd)
+

+ 5 - 0
jpfont_2_lcd/README.md

@@ -0,0 +1,5 @@
+# ST77xxを使用したLCDモジュールに日本語を出力する
+
+これはSPIのサンプルとして書籍に掲載することを考えていたものの、間に合わずに掲載できなかったサンプルコードです。[PFNFontクラス](https://future.quake4.jp/gogs/yoneda/C_Book_Examples/src/master/jpfont_test)を使って、1行分のバッファに日本語を描画し、DMAを使ってSPI接続のLCDに転送します。ダブルバッファを使用しているため、複数行の表示を行う場合にはフォントレンダリングとLCDへの出力が並列化され、高速に日本語をLCDに出力できているはずです。
+
+コードに使用されているDMAなどの各要素については、書籍に記述した範囲内ですので、書籍を参考にしてコードを呼んでみてください。

+ 341 - 0
jpfont_2_lcd/jpfont_2_lcd.cpp

@@ -0,0 +1,341 @@
+#include <stdio.h>
+#include <malloc.h>
+#include <memory.h>
+#include "pico/stdlib.h"
+#include "hardware/spi.h"
+#include "hardware/dma.h"
+#include "hardware/timer.h"
+#include "pfnfont.h"
+
+#define SPI_PORT spi0
+#define PIN_MISO 16
+#define PIN_CS   17
+#define PIN_SCK  18
+#define PIN_MOSI 19
+
+#define TFT_DC  20  // LOW:Command | HIGH:Data
+#define TFT_RES 21
+#define TFT_COMMAND     false
+#define TFT_DATA        true
+#define TFT_WIDTH       240     // LCD縦ピクセル数
+#define TFT_HEIGHT      240     // LCD横ピクセル数
+
+#define TFT_BYTES_PER_PIXEL 2
+
+#define	TFT_COLOR_BLACK   0x0000
+#define	TFT_COLOR_BLUE    0x001F
+#define	TFT_COLOR_RED     0xF800
+#define	TFT_COLOR_GREEN   0x07E0
+#define TFT_COLOR_CYAN    0x07FF
+#define TFT_COLOR_MAGENTA 0xF81F
+#define TFT_COLOR_YELLOW  0xFFE0
+#define TFT_COLOR_WHITE   0xFFFF
+
+#define SSTCMD_SWRESET  0x01
+#define SSTCMD_SLPOUT   0x11
+#define SSTCMD_NORON    0x13
+#define SSTCMD_INVOFF   0x20
+#define SSTCMD_INVON    0x21
+#define SSTCMD_DISPOFF  0x28
+#define SSTCMD_DISPON   0x29
+#define SSTCMD_MADCTL   0x36
+#define SSTCMD_COLMOD   0x3A
+#define SSTCMD_INVCTR   0xB4
+#define SSTCMD_CASET    0x2A
+#define SSTCMD_RASET    0x2B
+#define SSTCMD_RAMWR    0x2C
+
+typedef struct {
+    uint8_t start_x;
+    uint8_t start_y;
+    uint8_t end_x;
+    uint8_t end_y;
+} sst7735_rect;
+
+inline int sst7735_rect_len(sst7735_rect *r)
+{
+    return (r->end_x-r->start_x) * (r->end_y - r->start_y);
+}
+
+static inline void sst7735_send_cmd(uint8_t cmd)
+{
+    gpio_put(TFT_DC, TFT_COMMAND);
+    spi_set_format (SPI_PORT, 8, SPI_CPOL_1 , SPI_CPHA_1,SPI_MSB_FIRST );
+    spi_write_blocking(SPI_PORT, &cmd, sizeof(uint8_t));
+    gpio_put(TFT_DC, TFT_DATA);
+}
+
+static inline void sst7735_send_data8(uint8_t *data, size_t len)
+{
+    gpio_put(TFT_DC, TFT_DATA);
+    spi_set_format (SPI_PORT, 8, SPI_CPOL_1 , SPI_CPHA_1,SPI_MSB_FIRST );
+    spi_write_blocking(SPI_PORT, data, len);
+//    gpio_put(TFT_DC, TFT_DATA);
+}
+
+static inline void sst7735_send_data16(uint16_t *data, size_t len)
+{
+    gpio_put(TFT_DC, TFT_DATA);
+    spi_set_format (SPI_PORT, 16, SPI_CPOL_1 , SPI_CPHA_1,SPI_MSB_FIRST );
+    spi_write16_blocking(SPI_PORT, data, len);
+//    gpio_put(TFT_DC, TFT_DATA);
+}
+
+static inline void sst7735_reset()
+{
+    gpio_put(TFT_RES, true);
+    sleep_ms(500);              // 500ms
+    gpio_put(TFT_RES, false);
+    sleep_ms(100);              // RESET pulse 100ms
+    gpio_put(TFT_RES, true);
+    sleep_ms(500);              // 500ms
+    sst7735_send_cmd(SSTCMD_SWRESET);   // Software Reset
+    sleep_ms(100);              // delay 100ms
+}
+
+static inline void sst7735_init(void)
+{
+    uint8_t cmd_param[4];
+    sst7735_send_cmd(SSTCMD_DISPOFF);       // Display Off
+    sleep_ms(500);
+    sst7735_send_cmd(SSTCMD_SLPOUT);        // Sleep Out
+    sleep_ms(500);
+    sst7735_send_cmd(SSTCMD_DISPON);        // Display on
+    sleep_ms(100);
+    sst7735_send_cmd(SSTCMD_COLMOD);        // Color mode
+    cmd_param[0] = 0x55;                    // 16bit mode
+    sst7735_send_data8(cmd_param, 1);
+    sleep_ms(100);
+    sst7735_send_cmd(SSTCMD_MADCTL);
+    cmd_param[0] = 0x00;
+    sst7735_send_data8(cmd_param, 1);        // RGB 16bit mode
+    sleep_ms(100);
+
+//    sst7735_send_cmd(SSTCMD_NORON);
+    sst7735_send_cmd(SSTCMD_INVON);
+}
+
+void sst7735_draw_image(uint16_t *image, sst7735_rect *rect)
+{
+    uint8_t cmd_param[4] = {0,0,0,0};
+    cmd_param[1] = rect->start_x;
+    cmd_param[3] = rect->end_x;
+    sst7735_send_cmd(SSTCMD_CASET);     // Column Address Set
+    sst7735_send_data8(cmd_param, 4);
+    cmd_param[1] = rect->start_y;
+    cmd_param[3] = rect->end_y;
+    sst7735_send_cmd(SSTCMD_RASET);     // Row Address Set
+    sst7735_send_data8(cmd_param, 4);
+
+    sst7735_send_cmd(SSTCMD_RAMWR);     // RAM RW mode
+    sleep_us(100);
+    sst7735_send_data16((uint16_t *)image, sst7735_rect_len(rect));
+}
+
+void sst7735_clear(void)
+{
+    uint16_t *temp_buf;
+    sst7735_rect r = {start_x:0, start_y:0, end_x:TFT_WIDTH, end_y:TFT_HEIGHT};
+
+    temp_buf = (uint16_t *)calloc(TFT_HEIGHT * TFT_WIDTH, sizeof(uint16_t));
+    sst7735_draw_image(temp_buf, &r);
+    free(temp_buf);
+}
+
+int dma_ch = -1;                         // DMAチャンネル
+dma_channel_config dma_conf;             // DMA設定
+volatile bool dma_in_prgress = false;    // DMA転送中フラグ
+
+// ワンショットタイマー遅延呼び出し用
+int64_t schedule_deferred_call(alarm_id_t id, void *user_data)
+{
+    // DMA転送中フラグを落とす
+    dma_in_prgress = false;
+    return 0;
+}
+
+// DMA割り込み
+void dma_irq_handler()
+{
+    dma_channel_acknowledge_irq0(dma_ch);
+    // 遅延を入れないと死ぬ
+    // ワンショットタイマーで20マイクロ秒後にフラグを落とす
+    add_alarm_in_us(20, schedule_deferred_call, NULL, true);
+}
+
+uint8_t *string_line_buffer[2];             // 1行バッファ
+int     buffer_selector = 0;                // バッファセレクタ
+uint16_t back_color = TFT_COLOR_BLACK;      // 背景色
+uint16_t font_color = TFT_COLOR_WHITE;      // 文字色
+
+PFNFont *pfn;       // PFNフォントオブジェクト
+
+// 1行バッファ内にフォントグリフを描くコールバック関数
+void draw_font_in_buffer(font_gryph_t *gryph, int x, int y)
+{
+    // uint16_tポインタとしてバッファを扱う
+    uint16_t *line_buf = (uint16_t*)string_line_buffer[buffer_selector];
+
+    for(int r = 0; r < gryph->height; r++) {
+        for(int c = 0; c < gryph->width; c++) {
+            // 画面外描画をクリップ
+            if ((x + c) >= TFT_WIDTH) continue;
+
+            int bit_index = r * gryph->width + c;
+            int byte_index = bit_index / 8;
+            int bit_offset = bit_index % 8;
+            if ((gryph->bitmap[byte_index] >> (7 - bit_offset)) & 1) {
+                line_buf[r * TFT_WIDTH + (x + c)] = font_color;
+            } else {
+                // 背景塗りつぶし済み
+            }
+        }
+    }
+}
+
+// 文字列描画用DMA設定関数
+void configure_dma_channel(void)
+{
+    dma_ch = dma_claim_unused_channel(true);
+    dma_conf = dma_channel_get_default_config(dma_ch);
+    channel_config_set_transfer_data_size(&dma_conf, DMA_SIZE_16);
+    channel_config_set_read_increment(&dma_conf, true);
+    channel_config_set_write_increment(&dma_conf, false);
+    uint dreq = spi_get_dreq(SPI_PORT, true);
+    channel_config_set_dreq(&dma_conf, dreq);
+
+    // DMA割り込み設定
+    dma_channel_set_irq0_enabled(dma_ch, true);
+    irq_set_exclusive_handler(DMA_IRQ_0, dma_irq_handler);
+    irq_set_enabled(DMA_IRQ_0, true);
+
+    dma_channel_configure(
+        dma_ch,
+        &dma_conf,
+        &spi_get_hw(SPI_PORT)->dr,  // DATAレジスタ(SSPDR)
+        string_line_buffer[buffer_selector],// Dummy(後で設定)
+        2,                          // Dummy
+        false
+    );
+}
+
+// 文字をLCDにレンダリングする
+void render_string(const char *str, int x, int y)
+{
+    int render_width = 0;
+    // DMAが設定されていなければ何もしない
+    if(dma_ch < 0)
+        return;
+    // 1行バッファのサイズ
+    size_t line_buffer_size = TFT_WIDTH * pfn->get_max_font_height() * TFT_BYTES_PER_PIXEL;
+    // バッファを背景色で塗りつぶし
+    uint16_t* current_buffer = (uint16_t*)string_line_buffer[buffer_selector];
+    for (int i = 0; i < line_buffer_size / TFT_BYTES_PER_PIXEL; ++i) {
+        current_buffer[i] = back_color;
+    }
+    // 文字をバッファに描画
+    render_width = pfn->draw_string(str, x, 0);
+    if(render_width == 0 )
+        return;
+
+    // 前のDMAを待つ
+    while(dma_in_prgress);
+
+    // SST77xxコマンド発行
+    uint8_t cmd_param[4] = {0,0,0,0};
+    cmd_param[1] = 0;
+    cmd_param[3] = TFT_WIDTH;
+    sst7735_send_cmd(SSTCMD_CASET);     // Column Address Set
+    sst7735_send_data8(cmd_param, 4);
+    cmd_param[1] = y;
+    cmd_param[3] = y + pfn->get_max_font_height();
+    sst7735_send_cmd(SSTCMD_RASET);     // Row Address Set
+    sst7735_send_data8(cmd_param, 4);
+    sst7735_send_cmd(SSTCMD_RAMWR);     // RAM RW mode
+    sleep_us(50);
+
+    gpio_put(TFT_DC, TFT_DATA);
+    spi_set_format (SPI_PORT, 16, SPI_CPOL_1 , SPI_CPHA_1,SPI_MSB_FIRST );
+    dma_channel_set_read_addr(dma_ch, string_line_buffer[buffer_selector], false);
+    dma_channel_set_trans_count(dma_ch, TFT_WIDTH * pfn->get_max_font_height(), true);
+    dma_in_prgress = true;
+    // バッファ反転
+    buffer_selector = !buffer_selector;
+}
+
+// フォントデータ
+__asm(\
+  ".section \".rodata\" \n"
+  ".balign 4\n"
+
+  ".global _pfnfont_data\n"
+  ".global _pfnfont_data_len\n"
+
+  "_pfnfont_data:\n"
+  ".incbin \"shnmk16u.pfn\"\n"
+  ".set _pfnfont_data_len, . - _pfnfont_data\n"
+  ".section \".text\"\n"
+);
+
+extern const uint8_t _pfnfont_data[];
+extern uint32_t _pfnfont_data_len;
+
+int main()
+{
+    stdio_init_all();
+    // SPI
+    spi_init(SPI_PORT, 20*1000*1000);
+    gpio_set_function(PIN_MISO, GPIO_FUNC_SPI);
+    gpio_set_function(PIN_CS,   GPIO_FUNC_SPI);
+    gpio_set_function(PIN_SCK,  GPIO_FUNC_SPI);
+    gpio_set_function(PIN_MOSI, GPIO_FUNC_SPI);
+    spi_set_format (SPI_PORT, 8, SPI_CPOL_1 , SPI_CPHA_1,SPI_MSB_FIRST );
+    // TFT Pin Init
+    gpio_init(TFT_DC);
+    gpio_set_dir(TFT_DC, GPIO_OUT);
+    gpio_init(TFT_RES);
+    gpio_set_dir(TFT_RES, GPIO_OUT);
+    // TFT INIT
+    sst7735_reset();
+    sst7735_init();
+    sst7735_clear();
+
+    // 描画コールバック関数を渡してPFNFontオブジェクトを生成
+    pfn = new PFNFont((uint8_t *)_pfnfont_data, (size_t)&_pfnfont_data_len, draw_font_in_buffer);
+
+    // フォントが正しく読み込めたかチェック
+    if (pfn->isValid()) {
+        printf("Font loaded successfully.\n");
+        
+        size_t line_buffer_size = TFT_WIDTH * pfn->get_max_font_height() * TFT_BYTES_PER_PIXEL;
+        string_line_buffer[0] = (uint8_t *)malloc(line_buffer_size);
+        string_line_buffer[1] = (uint8_t *)malloc(line_buffer_size);
+
+        // render_string()を使用する前にかならずconfigure_dma_channel()を呼ぶこと
+        configure_dma_channel();
+        int x = 0, y = 0;
+        while (true) {
+            back_color = TFT_COLOR_BLACK;
+            font_color = TFT_COLOR_WHITE;
+            render_string("Picoで日本語表示!", x, y);
+            font_color = TFT_COLOR_BLUE;
+            y += pfn->get_max_font_height();
+            render_string("Picoで日本語表示!", x, y);
+            font_color = TFT_COLOR_RED;
+            y += pfn->get_max_font_height();
+            render_string("Picoで日本語表示!", x, y);
+            font_color = TFT_COLOR_YELLOW;
+            y += pfn->get_max_font_height();
+            render_string("Picoで日本語表示!", x, y);
+            sleep_ms(500);
+            sst7735_clear();
+            y = 0;
+        }
+    }
+    else {
+        printf("Font loading failed.\n");
+        delete pfn;
+        while(true) tight_loop_contents();
+    }
+    return 0;
+}

+ 215 - 0
jpfont_2_lcd/pfnfont.cpp

@@ -0,0 +1,215 @@
+#include <stdio.h>
+#include <string.h>
+#include "pfnfont.h"
+
+PFNFont::PFNFont(uint8_t *pfn_font, size_t pfnfont_size, void (*draw_font)(font_gryph_t *,int,int))
+{
+    is_valid = false;
+    font_data = pfn_font;
+    font_size = pfnfont_size;
+    draw_font_callback = draw_font;
+    aligned_bitmap_buffer = NULL;
+
+    // ヘッダサイズとシグニチャを検証
+    if (pfnfont_size < sizeof(pfn_font_header_t) || memcmp(font_data, "PINOTFN", PFN_SIG_SIZE) != 0) {
+        return; // 不正なフォントデータ
+    }
+    // first blockの先頭アドレス
+    first_block = font_data + sizeof(pfn_font_header_t);
+    if (first_block >= font_data + pfnfont_size) return; // データサイズが不正
+
+    // フォント名のプロパティをセット
+    memcpy(font_name, ((pfn_font_header_t *)font_data)->fontname, PFN_FONT_NAME_SIZE);
+    font_name[PFN_FONT_NAME_SIZE] = '\0';
+
+    // ブロックをたどりフォントビットマップの最大サイズを調べる
+    bitmap_buffer_size = 0;
+    max_font_height = 0;
+    max_font_width = 0;
+    uint8_t *current_block_ptr = first_block;
+    while(current_block_ptr < (font_data + pfnfont_size)) {
+        pfn_block_header_t *current_block_header = (pfn_block_header_t *)current_block_ptr;
+        int bitmap_size = (current_block_header->width * current_block_header->height + 7) / 8;
+        max_font_height = max(max_font_height, current_block_header->height);
+        max_font_width = max(max_font_width, current_block_header->width);
+
+        // このブロックのデータ部分の合計サイズ
+        int block_data_size = current_block_header->num_glyphs * (bitmap_size + current_block_header->codepoint_size);
+        // 次のブロックへポインタを進める (ヘッダサイズ + データサイズ)
+        current_block_ptr += sizeof(pfn_block_header_t) + block_data_size;
+    }
+    // ビットマップの最大サイズ(バイト単位)
+    bitmap_buffer_size = (max_font_width * max_font_height + 7) / 8;
+
+    // 4バイトアラインメントの配列としてメモリを確保
+    if (bitmap_buffer_size > 0) {
+        aligned_bitmap_buffer = (uint8_t *)new uint32_t[(bitmap_buffer_size + 3) / 4];
+    } else {
+        aligned_bitmap_buffer = NULL;
+    }
+
+    is_valid = true;
+}
+
+PFNFont::~PFNFont()
+{
+    // 確保したメモリを開放
+    delete[] aligned_bitmap_buffer;
+}
+
+// UTF-8文字列へのポインタを受け取り、1文字デコードしてコードポイントを返す
+uint32_t PFNFont::utf8_to_codepoint(const char **utf8_str)
+{
+    uint32_t codepoint = 0;
+    const uint8_t* s = (const uint8_t*)*utf8_str;
+    
+    if (s[0] < 0x80) { // 1-byte (ASCII)
+        codepoint = s[0];
+        *utf8_str += 1;
+    } else if ((s[0] & 0xE0) == 0xC0) { // 2-byte sequence
+        codepoint = ((s[0] & 0x1F) << 6) | (s[1] & 0x3F);
+        *utf8_str += 2;
+    } else if ((s[0] & 0xF0) == 0xE0) { // 3-byte sequence
+        codepoint = ((s[0] & 0x0F) << 12) | ((s[1] & 0x3F) << 6) | (s[2] & 0x3F);
+        *utf8_str += 3;
+    } else if ((s[0] & 0xF8) == 0xF0) { // 4-byte sequence
+        codepoint = ((s[0] & 0x07) << 18) | ((s[1] & 0x3F) << 12) | ((s[2] & 0x3F) << 6) | (s[3] & 0x3F);
+        *utf8_str += 4;
+    } else {
+        // 不正なUTF-8シーケンスの場合は、1バイト進めてエラーを示す
+        codepoint = '?'; 
+        *utf8_str += 1;
+    }
+    return codepoint;
+}
+
+// コードポイントサイズに合わせてgryphのコードポイントを得るユーティリティ関数
+inline uint32_t PFNFont::get_codepoint_from_glyph(uint8_t *glyph_data, uint8_t codepoint_size)
+{
+    uint32_t codepoint = 0;
+
+    if(codepoint_size == 1) {
+        pfn_glyph1_t *glyph = (pfn_glyph1_t *)glyph_data;
+        codepoint = glyph->codepoint;
+    }
+    else if(codepoint_size == 2){
+        pfn_glyph2_t *glyph = (pfn_glyph2_t *)glyph_data;
+        codepoint = glyph->codepoint;
+    }
+    else if(codepoint_size == 4){
+        pfn_glyph4_t *glyph = (pfn_glyph4_t *)glyph_data;
+        codepoint = glyph->codepoint;
+    }
+
+    return codepoint;
+}
+
+// コードポイントがあるブロックを返す
+uint8_t* PFNFont::find_glyph_in_block(uint32_t codepoint)
+{
+    uint8_t *current_block = first_block;
+
+    do {
+        pfn_block_header_t *current_block_header = (pfn_block_header_t *)current_block;
+        uint8_t codepoint_size = current_block_header->codepoint_size;
+        uint8_t font_height = current_block_header->height;
+        uint8_t font_width = current_block_header->width;
+        uint16_t num_glyphs = current_block_header->num_glyphs;
+
+        int glyph_size = (font_width * font_height + 7) / 8 + codepoint_size;
+        // ブロックのデータ部分のサイズ
+        int block_data_size = glyph_size * num_glyphs;
+
+        uint8_t *first_glyph = current_block + sizeof(pfn_block_header_t);
+        uint32_t first_codepoint = get_codepoint_from_glyph(first_glyph, codepoint_size);
+        uint8_t *last_glyph = first_glyph + (num_glyphs - 1) * glyph_size;
+        uint32_t last_codepoint = get_codepoint_from_glyph(last_glyph, codepoint_size);
+
+        if(codepoint < first_codepoint || codepoint > last_codepoint) {
+            // 次のブロックへ移動 (ヘッダサイズ + データサイズ)
+            current_block += sizeof(pfn_block_header_t) + block_data_size;
+            if(current_block >= font_data + font_size) {
+                current_block = NULL;
+                break;
+            }
+        }
+        else {
+            break;
+        }
+    } while(true);
+
+    return current_block;
+}
+
+// コードポイントからフォントグリフを得る
+font_gryph_t *PFNFont::get_gryph_data(uint32_t codepoint)
+{
+    uint8_t *block = find_glyph_in_block(codepoint);
+    if (block == NULL) {
+        // 要求されたグリフが見つからない場合、代替文字('?')を探すか、NULLを返す
+        // ここでは'?'が存在すればそれを返し、なければNULLを返す
+        if (codepoint != '?') {
+            return get_gryph_data('?');
+        }
+        return NULL;
+    }
+
+    pfn_block_header_t *block_header = (pfn_block_header_t *)block;
+    uint8_t font_height = block_header->height;
+    uint8_t font_width = block_header->width;
+    uint16_t num_glyphs = block_header->num_glyphs;
+    uint8_t codepoint_size = block_header->codepoint_size;
+    uint8_t *first_glyph = block + sizeof(pfn_block_header_t);  
+    int glyph_size = (font_width * font_height + 7) / 8 + codepoint_size;
+
+    // 二分探索でグリフを検索
+    int low = 0;
+    int high = num_glyphs - 1;
+    uint8_t *found_glyph = NULL;
+
+    while (low <= high) {
+        int mid = low + (high - low) / 2;
+        uint8_t *current_glyph = first_glyph + mid * glyph_size;
+        uint32_t current_codepoint = get_codepoint_from_glyph(current_glyph, codepoint_size);
+
+        if (current_codepoint == codepoint) {
+            found_glyph = current_glyph;
+            break;
+        } else if (current_codepoint < codepoint) {
+            low = mid + 1;
+        } else {
+            high = mid - 1;
+        }
+    }
+
+    if (found_glyph == NULL) {
+        // find_glyph_in_blockが成功していればここには到達しないはずだが、念のため
+        return (codepoint != '?') ? get_gryph_data('?') : NULL;
+    }
+
+    int bitmap_data_size = (font_width * font_height + 7) / 8;
+    gryph_data.width = font_width;
+    gryph_data.height = font_height;
+    // ビットマップデータをアラインされたバッファにコピー
+    memcpy(aligned_bitmap_buffer, found_glyph + codepoint_size, bitmap_data_size);
+    gryph_data.bitmap = aligned_bitmap_buffer;
+
+    return &gryph_data;
+}
+
+// 文字列を描画し描画した幅(ピクセル)を返す
+int PFNFont::draw_string(const char *str, int x, int y)
+{
+    const char *p = str;
+    int cursor_x = x;
+
+    while(*p != '\0') {
+        uint32_t codepoint = utf8_to_codepoint(&p);
+        font_gryph_t *gryph = get_gryph_data(codepoint);
+        if(gryph != NULL && draw_font_callback != NULL) {
+            draw_font_callback(gryph, cursor_x, y);
+            cursor_x += gryph->width;
+        }
+    }
+    return cursor_x - x;
+}

+ 92 - 0
jpfont_2_lcd/pfnfont.h

@@ -0,0 +1,92 @@
+#ifndef __PFNFONT
+#define __PFNFONT 1
+
+#include <stdint.h>
+
+#ifndef max
+#define max(a,b) ((a) > (b) ? (a) : (b))
+#endif
+
+#define PFN_FONT_NAME_SIZE  8
+#define PFN_SIG_SIZE        7
+#define PFN_VERSION         1
+
+typedef struct __attribute__((packed)) {
+    char    signature[PFN_SIG_SIZE];       // シグニチャ、文字列PINOTFN固定
+    uint8_t version;            // バージョン(1)
+    char    fontname[PFN_FONT_NAME_SIZE];        // フォント名(文字列shnmk14u)
+} pfn_font_header_t;
+
+typedef struct __attribute__((packed)) {
+    uint8_t     width;          // フォントの幅
+    uint8_t     height;         // フォントの高さ
+    uint8_t     codepoint_size; // コードポイントのサイズ(1~4)
+    uint8_t     attribute;      // 予約(未使用)
+    uint16_t    num_glyphs;     // このブロックに含まれるグリフの数
+} pfn_block_header_t;
+
+typedef struct __attribute__((packed)) {
+    uint8_t     codepoint;
+    uint8_t     glyph_data[];
+} pfn_glyph1_t;
+
+typedef struct __attribute__((packed)) {
+    uint16_t     codepoint;
+    uint8_t     glyph_data[];
+} pfn_glyph2_t;
+
+typedef struct __attribute__((packed)) {
+    uint32_t     codepoint;
+    uint8_t     glyph_data[];
+} pfn_glyph4_t;
+
+typedef struct __attribute__((packed)) {
+    uint8_t     width;          // フォントの幅
+    uint8_t     height;         // フォントの高さ
+    uint8_t     *bitmap;        // フォントのビットマップ
+} font_gryph_t;
+
+
+class PFNFont {
+ private:
+    uint8_t *font_data;     // フォントデータの先頭アドレス
+    size_t font_size;       // フォントデータのサイズ
+    uint8_t *first_block;    // 最初のフォントブロック
+    font_gryph_t gryph_data;        // get_gryph_dataが返すグリフデータ
+    uint8_t *aligned_bitmap_buffer; // グリフのビットマップ用バッファ
+    size_t bitmap_buffer_size;      // ビットマップバッファのサイズ
+    bool is_valid;                  // フォントデータが有効かどうかのフラグ
+    char font_name[PFN_FONT_NAME_SIZE+1];    // フォント名
+    uint8_t max_font_width;             // フォントの最大幅
+    uint8_t max_font_height;            // フォントの最大高
+
+    // コードポイントを含むフォントブロックを返す
+    uint8_t* find_glyph_in_block(uint32_t codepoint);
+    // グリフのコードポイントを返す
+    inline uint32_t get_codepoint_from_glyph(uint8_t *glyph_data, uint8_t codepoint_size);
+    // 文字列を描画するコールバック関数
+    void (*draw_font_callback)(font_gryph_t *,int x, int y);
+
+ public:
+    // PFNFontコンストラクタ
+    // uint8_t *pfn_font: pfnフォントデータの先頭アドレス
+    // size_t pfnfont_size: pfnフォントデータのサイズ
+    // void *draw_font(font_gryph_t *,int,int): 文字描画のコールバック関数
+    PFNFont(uint8_t *pfn_font, size_t pfnfont_size, void (*draw_font)(font_gryph_t *,int,int));
+    ~PFNFont();
+    // フォントが有効かチェックする
+    bool isValid() { return is_valid; }
+    // UTF-8をUTF-32コードポイントに変換
+    uint32_t utf8_to_codepoint(const char **utf8_str);
+    // コードポイントからフォントグリフのデータを得る
+    font_gryph_t *get_gryph_data(uint32_t codepoint);
+    // 文字列を描画し、描画した幅(ピクセル)を返す
+    int draw_string(const char *str, int x, int y);
+    // このフォントデータの最大フォント高を返す
+    uint8_t get_max_font_height() { return max_font_height; }
+    // このフォントデータの最大フォント幅を返す
+    uint8_t get_max_font_width() { return max_font_width; }
+    // フォント名を返す
+    const char *get_font_name() { return font_name; }
+};
+#endif

+ 121 - 0
jpfont_2_lcd/pico_sdk_import.cmake

@@ -0,0 +1,121 @@
+# This is a copy of <PICO_SDK_PATH>/external/pico_sdk_import.cmake
+
+# This can be dropped into an external project to help locate this SDK
+# It should be include()ed prior to project()
+
+# Copyright 2020 (c) 2020 Raspberry Pi (Trading) Ltd.
+#
+# 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 its contributors 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.
+
+if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH))
+    set(PICO_SDK_PATH $ENV{PICO_SDK_PATH})
+    message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')")
+endif ()
+
+if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT))
+    set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT})
+    message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')")
+endif ()
+
+if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH))
+    set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH})
+    message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')")
+endif ()
+
+if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_TAG} AND (NOT PICO_SDK_FETCH_FROM_GIT_TAG))
+    set(PICO_SDK_FETCH_FROM_GIT_TAG $ENV{PICO_SDK_FETCH_FROM_GIT_TAG})
+    message("Using PICO_SDK_FETCH_FROM_GIT_TAG from environment ('${PICO_SDK_FETCH_FROM_GIT_TAG}')")
+endif ()
+
+if (PICO_SDK_FETCH_FROM_GIT AND NOT PICO_SDK_FETCH_FROM_GIT_TAG)
+  set(PICO_SDK_FETCH_FROM_GIT_TAG "master")
+  message("Using master as default value for PICO_SDK_FETCH_FROM_GIT_TAG")
+endif()
+
+set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK")
+set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable")
+set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK")
+set(PICO_SDK_FETCH_FROM_GIT_TAG "${PICO_SDK_FETCH_FROM_GIT_TAG}" CACHE FILEPATH "release tag for SDK")
+
+if (NOT PICO_SDK_PATH)
+    if (PICO_SDK_FETCH_FROM_GIT)
+        include(FetchContent)
+        set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR})
+        if (PICO_SDK_FETCH_FROM_GIT_PATH)
+            get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}")
+        endif ()
+        FetchContent_Declare(
+                pico_sdk
+                GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
+                GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG}
+        )
+
+        if (NOT pico_sdk)
+            message("Downloading Raspberry Pi Pico SDK")
+            # GIT_SUBMODULES_RECURSE was added in 3.17
+            if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.17.0")
+                FetchContent_Populate(
+                        pico_sdk
+                        QUIET
+                        GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
+                        GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG}
+                        GIT_SUBMODULES_RECURSE FALSE
+
+                        SOURCE_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-src
+                        BINARY_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-build
+                        SUBBUILD_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-subbuild
+                )
+            else ()
+                FetchContent_Populate(
+                        pico_sdk
+                        QUIET
+                        GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
+                        GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG}
+
+                        SOURCE_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-src
+                        BINARY_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-build
+                        SUBBUILD_DIR ${FETCHCONTENT_BASE_DIR}/pico_sdk-subbuild
+                )
+            endif ()
+
+            set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR})
+        endif ()
+        set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE})
+    else ()
+        message(FATAL_ERROR
+                "SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git."
+                )
+    endif ()
+endif ()
+
+get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
+if (NOT EXISTS ${PICO_SDK_PATH})
+    message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found")
+endif ()
+
+set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake)
+if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE})
+    message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK")
+endif ()
+
+set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE)
+
+include(${PICO_SDK_INIT_CMAKE_FILE})

二进制
jpfont_2_lcd/shnmk12u.pfn


二进制
jpfont_2_lcd/shnmk14u.pfn


二进制
jpfont_2_lcd/shnmk16u.pfn


+ 12 - 4
jpfont_test/pfnfont.cpp

@@ -11,28 +11,36 @@ PFNFont::PFNFont(uint8_t *pfn_font, size_t pfnfont_size, void (*draw_font)(font_
     aligned_bitmap_buffer = NULL;
     aligned_bitmap_buffer = NULL;
 
 
     // ヘッダサイズとシグニチャを検証
     // ヘッダサイズとシグニチャを検証
-    if (pfnfont_size < sizeof(pfn_font_header_t) || memcmp(font_data, "PINOTFN", 7) != 0) {
+    if (pfnfont_size < sizeof(pfn_font_header_t) || memcmp(font_data, "PINOTFN", PFN_SIG_SIZE) != 0) {
         return; // 不正なフォントデータ
         return; // 不正なフォントデータ
     }
     }
-
     // first blockの先頭アドレス
     // first blockの先頭アドレス
     first_block = font_data + sizeof(pfn_font_header_t);
     first_block = font_data + sizeof(pfn_font_header_t);
     if (first_block >= font_data + pfnfont_size) return; // データサイズが不正
     if (first_block >= font_data + pfnfont_size) return; // データサイズが不正
 
 
+    // フォント名のプロパティをセット
+    memcpy(font_name, ((pfn_font_header_t *)font_data)->fontname, PFN_FONT_NAME_SIZE);
+    font_name[PFN_FONT_NAME_SIZE] = '\0';
+
     // ブロックをたどりフォントビットマップの最大サイズを調べる
     // ブロックをたどりフォントビットマップの最大サイズを調べる
     bitmap_buffer_size = 0;
     bitmap_buffer_size = 0;
+    max_font_height = 0;
+    max_font_width = 0;
     uint8_t *current_block_ptr = first_block;
     uint8_t *current_block_ptr = first_block;
     while(current_block_ptr < (font_data + pfnfont_size)) {
     while(current_block_ptr < (font_data + pfnfont_size)) {
         pfn_block_header_t *current_block_header = (pfn_block_header_t *)current_block_ptr;
         pfn_block_header_t *current_block_header = (pfn_block_header_t *)current_block_ptr;
-        // このブロックのビットマップサイズ(バイト単位)
         int bitmap_size = (current_block_header->width * current_block_header->height + 7) / 8;
         int bitmap_size = (current_block_header->width * current_block_header->height + 7) / 8;
-        bitmap_buffer_size = max(bitmap_buffer_size, bitmap_size);
+        max_font_height = max(max_font_height, current_block_header->height);
+        max_font_width = max(max_font_width, current_block_header->width);
 
 
         // このブロックのデータ部分の合計サイズ
         // このブロックのデータ部分の合計サイズ
         int block_data_size = current_block_header->num_glyphs * (bitmap_size + current_block_header->codepoint_size);
         int block_data_size = current_block_header->num_glyphs * (bitmap_size + current_block_header->codepoint_size);
         // 次のブロックへポインタを進める (ヘッダサイズ + データサイズ)
         // 次のブロックへポインタを進める (ヘッダサイズ + データサイズ)
         current_block_ptr += sizeof(pfn_block_header_t) + block_data_size;
         current_block_ptr += sizeof(pfn_block_header_t) + block_data_size;
     }
     }
+    // ビットマップの最大サイズ(バイト単位)
+    bitmap_buffer_size = (max_font_width * max_font_height + 7) / 8;
+
     // 4バイトアラインメントの配列としてメモリを確保
     // 4バイトアラインメントの配列としてメモリを確保
     if (bitmap_buffer_size > 0) {
     if (bitmap_buffer_size > 0) {
         aligned_bitmap_buffer = (uint8_t *)new uint32_t[(bitmap_buffer_size + 3) / 4];
         aligned_bitmap_buffer = (uint8_t *)new uint32_t[(bitmap_buffer_size + 3) / 4];

+ 15 - 2
jpfont_test/pfnfont.h

@@ -7,10 +7,14 @@
 #define max(a,b) ((a) > (b) ? (a) : (b))
 #define max(a,b) ((a) > (b) ? (a) : (b))
 #endif
 #endif
 
 
+#define PFN_FONT_NAME_SIZE  8
+#define PFN_SIG_SIZE        7
+#define PFN_VERSION         1
+
 typedef struct __attribute__((packed)) {
 typedef struct __attribute__((packed)) {
-    char    signature[7];       // シグニチャ、文字列PINOTFN固定
+    char    signature[PFN_SIG_SIZE];       // シグニチャ、文字列PINOTFN固定
     uint8_t version;            // バージョン(1)
     uint8_t version;            // バージョン(1)
-    char    fontname[8];        // フォント名(文字列shnmk14u)
+    char    fontname[PFN_FONT_NAME_SIZE];        // フォント名(文字列shnmk14u)
 } pfn_font_header_t;
 } pfn_font_header_t;
 
 
 typedef struct __attribute__((packed)) {
 typedef struct __attribute__((packed)) {
@@ -52,6 +56,9 @@ class PFNFont {
     uint8_t *aligned_bitmap_buffer; // グリフのビットマップ用バッファ
     uint8_t *aligned_bitmap_buffer; // グリフのビットマップ用バッファ
     size_t bitmap_buffer_size;      // ビットマップバッファのサイズ
     size_t bitmap_buffer_size;      // ビットマップバッファのサイズ
     bool is_valid;                  // フォントデータが有効かどうかのフラグ
     bool is_valid;                  // フォントデータが有効かどうかのフラグ
+    char font_name[PFN_FONT_NAME_SIZE+1];    // フォント名
+    uint8_t max_font_width;             // フォントの最大幅
+    uint8_t max_font_height;            // フォントの最大高
 
 
     // コードポイントを含むフォントブロックを返す
     // コードポイントを含むフォントブロックを返す
     uint8_t* find_glyph_in_block(uint32_t codepoint);
     uint8_t* find_glyph_in_block(uint32_t codepoint);
@@ -75,5 +82,11 @@ class PFNFont {
     font_gryph_t *get_gryph_data(uint32_t codepoint);
     font_gryph_t *get_gryph_data(uint32_t codepoint);
     // 文字列を描画する
     // 文字列を描画する
     void draw_string(const char *str, int x, int y);
     void draw_string(const char *str, int x, int y);
+    // このフォントデータの最大フォント高を返す
+    uint8_t get_max_font_height() { return max_font_height; }
+    // このフォントデータの最大フォント幅を返す
+    uint8_t get_max_font_width() { return max_font_width; }
+    // フォント名を返す
+    const char *get_font_name() { return font_name; }
 };
 };
 #endif
 #endif