Skip to content

Commit

Permalink
QR Code of address (#16)
Browse files Browse the repository at this point in the history
* update USB strings for release

* with qr code of puvlic address
  • Loading branch information
m2049r authored Feb 10, 2018
1 parent 266a75b commit c50e7a5
Show file tree
Hide file tree
Showing 9 changed files with 1,405 additions and 15 deletions.
4 changes: 4 additions & 0 deletions include/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#define OLED_HEIGHT 64
#define OLED_BUFSIZE (OLED_WIDTH * OLED_HEIGHT / 8)

#define OLED_CONTRAST_DEFAULT (0x7f)

#define OLED_CHAR_SPACE 1

void oled_setup(void);
Expand Down Expand Up @@ -65,4 +67,6 @@ void oledSwipeRight(void);

void SPISend(uint8_t *data, int len, bool isData);

void oledSetContrast(uint8_t level);

#endif
File renamed without changes.
25 changes: 25 additions & 0 deletions include/qrcode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* This file is part of monerujo-hw
*
* Copyright (C) 2018 m2049r <[email protected]>
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __QRCODE_H__
#define __QRCODE_H__

bool drawQrCode(char* text);

#endif
9 changes: 8 additions & 1 deletion src/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ void oledInvertPixel(int x, int y) {
_oledBuffer[OLED_OFFSET(x, y)] ^= OLED_MASK(x, y);
}

void oledSetContrast(uint8_t level) {
static uint8_t commands[] = {
OLED_SETCONTRAST, 0};
commands[1] = level;
SPISend(commands, sizeof(commands), false);
}

/*
* Initialize the display.
*/
Expand All @@ -124,6 +131,7 @@ void oled_setup() {
static uint8_t initCommands[] = {
OLED_MEMORYMODE, 0x00, // Horizontal Addressing Mode
OLED_CHARGEPUMP, 0x14, // enable charge pump
OLED_SETCONTRAST, OLED_CONTRAST_DEFAULT,
OLED_DISPLAYON };

/*
Expand Down Expand Up @@ -227,7 +235,6 @@ void oledDrawChar_(int x, int y, char c, int zoom, bool inverted) {
}
}


int oledStringWidth(const char *text) {
if (!text)
return 0;
Expand Down
50 changes: 38 additions & 12 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,23 @@
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/

#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#include <libopencm3/stm32/gpio.h>
#include "libopencm3/stm32/rng.h"
#include "libopencm3/cm3/vector.h"
#include "bitmaps.h"
#include "display.h"
#include "setup.h"
#include "usb.h"
#include "util.h"
#include <string.h>
#include <stddef.h>
#include "crypto/sha3.h"
#include "crypto.h"
#include "libopencm3/stm32/rng.h"
#include <stdlib.h>
#include "mnemonics/mnemonics.h"
#include "mnemonics.h"
#include "timer.h"
#include "button.h"
#include "libopencm3/cm3/vector.h"
#include "qrcode.h"

#define NEXT_LINE (FONT_HEIGHT+2)

Expand All @@ -53,26 +54,49 @@ static char public_address[200]; //TODO: correct size + check buffer size on cre
static void showLeftButtonLabel(char* label) {
int x = 2;
int y = OLED_HEIGHT - 1 - FONT_HEIGHT - 1;
oledBox(x - 2, y - 2, x + oledStringWidth(label) - OLED_CHAR_SPACE + 2 - 1, y + FONT_HEIGHT + 1 - 1, true);
oledBox(x - 2, y - 3, x + oledStringWidth(label) - OLED_CHAR_SPACE + 2 - 1 + 1, y + FONT_HEIGHT + 1 + 1, false);
oledBox(x - 2, y - 2, x + oledStringWidth(label) - OLED_CHAR_SPACE + 2 - 1, y + FONT_HEIGHT + 1, true);
oledDrawStringInverted(x, y, label);
}

static void showRightButtonLabel(char* label) {
int x = (OLED_WIDTH - 1) - oledStringWidth(label) - OLED_CHAR_SPACE;
int y = OLED_HEIGHT - 1 - FONT_HEIGHT - 1;
oledBox(x - 2, y - 2, x + oledStringWidth(label) - OLED_CHAR_SPACE + 2 - 1, y + FONT_HEIGHT + 1 - 1, true);
oledBox(x - 2, y - 3, x + oledStringWidth(label) - OLED_CHAR_SPACE + 2 - 1 + 1, y + FONT_HEIGHT + 1 + 1, false);
oledBox(x - 2, y - 2, x + oledStringWidth(label) - OLED_CHAR_SPACE + 2 - 1, y + FONT_HEIGHT + 1, true);
oledDrawStringInverted(x, y, label);
}

static void nextWord(void);
static void prevWord(void);
static void showAddress(void);

#define OLED_CONTRAST_QR 0

#define MONERO_SCHEMA "monero:"

static void showQrAddress(void) {
oledClear();
char* uri[95+7+1];
strcpy(uri, MONERO_SCHEMA);
strcat(uri, public_address);
if (!drawQrCode(uri)) {
oledDrawStringCenter(28, "QR CODE FAILED");
} else {
oledSetContrast(OLED_CONTRAST_QR);
}
showLeftButtonLabel("Address");
oledRefresh();
rightButton.pressed = NULL;
leftButton.pressed = showAddress;
}

static void showAddress(void) {
char myAddress[96]; // 95+terminator
oledClear();
int l = 0;
oledDrawStringCenter(l, "New Wallet Address");
l += (int) (1.5 * NEXT_LINE);
oledDrawStringCenter(l, "New Wallet Address:");
l += NEXT_LINE;
char* p = myAddress;
while (p < myAddress + sizeof(myAddress) - 1) {
strlcpy(myAddress, public_address, sizeof(myAddress));
Expand All @@ -85,8 +109,10 @@ static void showAddress(void) {
l += NEXT_LINE;
p = q; // where we stopped
}
showRightButtonLabel("QR");
oledSetContrast(OLED_CONTRAST_DEFAULT); // because we may be coming from the QR code
oledRefresh();
rightButton.pressed = NULL;
rightButton.pressed = showQrAddress;
leftButton.pressed = NULL;
}

Expand Down Expand Up @@ -229,7 +255,7 @@ static void generateWallet(void) {
showRightButtonLabel("SEED");
oledRefresh();
currentWord = -1;
leftButton.pressed = NULL;
leftButton.pressed = showQrAddress;
rightButton.pressed = nextWord;
}

Expand Down
37 changes: 37 additions & 0 deletions src/qrcode/qrcode.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <stdbool.h>
#include <stdint.h>
#include "qrcodegen.h"
#include "display.h"
#include "qrcode.h"
#include "qrcodegen.h"

static uint8_t qr0[qrcodegen_BUFFER_LEN_MAX];
static uint8_t tempBuffer[qrcodegen_BUFFER_LEN_MAX];

#define MODULE_SIZE 1
// space around qr code
#define DEAD_SPACE (4*MODULE_SIZE)

bool drawQrCode(char* text) {
bool ok = qrcodegen_encodeText(text, tempBuffer, qr0, qrcodegen_Ecc_LOW,
qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true);
if (!ok)
return false;

//oledBox(0, 0, OLED_WIDTH, OLED_HEIGHT, true);
int size = qrcodegen_getSize(qr0);
int x0 = DEAD_SPACE + ((OLED_WIDTH - 2 * DEAD_SPACE) - MODULE_SIZE * size) / 2;
int y0 = DEAD_SPACE + (((OLED_HEIGHT - 1 - FONT_HEIGHT - 1) - 2 * DEAD_SPACE) - MODULE_SIZE * size) / 2;
oledBox(0, 0, OLED_WIDTH - 1, OLED_HEIGHT - 1, true);
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
if (qrcodegen_getModule(qr0, x, y)) {
//oledClearPixel(x0 + x, y0 + y);
oledBox(x0 + MODULE_SIZE * x, y0 + MODULE_SIZE * y, x0 + MODULE_SIZE * x + (MODULE_SIZE - 1),
y0 + MODULE_SIZE * y + (MODULE_SIZE - 1),
false);
}
}
}
return true;
}
Loading

0 comments on commit c50e7a5

Please sign in to comment.