Skip to content

Commit

Permalink
Merge pull request #1 from qgyhd1234/master
Browse files Browse the repository at this point in the history
【添加】qrcode package
  • Loading branch information
armink authored Oct 19, 2018
2 parents b4a195b + ce052f3 commit fd7e4d8
Show file tree
Hide file tree
Showing 7 changed files with 1,141 additions and 0 deletions.
52 changes: 52 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Prerequisites
*.d

# Object files
*.o
*.ko
*.obj
*.elf

# Linker output
*.ilk
*.map
*.exp

# Precompiled Headers
*.gch
*.pch

# Libraries
*.lib
*.a
*.la
*.lo

# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib

# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex

# Debug files
*.dSYM/
*.su
*.idb
*.pdb

# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf
94 changes: 94 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# qrcode

## 1、介绍

**qrcode** 是一个用于将字符串生成二维码的软件包。该软件包是 **RT-Thread** 基于 [ricmoo/QRCode](https://github.com/ricmoo/QRCode) 开源库的移植。

### 1.1 目录结构

| 名称 | 说明 |
| ---- | ---- |
| examples | 例子目录,并有相应的一些说明 |
| inc | 头文件目录 |
| src | 源代码目录 |

### 1.2 许可证

`qrcode` 软件包延用 `QRCode` 软件包许可协议,请见 `qrcode/LICENSE` 文件。

### 1.3 依赖

- RT-Thread 3.0+

## 2、如何打开 qrcode

使用 qrcodepackage 需要在 RT-Thread 的包管理器中选择它,具体路径如下:

```shell
RT-Thread online packages
tools packages --->
[*] qrcode: A simple library for generating QR codes in C
[*] Enable qrcode sample
```
- `Enable qrcode sample`: 开启二维码例程;
然后让 RT-Thread 的包管理器自动更新,或者使用 `pkgs --update` 命令更新包到 BSP 中。
## 3、使用 qrcode
### 生成二维码
在使用 qrcode 软件包时首先要定义一个结构体来管理二维码,
```c
QRCode qrcode;
```
然后根据要版本号来申请动态内存,用来保存生成的二维码,
```c
uint8_t *qrcodeBytes = (uint8_t *)rt_calloc(1, qrcode_getBufferSize(DEFAULT_QR_VERSION));
```
最后使用二维码生成函数生成二维码。
```c
qrcode_initText(&qrcode, qrcodeBytes, DEFAULT_QR_VERSION, ECC_LOW, "HELLO WORLD");
```
### 打印二维码
生成的二维码是点阵数据,8个点构成一个 Byte。显示二维码可以使用以下代码
```c
for (uint8 y = 0; y < qrcode.size; y++) {
for (uint8 x = 0; x < qrcode.size; x++) {
if (qrcode_getModule(&qrcode, x, y) {
rt_kprintf("**");
} else {
rt_kprintf(" ");
}
}
Serial.print("\n");
}
```
## 4、示例演示
本示例需要在 qrcode 软件包中开启二维码例程。
在 MSH 中输入命令 qrcode RT-Thread,可以在串口助手上打印出一个二维码来,利用手机扫码软件扫描后可以看到结果为 RT-Thread。
![qrcode](figures/qrcode.gif)
## 5、注意事项
- 在生成二维码时,使用的版本越高,需要申请的动态内存就越大。
- 扫描串口助手上的二维码时,注意设置串口助手白底黑字。
- 扫描串口助手上的二维码时,注意缩小文字显示,让*间的间距减小。
## 6、联系方式 & 感谢
* 维护:[qgyhd1234](https://github.com/qgyhd1234)
* 主页:https://github.com/RT-Thread-packages/qrcode
14 changes: 14 additions & 0 deletions SConscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from building import *

cwd = GetCurrentDir()

src = Glob('src/*.c')

if GetDepend(['PKG_QRCODE_SAMPLE']):
src += Glob('samples/*.c')

CPPPATH = [cwd + '/inc']

group = DefineGroup('qrcode', src, depend = ['PKG_USING_QRCODE'], CPPPATH = CPPPATH)

Return('group')
Binary file added figures/qrcode.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
86 changes: 86 additions & 0 deletions inc/qrcode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017 Richard Moore
*
* 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.
*/

/**
* Special thanks to Nayuki (https://www.nayuki.io/) from which this library was
* heavily inspired and compared against.
*
* See: https://github.com/nayuki/QR-Code-generator/tree/master/cpp
*/


#ifndef __QRCODE_H_
#define __QRCODE_H_

#include <stdint.h>
#include <stdbool.h>

// QR Code Format Encoding
#define MODE_NUMERIC 0
#define MODE_ALPHANUMERIC 1
#define MODE_BYTE 2

// Error Correction Code Levels
#define ECC_LOW 0
#define ECC_MEDIUM 1
#define ECC_QUARTILE 2
#define ECC_HIGH 3


// If set to non-zero, this library can ONLY produce QR codes at that version
// This saves a lot of dynamic memory, as the codeword tables are skipped
#ifndef LOCK_VERSION
#define LOCK_VERSION 0
#endif


typedef struct QRCode {
uint8_t version;
uint8_t size;
uint8_t ecc;
uint8_t mode;
uint8_t mask;
uint8_t *modules;
} QRCode;


#ifdef __cplusplus
extern "C"{
#endif /* __cplusplus */



uint16_t qrcode_getBufferSize(uint8_t version);

int8_t qrcode_initText(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8_t ecc, const char *data);
int8_t qrcode_initBytes(QRCode *qrcode, uint8_t *modules, uint8_t version, uint8_t ecc, uint8_t *data, uint16_t length);

bool qrcode_getModule(QRCode *qrcode, uint8_t x, uint8_t y);

#ifdef __cplusplus
}
#endif /* __cplusplus */


#endif /* __QRCODE_H_ */
56 changes: 56 additions & 0 deletions samples/qrcode_sample.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <rtthread.h>
#include "qrcode.h"

#if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH)
#include <finsh.h>
static void qrcode(uint8_t argc, char **argv)
{
#define DEFAULT_QR_VERSION 3
#define DEFAULT_QR_STRING "HELLO WORLD"

QRCode qrc;
uint8_t x, y, *qrcodeBytes = (uint8_t *)rt_calloc(1, qrcode_getBufferSize(DEFAULT_QR_VERSION));
int8_t result;
char *qrstr = DEFAULT_QR_STRING;

if (qrcodeBytes)
{
if (argc > 1)
{
qrstr = argv[1];
}

result = qrcode_initText(&qrc, qrcodeBytes, DEFAULT_QR_VERSION, ECC_LOW, qrstr);

if (result >= 0)
{
rt_kprintf("\n");
for (y = 0; y < qrc.size; y++)
{
for (x = 0; x < qrc.size; x++)
{
if (qrcode_getModule(&qrc, x, y))
{
rt_kprintf("**");
}
else
{
rt_kprintf(" ");
}
}
rt_kprintf("\n");
}
}
else
{
rt_kprintf("QR CODE(%s) General FAILED(%d)\n", qrstr, result);
}
rt_free(qrcodeBytes);
}
else
{
rt_kprintf("Warning: no memory!\n");
}
}
MSH_CMD_EXPORT(qrcode, qrcode generator: qrcode [string]);
#endif /* defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) */
Loading

0 comments on commit fd7e4d8

Please sign in to comment.