waveshare_epaper

Waveshare e-paper display shenanigans
git clone git://bsandro.tech/waveshare_epaper
Log | Files | Refs | README

EPD_7in5_V2_test.c (7266B)


      1 /*****************************************************************************
      2 * | File      	:   EPD_7in5_V2_test.c
      3 * | Author      :   Waveshare team
      4 * | Function    :   7.5inch e-paper test demo
      5 * | Info        :
      6 *----------------
      7 * |	This version:   V1.0
      8 * | Date        :   2019-06-13
      9 * | Info        :
     10 #
     11 # Permission is hereby granted, free of charge, to any person obtaining a copy
     12 # of this software and associated documnetation files (the "Software"), to deal
     13 # in the Software without restriction, including without limitation the rights
     14 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     15 # copies of the Software, and to permit persons to  whom the Software is
     16 # furished to do so, subject to the following conditions:
     17 #
     18 # The above copyright notice and this permission notice shall be included in
     19 # all copies or substantial portions of the Software.
     20 #
     21 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     22 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     23 # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     24 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     25 # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     26 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     27 # THE SOFTWARE.
     28 #
     29 ******************************************************************************/
     30 #include "EPD_Test.h"
     31 #include "EPD_7in5_V2.h"
     32 #include <assert.h>
     33 #include <sys/stat.h>
     34 
     35 #define STB_TRUETYPE_IMPLEMENTATION
     36 #include "stb_truetype.h"
     37 #include <uchar.h>
     38 #include <wchar.h>
     39 #include <stdlib.h>
     40 #include <locale.h>
     41 
     42 char * execproc(char *cmdname)
     43 {
     44     const size_t buf_size = 4096;
     45     FILE *cmd = popen(cmdname, "r");
     46     if (!cmd)
     47         return NULL;
     48 
     49     char *cmd_out = malloc(buf_size);
     50     if (cmd_out == NULL) {
     51         perror("malloc error");
     52         exit(1);
     53     }
     54     bzero(cmd_out, buf_size);
     55     int len = 0;
     56     int cnt = 0;
     57     while (fgets(cmd_out+len, buf_size, cmd) != NULL) {
     58         //cmd_out[strcspn(cmd_out, "\r\n")] = 0;
     59         printf("len: %ld/%d\n", strlen(cmd_out+len), len);
     60         //printf("str: %s\n", cmd_out+len);
     61         len += strlen(cmd_out+len);
     62         if (++cnt > 6) break; // only top part without next days table
     63     }
     64     pclose(cmd);
     65     //printf("out: %s\n", cmd_out);
     66 
     67     return cmd_out;
     68 }
     69 
     70 // the number of characters in a multibyte string is the sum of mblen()'s
     71 // note: the simpler approach is mbstowcs(NULL, str, sz)
     72 size_t strlen_mb(const char* ptr)
     73 {
     74     size_t result = 0;
     75     const char* end = ptr + strlen(ptr);
     76     mblen(NULL, 0); // reset the conversion state
     77     while(ptr < end) {
     78         int next = mblen(ptr, end - ptr);
     79         if (next == -1) {
     80            perror("strlen_mb");
     81            break;
     82         }
     83         ptr += next;
     84         ++result;
     85     }
     86     return result;
     87 }
     88 
     89 int EPD_7in5_V2_test(void)
     90 {
     91     printf("EPD_7IN5_V2_test Demo\r\n");
     92     setlocale(LC_ALL, "en_US.utf8");
     93 
     94     if (DEV_Module_Init() != 0) {
     95         return -1;
     96     }
     97 
     98     printf("e-Paper Init and Clear...\n");
     99     EPD_7IN5_V2_Init();
    100     printf("e-paper init ok\n");
    101 //    EPD_7IN5_V2_Clear();
    102 //    DEV_Delay_ms(500);
    103 
    104     //Create a new image cache
    105     UBYTE *BlackImage;
    106     /* you have to edit the startup_stm32fxxx.s file and set a big enough heap size */
    107     UWORD Imagesize = ((EPD_7IN5_V2_WIDTH % 8 == 0)? (EPD_7IN5_V2_WIDTH / 8 ): (EPD_7IN5_V2_WIDTH / 8 + 1)) * EPD_7IN5_V2_HEIGHT;
    108     printf("imgsize: %u\n", Imagesize);
    109     if ((BlackImage = (UBYTE *)malloc(Imagesize)) == NULL) {
    110         printf("Failed to apply for black memory...\r\n");
    111         return -1;
    112     }
    113     Paint_NewImage(BlackImage, EPD_7IN5_V2_WIDTH, EPD_7IN5_V2_HEIGHT, 0, WHITE);
    114     printf("Paint_NewImage ok\n");
    115 
    116 // load font
    117     const char *fontname = "/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf";
    118     struct stat fileinfo;
    119     if (stat(fontname, &fileinfo) != 0) {
    120         perror("stat(fontname)");
    121         return -1;
    122     }
    123     printf("%s size: %lu bytes\n", fontname, fileinfo.st_size);
    124 
    125     unsigned char *ttf_buf = malloc(fileinfo.st_size);
    126     if (ttf_buf == NULL) {
    127         perror("malloc");
    128         return -1;
    129     }
    130 
    131     if (fread(ttf_buf, 1, fileinfo.st_size, fopen(fontname, "rb")) != fileinfo.st_size) {
    132         printf("file read error\n");
    133         return -1;
    134     }
    135     stbtt_fontinfo font;
    136     stbtt_InitFont(&font, ttf_buf, stbtt_GetFontOffsetForIndex(ttf_buf, 0));
    137     printf("font init ok\n");
    138 
    139     //1.Select Image
    140     printf("SelectImage:BlackImage\r\n");
    141     Paint_SelectImage(BlackImage);
    142     Paint_Clear(WHITE);
    143 
    144     // 2.Drawing on the image
    145     printf("Drawing:BlackImage\r\n");
    146 
    147     char *data = execproc("/home/bsandro/src/wego/wego");
    148     char *token, *string, *tofree;
    149     tofree = string = strdup(data);
    150     assert(string != NULL);
    151     int offset = 0;
    152     int fontsize = 50;
    153     float scale = stbtt_ScaleForPixelHeight(&font, fontsize);
    154     printf("scale ok\n");
    155     int ascent = 0;
    156     stbtt_GetFontVMetrics(&font, &ascent, 0, 0);
    157     printf("get vmetrics ok\n");
    158     int baseline = (int)(ascent*scale);
    159     unsigned char screen[EPD_7IN5_V2_HEIGHT][EPD_7IN5_V2_WIDTH];
    160     memset(&screen, 0, sizeof(screen));
    161 
    162     while ((token = strsep(&string, "\r\n")) != NULL) {
    163         printf("%3d: %s\n", offset, token);
    164         int ch = 0;
    165         float xpos = 2;
    166         size_t token_len = strlen_mb(token);
    167         wchar_t u32token[token_len];
    168         mbstowcs(u32token, token, token_len);
    169 
    170         while (ch < token_len) { //(u32token[ch]) {
    171             int advance, lsb, x0, y0, x1, y1;
    172             stbtt_GetCodepointHMetrics(&font, u32token[ch], &advance, &lsb);
    173             stbtt_GetCodepointBitmapBox(&font, u32token[ch], scale, scale, &x0, &y0, &x1, &y1);
    174             stbtt_MakeCodepointBitmap(
    175                 &font,
    176                 &screen[offset+baseline+y0][(int)xpos+x0],
    177                 x1-x0,
    178                 y1-y0,
    179                 EPD_7IN5_V2_WIDTH,
    180                 scale,
    181                 scale,
    182                 u32token[ch]
    183             );
    184             xpos += (advance*scale);
    185             if (ch+1 < token_len) {//u32token[ch+1]) {
    186                 xpos += scale*stbtt_GetCodepointKernAdvance(&font, u32token[ch], u32token[ch+1]);
    187             }
    188             ++ch;
    189         }
    190 
    191         offset += fontsize;
    192     }
    193     printf("offset:%d\n", offset);
    194     free(tofree);
    195     free(data);
    196 
    197     /*for (int j=0; j < EPD_7IN5_V2_HEIGHT/2; ++j) {
    198       for (int i=0; i < EPD_7IN5_V2_WIDTH*0.7; ++i)
    199          putchar(" .:ioVM@"[screen[j][i]>>5]);
    200       putchar('\n');
    201     }*/
    202 
    203     Paint_SelectImage(BlackImage);
    204     Paint_Clear(WHITE);
    205     for (int y = 0; y < EPD_7IN5_V2_HEIGHT; ++y) {
    206         for (int x = 0; x < EPD_7IN5_V2_WIDTH; ++x) {
    207             if (screen[y][x]) {
    208                 Paint_DrawPoint(x, y, BLACK, DOT_PIXEL_1X1, DOT_FILL_AROUND);
    209             }
    210         }
    211     }
    212 
    213     printf("EPD_Display\r\n");
    214     EPD_7IN5_V2_Display(BlackImage);
    215     //DEV_Delay_ms(2000);
    216 
    217     //printf("Clear...\r\n");
    218     //EPD_7IN5_V2_Clear();
    219 
    220     printf("Goto Sleep...\r\n");
    221     EPD_7IN5_V2_Sleep();
    222     free(BlackImage);
    223     BlackImage = NULL;
    224 
    225     // close 5V
    226     printf("close 5V, Module enters 0 power consumption ...\r\n");
    227     DEV_Module_Exit();
    228 
    229     return 0;
    230 }
    231