Add basic bitmap output

This commit is contained in:
fruchti 2020-05-23 22:11:37 +02:00
parent 31c9a40d99
commit 2a0b7e31a1
14 changed files with 8341 additions and 6 deletions

93
src/bmp.c Normal file
View file

@ -0,0 +1,93 @@
#include "bmp.h"
#include "ff.h"
typedef struct
{
uint8_t b;
uint8_t m;
uint32_t bitmap_file_size;
uint32_t reserved;
uint32_t bitmap_data_offset;
} __attribute__((packed)) BMP_FileHeader_t;
typedef struct
{
uint32_t header_size;
uint16_t bitmap_width;
uint16_t bitmap_height;
uint16_t colour_planes;
uint16_t bits_per_pixel;
} __attribute((packed)) BMP_CoreHeader_t;
void BMP_Save(uint8_t *data, int width, int height)
{
FATFS fs;
FIL fp;
FRESULT rc;
unsigned int bw;
rc = f_mount(&fs, "", 0);
if(rc)
{
return;
}
rc = f_open(&fp, "IMAGE.BMP", FA_WRITE | FA_CREATE_ALWAYS);
if(rc)
{
return;
}
// Rows must be padded to a multiple if 4 bytes
int row_size = width / 8;
int padding_length = (4 - (row_size % 4)) % 4;
uint8_t padding_bytes[4] = {0};
row_size += padding_length;
BMP_FileHeader_t file_header =
{
.b = 'B',
.m = 'M',
.bitmap_file_size = 14 + 12 + row_size * height,
.bitmap_data_offset = 14 + 12 + 6
};
// Use the simplest possible header
BMP_CoreHeader_t core_header =
{
.header_size = 12,
.bitmap_width = width,
.bitmap_height = height,
.colour_planes = 1,
.bits_per_pixel = 1
};
// At one bit per pixel, we need a colour table
uint8_t colour_table[6] =
{
0xff, 0xff, 0xff,
0x00, 0x00, 0x00
};
if(f_write(&fp, &file_header, sizeof(file_header), &bw) != FR_OK)
return;
if(f_write(&fp, &core_header, sizeof(core_header), &bw) != FR_OK)
return;
if(f_write(&fp, &colour_table, sizeof(colour_table), &bw) != FR_OK)
return;
// BMPs are stored with their rows from the bottom upwards
for(int y = height - 1; y >= 0; y--)
{
uint8_t *row = data + y * (width / 8);
if(f_write(&fp, row, width / 8, &bw) != FR_OK)
return;
if(padding_length != 0)
{
if(f_write(&fp, padding_bytes, padding_length, &bw) != FR_OK)
return;
}
}
f_close(&fp);
}

5
src/bmp.h Normal file
View file

@ -0,0 +1,5 @@
#pragma once
#include <stdint.h>
void BMP_Save(uint8_t *data, int width, int height);

View file

@ -2,12 +2,19 @@
int main(void)
{
RCC->APB2ENR |= RCC_APB2ENR_IOPCEN;
RCC->APB2ENR |= RCC_APB2ENR_IOPBEN | RCC_APB2ENR_IOPCEN;
GPIOC->CRH = (GPIOC->CRH
& ~(0x0f << (4 * PIN_SUPPLY - 32)))
| (0x01 << (4 * PIN_SUPPLY - 32)) // Output, max. 10 MHz
;
GPIOB->CRL = (GPIOB->CRL
& ~(0x0f << (4 * PIN_SD_CARD_DETECT)))
| (0x08 << (4 * PIN_SD_CARD_DETECT)) // Input with pull-up/-down
;
// Use pull-up
GPIOB->BSRR = (1 << PIN_SD_CARD_DETECT);
GPIOC->BSRR = (1 << PIN_SUPPLY);
@ -25,6 +32,8 @@ int main(void)
LTP1245_FeedPaper(100);
LTP1245_FeedPaper(10);
BMP_Save(ImageBuffer, CAMERA_IMAGE_WIDTH, CAMERA_IMAGE_HEIGHT);
GPIOC->BRR = (1 << PIN_SUPPLY);
for(;;)
@ -32,4 +41,3 @@ int main(void)
__WFI();
}
}

View file

@ -15,6 +15,7 @@
#include "font_messe_duesseldorf_39.h"
#include "font_arpegius_16.h"
#include "font_arpegius_32.h"
#include "bmp.h"
int main(void);

View file

@ -18,10 +18,13 @@
// Port B
#define PIN_THERMISTOR 0 // PB0 - Thermistor (ADC12_IN8)
#define PIN_SD_SCK 1 // PB1 - SD card SCK
#define PIN_SD_CARD_DETECT 2 // PB2 - SD card detect
#define PIN_PAPER 3 // PB3 - Paper detect
#define PIN_CAMERA_PCLK 4 // PB4 - Camera pixel clock (TIM3_CH1)
#define PIN_CAMERA_HSYNC 5 // PB5 - Camera VSYNC (TIM3_CH2)
#define PIN_HEAD 6 // PB6 - Head up sensor
#define PIN_SD_MISO 7 // PB7 - SD card MISO
#define PIN_CAMERA_SCL 8 // PB8 - Camera control I2C (I2C1_SCL)
#define PIN_CAMERA_SDA 9 // PB9 - Camera control I2C (I2C1_SDA)
#define PIN_DST1 10 // PB10 - Head drive signal 1 (TIM2_CH3)
@ -32,4 +35,6 @@
#define PIN_DIN 15 // PB15 - Thermal printer MOSI (SPI2)
// Port C
#define PIN_SUPPLY 14 // PC14 - Voltage regulator enable
#define PIN_SD_MOSI 13 // PC13 - SD card MOSI
#define PIN_SUPPLY 14 // PC14 - Voltage regulator enable
#define PIN_SD_CS 15 // PC15 - SD card chip select