Skip to content

Commit 4b85a74

Browse files
committed
add QR viewer application with library
add qrcode library add 29x29 QR viewer viewer support for all QR sizes change to c library for qrcodegeneration add ble QrService Add Qr app to application screen 3 improve Qr::drawQr update QR code automatically center QR code on screen add possibility for 4 different qr codes
1 parent eedff2c commit 4b85a74

File tree

12 files changed

+1734
-4
lines changed

12 files changed

+1734
-4
lines changed

src/CMakeLists.txt

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,10 @@ set(LVGL_SRC
345345
libs/lvgl/src/lv_widgets/lv_win.c
346346
)
347347

348+
set(QRCODE_SRC
349+
libs/qrcodegenerator/qrcodegen.c
350+
)
351+
348352
list(APPEND IMAGE_FILES
349353
displayapp/icons/battery/os_battery_error.c
350354
displayapp/icons/battery/os_battery_100.c
@@ -401,6 +405,7 @@ list(APPEND SOURCE_FILES
401405
displayapp/screens/ApplicationList.cpp
402406
displayapp/screens/Notifications.cpp
403407
displayapp/screens/Twos.cpp
408+
displayapp/screens/Qr.cpp
404409
displayapp/screens/HeartRate.cpp
405410
displayapp/screens/Motion.cpp
406411
displayapp/screens/FlashLight.cpp
@@ -453,6 +458,7 @@ list(APPEND SOURCE_FILES
453458
components/ble/BatteryInformationService.cpp
454459
components/ble/ImmediateAlertService.cpp
455460
components/ble/ServiceDiscovery.cpp
461+
components/ble/QrService.cpp
456462
components/ble/HeartRateService.cpp
457463
components/firmwarevalidator/FirmwareValidator.cpp
458464
components/motor/MotorController.cpp
@@ -515,6 +521,7 @@ list(APPEND RECOVERY_SOURCE_FILES
515521
components/ble/ServiceDiscovery.cpp
516522
components/ble/NavigationService.cpp
517523
components/ble/HeartRateService.cpp
524+
components/ble/QrService.cpp
518525
components/firmwarevalidator/FirmwareValidator.cpp
519526
components/settings/Settings.cpp
520527
drivers/Cst816s.cpp
@@ -616,6 +623,7 @@ set(INCLUDE_FILES
616623
components/ble/BleClient.h
617624
components/ble/HeartRateService.h
618625
components/settings/Settings.h
626+
components/ble/QrService.h
619627
drivers/Cst816s.h
620628
FreeRTOS/portmacro.h
621629
FreeRTOS/portmacro_cmsis.h
@@ -627,6 +635,7 @@ set(INCLUDE_FILES
627635
libs/date/includes/date/julian.h
628636
libs/date/includes/date/ptz.h
629637
libs/date/includes/date/tz_private.h
638+
libs/qrcodegenerator/qrcodegen.h
630639
displayapp/LittleVgl.h
631640
displayapp/lv_pinetime_theme.h
632641
systemtask/SystemTask.h
@@ -647,6 +656,7 @@ include_directories(
647656
libs/
648657
FreeRTOS/
649658
libs/date/includes
659+
libs/qrcodegenerator
650660
libs/mynewt-nimble/porting/npl/freertos/include
651661
libs/mynewt-nimble/nimble/include
652662
libs/mynewt-nimble/porting/nimble/include
@@ -787,13 +797,25 @@ target_compile_options(lvgl PRIVATE
787797
$<$<COMPILE_LANGUAGE:ASM>: -MP -MD -x assembler-with-cpp>
788798
)
789799

800+
# qrcode
801+
add_library(qrcode STATIC ${QRCODE_SRC})
802+
target_include_directories(qrcode SYSTEM PUBLIC . ../)
803+
target_include_directories(qrcode SYSTEM PUBLIC ${INCLUDES_FROM_LIBS})
804+
target_compile_options(qrcode PRIVATE
805+
$<$<AND:$<COMPILE_LANGUAGE:C>,$<CONFIG:DEBUG>>: ${COMMON_FLAGS} -O0 -g3>
806+
$<$<AND:$<COMPILE_LANGUAGE:C>,$<CONFIG:RELEASE>>: ${COMMON_FLAGS} -O3>
807+
$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CONFIG:DEBUG>>: ${COMMON_FLAGS} -O0 -g3>
808+
$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CONFIG:RELEASE>>: ${COMMON_FLAGS} -O3>
809+
$<$<COMPILE_LANGUAGE:ASM>: -MP -MD -std=c99 -x assembler-with-cpp>
810+
)
811+
790812
# Build autonomous binary (without support for bootloader)
791813
set(EXECUTABLE_NAME "pinetime-app")
792814
set(EXECUTABLE_FILE_NAME ${EXECUTABLE_NAME}-${pinetime_VERSION_MAJOR}.${pinetime_VERSION_MINOR}.${pinetime_VERSION_PATCH})
793815
set(NRF5_LINKER_SCRIPT "${CMAKE_SOURCE_DIR}/gcc_nrf52.ld")
794816
add_executable(${EXECUTABLE_NAME} ${SOURCE_FILES})
795817
set_target_properties(${EXECUTABLE_NAME} PROPERTIES OUTPUT_NAME ${EXECUTABLE_FILE_NAME})
796-
target_link_libraries(${EXECUTABLE_NAME} nimble nrf-sdk lvgl)
818+
target_link_libraries(${EXECUTABLE_NAME} nimble nrf-sdk lvgl qrcode)
797819
target_compile_options(${EXECUTABLE_NAME} PUBLIC
798820
$<$<AND:$<COMPILE_LANGUAGE:C>,$<CONFIG:DEBUG>>: ${COMMON_FLAGS} -O0 -g3>
799821
$<$<AND:$<COMPILE_LANGUAGE:C>,$<CONFIG:RELEASE>>: ${COMMON_FLAGS} -O3>
@@ -822,7 +844,7 @@ set(IMAGE_MCUBOOT_FILE_NAME ${EXECUTABLE_MCUBOOT_NAME}-image-${pinetime_VERSION_
822844
set(DFU_MCUBOOT_FILE_NAME ${EXECUTABLE_MCUBOOT_NAME}-dfu-${pinetime_VERSION_MAJOR}.${pinetime_VERSION_MINOR}.${pinetime_VERSION_PATCH}.zip)
823845
set(NRF5_LINKER_SCRIPT_MCUBOOT "${CMAKE_SOURCE_DIR}/gcc_nrf52-mcuboot.ld")
824846
add_executable(${EXECUTABLE_MCUBOOT_NAME} ${SOURCE_FILES})
825-
target_link_libraries(${EXECUTABLE_MCUBOOT_NAME} nimble nrf-sdk lvgl)
847+
target_link_libraries(${EXECUTABLE_MCUBOOT_NAME} nimble nrf-sdk lvgl qrcode)
826848
set_target_properties(${EXECUTABLE_MCUBOOT_NAME} PROPERTIES OUTPUT_NAME ${EXECUTABLE_MCUBOOT_FILE_NAME})
827849
target_compile_options(${EXECUTABLE_MCUBOOT_NAME} PUBLIC
828850
$<$<AND:$<COMPILE_LANGUAGE:C>,$<CONFIG:DEBUG>>: ${COMMON_FLAGS} -O0 -g3>

src/components/ble/NimbleController.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask,
3838
navService {systemTask},
3939
batteryInformationService {batteryController},
4040
immediateAlertService {systemTask, notificationManager},
41+
qrService(systemTask),
4142
heartRateService {systemTask, heartRateController},
4243
serviceDiscovery({&currentTimeClient, &alertNotificationClient}) {
4344
}
@@ -63,6 +64,7 @@ void NimbleController::Init() {
6364
dfuService.Init();
6465
batteryInformationService.Init();
6566
immediateAlertService.Init();
67+
qrService.Init();
6668
heartRateService.Init();
6769
int res;
6870
res = ble_hs_util_ensure_addr(0);

src/components/ble/NimbleController.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "DfuService.h"
1717
#include "ImmediateAlertService.h"
1818
#include "MusicService.h"
19+
#include "QrService.h"
1920
#include "NavigationService.h"
2021
#include "ServiceDiscovery.h"
2122
#include "HeartRateService.h"
@@ -68,6 +69,10 @@ namespace Pinetime {
6869
Pinetime::Controllers::AlertNotificationService& alertService() {
6970
return anService;
7071
};
72+
Pinetime::Controllers::QrService& qr() {
73+
return qrService;
74+
};
75+
7176

7277
uint16_t connHandle();
7378

@@ -89,6 +94,7 @@ namespace Pinetime {
8994
NavigationService navService;
9095
BatteryInformationService batteryInformationService;
9196
ImmediateAlertService immediateAlertService;
97+
QrService qrService;
9298
HeartRateService heartRateService;
9399

94100
uint8_t addrType; // 1 = Random, 0 = PUBLIC

src/components/ble/QrService.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include "QrService.h"
2+
#include "systemtask/SystemTask.h"
3+
4+
using namespace Pinetime::Controllers;
5+
6+
int QRCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) {
7+
auto qrService = static_cast<QrService *>(arg);
8+
return qrService->OnCommand(conn_handle, attr_handle, ctxt);
9+
}
10+
11+
QrService::QrService(Pinetime::System::SystemTask &system) : m_system(system) {
12+
qrsTextCharUuid.value[11] = qrsTextCharId[0];
13+
qrsTextCharUuid.value[12] = qrsTextCharId[1];
14+
15+
characteristicDefinition[0] = {.uuid = (ble_uuid_t *) (&qrsTextCharUuid),
16+
.access_cb = QRCallback,
17+
.arg = this,
18+
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ
19+
};
20+
characteristicDefinition[1] = {0};
21+
22+
serviceDefinition[0] = {
23+
.type = BLE_GATT_SVC_TYPE_PRIMARY,
24+
.uuid = (ble_uuid_t *) &qrsUuid,
25+
.characteristics = characteristicDefinition
26+
};
27+
serviceDefinition[1] = {0};
28+
29+
qrList[0].name = "Github";
30+
qrList[0].text = "https://github.com/JF002/InfiniTime";
31+
qrList[1].name = "Twitter";
32+
qrList[1].text = "https://twitter.com/codingfield";
33+
qrList[2].name = "Mastodon";
34+
qrList[2].text = "https://mastodon.codingfield.com/@JF";
35+
qrList[3].name = "Donate!";
36+
qrList[3].text = "https://liberapay.com/JF002";
37+
}
38+
39+
void QrService::Init() {
40+
int res = 0;
41+
res = ble_gatts_count_cfg(serviceDefinition);
42+
ASSERT(res == 0);
43+
44+
res = ble_gatts_add_svcs(serviceDefinition);
45+
ASSERT(res == 0);
46+
}
47+
48+
int QrService::OnCommand(uint16_t conn_handle, uint16_t attr_handle,
49+
struct ble_gatt_access_ctxt *ctxt) {
50+
if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
51+
size_t notifSize = OS_MBUF_PKTLEN(ctxt->om);
52+
uint8_t data[notifSize + 1];
53+
data[notifSize] = '\0';
54+
os_mbuf_copydata(ctxt->om, 0, notifSize, data);
55+
char *s = (char *) &data[0];
56+
NRF_LOG_INFO("DATA : %s", s);
57+
58+
const char d[2] = "|";
59+
char* qrId = strtok(s, d);
60+
char* qrName = strtok(NULL, d);
61+
char* qrText = strtok(NULL, d);
62+
63+
if (ble_uuid_cmp(ctxt->chr->uuid, (ble_uuid_t *) &qrsTextCharUuid) == 0) {
64+
if (std::stoi(qrId) < MAXLISTITEMS && qrId > 0 && qrName != NULL && qrText != NULL) {
65+
qrList[std::stoi(qrId)].name = qrName;
66+
qrList[std::stoi(qrId)].text = qrText;
67+
}
68+
}
69+
}
70+
return 0;
71+
}
72+
73+
std::array<QrService::QrInfo, 4> QrService::getQrList() {
74+
return qrList;
75+
}

src/components/ble/QrService.h

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
#include <string>
5+
#include <array>
6+
#define min // workaround: nimble's min/max macros conflict with libstdc++
7+
#define max
8+
#include <host/ble_gap.h>
9+
#include <host/ble_uuid.h>
10+
#undef max
11+
#undef min
12+
13+
//00030000-78fc-48fe-8e23-433b3a1942d0
14+
#define QR_SERVICE_UUID_BASE {0xd0, 0x42, 0x19, 0x3a, 0x3b, 0x43, 0x23, 0x8e, 0xfe, 0x48, 0xfc, 0x78, 0x00, 0x00, 0x03, 0x00}
15+
16+
#define MAXLISTITEMS 4
17+
18+
namespace Pinetime {
19+
namespace System {
20+
class SystemTask;
21+
}
22+
namespace Controllers {
23+
class QrService {
24+
public:
25+
struct QrInfo {
26+
std::string name;
27+
std::string text;
28+
bool operator==(const QrInfo &rhs) const {
29+
return (rhs.name == name && rhs.text == text);
30+
}
31+
};
32+
33+
explicit QrService(Pinetime::System::SystemTask &system);
34+
35+
void Init();
36+
37+
int OnCommand(uint16_t conn_handle, uint16_t attr_handle,
38+
struct ble_gatt_access_ctxt *ctxt);
39+
40+
std::array<QrInfo, 4> getQrList();
41+
42+
private:
43+
44+
static constexpr uint8_t qrsTextCharId[2] = {0x00, 0x01};
45+
46+
ble_uuid128_t qrsUuid{
47+
.u = {.type = BLE_UUID_TYPE_128},
48+
.value = QR_SERVICE_UUID_BASE
49+
};
50+
51+
ble_uuid128_t qrsTextCharUuid{
52+
.u = {.type = BLE_UUID_TYPE_128},
53+
.value = QR_SERVICE_UUID_BASE
54+
};
55+
56+
struct ble_gatt_chr_def characteristicDefinition[2];
57+
struct ble_gatt_svc_def serviceDefinition[2];
58+
59+
std::array<QrInfo, 4> qrList;
60+
61+
Pinetime::System::SystemTask &m_system;
62+
63+
};
64+
}
65+
}

src/displayapp/Apps.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ namespace Pinetime {
2121
Navigation,
2222
StopWatch,
2323
Motion,
24+
Qr,
2425
QuickSettings,
2526
Settings,
2627
SettingWatchFace,

src/displayapp/DisplayApp.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include "displayapp/screens/Twos.h"
2525
#include "displayapp/screens/FlashLight.h"
2626
#include "displayapp/screens/BatteryInfo.h"
27-
27+
#include "displayapp/screens/Qr.h"
2828
#include "drivers/Cst816s.h"
2929
#include "drivers/St7789.h"
3030
#include "drivers/Watchdog.h"
@@ -324,6 +324,9 @@ void DisplayApp::LoadApp(Apps app, DisplayApp::FullRefreshDirections direction)
324324
case Apps::Motion:
325325
currentScreen = std::make_unique<Screens::Motion>(this, motionController);
326326
break;
327+
case Apps::Qr:
328+
currentScreen = std::make_unique<Screens::Qr>(this, lvgl, systemTask.nimble().qr());
329+
break;
327330
}
328331
currentApp = app;
329332
}

src/displayapp/screens/ApplicationList.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ std::unique_ptr<Screen> ApplicationList::CreateScreen2() {
6262
{Symbols::paintbrush, Apps::Paint},
6363
{Symbols::paddle, Apps::Paddle},
6464
{"2", Apps::Twos},
65-
{"", Apps::None},
65+
{Symbols::qrcode, Apps::Qr},
6666
{"", Apps::None},
6767
{"", Apps::None},
6868
}};

0 commit comments

Comments
 (0)