• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

鸿蒙开发板Hi3861_驱动0.95_oled显示屏ssd1306_code-2.0-CANARY_双显示屏效果

武飞扬头像
txwtech
帮助1

鸿蒙开发板Hi3861_驱动0.95_oled显示屏ssd1306_code-2.0-CANARY_双显示屏效果

2.0支持windows编译与上传,不需要ubuntu编译

环境搭建需要有耐心:

https://www.cnblogs.com/txwtech/p/15041927.html

首先学会点亮LED的实验

https://www.cnblogs.com/txwtech/p/15139405.html

//驱动oled源代码

源码下载:

https://gitee.com/hihopeorg/harmonyos-ssd1306

下载的源代码有三个文件夹,examples,libm_portm,ssd1306,拷贝到如下目录

F:\code-2.0-canary\applications\sample\wifi-iot\app

参考链接:

https://harmonyos.51cto.com/posts/2126#comment

oled SDA接gpio13

oled SCL接gpio14

VCC --3V3

GND--GND

打开vscode,源码是1.0的,参考如下方法修改:

https://blog.csdn.net/YangHxuan/article/details/119116051

学新通

 学新通

需要修改的几个关键文件:

F:\code-2.0-canary\device\hisilicon\hispark_pegasus\sdk_liteos\build\config

找到usr_config.mk修改

添加:

CONFIG_I2C_SUPPORT=y

ssd1306_demo.c,这个是根据1.0修改为2.0版本的

  1.  
    /*
  2.  
    * Copyright (c) 2020, HiHope Community.
  3.  
    *
  4.  
    * Redistribution and use in source and binary forms, with or without
  5.  
    * modification, are permitted provided that the following conditions are met:
  6.  
    *
  7.  
    * 1. Redistributions of source code must retain the above copyright notice, this
  8.  
    * list of conditions and the following disclaimer.
  9.  
    *
  10.  
    * 2. Redistributions in binary form must reproduce the above copyright notice,
  11.  
    * this list of conditions and the following disclaimer in the documentation
  12.  
    * and/or other materials provided with the distribution.
  13.  
    *
  14.  
    * 3. Neither the name of the copyright holder nor the names of its
  15.  
    * contributors may be used to endorse or promote products derived from
  16.  
    * this software without specific prior written permission.
  17.  
    *
  18.  
    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19.  
    * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20.  
    * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21.  
    * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  22.  
    * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23.  
    * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  24.  
    * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25.  
    * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26.  
    * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27.  
    * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28.  
    */
  29.  
     
  30.  
    #include <ctype.h>
  31.  
    #include <stdio.h>
  32.  
    #include <unistd.h>
  33.  
     
  34.  
    #include "ohos_init.h"
  35.  
    #include "cmsis_os2.h"
  36.  
    //#include "wifiiot_gpio.h"
  37.  
    #include "hi_io.h"
  38.  
    //#include "wifiiot_gpio_ex.h"
  39.  
    //#include "wifiiot_pwm.h"
  40.  
    #include "hi_pwm.h"
  41.  
    #include "wifiiot_adc.h"
  42.  
    #include "hi_adc.h"
  43.  
    //#include "wifiiot_i2c.h"
  44.  
    #include "hi_i2c.h"
  45.  
    //#include "wifiiot_errno.h"
  46.  
    #include "hi_errno.h"
  47.  
    //#include "wifiiot_watchdog.h"
  48.  
    #include "hi_watchdog.h"
  49.  
     
  50.  
    #include "ssd1306.h"
  51.  
    #include "ssd1306_tests.h"
  52.  
     
  53.  
    #define OLED_I2C_BAUDRATE 400*1000
  54.  
     
  55.  
     
  56.  
     
  57.  
    const unsigned char headSize[] = { 64, 64 };
  58.  
    const unsigned char headData[] = {
  59.  
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60.  
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61.  
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  62.  
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  63.  
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  64.  
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  65.  
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  66.  
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  67.  
    0x00, 0x00, 0x00, 0x00, 0x1F, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0xFF, 0x80, 0x00,
  68.  
    0x00, 0x00, 0x00, 0x01, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x07, 0xFF, 0xFF, 0xE0, 0x00,
  69.  
    0x00, 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xF0, 0x00,
  70.  
    0x00, 0x00, 0x00, 0x3F, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x3F, 0xFF, 0xFF, 0xF0, 0x00,
  71.  
    0x00, 0x00, 0x00, 0x7F, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x7F, 0xFF, 0xFF, 0xF0, 0x00,
  72.  
    0x00, 0x00, 0x00, 0xFF, 0x83, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xE3, 0xF0, 0x18, 0x00,
  73.  
    0x00, 0x00, 0x01, 0xFF, 0xE3, 0xE0, 0x10, 0x00, 0x00, 0x00, 0x61, 0xFE, 0x03, 0xE1, 0xF8, 0x00,
  74.  
    0x00, 0xC1, 0xF1, 0xF8, 0x97, 0xE0, 0x18, 0x00, 0x00, 0x00, 0xB9, 0xFF, 0xFF, 0xF8, 0x88, 0x00,
  75.  
    0x00, 0x00, 0xFD, 0xFF, 0xFF, 0xFD, 0xF8, 0x00, 0x00, 0x00, 0xFD, 0xFF, 0xFF, 0xFE, 0x78, 0x00,
  76.  
    0x00, 0x00, 0xCD, 0xFF, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x40, 0xDF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00,
  77.  
    0x00, 0x40, 0xDF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x40, 0x7F, 0xFF, 0xFF, 0xFF, 0xF8, 0x00,
  78.  
    0x00, 0xA0, 0x7F, 0xFF, 0xFF, 0xFF, 0xF9, 0x00, 0x01, 0x20, 0x3F, 0xFF, 0xFC, 0x47, 0xF8, 0x00,
  79.  
    0x02, 0x10, 0x1F, 0xFF, 0xFF, 0x87, 0xF0, 0x00, 0x0C, 0x18, 0x0F, 0xFF, 0xFF, 0xFF, 0xF0, 0x00,
  80.  
    0x00, 0x08, 0x07, 0xFF, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x0D, 0x07, 0xFF, 0xFC, 0x07, 0xF0, 0x00,
  81.  
    0x00, 0x05, 0x07, 0xFF, 0xC0, 0x01, 0xF0, 0x00, 0x00, 0x05, 0x07, 0xFF, 0xDF, 0xE1, 0xE0, 0x00,
  82.  
    0x02, 0x06, 0x07, 0xFF, 0xFF, 0x09, 0xE0, 0x00, 0x00, 0x02, 0x07, 0xFF, 0xFE, 0x1F, 0xC0, 0x00,
  83.  
    0x00, 0x02, 0x07, 0xFF, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x60, 0x3F, 0xFF, 0xFF, 0xFF, 0x80, 0x00,
  84.  
    0x00, 0x30, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x32, 0xFF, 0xFF, 0xFF, 0xFC, 0x00, 0x00,
  85.  
    0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0x18, 0x00, 0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xF0, 0x00, 0x00,
  86.  
    0x00, 0x0F, 0xFF, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0xFC, 0xF0, 0x00, 0x00,
  87.  
    0x00, 0x07, 0xFF, 0xFF, 0xFE, 0xF0, 0x00, 0x01, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xF0, 0x00, 0x07,
  88.  
    0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xF0, 0xE0, 0x3F, 0x00, 0x23, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  89.  
    0x00, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  90.  
    0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF,
  91.  
    };
  92.  
     
  93.  
    /**
  94.  
    * 汉字字模在线: https://www.23bei.com/tool-223.html
  95.  
    * 数据排列:从左到右从上到下
  96.  
    * 取模方式:横向8位左高位
  97.  
    **/
  98.  
    void TestDrawChinese1(void)
  99.  
    {
  100.  
    const uint32_t W = 16, H = 16;
  101.  
    uint8_t fonts[][32] = {
  102.  
    {
  103.  
    /*-- ID:0,字符:"你",ASCII编码:C4E3,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
  104.  
    //0x11,0x00,0x11,0x00,0x11,0x00,0x23,0xFC,0x22,0x04,0x64,0x08,0xA8,0x40,0x20,0x40,
  105.  
    //0x21,0x50,0x21,0x48,0x22,0x4C,0x24,0x44,0x20,0x40,0x20,0x40,0x21,0x40,0x20,0x80,
  106.  
    /*-- ID:0,字符:"阿",ASCII编码:B0A2,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
  107.  
    0x00,0x04,0x7F,0xFE,0x44,0x08,0x48,0x08,0x48,0x28,0x51,0xF8,0x49,0x28,0x49,0x28,
  108.  
    0x45,0x28,0x45,0x28,0x45,0x28,0x69,0xE8,0x51,0x28,0x40,0x08,0x40,0x28,0x40,0x10,
  109.  
    },{
  110.  
    /*-- ID:1,字符:"好",ASCII编码:BAC3,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
  111.  
    //0x10,0x00,0x11,0xFC,0x10,0x04,0x10,0x08,0xFC,0x10,0x24,0x20,0x24,0x24,0x27,0xFE,
  112.  
    //0x24,0x20,0x44,0x20,0x28,0x20,0x10,0x20,0x28,0x20,0x44,0x20,0x84,0xA0,0x00,0x40,
  113.  
    /*-- ID:1,字符:"武",ASCII编码:CEE4,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
  114.  
    0x00,0x40,0x02,0x50,0x3F,0x48,0x00,0x48,0x00,0x40,0xFF,0xFE,0x04,0x40,0x04,0x40,
  115.  
    0x27,0x40,0x24,0x40,0x24,0x20,0x24,0x20,0x27,0xA0,0xF8,0x12,0x40,0x0A,0x00,0x06,
  116.  
     
  117.  
    },{
  118.  
    /*-- ID:2,字符:"鸿",ASCII编码:BAE8,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
  119.  
    //0x40,0x20,0x30,0x48,0x10,0xFC,0x02,0x88,0x9F,0xA8,0x64,0x88,0x24,0xA8,0x04,0x90,
  120.  
    //0x14,0x84,0x14,0xFE,0xE7,0x04,0x3C,0x24,0x29,0xF4,0x20,0x04,0x20,0x14,0x20,0x08,
  121.  
    /*-- ID:2,字符:"科",ASCII编码:BFC6,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
  122.  
    0x04,0x10,0x0E,0x10,0xF8,0x90,0x08,0x50,0x08,0x10,0xFE,0x90,0x08,0x50,0x1C,0x14,
  123.  
    0x1A,0x1E,0x29,0xF0,0x28,0x10,0x48,0x10,0x88,0x10,0x08,0x10,0x08,0x10,0x08,0x10,
  124.  
     
  125.  
    },{
  126.  
    /*-- ID:3,字符:"蒙",ASCII编码:C3C9,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
  127.  
    //0x04,0x48,0x7F,0xFC,0x04,0x40,0x7F,0xFE,0x40,0x02,0x8F,0xE4,0x00,0x00,0x7F,0xFC,
  128.  
    // 0x06,0x10,0x3B,0x30,0x05,0xC0,0x1A,0xA0,0x64,0x90,0x18,0x8E,0x62,0x84,0x01,0x00
  129.  
    /*-- ID:3,字符:"技",ASCII编码:BCBC,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
  130.  
    0x10,0x40,0x10,0x40,0x10,0x48,0x13,0xFC,0xFC,0x40,0x10,0x40,0x10,0x40,0x13,0xF8,
  131.  
    0x1A,0x08,0x31,0x10,0xD1,0x10,0x10,0xA0,0x10,0x40,0x10,0xB0,0x51,0x0E,0x26,0x04,
  132.  
    },
  133.  
    {/*-- ID:4,字符:"t",ASCII编码:A3F4,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
  134.  
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x01,0x80,0x01,0x80,0x07,0xE0,0x01,0x80,
  135.  
    0x01,0x80,0x01,0x80,0x01,0x90,0x01,0x90,0x00,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,
  136.  
     
  137.  
    },
  138.  
    {/*-- ID:5,字符:"x",ASCII编码:A3F8,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
  139.  
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x3C,0x0C,0x30,
  140.  
    0x03,0x60,0x01,0xC0,0x03,0x60,0x0C,0x30,0x3E,0x7C,0x00,0x00,0x00,0x00,0x00,0x00,},
  141.  
    {/*-- ID:6,字符:"w",ASCII编码:A3F7,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
  142.  
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3D,0xEE,0x18,0xC4,
  143.  
    0x08,0xCC,0x0D,0x68,0x05,0x28,0x06,0x30,0x02,0x10,0x00,0x00,0x00,0x00,0x00,0x00,},
  144.  
    {/*-- ID:4,字符:"t",ASCII编码:A3F4,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
  145.  
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x01,0x80,0x01,0x80,0x07,0xE0,0x01,0x80,
  146.  
    0x01,0x80,0x01,0x80,0x01,0x90,0x01,0x90,0x00,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,
  147.  
    },
  148.  
    {/*-- ID:7,字符:"e",ASCII编码:A3E5,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
  149.  
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xE0,0x0C,0x30,
  150.  
    0x0C,0x30,0x0F,0xF0,0x0C,0x00,0x0C,0x30,0x07,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,
  151.  
    },
  152.  
    {/*-- ID:8,字符:"c",ASCII编码:A3E3,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
  153.  
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xE0,0x0C,0x30,
  154.  
    0x0C,0x00,0x0C,0x00,0x0C,0x00,0x0C,0x30,0x07,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,
  155.  
    },
  156.  
    {/*-- ID:9,字符:"h",ASCII编码:A3E8,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
  157.  
    0x00,0x00,0x00,0x00,0x0E,0x00,0x06,0x00,0x06,0x00,0x06,0x00,0x06,0xE0,0x07,0x30,
  158.  
    0x06,0x30,0x06,0x30,0x06,0x30,0x06,0x30,0x0E,0x78,0x00,0x00,0x00,0x00,0x00,0x00,}
  159.  
     
  160.  
    };
  161.  
     
  162.  
     
  163.  
     
  164.  
     
  165.  
     
  166.  
     
  167.  
     
  168.  
    ssd1306_Fill(Black);
  169.  
    for (size_t i = 0; i < sizeof(fonts)/sizeof(fonts[0]); i ) {
  170.  
    ssd1306_DrawRegion(i * W, 0, W, H, fonts[i], sizeof(fonts[0]), W);
  171.  
    }
  172.  
     
  173.  
    ssd1306_SetCursor(64, 0);
  174.  
    ssd1306_DrawRegion(64, 0, headSize[0], headSize[1], headData, sizeof(headData), headSize[0]);
  175.  
    ssd1306_UpdateScreen();
  176.  
    }
  177.  
     
  178.  
    void TestDrawChinese2(void)
  179.  
    {
  180.  
    const uint32_t W = 12, H = 12, S = 16;
  181.  
    uint8_t fonts[][24] = {
  182.  
    {
  183.  
    /*-- ID:0,字符:"你",ASCII编码:C4E3,对应字:宽x高=12x12,画布:宽W=16 高H=12,共24字节*/
  184.  
    0x12,0x00,0x12,0x00,0x27,0xF0,0x24,0x20,0x69,0x40,0xA1,0x00,0x25,0x40,0x25,0x20,
  185.  
    0x29,0x10,0x31,0x10,0x25,0x00,0x22,0x00,
  186.  
    },{
  187.  
    /*-- ID:1,字符:"好",ASCII编码:BAC3,对应字:宽x高=12x12,画布:宽W=16 高H=12,共24字节*/
  188.  
    0x20,0x00,0x27,0xE0,0x20,0x40,0xF8,0x80,0x48,0x80,0x48,0xA0,0x57,0xF0,0x50,0x80,
  189.  
    0x30,0x80,0x28,0x80,0x4A,0x80,0x81,0x00,
  190.  
    },{
  191.  
    /*-- ID:2,字符:"鸿",ASCII编码:BAE8,对应字:宽x高=12x12,画布:宽W=16 高H=12,共24字节*/
  192.  
    0x00,0x40,0x80,0x80,0x5D,0xE0,0x09,0x20,0xC9,0xA0,0x09,0x60,0x29,0x00,0xCD,0xF0,
  193.  
    0x58,0x10,0x43,0xD0,0x40,0x10,0x40,0x60,
  194.  
    },{
  195.  
    /*-- ID:3,字符:"蒙",ASCII编码:C3C9,对应字:宽x高=12x12,画布:宽W=16 高H=12,共24字节*/
  196.  
    0x09,0x00,0x7F,0xE0,0x09,0x00,0x7F,0xF0,0x80,0x10,0x7F,0xE0,0x0C,0x40,0x32,0x80,
  197.  
    0xC7,0x00,0x0A,0x80,0x32,0x70,0xC6,0x20
  198.  
    }
  199.  
    };
  200.  
     
  201.  
    ssd1306_Fill(Black);
  202.  
    for (size_t i = 0; i < sizeof(fonts)/sizeof(fonts[0]); i ) {
  203.  
    ssd1306_DrawRegion(i * H, 0, W, H, fonts[i], sizeof(fonts[0]), S);
  204.  
    }
  205.  
     
  206.  
    ssd1306_SetCursor(64, 0);
  207.  
    ssd1306_DrawRegion(64, 0, headSize[0], headSize[1], headData, sizeof(headData), headSize[0]);
  208.  
     
  209.  
    ssd1306_SetCursor(0, 64 - 8);
  210.  
    ssd1306_DrawString("txwtech.com", Font_6x8, White);
  211.  
     
  212.  
    ssd1306_SetCursor(0, 64 - 8*2);
  213.  
    ssd1306_DrawString("@xtang10", Font_6x8, White);
  214.  
     
  215.  
    ssd1306_UpdateScreen();
  216.  
    }
  217.  
     
  218.  
    void TestShowChars(FontDef font, uint8_t w, uint8_t h)
  219.  
    {
  220.  
    ssd1306_Fill(Black);
  221.  
     
  222.  
    uint8_t x = 0, y = 0;
  223.  
    for (uint8_t c = 1; c < 128; c ) {
  224.  
    if (isprint(c)) {
  225.  
    ssd1306_SetCursor(x, y);
  226.  
    ssd1306_DrawChar((char) c, font, White);
  227.  
    x = w;
  228.  
    if (x >= SSD1306_WIDTH) {
  229.  
    x = 0;
  230.  
    y = h;
  231.  
    }
  232.  
    }
  233.  
    }
  234.  
     
  235.  
    ssd1306_UpdateScreen();
  236.  
    }
  237.  
     
  238.  
    void Ssd1306TestTask(void* arg)
  239.  
    {
  240.  
    (void) arg;
  241.  
    // GpioInit();
  242.  
    hi_gpio_init();
  243.  
    // IoSetFunc(WIFI_IOT_IO_NAME_GPIO_13, WIFI_IOT_IO_FUNC_GPIO_13_I2C0_SDA);
  244.  
    // IoSetFunc(WIFI_IOT_IO_NAME_GPIO_14, WIFI_IOT_IO_FUNC_GPIO_14_I2C0_SCL);
  245.  
    // I2cInit(WIFI_IOT_I2C_IDX_0, OLED_I2C_BAUDRATE);
  246.  
     
  247.  
    hi_io_set_func(HI_IO_NAME_GPIO_13, HI_IO_FUNC_GPIO_13_I2C0_SDA);
  248.  
    hi_io_set_func(HI_IO_NAME_GPIO_14, HI_IO_FUNC_GPIO_14_I2C0_SCL);
  249.  
    hi_io_set_func(HI_I2C_IDX_0, OLED_I2C_BAUDRATE);
  250.  
     
  251.  
    // WatchDogDisable();
  252.  
    hi_watchdog_disable();
  253.  
     
  254.  
    usleep(20*1000);
  255.  
    ssd1306_Init();
  256.  
    ssd1306_Fill(Black);
  257.  
    ssd1306_SetCursor(0, 0);
  258.  
    ssd1306_DrawString("Hello HarmonyOS!", Font_7x10, White);
  259.  
     
  260.  
    uint32_t start = HAL_GetTick();
  261.  
    ssd1306_UpdateScreen();
  262.  
    uint32_t end = HAL_GetTick();
  263.  
    printf("ssd1306_UpdateScreen time cost: %d ms.\r\n", end - start);
  264.  
     
  265.  
    while (1) {
  266.  
    TestDrawChinese1();
  267.  
    osDelay(500);
  268.  
     
  269.  
    TestDrawChinese2();
  270.  
    osDelay(500);
  271.  
     
  272.  
    // TestShowChars(Font_6x8, 6, 8);
  273.  
    // osDelay(500);
  274.  
     
  275.  
    // TestShowChars(Font_7x10, 7, 10);
  276.  
    // osDelay(500);
  277.  
     
  278.  
    ssd1306_TestAll();
  279.  
    }
  280.  
    }
  281.  
     
  282.  
    void Ssd1306TestDemo(void)
  283.  
    {
  284.  
    osThreadAttr_t attr;
  285.  
     
  286.  
    attr.name = "Ssd1306Task";
  287.  
    attr.attr_bits = 0U;
  288.  
    attr.cb_mem = NULL;
  289.  
    attr.cb_size = 0U;
  290.  
    attr.stack_mem = NULL;
  291.  
    attr.stack_size = 10240;
  292.  
    attr.priority = osPriorityNormal;
  293.  
     
  294.  
    if (osThreadNew(Ssd1306TestTask, NULL, &attr) == NULL) {
  295.  
    printf("[Ssd1306TestDemo] Falied to create Ssd1306TestTask!\n");
  296.  
    }
  297.  
    }
  298.  
    APP_FEATURE_INIT(Ssd1306TestDemo);

examples文件夹的build.gn

  1.  
    # Copyright (c) 2020, HiHope Community.
  2.  
    #
  3.  
    # Redistribution and use in source and binary forms, with or without
  4.  
    # modification, are permitted provided that the following conditions are met:
  5.  
    #
  6.  
    # 1. Redistributions of source code must retain the above copyright notice, this
  7.  
    # list of conditions and the following disclaimer.
  8.  
    #
  9.  
    # 2. Redistributions in binary form must reproduce the above copyright notice,
  10.  
    # this list of conditions and the following disclaimer in the documentation
  11.  
    # and/or other materials provided with the distribution.
  12.  
    #
  13.  
    # 3. Neither the name of the copyright holder nor the names of its
  14.  
    # contributors may be used to endorse or promote products derived from
  15.  
    # this software without specific prior written permission.
  16.  
    #
  17.  
    # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18.  
    # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19.  
    # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20.  
    # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  21.  
    # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22.  
    # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23.  
    # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24.  
    # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25.  
    # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26.  
    # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  
     
  28.  
    static_library("oled_test") {
  29.  
    sources = [
  30.  
    "ssd1306_demo.c",
  31.  
    "ssd1306_tests.c",
  32.  
    ]
  33.  
     
  34.  
    include_dirs = [
  35.  
    "../ssd1306",
  36.  
    "//utils/native/lite/include",
  37.  
    "//kernel/liteos_m/components/cmsis/2.0",
  38.  
    "//base/iot_hardware/interfaces/kits/wifiiot_lite",
  39.  
    "//applications/sample/wifi-iot/app/ssd1306",
  40.  
    ]
  41.  
    }
  42.  
     
  43.  
     

ssd1306文件夹的build.gn

  1.  
    # Copyright (c) 2020, HiHope Community.
  2.  
    #
  3.  
    # Redistribution and use in source and binary forms, with or without
  4.  
    # modification, are permitted provided that the following conditions are met:
  5.  
    #
  6.  
    # 1. Redistributions of source code must retain the above copyright notice, this
  7.  
    # list of conditions and the following disclaimer.
  8.  
    #
  9.  
    # 2. Redistributions in binary form must reproduce the above copyright notice,
  10.  
    # this list of conditions and the following disclaimer in the documentation
  11.  
    # and/or other materials provided with the distribution.
  12.  
    #
  13.  
    # 3. Neither the name of the copyright holder nor the names of its
  14.  
    # contributors may be used to endorse or promote products derived from
  15.  
    # this software without specific prior written permission.
  16.  
    #
  17.  
    # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18.  
    # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19.  
    # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20.  
    # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  21.  
    # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22.  
    # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23.  
    # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24.  
    # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25.  
    # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26.  
    # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  
     
  28.  
    static_library("oled_ssd1306") {
  29.  
    sources = [
  30.  
    "ssd1306.c",
  31.  
    "ssd1306_fonts.c",
  32.  
    ]
  33.  
     
  34.  
    include_dirs = [
  35.  
    "//utils/native/lite/include",
  36.  
    "//kernel/liteos_m/components/cmsis/2.0",
  37.  
    "//base/iot_hardware/interfaces/kits/wifiiot_lite",
  38.  
    ]
  39.  
    }
  40.  
     
  41.  
     
  42.  
     

ssd1306文件夹的ssd1306.c

  1.  
    #include "ssd1306.h"
  2.  
    #include <math.h>
  3.  
    #include <stdlib.h>
  4.  
    #include <string.h> // For memcpy
  5.  
     
  6.  
    #include <stdio.h>
  7.  
    #include <unistd.h>
  8.  
    #include "cmsis_os2.h"
  9.  
    //#include "wifiiot_i2c.h"
  10.  
    //#include "wifiiot_errno.h"
  11.  
    #include "hi_errno.h" //code-2.0-canary,新添加的。txwtech
  12.  
    #include "hi_i2c.h" //code-2.0-canary,新添加的。txwtech
  13.  
     
  14.  
    #if defined(SSD1306_USE_I2C)
  15.  
    //#define SSD1306_I2C_IDX WIFI_IOT_I2C_IDX_0
  16.  
    #define SSD1306_I2C_IDX HI_I2C_IDX_0 //code-2.0-canary,新添加的。txwtech
  17.  
     
  18.  
    #define SSD1306_CTRL_CMD 0x00
  19.  
    #define SSD1306_CTRL_DATA 0x40
  20.  
    #define SSD1306_MASK_CONT (0x1<<7)
  21.  
     
  22.  
    void ssd1306_Reset(void) {
  23.  
    /* for I2C - do nothing */
  24.  
    }
  25.  
     
  26.  
    void HAL_Delay(uint32_t ms)
  27.  
    {
  28.  
    uint32_t msPerTick = 1000 / osKernelGetTickFreq(); // 10ms
  29.  
    if (ms >= msPerTick) {
  30.  
    osDelay(ms / msPerTick);
  31.  
    }
  32.  
     
  33.  
    uint32_t restMs = ms % msPerTick;
  34.  
    if (restMs > 0) {
  35.  
    usleep(restMs * 1000);
  36.  
    }
  37.  
    }
  38.  
     
  39.  
    uint32_t HAL_GetTick(void)
  40.  
    {
  41.  
    uint32_t msPerTick = 1000 / osKernelGetTickFreq(); // 10ms
  42.  
    uint32_t tickMs = osKernelGetTickCount() * msPerTick;
  43.  
     
  44.  
    uint32_t csPerMs = osKernelGetSysTimerFreq() / 1000; // 160K cycle/ms
  45.  
    uint32_t csPerTick = csPerMs * msPerTick; // 1600K cycles/tick
  46.  
    uint32_t restMs = osKernelGetSysTimerCount() % csPerTick / csPerMs;
  47.  
     
  48.  
    return tickMs restMs;
  49.  
    }
  50.  
     
  51.  
    static uint32_t ssd1306_SendData(uint8_t* data, size_t size)
  52.  
    {
  53.  
    // WifiIotI2cIdx id = SSD1306_I2C_IDX;
  54.  
    hi_i2c_idx id = SSD1306_I2C_IDX;
  55.  
    // WifiIotI2cData i2cData = {0};
  56.  
    hi_i2c_data i2cData = {0};
  57.  
    // i2cData.sendBuf = data;
  58.  
    // i2cData.sendLen = size;
  59.  
    i2cData.send_buf = data;
  60.  
    i2cData.send_len = size;
  61.  
     
  62.  
    // return I2cWrite(id, SSD1306_I2C_ADDR, &i2cData);
  63.  
    return hi_i2c_write(id, SSD1306_I2C_ADDR, &i2cData);
  64.  
    }
  65.  
     
  66.  
    static uint32_t ssd1306_WiteByte(uint8_t regAddr, uint8_t byte)
  67.  
    {
  68.  
    uint8_t buffer[] = {regAddr, byte};
  69.  
    return ssd1306_SendData(buffer, sizeof(buffer));
  70.  
    }
  71.  
     
  72.  
    // Send a byte to the command register
  73.  
    void ssd1306_WriteCommand(uint8_t byte) {
  74.  
    ssd1306_WiteByte(SSD1306_CTRL_CMD, byte);
  75.  
    }
  76.  
     
  77.  
    // Send data
  78.  
    void ssd1306_WriteData(uint8_t* buffer, size_t buff_size) {
  79.  
    uint8_t data[SSD1306_WIDTH * 2] = {0};
  80.  
    for (size_t i = 0; i < buff_size; i ) {
  81.  
    data[i*2] = SSD1306_CTRL_DATA | SSD1306_MASK_CONT;
  82.  
    data[i*2 1] = buffer[i];
  83.  
    }
  84.  
    data[(buff_size - 1) * 2] = SSD1306_CTRL_DATA;
  85.  
    ssd1306_SendData(data, sizeof(data));
  86.  
    }
  87.  
     
  88.  
    #elif defined(SSD1306_USE_SPI)
  89.  
     
  90.  
    void ssd1306_Reset(void) {
  91.  
    // CS = High (not selected)
  92.  
    HAL_GPIO_WritePin(SSD1306_CS_Port, SSD1306_CS_Pin, GPIO_PIN_SET);
  93.  
     
  94.  
    // Reset the OLED
  95.  
    HAL_GPIO_WritePin(SSD1306_Reset_Port, SSD1306_Reset_Pin, GPIO_PIN_RESET);
  96.  
    HAL_Delay(10);
  97.  
    HAL_GPIO_WritePin(SSD1306_Reset_Port, SSD1306_Reset_Pin, GPIO_PIN_SET);
  98.  
    HAL_Delay(10);
  99.  
    }
  100.  
     
  101.  
    // Send a byte to the command register
  102.  
    void ssd1306_WriteCommand(uint8_t byte) {
  103.  
    HAL_GPIO_WritePin(SSD1306_CS_Port, SSD1306_CS_Pin, GPIO_PIN_RESET); // select OLED
  104.  
    HAL_GPIO_WritePin(SSD1306_DC_Port, SSD1306_DC_Pin, GPIO_PIN_RESET); // command
  105.  
    HAL_SPI_Transmit(&SSD1306_SPI_PORT, (uint8_t *) &byte, 1, HAL_MAX_DELAY);
  106.  
    HAL_GPIO_WritePin(SSD1306_CS_Port, SSD1306_CS_Pin, GPIO_PIN_SET); // un-select OLED
  107.  
    }
  108.  
     
  109.  
    // Send data
  110.  
    void ssd1306_WriteData(uint8_t* buffer, size_t buff_size) {
  111.  
    HAL_GPIO_WritePin(SSD1306_CS_Port, SSD1306_CS_Pin, GPIO_PIN_RESET); // select OLED
  112.  
    HAL_GPIO_WritePin(SSD1306_DC_Port, SSD1306_DC_Pin, GPIO_PIN_SET); // data
  113.  
    HAL_SPI_Transmit(&SSD1306_SPI_PORT, buffer, buff_size, HAL_MAX_DELAY);
  114.  
    HAL_GPIO_WritePin(SSD1306_CS_Port, SSD1306_CS_Pin, GPIO_PIN_SET); // un-select OLED
  115.  
    }
  116.  
     
  117.  
    #else
  118.  
    #error "You should define SSD1306_USE_SPI or SSD1306_USE_I2C macro"
  119.  
    #endif
  120.  
     
  121.  
     
  122.  
    // Screenbuffer
  123.  
    static uint8_t SSD1306_Buffer[SSD1306_BUFFER_SIZE];
  124.  
     
  125.  
    // Screen object
  126.  
    static SSD1306_t SSD1306;
  127.  
     
  128.  
    /* Fills the Screenbuffer with values from a given buffer of a fixed length */
  129.  
    SSD1306_Error_t ssd1306_FillBuffer(uint8_t* buf, uint32_t len) {
  130.  
    SSD1306_Error_t ret = SSD1306_ERR;
  131.  
    if (len <= SSD1306_BUFFER_SIZE) {
  132.  
    memcpy(SSD1306_Buffer,buf,len);
  133.  
    ret = SSD1306_OK;
  134.  
    }
  135.  
    return ret;
  136.  
    }
  137.  
     
  138.  
    // Initialize the oled screen
  139.  
    void ssd1306_Init(void) {
  140.  
    // Reset OLED
  141.  
    ssd1306_Reset();
  142.  
     
  143.  
    // Wait for the screen to boot
  144.  
    HAL_Delay(100);
  145.  
     
  146.  
    // Init OLED
  147.  
    ssd1306_SetDisplayOn(0); //display off
  148.  
     
  149.  
    ssd1306_WriteCommand(0x20); //Set Memory Addressing Mode
  150.  
    ssd1306_WriteCommand(0x00); // 00b,Horizontal Addressing Mode; 01b,Vertical Addressing Mode;
  151.  
    // 10b,Page Addressing Mode (RESET); 11b,Invalid
  152.  
     
  153.  
    ssd1306_WriteCommand(0xB0); //Set Page Start Address for Page Addressing Mode,0-7
  154.  
     
  155.  
    #ifdef SSD1306_MIRROR_VERT
  156.  
    ssd1306_WriteCommand(0xC0); // Mirror vertically
  157.  
    #else
  158.  
    ssd1306_WriteCommand(0xC8); //Set COM Output Scan Direction
  159.  
    #endif
  160.  
     
  161.  
    ssd1306_WriteCommand(0x00); //---set low column address
  162.  
    ssd1306_WriteCommand(0x10); //---set high column address
  163.  
     
  164.  
    ssd1306_WriteCommand(0x40); //--set start line address - CHECK
  165.  
     
  166.  
    ssd1306_SetContrast(0xFF);
  167.  
     
  168.  
    #ifdef SSD1306_MIRROR_HORIZ
  169.  
    ssd1306_WriteCommand(0xA0); // Mirror horizontally
  170.  
    #else
  171.  
    ssd1306_WriteCommand(0xA1); //--set segment re-map 0 to 127 - CHECK
  172.  
    #endif
  173.  
     
  174.  
    #ifdef SSD1306_INVERSE_COLOR
  175.  
    ssd1306_WriteCommand(0xA7); //--set inverse color
  176.  
    #else
  177.  
    ssd1306_WriteCommand(0xA6); //--set normal color
  178.  
    #endif
  179.  
     
  180.  
    // Set multiplex ratio.
  181.  
    #if (SSD1306_HEIGHT == 128)
  182.  
    // Found in the Luma Python lib for SH1106.
  183.  
    ssd1306_WriteCommand(0xFF);
  184.  
    #else
  185.  
    ssd1306_WriteCommand(0xA8); //--set multiplex ratio(1 to 64) - CHECK
  186.  
    #endif
  187.  
     
  188.  
    #if (SSD1306_HEIGHT == 32)
  189.  
    ssd1306_WriteCommand(0x1F); //
  190.  
    #elif (SSD1306_HEIGHT == 64)
  191.  
    ssd1306_WriteCommand(0x3F); //
  192.  
    #elif (SSD1306_HEIGHT == 128)
  193.  
    ssd1306_WriteCommand(0x3F); // Seems to work for 128px high displays too.
  194.  
    #else
  195.  
    #error "Only 32, 64, or 128 lines of height are supported!"
  196.  
    #endif
  197.  
     
  198.  
    ssd1306_WriteCommand(0xA4); //0xa4,Output follows RAM content;0xa5,Output ignores RAM content
  199.  
     
  200.  
    ssd1306_WriteCommand(0xD3); //-set display offset - CHECK
  201.  
    ssd1306_WriteCommand(0x00); //-not offset
  202.  
     
  203.  
    ssd1306_WriteCommand(0xD5); //--set display clock divide ratio/oscillator frequency
  204.  
    ssd1306_WriteCommand(0xF0); //--set divide ratio
  205.  
     
  206.  
    ssd1306_WriteCommand(0xD9); //--set pre-charge period
  207.  
    ssd1306_WriteCommand(0x11); // 0x22 by default
  208.  
     
  209.  
    ssd1306_WriteCommand(0xDA); //--set com pins hardware configuration - CHECK
  210.  
    #if (SSD1306_HEIGHT == 32)
  211.  
    ssd1306_WriteCommand(0x02);
  212.  
    #elif (SSD1306_HEIGHT == 64)
  213.  
    ssd1306_WriteCommand(0x12);
  214.  
    #elif (SSD1306_HEIGHT == 128)
  215.  
    ssd1306_WriteCommand(0x12);
  216.  
    #else
  217.  
    #error "Only 32, 64, or 128 lines of height are supported!"
  218.  
    #endif
  219.  
     
  220.  
    ssd1306_WriteCommand(0xDB); //--set vcomh
  221.  
    ssd1306_WriteCommand(0x30); //0x20,0.77xVcc, 0x30,0.83xVcc
  222.  
     
  223.  
    ssd1306_WriteCommand(0x8D); //--set DC-DC enable
  224.  
    ssd1306_WriteCommand(0x14); //
  225.  
    ssd1306_SetDisplayOn(1); //--turn on SSD1306 panel
  226.  
     
  227.  
    // Clear screen
  228.  
    ssd1306_Fill(Black);
  229.  
     
  230.  
    // Flush buffer to screen
  231.  
    ssd1306_UpdateScreen();
  232.  
     
  233.  
    // Set default values for screen object
  234.  
    SSD1306.CurrentX = 0;
  235.  
    SSD1306.CurrentY = 0;
  236.  
     
  237.  
    SSD1306.Initialized = 1;
  238.  
    }
  239.  
     
  240.  
    // Fill the whole screen with the given color
  241.  
    void ssd1306_Fill(SSD1306_COLOR color) {
  242.  
    /* Set memory */
  243.  
    uint32_t i;
  244.  
     
  245.  
    for(i = 0; i < sizeof(SSD1306_Buffer); i ) {
  246.  
    SSD1306_Buffer[i] = (color == Black) ? 0x00 : 0xFF;
  247.  
    }
  248.  
    }
  249.  
     
  250.  
    // Write the screenbuffer with changed to the screen
  251.  
    void ssd1306_UpdateScreen(void) {
  252.  
    // Write data to each page of RAM. Number of pages
  253.  
    // depends on the screen height:
  254.  
    //
  255.  
    // * 32px == 4 pages
  256.  
    // * 64px == 8 pages
  257.  
    // * 128px == 16 pages
  258.  
     
  259.  
    uint8_t cmd[] = {
  260.  
    0X21, // 设置列起始和结束地址
  261.  
    0X00, // 列起始地址 0
  262.  
    0X7F, // 列终止地址 127
  263.  
    0X22, // 设置页起始和结束地址
  264.  
    0X00, // 页起始地址 0
  265.  
    0X07, // 页终止地址 7
  266.  
    };
  267.  
    uint32_t count = 0;
  268.  
    uint8_t data[sizeof(cmd)*2 SSD1306_BUFFER_SIZE 1] = {};
  269.  
     
  270.  
    // copy cmd
  271.  
    for (uint32_t i = 0; i < sizeof(cmd)/sizeof(cmd[0]); i ) {
  272.  
    data[count ] = SSD1306_CTRL_CMD | SSD1306_MASK_CONT;
  273.  
    data[count ] = cmd[i];
  274.  
    }
  275.  
     
  276.  
    // copy frame data
  277.  
    data[count ] = SSD1306_CTRL_DATA;
  278.  
    memcpy(&data[count], SSD1306_Buffer, sizeof(SSD1306_Buffer));
  279.  
    count = sizeof(SSD1306_Buffer);
  280.  
     
  281.  
    // send to i2c bus
  282.  
    uint32_t retval = ssd1306_SendData(data, count);
  283.  
    // if (retval != WIFI_IOT_SUCCESS) {
  284.  
    code-2.0-canary,新添加的。txwtech
  285.  
    if (retval != HI_ERR_SUCCESS) {
  286.  
     
  287.  
    printf("ssd1306_UpdateScreen send frame data filed: %d!\r\n", retval);
  288.  
    }
  289.  
    }
  290.  
     
  291.  
    // Draw one pixel in the screenbuffer
  292.  
    // X => X Coordinate
  293.  
    // Y => Y Coordinate
  294.  
    // color => Pixel color
  295.  
    void ssd1306_DrawPixel(uint8_t x, uint8_t y, SSD1306_COLOR color) {
  296.  
    if(x >= SSD1306_WIDTH || y >= SSD1306_HEIGHT) {
  297.  
    // Don't write outside the buffer
  298.  
    return;
  299.  
    }
  300.  
     
  301.  
    // Check if pixel should be inverted
  302.  
    if(SSD1306.Inverted) {
  303.  
    color = (SSD1306_COLOR)!color;
  304.  
    }
  305.  
     
  306.  
    // Draw in the right color
  307.  
    if(color == White) {
  308.  
    SSD1306_Buffer[x (y / 8) * SSD1306_WIDTH] |= 1 << (y % 8);
  309.  
    } else {
  310.  
    SSD1306_Buffer[x (y / 8) * SSD1306_WIDTH] &= ~(1 << (y % 8));
  311.  
    }
  312.  
    }
  313.  
     
  314.  
    // Draw 1 char to the screen buffer
  315.  
    // ch => char om weg te schrijven
  316.  
    // Font => Font waarmee we gaan schrijven
  317.  
    // color => Black or White
  318.  
    char ssd1306_DrawChar(char ch, FontDef Font, SSD1306_COLOR color) {
  319.  
    uint32_t i, b, j;
  320.  
     
  321.  
    // Check if character is valid
  322.  
    if (ch < 32 || ch > 126)
  323.  
    return 0;
  324.  
     
  325.  
    // Check remaining space on current line
  326.  
    if (SSD1306_WIDTH < (SSD1306.CurrentX Font.FontWidth) ||
  327.  
    SSD1306_HEIGHT < (SSD1306.CurrentY Font.FontHeight))
  328.  
    {
  329.  
    // Not enough space on current line
  330.  
    return 0;
  331.  
    }
  332.  
     
  333.  
    // Use the font to write
  334.  
    for(i = 0; i < Font.FontHeight; i ) {
  335.  
    b = Font.data[(ch - 32) * Font.FontHeight i];
  336.  
    for(j = 0; j < Font.FontWidth; j ) {
  337.  
    if((b << j) & 0x8000) {
  338.  
    ssd1306_DrawPixel(SSD1306.CurrentX j, (SSD1306.CurrentY i), (SSD1306_COLOR) color);
  339.  
    } else {
  340.  
    ssd1306_DrawPixel(SSD1306.CurrentX j, (SSD1306.CurrentY i), (SSD1306_COLOR)!color);
  341.  
    }
  342.  
    }
  343.  
    }
  344.  
     
  345.  
    // The current space is now taken
  346.  
    SSD1306.CurrentX = Font.FontWidth;
  347.  
     
  348.  
    // Return written char for validation
  349.  
    return ch;
  350.  
    }
  351.  
     
  352.  
    // Write full string to screenbuffer
  353.  
    char ssd1306_DrawString(char* str, FontDef Font, SSD1306_COLOR color) {
  354.  
    // Write until null-byte
  355.  
    while (*str) {
  356.  
    if (ssd1306_DrawChar(*str, Font, color) != *str) {
  357.  
    // Char could not be written
  358.  
    return *str;
  359.  
    }
  360.  
     
  361.  
    // Next char
  362.  
    str ;
  363.  
    }
  364.  
     
  365.  
    // Everything ok
  366.  
    return *str;
  367.  
    }
  368.  
     
  369.  
    // Position the cursor
  370.  
    void ssd1306_SetCursor(uint8_t x, uint8_t y) {
  371.  
    SSD1306.CurrentX = x;
  372.  
    SSD1306.CurrentY = y;
  373.  
    }
  374.  
     
  375.  
    // Draw line by Bresenhem's algorithm
  376.  
    void ssd1306_DrawLine(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, SSD1306_COLOR color) {
  377.  
    int32_t deltaX = abs(x2 - x1);
  378.  
    int32_t deltaY = abs(y2 - y1);
  379.  
    int32_t signX = ((x1 < x2) ? 1 : -1);
  380.  
    int32_t signY = ((y1 < y2) ? 1 : -1);
  381.  
    int32_t error = deltaX - deltaY;
  382.  
    int32_t error2;
  383.  
     
  384.  
    ssd1306_DrawPixel(x2, y2, color);
  385.  
    while((x1 != x2) || (y1 != y2))
  386.  
    {
  387.  
    ssd1306_DrawPixel(x1, y1, color);
  388.  
    error2 = error * 2;
  389.  
    if(error2 > -deltaY)
  390.  
    {
  391.  
    error -= deltaY;
  392.  
    x1 = signX;
  393.  
    }
  394.  
    else
  395.  
    {
  396.  
    /*nothing to do*/
  397.  
    }
  398.  
     
  399.  
    if(error2 < deltaX)
  400.  
    {
  401.  
    error = deltaX;
  402.  
    y1 = signY;
  403.  
    }
  404.  
    else
  405.  
    {
  406.  
    /*nothing to do*/
  407.  
    }
  408.  
    }
  409.  
    return;
  410.  
    }
  411.  
    //Draw polyline
  412.  
    void ssd1306_DrawPolyline(const SSD1306_VERTEX *par_vertex, uint16_t par_size, SSD1306_COLOR color) {
  413.  
    uint16_t i;
  414.  
    if(par_vertex != 0){
  415.  
    for(i = 1; i < par_size; i ){
  416.  
    ssd1306_DrawLine(par_vertex[i - 1].x, par_vertex[i - 1].y, par_vertex[i].x, par_vertex[i].y, color);
  417.  
    }
  418.  
    }
  419.  
    else
  420.  
    {
  421.  
    /*nothing to do*/
  422.  
    }
  423.  
    return;
  424.  
    }
  425.  
    /*Convert Degrees to Radians*/
  426.  
    static float ssd1306_DegToRad(float par_deg) {
  427.  
    return par_deg * 3.14 / 180.0;
  428.  
    }
  429.  
    /*Normalize degree to [0;360]*/
  430.  
    static uint16_t ssd1306_NormalizeTo0_360(uint16_t par_deg) {
  431.  
    uint16_t loc_angle;
  432.  
    if(par_deg <= 360)
  433.  
    {
  434.  
    loc_angle = par_deg;
  435.  
    }
  436.  
    else
  437.  
    {
  438.  
    loc_angle = par_deg % 360;
  439.  
    loc_angle = ((par_deg != 0)?par_deg:360);
  440.  
    }
  441.  
    return loc_angle;
  442.  
    }
  443.  
    /*DrawArc. Draw angle is beginning from 4 quart of trigonometric circle (3pi/2)
  444.  
    * start_angle in degree
  445.  
    * sweep in degree
  446.  
    */
  447.  
    void ssd1306_DrawArc(uint8_t x, uint8_t y, uint8_t radius, uint16_t start_angle, uint16_t sweep, SSD1306_COLOR color) {
  448.  
    #define CIRCLE_APPROXIMATION_SEGMENTS 36
  449.  
    float approx_degree;
  450.  
    uint32_t approx_segments;
  451.  
    uint8_t xp1,xp2;
  452.  
    uint8_t yp1,yp2;
  453.  
    uint32_t count = 0;
  454.  
    uint32_t loc_sweep = 0;
  455.  
    float rad;
  456.  
     
  457.  
    loc_sweep = ssd1306_NormalizeTo0_360(sweep);
  458.  
     
  459.  
    count = (ssd1306_NormalizeTo0_360(start_angle) * CIRCLE_APPROXIMATION_SEGMENTS) / 360;
  460.  
    approx_segments = (loc_sweep * CIRCLE_APPROXIMATION_SEGMENTS) / 360;
  461.  
    approx_degree = loc_sweep / (float)approx_segments;
  462.  
    while(count < approx_segments)
  463.  
    {
  464.  
    rad = ssd1306_DegToRad(count*approx_degree);
  465.  
    xp1 = x (int8_t)(sin(rad)*radius);
  466.  
    yp1 = y (int8_t)(cos(rad)*radius);
  467.  
    count ;
  468.  
    if(count != approx_segments)
  469.  
    {
  470.  
    rad = ssd1306_DegToRad(count*approx_degree);
  471.  
    }
  472.  
    else
  473.  
    {
  474.  
    rad = ssd1306_DegToRad(loc_sweep);
  475.  
    }
  476.  
    xp2 = x (int8_t)(sin(rad)*radius);
  477.  
    yp2 = y (int8_t)(cos(rad)*radius);
  478.  
    ssd1306_DrawLine(xp1,yp1,xp2,yp2,color);
  479.  
    }
  480.  
     
  481.  
    return;
  482.  
    }
  483.  
    //Draw circle by Bresenhem's algorithm
  484.  
    void ssd1306_DrawCircle(uint8_t par_x,uint8_t par_y,uint8_t par_r,SSD1306_COLOR par_color) {
  485.  
    int32_t x = -par_r;
  486.  
    int32_t y = 0;
  487.  
    int32_t err = 2 - 2 * par_r;
  488.  
    int32_t e2;
  489.  
     
  490.  
    if (par_x >= SSD1306_WIDTH || par_y >= SSD1306_HEIGHT) {
  491.  
    return;
  492.  
    }
  493.  
     
  494.  
    do {
  495.  
    ssd1306_DrawPixel(par_x - x, par_y y, par_color);
  496.  
    ssd1306_DrawPixel(par_x x, par_y y, par_color);
  497.  
    ssd1306_DrawPixel(par_x x, par_y - y, par_color);
  498.  
    ssd1306_DrawPixel(par_x - x, par_y - y, par_color);
  499.  
    e2 = err;
  500.  
    if (e2 <= y) {
  501.  
    y ;
  502.  
    err = err (y * 2 1);
  503.  
    if(-x == y && e2 <= x) {
  504.  
    e2 = 0;
  505.  
    }
  506.  
    else
  507.  
    {
  508.  
    /*nothing to do*/
  509.  
    }
  510.  
    }
  511.  
    else
  512.  
    {
  513.  
    /*nothing to do*/
  514.  
    }
  515.  
    if(e2 > x) {
  516.  
    x ;
  517.  
    err = err (x * 2 1);
  518.  
    }
  519.  
    else
  520.  
    {
  521.  
    /*nothing to do*/
  522.  
    }
  523.  
    } while(x <= 0);
  524.  
     
  525.  
    return;
  526.  
    }
  527.  
     
  528.  
    //Draw rectangle
  529.  
    void ssd1306_DrawRectangle(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, SSD1306_COLOR color) {
  530.  
    ssd1306_DrawLine(x1,y1,x2,y1,color);
  531.  
    ssd1306_DrawLine(x2,y1,x2,y2,color);
  532.  
    ssd1306_DrawLine(x2,y2,x1,y2,color);
  533.  
    ssd1306_DrawLine(x1,y2,x1,y1,color);
  534.  
     
  535.  
    return;
  536.  
    }
  537.  
     
  538.  
    void ssd1306_DrawBitmap(const uint8_t* bitmap, uint32_t size)
  539.  
    {
  540.  
    uint8_t rows = size * 8 / SSD1306_WIDTH;
  541.  
    if (rows > SSD1306_HEIGHT) {
  542.  
    rows = SSD1306_HEIGHT;
  543.  
    }
  544.  
    for (uint8_t y = 0; y < rows; y ) {
  545.  
    for (uint8_t x = 0; x < SSD1306_WIDTH; x ) {
  546.  
    uint8_t byte = bitmap[(y * SSD1306_WIDTH / 8) (x / 8)];
  547.  
    uint8_t bit = byte & (0x80 >> (x % 8));
  548.  
    ssd1306_DrawPixel(x, y, bit ? White : Black);
  549.  
    }
  550.  
    }
  551.  
    }
  552.  
     
  553.  
    void ssd1306_DrawRegion(uint8_t x, uint8_t y, uint8_t w, uint8_t h, const uint8_t* data, uint32_t size, uint32_t stride)
  554.  
    {
  555.  
    if (x w > SSD1306_WIDTH || y h > SSD1306_HEIGHT || w * h == 0) {
  556.  
    printf("%dx%d @ %d,%d out of range or invalid!\r\n", w, h, x, y);
  557.  
    return;
  558.  
    }
  559.  
     
  560.  
    w = (w <= SSD1306_WIDTH ? w : SSD1306_WIDTH);
  561.  
    h = (h <= SSD1306_HEIGHT ? h : SSD1306_HEIGHT);
  562.  
    stride = (stride == 0 ? w : stride);
  563.  
     
  564.  
    uint8_t rows = size * 8 / stride;
  565.  
    for (uint8_t i = 0; i < rows; i ) {
  566.  
    uint32_t base = i * stride / 8;
  567.  
    for (uint8_t j = 0; j < w; j ) {
  568.  
    uint32_t idx = base (j / 8);
  569.  
    uint8_t byte = idx < size ? data[idx] : 0;
  570.  
    uint8_t bit = byte & (0x80 >> (j % 8));
  571.  
    ssd1306_DrawPixel(x j, y i, bit ? White : Black);
  572.  
    }
  573.  
    }
  574.  
    }
  575.  
     
  576.  
    void ssd1306_SetContrast(const uint8_t value) {
  577.  
    const uint8_t kSetContrastControlRegister = 0x81;
  578.  
    ssd1306_WriteCommand(kSetContrastControlRegister);
  579.  
    ssd1306_WriteCommand(value);
  580.  
    }
  581.  
     
  582.  
    void ssd1306_SetDisplayOn(const uint8_t on) {
  583.  
    uint8_t value;
  584.  
    if (on) {
  585.  
    value = 0xAF; // Display on
  586.  
    SSD1306.DisplayOn = 1;
  587.  
    } else {
  588.  
    value = 0xAE; // Display off
  589.  
    SSD1306.DisplayOn = 0;
  590.  
    }
  591.  
    ssd1306_WriteCommand(value);
  592.  
    }
  593.  
     
  594.  
    uint8_t ssd1306_GetDisplayOn() {
  595.  
    return SSD1306.DisplayOn;
  596.  
    }

app/build.gn

  1.  
    # Copyright (c) 2020 Huawei Device Co., Ltd.
  2.  
    # Licensed under the Apache License, Version 2.0 (the "License");
  3.  
    # you may not use this file except in compliance with the License.
  4.  
    # You may obtain a copy of the License at
  5.  
    #
  6.  
    # http://www.apache.org/licenses/LICENSE-2.0
  7.  
    #
  8.  
    # Unless required by applicable law or agreed to in writing, software
  9.  
    # distributed under the License is distributed on an "AS IS" BASIS,
  10.  
    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11.  
    # See the License for the specific language governing permissions and
  12.  
    # limitations under the License.
  13.  
     
  14.  
    import("//build/lite/config/component/lite_component.gni")
  15.  
     
  16.  
    lite_component("app") {
  17.  
    features = [
  18.  
    "ssd1306:oled_ssd1306",
  19.  
    "examples:oled_test",
  20.  
    "libm_port:libm_port"
  21.  
    ]
  22.  
    }

c_cpp_properties.json

  1.  
    {
  2.  
    "configurations": [
  3.  
    {
  4.  
    "name": "!!! WARNING !!! AUTO-GENERATED FILE, PLEASE DO NOT MODIFY IT"
  5.  
    },
  6.  
    {
  7.  
    "name": "Win32-debug",
  8.  
    "includePath": [
  9.  
    //"F:/code-2.0-canary/include",
  10.  
    "F:/code-2.0-canary/src",
  11.  
    "F:/utils/native/lite/include",
  12.  
    "F:/domains/iot/link/libbuild",
  13.  
    "F:/base/iot_hardware/peripheral/interfaces/kits",
  14.  
    "F:/third_party/cmsis/CMSIS/RTOS2/Include",
  15.  
    "F:/code-2.0-canary/device/hisilicon/hispark_pegasus/sdk_liteos/include",
  16.  
    "F:/code-2.0-canary/applications/sample/wifi-iot/app/ssd1306",
  17.  
    "F:/code-2.0-canary/applications/sample/wifi-iot/app/libm_port",
  18.  
    //"F:/code-2.0-canary/third_party/mingw-w64/mingw-w64-headers/crt",
  19.  
    ""
  20.  
    ],
  21.  
    "browse": {
  22.  
    "limitSymbolsToIncludedHeaders": true,
  23.  
    "path": [
  24.  
    "F:/code-2.0-canary/include",
  25.  
    "F:/code-2.0-canary/src",
  26.  
    "F:/code-2.0-canary/utils/native/lite/include",
  27.  
    ""
  28.  
    ]
  29.  
    },
  30.  
    "defines": [
  31.  
    ""
  32.  
    ],
  33.  
    "intelliSenseMode": "clang-x64",
  34.  
    "compilerPath": "F:/hcc_riscv32_win/bin/riscv32-unknown-elf-gcc.exe",
  35.  
    "compilerArgs": [
  36.  
    ""
  37.  
    ]
  38.  
    }
  39.  
    ],
  40.  
    "version": 4
  41.  
    }

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhibbabf
系列文章
更多 icon
同类精品
更多 icon
继续加载