Skip to content
Merged
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
14 changes: 12 additions & 2 deletions src/components/BitmapEditor/components/ExportDialog/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useBitmapsStore } from '@/stores/bitmaps';
import { DataFormat, Platform, SizeFormat, exportBitmap } from './utils';
import { useMemo } from 'react';
import { useEffect, useMemo, useRef } from 'react';
import { FormProvider, useForm } from 'react-hook-form';
import { CheckBox } from '@/components/CheckBox';
import { Input } from '@/components/Input';
Expand Down Expand Up @@ -46,9 +46,19 @@ export const ExportDialog = ({ bitmapId, area, onClose }: ExportDialogProps): JS
defaultValues: { ...defaultValues, name: bitmapEntity?.name ?? '' },
});

const { register, handleSubmit, watch } = methods;
const { register, handleSubmit, watch, setValue } = methods;

const formValues = watch();
const prevPlatformRef = useRef(formValues.platform);

// Auto-select Pico-friendly bit order only when switching into RP Pico platform.
useEffect(() => {
if (prevPlatformRef.current !== Platform.Pico && formValues.platform === Platform.Pico) {
setValue('bitOrder', BitOrder.LSB);
}
prevPlatformRef.current = formValues.platform;
}, [formValues.platform, setValue]);

const exportCode = useMemo<string>(
() => (bitmapEntity ? exportBitmap({ bitmapEntity, area, ...formValues }) : ''),
[area, bitmapEntity, formValues],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,13 @@ describe('BitmapEditor/ExportDialog/utils', () => {
});

expect(result).toContain('// testbitmap.h');
expect(result).toContain('#ifndef TESTBITMAP_H');
expect(result).toContain('#define TESTBITMAP_H');
expect(result).toContain('#pragma once');
expect(result).toContain('#include <stdint.h>');
expect(result).toContain('#include "bitmap.h"');
expect(result).toContain('const bitmap_t testbitmap = {');
expect(result).toContain('.width = 8,');
expect(result).toContain('.height = 8,');
expect(result).toContain(`.data = (uint8_t[]){${expectedDataHex}}`);
expect(result).toContain('#endif // TESTBITMAP_H');
});
});

Expand Down
9 changes: 3 additions & 6 deletions src/components/BitmapEditor/components/ExportDialog/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,19 @@ ${varType}${progMem} ${nameLower}[] = { ${dataArray} };
.replace(/\n$/, '');
};

const exportForPico = ({ nameLower, nameUpper, width, height, dataArray }: PlatformExportParams): string => {
const headerName = `${nameUpper}_H`;
const exportForPico = ({ nameLower, width, height, dataArray }: PlatformExportParams): string => {
const includesBlock = ['#include <stdint.h>', '#include "bitmap.h"'].filter(Boolean).join('\n');
return `
// ${nameLower}.h
#ifndef ${headerName}
#define ${headerName}
#pragma once

${includesBlock}

const bitmap_t ${nameLower} = {
.width = ${width},
.height = ${height},
.data = (uint8_t[]){${dataArray}}
};

#endif // ${headerName}
`
.replace(/^\n/, '')
.replace(/\n$/, '');
Expand Down
22 changes: 22 additions & 0 deletions src/pages/EditBitmap/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,28 @@ test('export bitmap', async () => {
expect(spy).toHaveBeenCalledWith(exportCode);
});

test('select pico sets LSB bit order automatically', async () => {
const { userEvent } = await setupTest();
const exportButton = screen.getByText('Export to C');
expect(exportButton).toBeEnabled();
await userEvent.click(exportButton);

const msbRadio = screen.getByLabelText('MSB-first (Adafruit)');
const lsbRadio = screen.getByLabelText('LSB-first (U8g2, Pico)');
const picoRadio = screen.getByLabelText('RP Pico');

await userEvent.click(msbRadio);
expect((msbRadio as HTMLInputElement).checked).toBeTruthy();
expect((lsbRadio as HTMLInputElement).checked).toBeFalsy();

await userEvent.click(picoRadio);
expect((lsbRadio as HTMLInputElement).checked).toBeTruthy();

await userEvent.click(msbRadio);
expect((msbRadio as HTMLInputElement).checked).toBeTruthy();
expect((lsbRadio as HTMLInputElement).checked).toBeFalsy();
});

test('grid settings', async () => {
const { userEvent, stores } = await setupTest();
const gridButton = screen.getByText('Grid');
Expand Down