当前位置:   article > 正文

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

鸿蒙hi3861风扇驱动

鸿蒙开发板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. #include <ctype.h>
  30. #include <stdio.h>
  31. #include <unistd.h>
  32. #include "ohos_init.h"
  33. #include "cmsis_os2.h"
  34. //#include "wifiiot_gpio.h"
  35. #include "hi_io.h"
  36. //#include "wifiiot_gpio_ex.h"
  37. //#include "wifiiot_pwm.h"
  38. #include "hi_pwm.h"
  39. #include "wifiiot_adc.h"
  40. #include "hi_adc.h"
  41. //#include "wifiiot_i2c.h"
  42. #include "hi_i2c.h"
  43. //#include "wifiiot_errno.h"
  44. #include "hi_errno.h"
  45. //#include "wifiiot_watchdog.h"
  46. #include "hi_watchdog.h"
  47. #include "ssd1306.h"
  48. #include "ssd1306_tests.h"
  49. #define OLED_I2C_BAUDRATE 400*1000
  50. const unsigned char headSize[] = { 64, 64 };
  51. const unsigned char headData[] = {
  52. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  53. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  54. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  55. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  56. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  57. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  58. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  59. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60. 0x00, 0x00, 0x00, 0x00, 0x1F, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0xFF, 0x80, 0x00,
  61. 0x00, 0x00, 0x00, 0x01, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x07, 0xFF, 0xFF, 0xE0, 0x00,
  62. 0x00, 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xF0, 0x00,
  63. 0x00, 0x00, 0x00, 0x3F, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x3F, 0xFF, 0xFF, 0xF0, 0x00,
  64. 0x00, 0x00, 0x00, 0x7F, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x7F, 0xFF, 0xFF, 0xF0, 0x00,
  65. 0x00, 0x00, 0x00, 0xFF, 0x83, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xE3, 0xF0, 0x18, 0x00,
  66. 0x00, 0x00, 0x01, 0xFF, 0xE3, 0xE0, 0x10, 0x00, 0x00, 0x00, 0x61, 0xFE, 0x03, 0xE1, 0xF8, 0x00,
  67. 0x00, 0xC1, 0xF1, 0xF8, 0x97, 0xE0, 0x18, 0x00, 0x00, 0x00, 0xB9, 0xFF, 0xFF, 0xF8, 0x88, 0x00,
  68. 0x00, 0x00, 0xFD, 0xFF, 0xFF, 0xFD, 0xF8, 0x00, 0x00, 0x00, 0xFD, 0xFF, 0xFF, 0xFE, 0x78, 0x00,
  69. 0x00, 0x00, 0xCD, 0xFF, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x40, 0xDF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00,
  70. 0x00, 0x40, 0xDF, 0xFF, 0xFF, 0xFF, 0xF8, 0x00, 0x00, 0x40, 0x7F, 0xFF, 0xFF, 0xFF, 0xF8, 0x00,
  71. 0x00, 0xA0, 0x7F, 0xFF, 0xFF, 0xFF, 0xF9, 0x00, 0x01, 0x20, 0x3F, 0xFF, 0xFC, 0x47, 0xF8, 0x00,
  72. 0x02, 0x10, 0x1F, 0xFF, 0xFF, 0x87, 0xF0, 0x00, 0x0C, 0x18, 0x0F, 0xFF, 0xFF, 0xFF, 0xF0, 0x00,
  73. 0x00, 0x08, 0x07, 0xFF, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x0D, 0x07, 0xFF, 0xFC, 0x07, 0xF0, 0x00,
  74. 0x00, 0x05, 0x07, 0xFF, 0xC0, 0x01, 0xF0, 0x00, 0x00, 0x05, 0x07, 0xFF, 0xDF, 0xE1, 0xE0, 0x00,
  75. 0x02, 0x06, 0x07, 0xFF, 0xFF, 0x09, 0xE0, 0x00, 0x00, 0x02, 0x07, 0xFF, 0xFE, 0x1F, 0xC0, 0x00,
  76. 0x00, 0x02, 0x07, 0xFF, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x60, 0x3F, 0xFF, 0xFF, 0xFF, 0x80, 0x00,
  77. 0x00, 0x30, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x32, 0xFF, 0xFF, 0xFF, 0xFC, 0x00, 0x00,
  78. 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0x18, 0x00, 0x00, 0x00, 0x1F, 0xFF, 0xFF, 0xFF, 0xF0, 0x00, 0x00,
  79. 0x00, 0x0F, 0xFF, 0xFF, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x0F, 0xFF, 0xFF, 0xFC, 0xF0, 0x00, 0x00,
  80. 0x00, 0x07, 0xFF, 0xFF, 0xFE, 0xF0, 0x00, 0x01, 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xF0, 0x00, 0x07,
  81. 0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xF0, 0xE0, 0x3F, 0x00, 0x23, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  82. 0x00, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  83. 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF,
  84. };
  85. /**
  86. * 汉字字模在线: https://www.23bei.com/tool-223.html
  87. * 数据排列:从左到右从上到下
  88. * 取模方式:横向8位左高位
  89. **/
  90. void TestDrawChinese1(void)
  91. {
  92. const uint32_t W = 16, H = 16;
  93. uint8_t fonts[][32] = {
  94. {
  95. /*-- ID:0,字符:"你",ASCII编码:C4E3,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
  96. //0x11,0x00,0x11,0x00,0x11,0x00,0x23,0xFC,0x22,0x04,0x64,0x08,0xA8,0x40,0x20,0x40,
  97. //0x21,0x50,0x21,0x48,0x22,0x4C,0x24,0x44,0x20,0x40,0x20,0x40,0x21,0x40,0x20,0x80,
  98. /*-- ID:0,字符:"阿",ASCII编码:B0A2,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
  99. 0x00,0x04,0x7F,0xFE,0x44,0x08,0x48,0x08,0x48,0x28,0x51,0xF8,0x49,0x28,0x49,0x28,
  100. 0x45,0x28,0x45,0x28,0x45,0x28,0x69,0xE8,0x51,0x28,0x40,0x08,0x40,0x28,0x40,0x10,
  101. },{
  102. /*-- ID:1,字符:"好",ASCII编码:BAC3,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
  103. //0x10,0x00,0x11,0xFC,0x10,0x04,0x10,0x08,0xFC,0x10,0x24,0x20,0x24,0x24,0x27,0xFE,
  104. //0x24,0x20,0x44,0x20,0x28,0x20,0x10,0x20,0x28,0x20,0x44,0x20,0x84,0xA0,0x00,0x40,
  105. /*-- ID:1,字符:"武",ASCII编码:CEE4,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
  106. 0x00,0x40,0x02,0x50,0x3F,0x48,0x00,0x48,0x00,0x40,0xFF,0xFE,0x04,0x40,0x04,0x40,
  107. 0x27,0x40,0x24,0x40,0x24,0x20,0x24,0x20,0x27,0xA0,0xF8,0x12,0x40,0x0A,0x00,0x06,
  108. },{
  109. /*-- ID:2,字符:"鸿",ASCII编码:BAE8,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
  110. //0x40,0x20,0x30,0x48,0x10,0xFC,0x02,0x88,0x9F,0xA8,0x64,0x88,0x24,0xA8,0x04,0x90,
  111. //0x14,0x84,0x14,0xFE,0xE7,0x04,0x3C,0x24,0x29,0xF4,0x20,0x04,0x20,0x14,0x20,0x08,
  112. /*-- ID:2,字符:"科",ASCII编码:BFC6,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
  113. 0x04,0x10,0x0E,0x10,0xF8,0x90,0x08,0x50,0x08,0x10,0xFE,0x90,0x08,0x50,0x1C,0x14,
  114. 0x1A,0x1E,0x29,0xF0,0x28,0x10,0x48,0x10,0x88,0x10,0x08,0x10,0x08,0x10,0x08,0x10,
  115. },{
  116. /*-- ID:3,字符:"蒙",ASCII编码:C3C9,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
  117. //0x04,0x48,0x7F,0xFC,0x04,0x40,0x7F,0xFE,0x40,0x02,0x8F,0xE4,0x00,0x00,0x7F,0xFC,
  118. // 0x06,0x10,0x3B,0x30,0x05,0xC0,0x1A,0xA0,0x64,0x90,0x18,0x8E,0x62,0x84,0x01,0x00
  119. /*-- ID:3,字符:"技",ASCII编码:BCBC,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
  120. 0x10,0x40,0x10,0x40,0x10,0x48,0x13,0xFC,0xFC,0x40,0x10,0x40,0x10,0x40,0x13,0xF8,
  121. 0x1A,0x08,0x31,0x10,0xD1,0x10,0x10,0xA0,0x10,0x40,0x10,0xB0,0x51,0x0E,0x26,0x04,
  122. },
  123. {/*-- ID:4,字符:"t",ASCII编码:A3F4,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
  124. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x01,0x80,0x01,0x80,0x07,0xE0,0x01,0x80,
  125. 0x01,0x80,0x01,0x80,0x01,0x90,0x01,0x90,0x00,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,
  126. },
  127. {/*-- ID:5,字符:"x",ASCII编码:A3F8,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
  128. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x3C,0x0C,0x30,
  129. 0x03,0x60,0x01,0xC0,0x03,0x60,0x0C,0x30,0x3E,0x7C,0x00,0x00,0x00,0x00,0x00,0x00,},
  130. {/*-- ID:6,字符:"w",ASCII编码:A3F7,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
  131. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3D,0xEE,0x18,0xC4,
  132. 0x08,0xCC,0x0D,0x68,0x05,0x28,0x06,0x30,0x02,0x10,0x00,0x00,0x00,0x00,0x00,0x00,},
  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. {/*-- ID:7,字符:"e",ASCII编码:A3E5,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
  138. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xE0,0x0C,0x30,
  139. 0x0C,0x30,0x0F,0xF0,0x0C,0x00,0x0C,0x30,0x07,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,
  140. },
  141. {/*-- ID:8,字符:"c",ASCII编码:A3E3,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
  142. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xE0,0x0C,0x30,
  143. 0x0C,0x00,0x0C,0x00,0x0C,0x00,0x0C,0x30,0x07,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,
  144. },
  145. {/*-- ID:9,字符:"h",ASCII编码:A3E8,对应字:宽x高=16x16,画布:宽W=16 高H=16,共32字节*/
  146. 0x00,0x00,0x00,0x00,0x0E,0x00,0x06,0x00,0x06,0x00,0x06,0x00,0x06,0xE0,0x07,0x30,
  147. 0x06,0x30,0x06,0x30,0x06,0x30,0x06,0x30,0x0E,0x78,0x00,0x00,0x00,0x00,0x00,0x00,}
  148. };
  149. ssd1306_Fill(Black);
  150. for (size_t i = 0; i < sizeof(fonts)/sizeof(fonts[0]); i++) {
  151. ssd1306_DrawRegion(i * W, 0, W, H, fonts[i], sizeof(fonts[0]), W);
  152. }
  153. ssd1306_SetCursor(64, 0);
  154. ssd1306_DrawRegion(64, 0, headSize[0], headSize[1], headData, sizeof(headData), headSize[0]);
  155. ssd1306_UpdateScreen();
  156. }
  157. void TestDrawChinese2(void)
  158. {
  159. const uint32_t W = 12, H = 12, S = 16;
  160. uint8_t fonts[][24] = {
  161. {
  162. /*-- ID:0,字符:"你",ASCII编码:C4E3,对应字:宽x高=12x12,画布:宽W=16 高H=12,共24字节*/
  163. 0x12,0x00,0x12,0x00,0x27,0xF0,0x24,0x20,0x69,0x40,0xA1,0x00,0x25,0x40,0x25,0x20,
  164. 0x29,0x10,0x31,0x10,0x25,0x00,0x22,0x00,
  165. },{
  166. /*-- ID:1,字符:"好",ASCII编码:BAC3,对应字:宽x高=12x12,画布:宽W=16 高H=12,共24字节*/
  167. 0x20,0x00,0x27,0xE0,0x20,0x40,0xF8,0x80,0x48,0x80,0x48,0xA0,0x57,0xF0,0x50,0x80,
  168. 0x30,0x80,0x28,0x80,0x4A,0x80,0x81,0x00,
  169. },{
  170. /*-- ID:2,字符:"鸿",ASCII编码:BAE8,对应字:宽x高=12x12,画布:宽W=16 高H=12,共24字节*/
  171. 0x00,0x40,0x80,0x80,0x5D,0xE0,0x09,0x20,0xC9,0xA0,0x09,0x60,0x29,0x00,0xCD,0xF0,
  172. 0x58,0x10,0x43,0xD0,0x40,0x10,0x40,0x60,
  173. },{
  174. /*-- ID:3,字符:"蒙",ASCII编码:C3C9,对应字:宽x高=12x12,画布:宽W=16 高H=12,共24字节*/
  175. 0x09,0x00,0x7F,0xE0,0x09,0x00,0x7F,0xF0,0x80,0x10,0x7F,0xE0,0x0C,0x40,0x32,0x80,
  176. 0xC7,0x00,0x0A,0x80,0x32,0x70,0xC6,0x20
  177. }
  178. };
  179. ssd1306_Fill(Black);
  180. for (size_t i = 0; i < sizeof(fonts)/sizeof(fonts[0]); i++) {
  181. ssd1306_DrawRegion(i * H, 0, W, H, fonts[i], sizeof(fonts[0]), S);
  182. }
  183. ssd1306_SetCursor(64, 0);
  184. ssd1306_DrawRegion(64, 0, headSize[0], headSize[1], headData, sizeof(headData), headSize[0]);
  185. ssd1306_SetCursor(0, 64 - 8);
  186. ssd1306_DrawString("txwtech.com", Font_6x8, White);
  187. ssd1306_SetCursor(0, 64 - 8*2);
  188. ssd1306_DrawString("@xtang10", Font_6x8, White);
  189. ssd1306_UpdateScreen();
  190. }
  191. void TestShowChars(FontDef font, uint8_t w, uint8_t h)
  192. {
  193. ssd1306_Fill(Black);
  194. uint8_t x = 0, y = 0;
  195. for (uint8_t c = 1; c < 128; c++) {
  196. if (isprint(c)) {
  197. ssd1306_SetCursor(x, y);
  198. ssd1306_DrawChar((char) c, font, White);
  199. x += w;
  200. if (x >= SSD1306_WIDTH) {
  201. x = 0;
  202. y += h;
  203. }
  204. }
  205. }
  206. ssd1306_UpdateScreen();
  207. }
  208. void Ssd1306TestTask(void* arg)
  209. {
  210. (void) arg;
  211. // GpioInit();
  212. hi_gpio_init();
  213. // IoSetFunc(WIFI_IOT_IO_NAME_GPIO_13, WIFI_IOT_IO_FUNC_GPIO_13_I2C0_SDA);
  214. // IoSetFunc(WIFI_IOT_IO_NAME_GPIO_14, WIFI_IOT_IO_FUNC_GPIO_14_I2C0_SCL);
  215. // I2cInit(WIFI_IOT_I2C_IDX_0, OLED_I2C_BAUDRATE);
  216. hi_io_set_func(HI_IO_NAME_GPIO_13, HI_IO_FUNC_GPIO_13_I2C0_SDA);
  217. hi_io_set_func(HI_IO_NAME_GPIO_14, HI_IO_FUNC_GPIO_14_I2C0_SCL);
  218. hi_io_set_func(HI_I2C_IDX_0, OLED_I2C_BAUDRATE);
  219. // WatchDogDisable();
  220. hi_watchdog_disable();
  221. usleep(20*1000);
  222. ssd1306_Init();
  223. ssd1306_Fill(Black);
  224. ssd1306_SetCursor(0, 0);
  225. ssd1306_DrawString("Hello HarmonyOS!", Font_7x10, White);
  226. uint32_t start = HAL_GetTick();
  227. ssd1306_UpdateScreen();
  228. uint32_t end = HAL_GetTick();
  229. printf("ssd1306_UpdateScreen time cost: %d ms.\r\n", end - start);
  230. while (1) {
  231. TestDrawChinese1();
  232. osDelay(500);
  233. TestDrawChinese2();
  234. osDelay(500);
  235. // TestShowChars(Font_6x8, 6, 8);
  236. // osDelay(500);
  237. // TestShowChars(Font_7x10, 7, 10);
  238. // osDelay(500);
  239. ssd1306_TestAll();
  240. }
  241. }
  242. void Ssd1306TestDemo(void)
  243. {
  244. osThreadAttr_t attr;
  245. attr.name = "Ssd1306Task";
  246. attr.attr_bits = 0U;
  247. attr.cb_mem = NULL;
  248. attr.cb_size = 0U;
  249. attr.stack_mem = NULL;
  250. attr.stack_size = 10240;
  251. attr.priority = osPriorityNormal;
  252. if (osThreadNew(Ssd1306TestTask, NULL, &attr) == NULL) {
  253. printf("[Ssd1306TestDemo] Falied to create Ssd1306TestTask!\n");
  254. }
  255. }
  256. 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. static_library("oled_test") {
  28. sources = [
  29. "ssd1306_demo.c",
  30. "ssd1306_tests.c",
  31. ]
  32. include_dirs = [
  33. "../ssd1306",
  34. "//utils/native/lite/include",
  35. "//kernel/liteos_m/components/cmsis/2.0",
  36. "//base/iot_hardware/interfaces/kits/wifiiot_lite",
  37. "//applications/sample/wifi-iot/app/ssd1306",
  38. ]
  39. }

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. static_library("oled_ssd1306") {
  28. sources = [
  29. "ssd1306.c",
  30. "ssd1306_fonts.c",
  31. ]
  32. include_dirs = [
  33. "//utils/native/lite/include",
  34. "//kernel/liteos_m/components/cmsis/2.0",
  35. "//base/iot_hardware/interfaces/kits/wifiiot_lite",
  36. ]
  37. }

ssd1306文件夹的ssd1306.c

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

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. import("//build/lite/config/component/lite_component.gni")
  14. lite_component("app") {
  15. features = [
  16. "ssd1306:oled_ssd1306",
  17. "examples:oled_test",
  18. "libm_port:libm_port"
  19. ]
  20. }

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. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/223412?site
推荐阅读
相关标签
  

闽ICP备14008679号