Skip to content
Browse files

Moved bitslice() into bitslice.c, moved hexdump into util.h

  • Loading branch information...
1 parent 7a04877 commit 0d5cca09988b0818e143add22d0cab7a79075cc0 @osresearch committed Aug 11, 2013
Showing with 195 additions and 94 deletions.
  1. +1 −1 Makefile
  2. +89 −0 bitslice.c
  3. +25 −0 bitslice.h
  4. +8 −93 teensy-udp-rx.c
  5. +53 −0 test-bitslice.c
  6. +19 −0 util.h
View
2 Makefile
@@ -6,7 +6,7 @@ TARGETS += teensy-udp-rx
TARGETS += rgb-test
TARGETS += udp-rx
-LEDSCAPE_OBJS = ledscape.o pru.o
+LEDSCAPE_OBJS = ledscape.o pru.o bitslice.o
LEDSCAPE_LIB := libledscape.a
all: $(TARGETS) ws281x.bin
View
89 bitslice.c
@@ -0,0 +1,89 @@
+/** \file
+ * Slice an RGB image into an 8-pixel GRB packed format for the Teensy.
+ *
+ * Designed to clock data out to USB teensy3 serial devices, which require
+ * bitslicing into the raw form for the OctoWS2811 firmware.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <inttypes.h>
+#include "bitslice.h"
+
+
+/** Extract a ADDRESSING_HORIZONTAL_NORMAL pixel from the image.
+ *
+ * This reverses the y since the teensy's are at the bottom of the
+ * strips, while the image coordinate frame is at the top.
+ */
+static const uint8_t *
+bitmap_pixel(
+ const uint8_t * const bitmap,
+ const unsigned width,
+ const unsigned height,
+ const unsigned x,
+ const unsigned y
+)
+{
+ if (1)
+ return bitmap + ((height - y - 1) * width + x) * 3;
+ else
+ return bitmap + ((height - y - 1) + x * height) * 3;
+}
+
+
+/** Prepare one Teensy3 worth of image data.
+ *
+ * Each Teensy handles 8 rows of data and needs the bits sliced into
+ * each 8 rows.
+ *
+ * Since some of the pixels might have bad single channels,
+ * allow them to be masked out entirely.
+ */
+void
+bitslice(
+ uint8_t * const out,
+ const uint8_t * const bad_pixels,
+ const uint8_t * in,
+ const unsigned width,
+ const unsigned height,
+ const unsigned x_offset
+)
+{
+ // Reorder from RGB in the input to GRB in the output
+ static const uint8_t channel_map[] = { 1, 0, 2 };
+
+ for(unsigned y=0 ; y < height ; y++)
+ {
+ for (unsigned channel = 0 ; channel < 3 ; channel++)
+ {
+ const uint8_t mapped_channel
+ = channel_map[channel];
+
+ for (unsigned bit_num = 0 ; bit_num < 8 ; bit_num++)
+ {
+ uint8_t b = 0;
+ const uint8_t mask = 1 << (7 - bit_num);
+
+ for(unsigned x = 0 ; x < 8 ; x++)
+ {
+ const uint8_t bit_pos = 1 << x;
+ if (bad_pixels
+ && bad_pixels[y] & bit_pos)
+ continue;
+ const uint8_t * const p
+ = bitmap_pixel(in, width, height, x + x_offset, y);
+ const uint8_t v = p[channel];
+
+ // If the bit_num'th bit in v is
+ // set, then mark that the x'th bit
+ // in this output byte should be set.
+ if (v & mask)
+ b |= bit_pos;
+ }
+
+ out[24*y + 8*mapped_channel + bit_num] = b;
+ }
+ }
+ }
+}
View
25 bitslice.h
@@ -0,0 +1,25 @@
+#ifndef _ledscape_bitslice_h_
+#define _ledscape_bitslice_h_
+
+#include <stdint.h>
+
+
+/** Prepare one Teensy3 worth of image data.
+ *
+ * Each Teensy handles 8 rows of data and needs the bits sliced into
+ * each 8 rows.
+ *
+ * Since some of the pixels might have bad single channels,
+ * allow them to be masked out entirely.
+ */
+extern void
+bitslice(
+ uint8_t * const out,
+ const uint8_t * const bad_pixels,
+ const uint8_t * in,
+ const unsigned width,
+ const unsigned height,
+ const unsigned x_offset
+);
+
+#endif
View
101 teensy-udp-rx.c
@@ -27,82 +27,12 @@
#include <string.h>
#include <pthread.h>
#include "util.h"
+#include "bitslice.h"
static int port = 9999;
static unsigned width = 64;
static unsigned height = 210;
-
-/** Extract a ADDRESSING_HORIZONTAL_NORMAL pixel from the image.
- *
- * This reverses the y since the teensy's are at the bottom of the
- * strips, while the image coordinate frame is at the top.
- */
-static const uint8_t *
-bitmap_pixel(
- const uint8_t * const bitmap,
- const unsigned x,
- const unsigned y
-)
-{
- return bitmap + ((height - y - 1) * width + x) * 3;
-}
-
-
-/** Prepare one Teensy3 worth of image data.
- *
- * Each Teensy handles 8 rows of data and needs the bits sliced into
- * each 8 rows.
- *
- * Since some of the pixels might have bad single channels,
- * allow them to be masked out entirely.
- */
-void
-bitslice(
- uint8_t * const out,
- const uint8_t * in,
- const unsigned x_offset,
- const uint8_t * const bad_pixels
-)
-{
- // Reorder from RGB in the input to GRB in the output
- static const uint8_t channel_map[] = { 1, 0, 2 };
-
- for(unsigned y=0 ; y < height ; y++)
- {
- for (unsigned channel = 0 ; channel < 3 ; channel++)
- {
- const uint8_t mapped_channel
- = channel_map[channel];
-
- for (unsigned bit_num = 0 ; bit_num < 8 ; bit_num++)
- {
- uint8_t b = 0;
- const uint8_t mask = 1 << (7 - bit_num);
-
- for(unsigned x = 0 ; x < 8 ; x++)
- {
- const uint8_t bit_pos = 1 << x;
- if (bad_pixels[y] & bit_pos)
- continue;
- const uint8_t * const p
- = bitmap_pixel(in, x + x_offset, y);
- const uint8_t v = p[channel];
-
- // If the bit_num'th bit in v is
- // set, then mark that the x'th bit
- // in this output byte should be set.
- if (v & mask)
- b |= bit_pos;
- }
-
- out[24*y + 8*mapped_channel + bit_num] = b;
- }
- }
- }
-}
-
-
static int
udp_socket(
const int port
@@ -439,25 +369,6 @@ read_config(
}
-static void
-hexdump(
- const void * const buf,
- const size_t len
-)
-{
- const uint8_t * const p = buf;
-
- for(size_t i = 0 ; i < len ; i++)
- {
- if (i % 16 == 0)
- printf("%s%04zu:", i == 0 ? "": "\n", i);
- printf(" %02x", p[i]);
- }
-
- printf("\n");
-}
-
-
int
main(
int argc,
@@ -534,9 +445,11 @@ main(
#if 0
bitslice(
slice + 3,
+ strips[0].bad
buf + 1,
+ width,
+ height
8,
- strips[0].bad
);
//hexdump(buf+1, rlen-1);
@@ -556,9 +469,11 @@ main(
bitslice(
slice + 3,
+ strip->bad,
buf + 1,
- strip->x_offset,
- strip->bad
+ width,
+ height,
+ strip->x_offset
);
const ssize_t rc
View
53 test-bitslice.c
@@ -0,0 +1,53 @@
+#include <stdint.h>
+#include "bitslice.h"
+#include "util.h"
+
+int main(void)
+{
+ uint8_t bitmap[9][2][3] = {
+ {
+ { 0xFF, 0xFF, 0xFF },
+ { 0x00, 0x00, 0x00 },
+ },
+ {
+ { 0xFF, 0xFF, 0xFF },
+ { 0x00, 0x00, 0x00 },
+ },
+ {
+ { 0xFF, 0xFF, 0xFF },
+ { 0x00, 0x00, 0x00 },
+ },
+ {
+ { 0xFF, 0xFF, 0xFF },
+ { 0x00, 0x00, 0x00 },
+ },
+ {
+ { 0xFF, 0xFF, 0xFF },
+ { 0x00, 0x00, 0x00 },
+ },
+ {
+ { 0xFF, 0xFF, 0xFF },
+ { 0x00, 0x00, 0x00 },
+ },
+ {
+ { 0xFF, 0xFF, 0xFF },
+ { 0x00, 0x00, 0x00 },
+ },
+ {
+ { 0xFF, 0xFF, 0xFF },
+ { 0x00, 0x00, 0x00 },
+ },
+ {
+ { 0xFF, 0xFF, 0xFF },
+ { 0x00, 0x00, 0x00 },
+ },
+ };
+
+ hexdump(bitmap, sizeof(bitmap));
+
+ uint8_t out[1024];
+
+ bitslice(out, NULL, bitmap, 9, 2, 1);
+
+ hexdump(out, 8 * 2 * 3);
+}
View
19 util.h
@@ -31,4 +31,23 @@
exit(EXIT_FAILURE); \
} while(0)
+
+static inline void
+hexdump(
+ const void * const buf,
+ const size_t len
+)
+{
+ const uint8_t * const p = buf;
+
+ for(size_t i = 0 ; i < len ; i++)
+ {
+ if (i % 16 == 0)
+ printf("%s%04zu:", i == 0 ? "": "\n", i);
+ printf(" %02x", p[i]);
+ }
+
+ printf("\n");
+}
+
#endif

0 comments on commit 0d5cca0

Please sign in to comment.
Something went wrong with that request. Please try again.