在我們生活中隨處可見二維碼,我們在零知板上使用軟件庫來生成并顯示二維碼。
硬件:零知-標準板、OLED(最好用像素更大的LCD)
連線:連線請參照OLED模塊使用教程進行。
程序源碼如下:
- /**
- * QRCode 零知板-二維碼生成與顯示
- * 2018年7月31日16:23:41
- * by 零知實驗室
- *
- */
- #include <qrcode.h>
- #include <Adafruit_SSD1306.h>
- #define OLED_RESET 4
- Adafruit_SSD1306 display(OLED_RESET);
- void setup() {
- Serial.begin(9600);
-
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
- display.clearDisplay();
- // Start time
- uint32_t dt = millis();
-
- // 根據(jù)字符串生成二維碼
- QRCode qrcode;
- uint8_t qrcodeData[qrcode_getBufferSize(3)];
- qrcode_initText(&qrcode, qrcodeData, 3, 0, "http://www.lingzhilab.com");
-
- // Delta time
- dt = millis() - dt;
- Serial.print("QR Code Generation Time: ");
- Serial.print(dt);
- Serial.print("\n");
- // Top quiet zone
- Serial.print("\n\n\n\n");
-
- //顯示到OLED上
- for (uint8_t y = 0; y < qrcode.size; y++) {
- // Left quiet zone
- Serial.print(" ");
- // Each horizontal module
- for (uint8_t x = 0; x < qrcode.size; x++) {
- // Print each module (UTF-8 \u2588 is a solid block)
- //Serial.print(qrcode_getModule(&qrcode, x, y) ? "\u2588\u2588": " ");
-
- if(qrcode_getModule(&qrcode,x,y)){
- display.drawPixel(x, y, WHITE);
- }else{
- display.drawPixel(x, y, BLACK);
- }
- }
- Serial.print("\n");
- }
-
- display.display();
- // Bottom quiet zone
- Serial.print("\n\n\n\n");
- }
- void loop() {
- }
復制代碼
最后在OLED上可以看到我們生成的二維碼:
31.jpg (282.43 KB, 下載次數(shù): 57)
下載附件
2018-7-31 16:39 上傳
|