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

feat: add QR code generator #191

Merged
merged 22 commits into from
Sep 30, 2024
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
28 changes: 28 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,31 @@ Notice file for preview/fonts/FreeSans*.cpp
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.

Notice file for preview/interfaces/QrCode.cpp/hpp

The MIT License (MIT)

This library is written and maintained by Richard Moore.
Major parts were derived from Project Nayuki's library.

Copyright (c) 2017 Richard Moore (https://github.com/ricmoo/QRCode)
Copyright (c) 2017 Project Nayuki (https://www.nayuki.io/page/qr-code-generator-library)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
add_subdirectory(clicking_scrolling)
add_subdirectory(qr_code)
33 changes: 33 additions & 0 deletions examples/qr_code/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
if (EMIL_BUILD_WIN OR TARGET_MCU STREQUAL stm32f746)
add_executable(examples.qr_code)
emil_build_for(examples.qr_code HOST Windows TARGET_MCU stm32f746 PREREQUISITE_BOOL PREVIEW_BUILD_EXAMPLES)

set_target_properties(examples.qr_code PROPERTIES WIN32_EXECUTABLE $<BOOL:${EMIL_BUILD_WIN}>)
if (EMIL_BUILD_WIN)
target_compile_definitions(examples.qr_code PUBLIC NOMINMAX)
endif()

target_include_directories(examples.qr_code PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/../..>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
)

target_sources(examples.qr_code PRIVATE
$<$<BOOL:${EMIL_BUILD_WIN}>:MainWin.cpp>
$<$<STREQUAL:${TARGET_MCU},stm32f746>:MainStm32f746.cpp>
)

target_link_libraries(examples.qr_code PUBLIC
$<$<BOOL:${EMIL_BUILD_WIN}>:hal.generic>
$<$<BOOL:${EMIL_BUILD_WIN}>:preview.sdl>
$<$<STREQUAL:${TARGET_MCU},stm32f746>:hal_st.stm32fxxx>
$<$<STREQUAL:${TARGET_MCU},stm32f746>:preview.stm32fxxx>
preview.touch
preview.views
)

if(TARGET_MCU STREQUAL stm32f746)
halst_target_default_linker_scripts(examples.qr_code)
halst_target_default_init(examples.qr_code)
endif()
endif()
59 changes: 59 additions & 0 deletions examples/qr_code/MainStm32f746.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "generated/stm32fxxx/PinoutTableDefault.hpp"
#include "hal_st/stm32fxxx/DefaultClockDiscoveryF746G.hpp"
#include "hal_st/stm32fxxx/GpioStm.hpp"
#include "hal_st/stm32fxxx/I2cStm.hpp"
#include "hal_st/stm32fxxx/SdRamStm.hpp"
#include "hal_st/stm32fxxx/SystemTickTimerService.hpp"
#include "infra/event/EventDispatcherWithWeakPtr.hpp"
#include "preview/interfaces/QrCode.hpp"
#include "preview/interfaces/ViewPainterDoubleBufferDisplay.hpp"
#include "preview/interfaces/ViewRepainter.hpp"
#include "preview/stm32fxxx/BitmapPainterStm.hpp"
#include "preview/stm32fxxx/LcdStm.hpp"
#include "preview/views/ViewBitmap.hpp"
#include "services/util/DebugLed.hpp"

unsigned int hse_value = 25000000;

namespace main_
{
struct Lcd
{
hal::MultiGpioPinStm sdRamPins{ hal::stm32f7discoveryFmcPins };
hal::SdRamStm sdRam{ sdRamPins, hal::stm32f7discoverySdRamConfig };

hal::MultiGpioPinStm lcdPins{ hal::stm32f7discoveryLcdPins };
hal::GpioPinStm displayEnable{ hal::Port::I, 12 };
hal::GpioPinStm backlightEnable{ hal::Port::K, 3 };
uint32_t bufferSize{ infra::Bitmap::BufferSize(hal::stm32f7discoveryLcdConfig.width, hal::stm32f7discoveryLcdConfig.height, hal::stm32f7discoveryLcdConfig.pixelFormat) };
infra::ByteRange lcdBuffer0{ infra::Head(sdRam.Memory(), bufferSize) };
infra::ByteRange lcdBuffer1{ infra::Head(infra::DiscardHead(sdRam.Memory(), bufferSize), bufferSize) };
infra::Bitmap bitmap0{ lcdBuffer0, infra::Vector(480, 272), infra::PixelFormat::rgb565 };
infra::Bitmap bitmap1{ lcdBuffer1, infra::Vector(480, 272), infra::PixelFormat::rgb565 };
hal::LcdStmDoubleBuffer display{ lcdPins, displayEnable, backlightEnable, lcdBuffer0, lcdBuffer1, hal::stm32f7discoveryLcdConfig };
};
}

int main()
{
HAL_Init();
ConfigureDefaultClockDiscoveryF746G();

static hal::InterruptTable::WithStorage<128> interruptTable;
static infra::EventDispatcherWithWeakPtr::WithSize<50> eventDispatcher;
static hal::GpioStm gpio(hal::pinoutTableDefaultStm);
static hal::SystemTickTimerService systemTick;

static main_::Lcd lcd;
static hal::BitmapPainterStm bitmapPainter;
static services::ViewPainterDoubleBufferDisplay painter(lcd.display, bitmapPainter);

services::QrCode<3, services::QrCodeEcc::low> qrcode("https://github.com/philips-software/amp-preview");
services::ViewBitmap viewBitmap(qrcode);

static services::ViewRepainterPaintWhenDirty repainter(painter, viewBitmap);
viewBitmap.ResetLayout(lcd.display.DisplaySize());

eventDispatcher.Run();
__builtin_unreachable();
}
30 changes: 30 additions & 0 deletions examples/qr_code/MainWin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "hal/generic/TimerServiceGeneric.hpp"
#include "infra/event/LowPowerEventDispatcher.hpp"
#include "preview/interfaces/QrCode.hpp"
#include "preview/interfaces/ViewPainterDirectDisplay.hpp"
#include "preview/interfaces/ViewRepainter.hpp"
#include "preview/sdl/DirectDisplaySdl.hpp"
#include "preview/sdl/LowPowerStrategySdl.hpp"
#include "preview/sdl/SdlTouchInteractor.hpp"
#include "preview/views/ViewBitmap.hpp"
#include <Windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
hal::TimerServiceGeneric timerService;
services::LowPowerStrategySdl lowPowerStrategy(timerService);
infra::LowPowerEventDispatcher::WithSize<50> eventDispatcher(lowPowerStrategy);

services::QrCode<4, services::QrCodeEcc::low> qrcode("https://github.com/philips-software/amp-preview/pull/191");
services::ViewBitmap viewBitmap(qrcode);

hal::DirectDisplaySdl display(infra::Vector(480, 272));
services::ViewPainterDirectDisplay painter(display);

services::ViewRepainterPaintWhenDirty repainter(painter, viewBitmap);
viewBitmap.ResetLayout(display.Size());

eventDispatcher.Run();

return 0;
}
7 changes: 6 additions & 1 deletion preview/interfaces/Bitmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ namespace infra
assert(buffer.size() == BufferSize(size.deltaX, size.deltaY, pixelFormat));
}

void Bitmap::Clear()
{
std::fill(buffer.begin(), buffer.end(), 0);
}

const uint8_t* Bitmap::BufferAddress(infra::Point position) const
{
assert(pixelFormat != PixelFormat::blackandwhite);
Expand Down Expand Up @@ -40,7 +45,7 @@ namespace infra
assert(pixelFormat == PixelFormat::blackandwhite);

auto bitIndex = position.y * size.deltaX + position.x;
return (buffer[bitIndex / 8] & (1 << (7 - bitIndex % 8))) != 0;
return (buffer[bitIndex / 8] & (1 << (bitIndex % 8))) != 0;
}

uint32_t Bitmap::PixelColour(infra::Point position) const
Expand Down
2 changes: 2 additions & 0 deletions preview/interfaces/Bitmap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace infra

Bitmap(infra::ByteRange buffer, infra::Vector size, PixelFormat pixelFormat);

void Clear();

const uint8_t* BufferAddress(infra::Point position) const;
uint8_t* BufferAddress(infra::Point position);
void SetBlackAndWhitePixel(infra::Point position, bool pixel);
Expand Down
2 changes: 2 additions & 0 deletions preview/interfaces/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ target_sources(preview.interfaces PRIVATE
Geometry.cpp
Geometry.hpp
MultiBufferDisplay.hpp
QrCode.cpp
QrCode.hpp
View.cpp
View.hpp
ViewOverlay.cpp
Expand Down
58 changes: 58 additions & 0 deletions preview/interfaces/Geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,59 @@ namespace infra
return !(*this == other);
}

RowFirstPoints::RowFirstPoints(infra::Region region)
: region(region)
, current(region.TopLeft())
{}

RowFirstPoints RowFirstPoints::begin() const
{
return *this;
}

RowFirstPoints RowFirstPoints::end() const
{
RowFirstPoints result(*this);
result.current = region.BottomRight();
result.current.x = region.Left();
return result;
}

Point RowFirstPoints::operator*() const
{
return current;
richardapeters marked this conversation as resolved.
Show resolved Hide resolved
}

RowFirstPoints& RowFirstPoints::operator++()
{
++current.x;

if (current.x == region.Right())
{
current.x = region.Left();
++current.y;
}

return *this;
}

RowFirstPoints RowFirstPoints::operator++(int) const
{
RowFirstPoints result(*this);
++result;
return result;
}

bool RowFirstPoints::operator==(const RowFirstPoints& other) const
{
return region == other.region && current == other.current;
}

bool RowFirstPoints::operator!=(const RowFirstPoints& other) const
{
return !(*this == other);
}

Region Intersection(Region first, Region second)
{
if (first.Top() >= second.Bottom() || first.Bottom() <= second.Top() || first.Left() >= second.Right() || first.Right() <= second.Left())
Expand Down Expand Up @@ -583,6 +636,11 @@ namespace infra
return std::abs(first.x - second.x) + std::abs(first.y - second.y);
}

uint32_t ChebyshevDistance(Point first, Point second)
{
return std::max(std::abs(first.x - second.x), std::abs(first.y - second.y));
}

uint32_t Distance(Point first, Point second)
{
return SquareRoot(Square(first.x - second.x) + Square(first.y - second.y));
Expand Down
21 changes: 21 additions & 0 deletions preview/interfaces/Geometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,26 @@ namespace infra
Vector size;
};

class RowFirstPoints
{
public:
explicit RowFirstPoints(infra::Region region);

RowFirstPoints begin() const;
RowFirstPoints end() const;

Point operator*() const;
RowFirstPoints& operator++();
RowFirstPoints operator++(int) const;

bool operator==(const RowFirstPoints& other) const;
bool operator!=(const RowFirstPoints& other) const;

private:
infra::Region region;
infra::Point current;
};

Region Intersection(Region first, Region second);
Region Union(Region first, Region second);
Region operator&(Region first, Region second);
Expand All @@ -186,6 +206,7 @@ namespace infra
Vector Flip(Vector vector);
Region Flip(Region region);
uint32_t ManhattanDistance(Point first, Point second);
uint32_t ChebyshevDistance(Point first, Point second);
uint32_t Distance(Point first, Point second);
Point AlignedUp(Point point, uint16_t alignX, uint16_t alignY);
Point AlignedDown(Point point, uint16_t alignX, uint16_t alignY);
Expand Down
Loading
Loading