Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- main.c (just the rendering and wiringPi part):
- #include <raylib.h>
- #ifdef RPI
- #include "sharp.h"
- #include <wiringPi.h>
- #endif // RPI
- int main(void)
- {
- // ...
- #ifdef RPI
- wiringPiSetup();
- sharp_begin();
- #endif // RPI
- // ...
- SetTargetFPS(50);
- while (!WindowShouldClose()) {
- // ...
- {
- static unsigned char const set[8] = { 1, 2, 4, 8, 16, 32, 64, 128 };
- static unsigned char const clr[8] = { 0xFE, 0xFD, 0xFB, 0xF7, 0xEF, 0xDF, 0xBF, 0x7F };
- SetTraceLogLevel(LOG_NONE);
- Image img = LoadImageFromTexture(tex_composite.texture);
- for (int y = 0; y < 240; y++) {
- for (int x = 0; x < 400; x++) {
- Color c = GetImageColor(img, x, y);
- int avg = (c.r + c.g + c.b) / 3;
- int byte_idx = (y * 400 + x) / 8;
- int bit_idx = x & 7;
- if (avg > 127)
- g_sharp_buf[byte_idx] |= set[bit_idx];
- else
- g_sharp_buf[byte_idx] &= clr[bit_idx];
- }
- }
- sharp_push();
- UnloadImage(img);
- SetTraceLogLevel(LOG_TRACE);
- }
- }
- // ...
- }
- sharp.c:
- #include "sharp.h"
- #include <stdlib.h>
- #include <string.h>
- #include <raylib.h>
- #include <wiringPi.h>
- #include <wiringPiSPI.h>
- #define WIDTH 400
- #define HEIGHT 240
- #define BUFFER_SIZE ((WIDTH * HEIGHT) / 8)
- #define STRIDE (WIDTH / 8)
- #define PACKET_SIZE (STRIDE + 2)
- #define SHARPMEM_BIT_VCOM 0x40
- #define SHARPMEM_BIT_CLEAR 0x20
- #define SHARPMEM_BIT_WRITECMD 0x80
- static int const CS_PIN = 23; // wiringPi pin for CS
- static int const SPI_CHAN = 0; // Using /dev/spidev0.0
- static int const SPI_SPD = 2000000; // 2MHz SPI clock
- unsigned char *g_sharp_buf = NULL;
- unsigned char bit_swap(unsigned char b)
- {
- b = ((b & 0xF0) >> 4) | ((b & 0x0F) << 4);
- b = ((b & 0xCC) >> 2) | ((b & 0x33) << 2);
- b = ((b & 0xAA) >> 1) | ((b & 0x55) << 1);
- return b;
- }
- int sharp_begin(void)
- {
- if (wiringPiSPISetup(SPI_CHAN, SPI_SPD) < 0) {
- TraceLog(LOG_FATAL, "SPI setup failed\n");
- return 0;
- }
- pinMode(CS_PIN, OUTPUT);
- digitalWrite(CS_PIN, LOW);
- g_sharp_buf = (unsigned char *)malloc(BUFFER_SIZE);
- if (!g_sharp_buf) {
- TraceLog(LOG_FATAL, "Buffer allocation failed\n");
- return 0;
- }
- memset(g_sharp_buf, 0xFF, BUFFER_SIZE);
- return 1;
- }
- void sharp_push(void)
- {
- static unsigned char vcom = SHARPMEM_BIT_VCOM;
- digitalWrite(CS_PIN, HIGH);
- unsigned char cmd = vcom | SHARPMEM_BIT_WRITECMD;
- wiringPiSPIDataRW(SPI_CHAN, &cmd, 1);
- vcom = vcom ? 0x00 : SHARPMEM_BIT_VCOM;
- static unsigned char line_data[PACKET_SIZE];
- for (int i = 0; i < BUFFER_SIZE; i += STRIDE) {
- int currentLine = (i / STRIDE) + 1;
- line_data[0] = bit_swap((unsigned char)currentLine);
- for (int j = 0; j < STRIDE; j++) {
- line_data[1 + j] = bit_swap(g_sharp_buf[i + j]);
- }
- line_data[STRIDE + 1] = 0x00; // End-of-line marker
- wiringPiSPIDataRW(SPI_CHAN, line_data, PACKET_SIZE);
- }
- unsigned char trailing = 0x00;
- wiringPiSPIDataRW(SPI_CHAN, &trailing, 1);
- digitalWrite(CS_PIN, LOW);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement