QR Code generation with 3.5" TFT Touch screen on Arudino MEGA2560
Jump to navigation
Jump to search
Synopis
Displays a QR Code on the LCD screen
Notes
This will display a Version 1 QR code. The scaling and centering features are incomplete, but the overall code is functional in this precise instance.
Code
1 #define GUARD 8 //number of guard blocks to create around QR code
2 #define QR_VERSION 3 //Version of the QR Code
3 #define ECC 0 //Error correction level
4 #define SAFE_MIN 1 //Smallest module size before warning is issued
5
6 #include "qrcode.h"
7 #include <LCDWIKI_GUI.h> //Core graphics library
8 #include <LCDWIKI_KBV.h> //Hardware-specific library
9
10 const int spiCSPin = 53;
11 LCDWIKI_KBV mylcd(ILI9486,A3,A2,A1,A0,A4); //model,cs,cd,wr,rd,reset
12
13
14 void setup() {
15 int height = mylcd.Get_Display_Height(); //Formerly 'l'
16 int width = mylcd.Get_Display_Width(); //Formerly 'w'
17 int module_size; //Formerly 'm'
18 int width_offset; //Formerly xo, yo
19 int height_offset;
20 int width_center;
21 int height_center;
22
23 Serial.begin(115200);
24 mylcd.Init_LCD();
25 mylcd.Fill_Screen(0xFFFF);
26 delay(2000);
27 mylcd.Set_Draw_color(0x0000);
28
29 // Create the QR code
30 QRCode qrcode;
31 uint8_t qrcodeData[qrcode_getBufferSize(QR_VERSION)];
32 qrcode_initText(&qrcode, qrcodeData, QR_VERSION, ECC, "Arduino");
33
34 //which dimension constrains the QR code dimensions?
35 //Calculate the maximum length of the module size in pixels
36 //Integer division always truncates to a safe lower value.
37 if(height<width)
38 module_size=height/(qrcode.size+2*GUARD);
39 else
40 module_size=width/(qrcode.size+2*GUARD);
41 if(module_size<SAFE_MIN)
42 Serial.println("Warning: QR Code may be difficult or impossible to scan given display resolution.");
43 height_center = (height - module_size*(qrcode.size+2*GUARD))/2;
44 width_center = (width - module_size*(qrcode.size+2*GUARD))/2;
45
46 Serial.print("Each module is ");
47 Serial.print(module_size);
48 Serial.println(" pixels wide.");
49 Serial.print("QR Size is ");
50 Serial.print(qrcode.size);
51 Serial.println(".");
52
53 // Each vertical module
54 for (uint8_t y = 0; y < qrcode.size; y++)
55 {
56 // Each horizontal module
57 for (uint8_t x = 0; x < qrcode.size; x++)
58 {
59 if(qrcode_getModule(&qrcode, x, y))
60 {
61 width_offset = width_center + module_size*GUARD + module_size*x;
62 height_offset = height_center + module_size*GUARD + module_size*y;
63 for(int i=0;i<module_size;i++)
64 {
65 for(int j=0;j<module_size;j++)
66 {
67 mylcd.Draw_Pixel(width_offset+i,height_offset+j);
68 }
69 }
70 }
71 }
72 }
73 }
74
75 void loop() {
76
77 }