Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support using the bitmap device in the Arduino library #2486

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 0 additions & 65 deletions sys/bitmap/common/u8x8_d_bitmap.c → csrc/u8x8_d_bitmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "stdlib.h" /* malloc */
#include "stdint.h" /* uint16_t */
#include "string.h" /* memcpy */
#include "stdio.h" /* FILE */
#include "u8g2.h" /* because of u8g2_Setup... */

/*========================================================*/
Expand Down Expand Up @@ -79,65 +78,6 @@ uint8_t u8x8_bitmap_GetPixel(u8x8_bitmap_t *b, uint16_t x, uint16_t y)
return 1;
}

static void tga_write_byte(FILE *fp, uint8_t byte)
{
fputc(byte, fp);
}

static void tga_write_word(FILE *fp, uint16_t word)
{
tga_write_byte(fp, word&255);
tga_write_byte(fp, word>>8);
}

void u8x8_bitmap_SaveTGA(u8x8_bitmap_t *b, const char *name)
{
FILE *fp;
uint16_t x, y;

fp = fopen(name, "wb");
if ( fp != NULL )
{
tga_write_byte(fp, 0); /* no ID */
tga_write_byte(fp, 0); /* no color map */
tga_write_byte(fp, 2); /* uncompressed true color */
tga_write_word(fp, 0);
tga_write_word(fp, 0);
tga_write_byte(fp, 0);
tga_write_word(fp, 0); /* x origin */
tga_write_word(fp, 0); /* y origin */
tga_write_word(fp, b->pixel_width); /* width */
tga_write_word(fp, b->pixel_height); /* height */
tga_write_byte(fp, 24); /* color depth */
tga_write_byte(fp, 0);
for( y = 0; y < b->pixel_height; y++ )
{
for( x = 0; x < b->pixel_width; x++ )
{
if ( u8x8_bitmap_GetPixel(b, x, b->pixel_height-y-1) == 0 )
{
tga_write_byte(fp, 255); /* R */
tga_write_byte(fp, 255); /* G */
tga_write_byte(fp, 255); /* B */
}
else
{
tga_write_byte(fp, 0); /* R */
tga_write_byte(fp, 0); /* G */
tga_write_byte(fp, 0); /* B */
}
}
}
tga_write_word(fp, 0);
tga_write_word(fp, 0);
tga_write_word(fp, 0);
tga_write_word(fp, 0);
fwrite("TRUEVISION-XFILE.", 18, 1, fp);
fclose(fp);
}
}


/*========================================================*/
/* global objects for the bitmap */

Expand Down Expand Up @@ -199,11 +139,6 @@ uint8_t u8x8_GetBitmapPixel(U8X8_UNUSED u8x8_t *u8x8, uint16_t x, uint16_t y)
return u8x8_bitmap_GetPixel(&u8x8_bitmap, x, y);
}

void u8x8_SaveBitmapTGA(U8X8_UNUSED u8x8_t *u8x8, const char *filename)
{
u8x8_bitmap_SaveTGA(&u8x8_bitmap, filename);
}

/*========================================================*/

static uint8_t u8x8_d_bitmap(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
Expand Down
73 changes: 73 additions & 0 deletions sys/arduino/u8g2_bitmap_dev/HelloWorld/HelloWorld.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*

HelloWorld.ino

Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)

Copyright (c) 2016, [email protected]
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <Arduino.h>
#include <U8g2lib.h>

/*
This is a memory-backed bitmap rendering example
*/

#define WIDTH 128
#define HEIGHT 32

U8G2_BITMAP u8g2(WIDTH, HEIGHT, U8G2_R0);

void setup(void) {
Serial.begin(115200);
u8g2.begin();
}

void loop(void) {
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_10x20_tr); // choose a suitable font
u8g2.drawStr(0,30,"Hello World!"); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display

for (unsigned y=0; y < HEIGHT; ++y) {
Serial.print("|");
for (unsigned x=0; x < WIDTH; ++x) {
if (u8x8_GetBitmapPixel(u8g2.getU8x8(), x, y))
Serial.print("X");
else
Serial.print(" ");
}
Serial.println("|");
}
for (unsigned x=0; x < WIDTH + 2; ++x)
Serial.print("-");
Serial.println();

delay(1000);
}
147 changes: 147 additions & 0 deletions sys/arduino/u8g2_bitmap_dev/HelloWorld/Makefile.184.uno
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
#
# Makefile.184 for Arduino/AVR
#
# Note:
# Display list make database: make -p -f/dev/null | less


# Install path of the arduino software. Requires a '/' at the end.
ARDUINO_PATH:=/home/matthijs/arduino-1.8.4/

# Board (and prozessor) information: see $(ARDUINO_PATH)hardware/arduino/avr/boards.txt
# Some examples:
# BOARD DESCRIPTION
# uno Arduino Uno
# atmega328 Arduino Duemilanove or Nano w/ ATmega328
# diecimila Arduino Diecimila, Duemilanove, or Nano w/ ATmega168
# mega Arduino Mega
# mega2560 Arduino Mega2560
# mini Arduino Mini
# lilypad328 LilyPad Arduino w/ ATmega328
BOARD:=uno

# The unix device where we can reach the arduino board
# Uno: /dev/ttyACM0
# Duemilanove: /dev/ttyUSB0
AVRDUDE_PORT:=/dev/ttyACM0



SRC_DIRS=$(ARDUINO_PATH)hardware/arduino/avr/cores/arduino/
SRC_DIRS+=$(ARDUINO_PATH)hardware/arduino/avr/libraries/SPI/src/
SRC_DIRS+=$(ARDUINO_PATH)hardware/arduino/avr/libraries/SPI/src/utility/
SRC_DIRS+=$(ARDUINO_PATH)hardware/arduino/avr/libraries/Wire/src/
SRC_DIRS+=$(ARDUINO_PATH)hardware/arduino/avr/libraries/Wire/src/utility/
SRC_DIRS+=../../../../csrc/
SRC_DIRS+=../../../../cppsrc/

#=== suffixes ===
.SUFFIXES: .elf .hex .ino

#=== identify user files ===
INOSRC:=$(shell ls *.ino)
TARGETNAME=$(basename $(INOSRC))

#=== internal names ===
LIBNAME:=$(TARGETNAME).a
ELFNAME:=$(TARGETNAME).elf
HEXNAME:=$(TARGETNAME).hex
BINNAME:=$(TARGETNAME).bin
DISNAME:=$(TARGETNAME).dis
MAPNAME:=$(TARGETNAME).map


#=== replace standard tools ===
CC:=$(ARDUINO_PATH)hardware/tools/avr/bin/avr-gcc
CXX:=$(ARDUINO_PATH)hardware/tools/avr/bin/avr-g++
AR:=$(ARDUINO_PATH)hardware/tools/avr/bin/avr-gcc-ar
OBJCOPY:=$(ARDUINO_PATH)hardware/tools/avr/bin/avr-objcopy
OBJDUMP:=$(ARDUINO_PATH)hardware/tools/avr/bin/avr-objdump
SIZE:=$(ARDUINO_PATH)hardware/tools/avr/bin/avr-size

AVRDUDE = $(ARDUINO_PATH)hardware/tools/avr/bin/avrdude


#=== get values from boards.txt ===
BOARDS_TXT:=$(ARDUINO_PATH)hardware/arduino/avr/boards.txt

# get the MCU value from the $(BOARD).build.mcu variable. For the atmega328 board this is atmega328p
MCU:=$(shell sed -n -e "s/$(BOARD).build.mcu=\(.*\)/\1/p" $(BOARDS_TXT))
# get the F_CPU value from the $(BOARD).build.f_cpu variable. For the atmega328 board this is 16000000
F_CPU:=$(shell sed -n -e "s/$(BOARD).build.f_cpu=\(.*\)/\1/p" $(BOARDS_TXT))
# get variant subfolder
VARIANT:=$(shell sed -n -e "s/$(BOARD).build.variant=\(.*\)/\1/p" $(BOARDS_TXT))
UPLOAD_SPEED:=$(shell sed -n -e "s/$(BOARD).upload.speed=\(.*\)/\1/p" $(BOARDS_TXT))
# get the AVRDUDE_PROGRAMMER value from the $(BOARD).upload.protocol variable. For the atmega328 board this is stk500
UPLOAD_PROTOCOL:=$(shell sed -n -e "s/$(BOARD).upload.protocol=\(.*\)/\1/p" $(BOARDS_TXT))
# use stk500v1, because stk500 will default to stk500v2
#UPLOAD_PROTOCOL:=stk500v1

AVRDUDE_FLAGS = -V -F
AVRDUDE_FLAGS += -C $(ARDUINO_PATH)/hardware/tools/avr/etc/avrdude.conf
AVRDUDE_FLAGS += -p $(MCU)
AVRDUDE_FLAGS += -P $(AVRDUDE_PORT)
AVRDUDE_FLAGS += -c $(UPLOAD_PROTOCOL)
AVRDUDE_FLAGS += -b $(UPLOAD_SPEED)
AVRDUDE_FLAGS += -U flash:w:$(HEXNAME)

#=== get all include dirs ===
INC_DIRS:=. $(SRC_DIRS) $(ARDUINO_PATH)hardware/arduino/avr/variants/$(VARIANT)
INC_OPTS:=$(addprefix -I,$(INC_DIRS))

#=== get all source files ===
CSRC:=$(shell ls $(addsuffix *.c,$(SRC_DIRS)) 2>/dev/null)
CPPSRC:=$(shell ls $(addsuffix *.cpp,$(SRC_DIRS)) 2>/dev/null)

#=== get all obj files ===
COBJ:=$(CSRC:.c=.o)
CPPOBJ:=$(CPPSRC:.cpp=.o)
OBJ:=$(COBJ) $(CPPOBJ) $(TARGETNAME).o


#=== options ===

COMMON_FLAGS = -g -Os -DF_CPU=$(F_CPU) -mmcu=$(MCU)
COMMON_FLAGS +=-DARDUINO=10800 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR
COMMON_FLAGS +=-ffunction-sections -fdata-sections -MMD -flto -fno-fat-lto-objects
COMMON_FLAGS +=$(INC_OPTS)
CFLAGS:=$(COMMON_FLAGS) -std=gnu99 -Wstrict-prototypes -Wall -Wextra
CXXFLAGS:=$(COMMON_FLAGS) -std=gnu++11 -fpermissive -fno-exceptions
LDFLAGS:=-g -Os -flto -fuse-linker-plugin -Wl,--gc-sections -mmcu=$(MCU) -Wl,--Map=$(MAPNAME)
LDLIBS:=-lm

all: $(HEXNAME) $(DISNAME)
$(SIZE) $(ELFNAME)

.PHONY: debug
debug:
@echo $(MCU) $(F_CPU) $(VARIANT) $(UPLOAD_SPEED) $(UPLOAD_PROTOCOL)
@echo $(SRC_DIRS)
@echo $(CSRC)
@echo $(CPPSRC)
@echo $(INC_OPTS)

.PHONY: clean
clean:
$(RM) $(OBJ) $(HEXNAME) $(ELFNAME) $(LIBNAME) $(DISNAME) $(MAPNAME) $(BINNAME)

.PHONY: upload
upload: $(HEXNAME)
stty -F $(AVRDUDE_PORT) hupcl
$(AVRDUDE) $(AVRDUDE_FLAGS)

# implicit rules
.ino.cpp:
@cp $< $@

.elf.hex:
@$(OBJCOPY) -O ihex -R .eeprom $< $@

# explicit rules
$(ELFNAME): $(LIBNAME)($(OBJ))
$(LINK.o) $(LFLAGS) $(LIBNAME) $(LDLIBS) -o $@

$(DISNAME): $(ELFNAME)
$(OBJDUMP) -D -S $< > $@


2 changes: 1 addition & 1 deletion sys/bitmap/cimg_annotate_screenshot/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CFLAGS = -g -Wall -I../../../csrc/ -ICImg
CXXFLAGS = $(CFLAGS)
LDFLAGS = -lpthread
CSRC = $(wildcard ls ../../../csrc/*.c) ../common/u8x8_d_bitmap.c
CSRC = $(wildcard ls ../../../csrc/*.c) ../common/*.c
CPPSRC = main.cpp
OBJ = $(CSRC:.c=.o) $(CPPSRC:.cpp=.o)

Expand Down
66 changes: 66 additions & 0 deletions sys/bitmap/common/u8x8_d_bitmap_tga.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "stdlib.h" /* malloc */
#include "stdint.h" /* uint16_t */
#include "stdio.h" /* FILE */
#include "u8g2.h"

static void tga_write_byte(FILE *fp, uint8_t byte)
{
fputc(byte, fp);
}

static void tga_write_word(FILE *fp, uint16_t word)
{
tga_write_byte(fp, word&255);
tga_write_byte(fp, word>>8);
}

void u8x8_SaveBitmapTGA(u8x8_t *u8x8, const char *name)
{
FILE *fp;
uint16_t x, y;

uint16_t pixel_width = u8x8->display_info->pixel_width;
uint16_t pixel_height = u8x8->display_info->pixel_height;

fp = fopen(name, "wb");
if ( fp != NULL )
{
tga_write_byte(fp, 0); /* no ID */
tga_write_byte(fp, 0); /* no color map */
tga_write_byte(fp, 2); /* uncompressed true color */
tga_write_word(fp, 0);
tga_write_word(fp, 0);
tga_write_byte(fp, 0);
tga_write_word(fp, 0); /* x origin */
tga_write_word(fp, 0); /* y origin */
tga_write_word(fp, pixel_width); /* width */
tga_write_word(fp, pixel_height); /* height */
tga_write_byte(fp, 24); /* color depth */
tga_write_byte(fp, 0);

for( y = 0; y < pixel_height; y++ )
{
for( x = 0; x < pixel_width; x++ )
{
if ( u8x8_GetBitmapPixel(u8x8, x, pixel_height-y-1) == 0 )
{
tga_write_byte(fp, 255); /* R */
tga_write_byte(fp, 255); /* G */
tga_write_byte(fp, 255); /* B */
}
else
{
tga_write_byte(fp, 0); /* R */
tga_write_byte(fp, 0); /* G */
tga_write_byte(fp, 0); /* B */
}
}
}
tga_write_word(fp, 0);
tga_write_word(fp, 0);
tga_write_word(fp, 0);
tga_write_word(fp, 0);
fwrite("TRUEVISION-XFILE.", 18, 1, fp);
fclose(fp);
}
}
2 changes: 1 addition & 1 deletion sys/bitmap/hello_world/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CFLAGS = -g -Wall -I../../../csrc/.

SRC = $(shell ls ../../../csrc/*.c) $(shell ls ../common/u8x8_d_bitmap.c ) main.c
SRC = $(shell ls ../../../csrc/*.c) $(shell ls ../common/*.c ) main.c

OBJ = $(SRC:.c=.o)

Expand Down
Loading