Advertisement
slendi_uwu

Untitled

Mar 5th, 2025
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.93 KB | None | 0 0
  1. main.c (just the rendering and wiringPi part):
  2.  
  3. #include <raylib.h>
  4.  
  5. #ifdef RPI
  6. #include "sharp.h"
  7. #include <wiringPi.h>
  8. #endif // RPI
  9.  
  10. int main(void)
  11. {
  12.     // ...
  13. #ifdef RPI
  14.     wiringPiSetup();
  15.     sharp_begin();
  16. #endif // RPI
  17.     // ...
  18.  
  19.     SetTargetFPS(50);
  20.  
  21.     while (!WindowShouldClose()) {
  22.         // ...
  23.         {
  24.             static unsigned char const set[8] = { 1, 2, 4, 8, 16, 32, 64, 128 };
  25.             static unsigned char const clr[8] = { 0xFE, 0xFD, 0xFB, 0xF7, 0xEF, 0xDF, 0xBF, 0x7F };
  26.  
  27.             SetTraceLogLevel(LOG_NONE);
  28.             Image img = LoadImageFromTexture(tex_composite.texture);
  29.             for (int y = 0; y < 240; y++) {
  30.                 for (int x = 0; x < 400; x++) {
  31.                     Color c = GetImageColor(img, x, y);
  32.                     int   avg = (c.r + c.g + c.b) / 3;
  33.                     int   byte_idx = (y * 400 + x) / 8;
  34.                     int   bit_idx = x & 7;
  35.                     if (avg > 127)
  36.                         g_sharp_buf[byte_idx] |= set[bit_idx];
  37.                     else
  38.                         g_sharp_buf[byte_idx] &= clr[bit_idx];
  39.                 }
  40.             }
  41.             sharp_push();
  42.             UnloadImage(img);
  43.             SetTraceLogLevel(LOG_TRACE);
  44.         }
  45.     }
  46.     // ...
  47. }
  48.  
  49.  
  50. sharp.c:
  51.  
  52. #include "sharp.h"
  53.  
  54. #include <stdlib.h>
  55. #include <string.h>
  56.  
  57. #include <raylib.h>
  58. #include <wiringPi.h>
  59. #include <wiringPiSPI.h>
  60.  
  61. #define WIDTH 400
  62. #define HEIGHT 240
  63. #define BUFFER_SIZE ((WIDTH * HEIGHT) / 8)
  64. #define STRIDE (WIDTH / 8)
  65. #define PACKET_SIZE (STRIDE + 2)
  66.  
  67. #define SHARPMEM_BIT_VCOM 0x40
  68. #define SHARPMEM_BIT_CLEAR 0x20
  69. #define SHARPMEM_BIT_WRITECMD 0x80
  70.  
  71. static int const CS_PIN = 23; // wiringPi pin for CS
  72. static int const SPI_CHAN = 0; // Using /dev/spidev0.0
  73. static int const SPI_SPD = 2000000; // 2MHz SPI clock
  74.  
  75. unsigned char *g_sharp_buf = NULL;
  76.  
  77. unsigned char bit_swap(unsigned char b)
  78. {
  79.     b = ((b & 0xF0) >> 4) | ((b & 0x0F) << 4);
  80.     b = ((b & 0xCC) >> 2) | ((b & 0x33) << 2);
  81.     b = ((b & 0xAA) >> 1) | ((b & 0x55) << 1);
  82.     return b;
  83. }
  84.  
  85. int sharp_begin(void)
  86. {
  87.     if (wiringPiSPISetup(SPI_CHAN, SPI_SPD) < 0) {
  88.         TraceLog(LOG_FATAL, "SPI setup failed\n");
  89.         return 0;
  90.     }
  91.     pinMode(CS_PIN, OUTPUT);
  92.     digitalWrite(CS_PIN, LOW);
  93.  
  94.     g_sharp_buf = (unsigned char *)malloc(BUFFER_SIZE);
  95.     if (!g_sharp_buf) {
  96.         TraceLog(LOG_FATAL, "Buffer allocation failed\n");
  97.         return 0;
  98.     }
  99.     memset(g_sharp_buf, 0xFF, BUFFER_SIZE);
  100.     return 1;
  101. }
  102.  
  103. void sharp_push(void)
  104. {
  105.     static unsigned char vcom = SHARPMEM_BIT_VCOM;
  106.  
  107.     digitalWrite(CS_PIN, HIGH);
  108.     unsigned char cmd = vcom | SHARPMEM_BIT_WRITECMD;
  109.     wiringPiSPIDataRW(SPI_CHAN, &cmd, 1);
  110.     vcom = vcom ? 0x00 : SHARPMEM_BIT_VCOM;
  111.  
  112.     static unsigned char line_data[PACKET_SIZE];
  113.     for (int i = 0; i < BUFFER_SIZE; i += STRIDE) {
  114.         int currentLine = (i / STRIDE) + 1;
  115.         line_data[0] = bit_swap((unsigned char)currentLine);
  116.         for (int j = 0; j < STRIDE; j++) {
  117.             line_data[1 + j] = bit_swap(g_sharp_buf[i + j]);
  118.         }
  119.         line_data[STRIDE + 1] = 0x00; // End-of-line marker
  120.         wiringPiSPIDataRW(SPI_CHAN, line_data, PACKET_SIZE);
  121.     }
  122.     unsigned char trailing = 0x00;
  123.     wiringPiSPIDataRW(SPI_CHAN, &trailing, 1);
  124.     digitalWrite(CS_PIN, LOW);
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement