diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/01.\346\225\231\345\255\246PPT\357\274\210PDF\347\211\210\357\274\211/\343\200\220\345\234\250\347\272\277\343\200\221\351\270\277\350\222\231\345\205\245\351\227\250\347\263\273\345\210\227\342\200\224\342\200\224Hi3861\345\274\200\345\217\221\346\235\277\345\237\272\347\241\200\350\257\276\347\250\213\342\200\224\342\200\224\347\254\2541\350\256\262\357\274\232Hi3861\345\274\200\345\217\221\347\216\257\345\242\203\346\220\255\345\273\272.pdf" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/01.\346\225\231\345\255\246PPT\357\274\210PDF\347\211\210\357\274\211/\343\200\220\345\234\250\347\272\277\343\200\221\351\270\277\350\222\231\345\205\245\351\227\250\347\263\273\345\210\227\342\200\224\342\200\224Hi3861\345\274\200\345\217\221\346\235\277\345\237\272\347\241\200\350\257\276\347\250\213\342\200\224\342\200\224\347\254\2541\350\256\262\357\274\232Hi3861\345\274\200\345\217\221\347\216\257\345\242\203\346\220\255\345\273\272.pdf" new file mode 100644 index 0000000000000000000000000000000000000000..2a15bb38a3cdfeccf4d095f0417e411ac445a25e Binary files /dev/null and "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/01.\346\225\231\345\255\246PPT\357\274\210PDF\347\211\210\357\274\211/\343\200\220\345\234\250\347\272\277\343\200\221\351\270\277\350\222\231\345\205\245\351\227\250\347\263\273\345\210\227\342\200\224\342\200\224Hi3861\345\274\200\345\217\221\346\235\277\345\237\272\347\241\200\350\257\276\347\250\213\342\200\224\342\200\224\347\254\2541\350\256\262\357\274\232Hi3861\345\274\200\345\217\221\347\216\257\345\242\203\346\220\255\345\273\272.pdf" differ diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/01.\346\225\231\345\255\246PPT\357\274\210PDF\347\211\210\357\274\211/\343\200\220\345\234\250\347\272\277\343\200\221\351\270\277\350\222\231\345\205\245\351\227\250\347\263\273\345\210\227\342\200\224\342\200\224Hi3861\345\274\200\345\217\221\346\235\277\345\237\272\347\241\200\350\257\276\347\250\213\342\200\224\342\200\224\347\254\2542\350\256\262\357\274\232Hi3861\344\270\212\350\277\220\350\241\214\347\254\254\344\270\200\344\270\252Hello.pdf" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/01.\346\225\231\345\255\246PPT\357\274\210PDF\347\211\210\357\274\211/\343\200\220\345\234\250\347\272\277\343\200\221\351\270\277\350\222\231\345\205\245\351\227\250\347\263\273\345\210\227\342\200\224\342\200\224Hi3861\345\274\200\345\217\221\346\235\277\345\237\272\347\241\200\350\257\276\347\250\213\342\200\224\342\200\224\347\254\2542\350\256\262\357\274\232Hi3861\344\270\212\350\277\220\350\241\214\347\254\254\344\270\200\344\270\252Hello.pdf" new file mode 100644 index 0000000000000000000000000000000000000000..a9544841e44f2c33249bacd12b370f17853fceb9 Binary files /dev/null and "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/01.\346\225\231\345\255\246PPT\357\274\210PDF\347\211\210\357\274\211/\343\200\220\345\234\250\347\272\277\343\200\221\351\270\277\350\222\231\345\205\245\351\227\250\347\263\273\345\210\227\342\200\224\342\200\224Hi3861\345\274\200\345\217\221\346\235\277\345\237\272\347\241\200\350\257\276\347\250\213\342\200\224\342\200\224\347\254\2542\350\256\262\357\274\232Hi3861\344\270\212\350\277\220\350\241\214\347\254\254\344\270\200\344\270\252Hello.pdf" differ diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/01.\346\225\231\345\255\246PPT\357\274\210PDF\347\211\210\357\274\211/\343\200\220\345\234\250\347\272\277\343\200\221\351\270\277\350\222\231\345\205\245\351\227\250\347\263\273\345\210\227\342\200\224\342\200\224Hi3861\345\274\200\345\217\221\346\235\277\345\237\272\347\241\200\350\257\276\347\250\213\342\200\224\342\200\224\347\254\2543\350\256\262\357\274\232Hi3861\345\244\226\350\256\276\342\200\224\342\200\224\346\231\272\350\203\275\347\272\242\347\273\277\347\201\257\346\235\277\345\274\200\345\217\221.pdf" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/01.\346\225\231\345\255\246PPT\357\274\210PDF\347\211\210\357\274\211/\343\200\220\345\234\250\347\272\277\343\200\221\351\270\277\350\222\231\345\205\245\351\227\250\347\263\273\345\210\227\342\200\224\342\200\224Hi3861\345\274\200\345\217\221\346\235\277\345\237\272\347\241\200\350\257\276\347\250\213\342\200\224\342\200\224\347\254\2543\350\256\262\357\274\232Hi3861\345\244\226\350\256\276\342\200\224\342\200\224\346\231\272\350\203\275\347\272\242\347\273\277\347\201\257\346\235\277\345\274\200\345\217\221.pdf" new file mode 100644 index 0000000000000000000000000000000000000000..994048ce42a19d93444cc4c84317b58e89344957 Binary files /dev/null and "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/01.\346\225\231\345\255\246PPT\357\274\210PDF\347\211\210\357\274\211/\343\200\220\345\234\250\347\272\277\343\200\221\351\270\277\350\222\231\345\205\245\351\227\250\347\263\273\345\210\227\342\200\224\342\200\224Hi3861\345\274\200\345\217\221\346\235\277\345\237\272\347\241\200\350\257\276\347\250\213\342\200\224\342\200\224\347\254\2543\350\256\262\357\274\232Hi3861\345\244\226\350\256\276\342\200\224\342\200\224\346\231\272\350\203\275\347\272\242\347\273\277\347\201\257\346\235\277\345\274\200\345\217\221.pdf" differ diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/01.\346\225\231\345\255\246PPT\357\274\210PDF\347\211\210\357\274\211/\343\200\220\345\234\250\347\272\277\343\200\221\351\270\277\350\222\231\345\205\245\351\227\250\347\263\273\345\210\227\342\200\224\342\200\224Hi3861\345\274\200\345\217\221\346\235\277\345\237\272\347\241\200\350\257\276\347\250\213\342\200\224\342\200\224\347\254\2544\350\256\262\357\274\232Hi3861\345\244\226\350\256\276\342\200\224\342\200\224\347\216\257\345\242\203\347\233\221\346\265\213\346\235\277\345\274\200\345\217\221.pdf" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/01.\346\225\231\345\255\246PPT\357\274\210PDF\347\211\210\357\274\211/\343\200\220\345\234\250\347\272\277\343\200\221\351\270\277\350\222\231\345\205\245\351\227\250\347\263\273\345\210\227\342\200\224\342\200\224Hi3861\345\274\200\345\217\221\346\235\277\345\237\272\347\241\200\350\257\276\347\250\213\342\200\224\342\200\224\347\254\2544\350\256\262\357\274\232Hi3861\345\244\226\350\256\276\342\200\224\342\200\224\347\216\257\345\242\203\347\233\221\346\265\213\346\235\277\345\274\200\345\217\221.pdf" new file mode 100644 index 0000000000000000000000000000000000000000..ade4142249e4dc8ba0ea1013900cc556e6ee0bda Binary files /dev/null and "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/01.\346\225\231\345\255\246PPT\357\274\210PDF\347\211\210\357\274\211/\343\200\220\345\234\250\347\272\277\343\200\221\351\270\277\350\222\231\345\205\245\351\227\250\347\263\273\345\210\227\342\200\224\342\200\224Hi3861\345\274\200\345\217\221\346\235\277\345\237\272\347\241\200\350\257\276\347\250\213\342\200\224\342\200\224\347\254\2544\350\256\262\357\274\232Hi3861\345\244\226\350\256\276\342\200\224\342\200\224\347\216\257\345\242\203\347\233\221\346\265\213\346\235\277\345\274\200\345\217\221.pdf" differ diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/01.\346\225\231\345\255\246PPT\357\274\210PDF\347\211\210\357\274\211/\343\200\220\345\234\250\347\272\277\343\200\221\351\270\277\350\222\231\345\205\245\351\227\250\347\263\273\345\210\227\342\200\224\342\200\224Hi3861\345\274\200\345\217\221\346\235\277\345\237\272\347\241\200\350\257\276\347\250\213\342\200\224\342\200\224\347\254\2545\350\256\262\357\274\232Hi3861\344\270\212\344\272\244\344\272\222\345\221\275\344\273\244AT\345\210\206\346\236\220\345\217\212\345\256\236\347\216\260.pdf" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/01.\346\225\231\345\255\246PPT\357\274\210PDF\347\211\210\357\274\211/\343\200\220\345\234\250\347\272\277\343\200\221\351\270\277\350\222\231\345\205\245\351\227\250\347\263\273\345\210\227\342\200\224\342\200\224Hi3861\345\274\200\345\217\221\346\235\277\345\237\272\347\241\200\350\257\276\347\250\213\342\200\224\342\200\224\347\254\2545\350\256\262\357\274\232Hi3861\344\270\212\344\272\244\344\272\222\345\221\275\344\273\244AT\345\210\206\346\236\220\345\217\212\345\256\236\347\216\260.pdf" new file mode 100644 index 0000000000000000000000000000000000000000..65d42a5811b0f0896746a403d5f6fac1d4cd6402 Binary files /dev/null and "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/01.\346\225\231\345\255\246PPT\357\274\210PDF\347\211\210\357\274\211/\343\200\220\345\234\250\347\272\277\343\200\221\351\270\277\350\222\231\345\205\245\351\227\250\347\263\273\345\210\227\342\200\224\342\200\224Hi3861\345\274\200\345\217\221\346\235\277\345\237\272\347\241\200\350\257\276\347\250\213\342\200\224\342\200\224\347\254\2545\350\256\262\357\274\232Hi3861\344\270\212\344\272\244\344\272\222\345\221\275\344\273\244AT\345\210\206\346\236\220\345\217\212\345\256\236\347\216\260.pdf" differ diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/01.\346\225\231\345\255\246PPT\357\274\210PDF\347\211\210\357\274\211/\343\200\220\345\234\250\347\272\277\343\200\221\351\270\277\350\222\231\345\205\245\351\227\250\347\263\273\345\210\227\342\200\224\342\200\224Hi3861\345\274\200\345\217\221\346\235\277\345\237\272\347\241\200\350\257\276\347\250\213\342\200\224\342\200\224\347\254\2546\350\256\262\357\274\232Hi3861\344\270\212WiFi\345\274\200\345\217\221.pdf" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/01.\346\225\231\345\255\246PPT\357\274\210PDF\347\211\210\357\274\211/\343\200\220\345\234\250\347\272\277\343\200\221\351\270\277\350\222\231\345\205\245\351\227\250\347\263\273\345\210\227\342\200\224\342\200\224Hi3861\345\274\200\345\217\221\346\235\277\345\237\272\347\241\200\350\257\276\347\250\213\342\200\224\342\200\224\347\254\2546\350\256\262\357\274\232Hi3861\344\270\212WiFi\345\274\200\345\217\221.pdf" new file mode 100644 index 0000000000000000000000000000000000000000..32c1ebabc7fc1df868a82c03f46f6e3b3d3da9c4 Binary files /dev/null and "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/01.\346\225\231\345\255\246PPT\357\274\210PDF\347\211\210\357\274\211/\343\200\220\345\234\250\347\272\277\343\200\221\351\270\277\350\222\231\345\205\245\351\227\250\347\263\273\345\210\227\342\200\224\342\200\224Hi3861\345\274\200\345\217\221\346\235\277\345\237\272\347\241\200\350\257\276\347\250\213\342\200\224\342\200\224\347\254\2546\350\256\262\357\274\232Hi3861\344\270\212WiFi\345\274\200\345\217\221.pdf" differ diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/01.\346\225\231\345\255\246PPT\357\274\210PDF\347\211\210\357\274\211/\343\200\220\345\234\250\347\272\277\343\200\221\351\270\277\350\222\231\345\205\245\351\227\250\347\263\273\345\210\227\342\200\224\342\200\224Hi3861\345\274\200\345\217\221\346\235\277\345\237\272\347\241\200\350\257\276\347\250\213\342\200\224\342\200\224\347\254\2547\350\256\262\357\274\232Hi3861\344\270\212\347\275\221\347\273\234\347\274\226\347\250\213\345\274\200\345\217\221.pdf" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/01.\346\225\231\345\255\246PPT\357\274\210PDF\347\211\210\357\274\211/\343\200\220\345\234\250\347\272\277\343\200\221\351\270\277\350\222\231\345\205\245\351\227\250\347\263\273\345\210\227\342\200\224\342\200\224Hi3861\345\274\200\345\217\221\346\235\277\345\237\272\347\241\200\350\257\276\347\250\213\342\200\224\342\200\224\347\254\2547\350\256\262\357\274\232Hi3861\344\270\212\347\275\221\347\273\234\347\274\226\347\250\213\345\274\200\345\217\221.pdf" new file mode 100644 index 0000000000000000000000000000000000000000..bda8cc81536075420539204bcb88ace454f0b8ac Binary files /dev/null and "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/01.\346\225\231\345\255\246PPT\357\274\210PDF\347\211\210\357\274\211/\343\200\220\345\234\250\347\272\277\343\200\221\351\270\277\350\222\231\345\205\245\351\227\250\347\263\273\345\210\227\342\200\224\342\200\224Hi3861\345\274\200\345\217\221\346\235\277\345\237\272\347\241\200\350\257\276\347\250\213\342\200\224\342\200\224\347\254\2547\350\256\262\357\274\232Hi3861\344\270\212\347\275\221\347\273\234\347\274\226\347\250\213\345\274\200\345\217\221.pdf" differ diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/01.\346\225\231\345\255\246PPT\357\274\210PDF\347\211\210\357\274\211/\343\200\220\345\234\250\347\272\277\343\200\221\351\270\277\350\222\231\345\205\245\351\227\250\347\263\273\345\210\227\342\200\224\342\200\224Hi3861\345\274\200\345\217\221\346\235\277\345\237\272\347\241\200\350\257\276\347\250\213\342\200\224\342\200\224\347\254\2548\350\256\262\357\274\232Hi3861\344\270\212\347\211\251\350\201\224\347\275\221\345\272\224\347\224\250\345\274\200\345\217\221.pdf" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/01.\346\225\231\345\255\246PPT\357\274\210PDF\347\211\210\357\274\211/\343\200\220\345\234\250\347\272\277\343\200\221\351\270\277\350\222\231\345\205\245\351\227\250\347\263\273\345\210\227\342\200\224\342\200\224Hi3861\345\274\200\345\217\221\346\235\277\345\237\272\347\241\200\350\257\276\347\250\213\342\200\224\342\200\224\347\254\2548\350\256\262\357\274\232Hi3861\344\270\212\347\211\251\350\201\224\347\275\221\345\272\224\347\224\250\345\274\200\345\217\221.pdf" new file mode 100644 index 0000000000000000000000000000000000000000..f0493c3802c218b9761cb0a3ace749bd15392fb2 Binary files /dev/null and "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/01.\346\225\231\345\255\246PPT\357\274\210PDF\347\211\210\357\274\211/\343\200\220\345\234\250\347\272\277\343\200\221\351\270\277\350\222\231\345\205\245\351\227\250\347\263\273\345\210\227\342\200\224\342\200\224Hi3861\345\274\200\345\217\221\346\235\277\345\237\272\347\241\200\350\257\276\347\250\213\342\200\224\342\200\224\347\254\2548\350\256\262\357\274\232Hi3861\344\270\212\347\211\251\350\201\224\347\275\221\345\272\224\347\224\250\345\274\200\345\217\221.pdf" differ diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_02/BUILD.gn" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_02/BUILD.gn" new file mode 100644 index 0000000000000000000000000000000000000000..55ac750e1368d8fe740fcad719496e1e9778e669 --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_02/BUILD.gn" @@ -0,0 +1,10 @@ + +static_library("myapp2") { + sources = [ + "hello_world.c" + ] + + include_dirs = [ + "//utils/native/lite/include" + ] +} diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_02/hello_world.c" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_02/hello_world.c" new file mode 100644 index 0000000000000000000000000000000000000000..238628517dd525f0505994cfb5c0c369e7d2103f --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_02/hello_world.c" @@ -0,0 +1,10 @@ +#include +#include "ohos_init.h" + + void hello_world(void){ + + printf("chinasoft ----->hello world!"); + + } + + SYS_RUN(hello_world);//启动恢复模块接口 \ No newline at end of file diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_03/BUILD.gn" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_03/BUILD.gn" new file mode 100644 index 0000000000000000000000000000000000000000..1f3dbbc9f271b1d73ee9371faed33d111c55491e --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_03/BUILD.gn" @@ -0,0 +1,15 @@ +static_library("chapter_03_demo") { + sources = [ + # "led_demo.c", + # "traffic_light_demo.c", + # "beeper_demo.c", + "beeper_music_demo.c", + ] + + include_dirs = [ + "//utils/native/lite/include", + "//kernel/liteos_m/components/cmsis/2.0", + "//base/iot_hardware/interfaces/kits/wifiiot_lite", + ] +} + diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_03/README.md" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_03/README.md" new file mode 100644 index 0000000000000000000000000000000000000000..98510981ac55bf4a311130fb864292878f60eb13 --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_03/README.md" @@ -0,0 +1,63 @@ +# Harmony OS物联网应用开发实战 第二章 + +**本节课程主要介绍如何在HiSpark WiFi IoT套件上使用Hamony OS进行编程,以及如何使用GPIO输入输出功能。** + + + +## 相关知识点 + +* 官方第一个应用程序示例演示 + * 熟悉使用DevEco Device Tool插件进行程序烧录 + * 熟悉串口调试工具sscom的使用 +* 官方文档中控制核心板上LED的演示 + * 源码路径:`applications/sample/wifi-iot/app/iothardware/led_example.c` + * Harmony OS IoT内核(liteos-m)接口——[CMSIS-RTOS API v2](https://arm-software.github.io/CMSIS_5/RTOS2/html/rtos_api2.html) + * 线程创建`osThreadNew` + * 鸿蒙系统启动注册接口——`ohos_init.h` + * `APP_FEATURE_INIT`注册自启动函数 + * 使用GPIO接口输出高低电平,实现控制LED灯亮灭 + * 设置管脚功能`IoSetFunc` + * 设置GPIO引脚方向`GpioSetDir` + * 使用GPIO接口注册中断处理函数,实现USER按键响应(输入) + * 设置内部上拉下拉状态`IoSetPull`(没有外部上拉电阻时需要设置上拉,才能保证能够检测到下降沿) + * 注册中断处理函数`GpioRegisterIsrFunc`,具体参数使用参考头文件说明 + * 具体使用参考`wifiiot_gpio.h`和`wifiiot_gpio_ex.h`文件里的注释 +* HiSpark Wi-Fi IoT 开发套件 交通灯板 控制 + * 使用PWM接口输出PWM方波,实现蜂鸣器控制 + * 使用GPIO接口的部分和上一个示例类似 + * 具体使用参考`wifiiot_pwm.h`文件里的注释 + + + +## 核心板和交通灯板上外设与主控芯片(Hi3861)引脚的对应关系 + +* 核心板 + * GPIO_9 连接LED灯,输出低电平点亮LED灯 + * GPIO_5 连接按键,按键中断控制LED灯状态反转 +* 交通灯板 + * GPIO_8 连接蜂鸣器,输出PWM波控制蜂鸣器发出声音 + * GPIO_9 连接按键,按键中断切换亮的LED灯,同时切换蜂鸣器响或者不响 + * GPIO_10 连接红色LED,输出高电平点亮红色LED灯 + * GPIO_11 连接绿色LED,输出高电平点亮绿色LED灯 + * GPIO_12 连接黄色LED,输出高电平点亮黄色LED灯 + + + +## 如何编译 + +1. 将此目录下的 `traffic_light_demo.c` 和 `BUILD.gn` 复制到openharmony源码的`applications\sample\wifi-iot\app\iothardware`目录下, +2. 修改openharmony源码的`applications\sample\wifi-iot\app\BUILD.gn`文件,将其中的 `features` 改为: +```python + features = [ + "iothardware:traffic_light_demo", + ] +``` +3. 在openharmony源码顶层目录执行:`python build.py wifiiot` + +### 报错解决 + +1. 编译过程中报错:undefined reference to `hi_pwm_init` 等几个 `hi_pwm_`开头的函数, + * **原因:** 因为默认情况下,hi3861_sdk中,PWM的CONFIG选项没有打开 + * **解决:** 修改`vendor\hisi\hi3861\hi3861\build\config\usr_config.mk`文件中的`CONFIG_PWM_SUPPORT`行: + * `# CONFIG_PWM_SUPPORT is not set`修改为`CONFIG_PWM_SUPPORT=y` + diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_03/beeper_demo.c" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_03/beeper_demo.c" new file mode 100644 index 0000000000000000000000000000000000000000..01c4340d88a4324a4b43c74cde16364ae9c5608d --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_03/beeper_demo.c" @@ -0,0 +1,34 @@ +#include +#include +#include "ohos_init.h" +#include "cmsis_os2.h" +#include "wifiiot_gpio.h" +#include "wifiiot_gpio_ex.h" +#include "wifiiot_pwm.h" +#include "hi_pwm.h" + +void init(void) +{ + GpioInit(); + + // 蜂鸣器引脚 设置为 PWM功能 + IoSetFunc(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_IO_FUNC_GPIO_9_PWM0_OUT); + PwmInit(WIFI_IOT_PWM_PORT_PWM0); +} + +void BeeperDemo(void) +{ + init(); + while (1) + { + + uint16_t freqDivisor = 34052; + // 占空比, 频率 + PwmStart(WIFI_IOT_PWM_PORT_PWM0, freqDivisor / 2, freqDivisor); + usleep(2000000); + PwmStop(WIFI_IOT_PWM_PORT_PWM0); + usleep(1000000); + } +} + +SYS_RUN(BeeperDemo); \ No newline at end of file diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_03/beeper_music_demo.c" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_03/beeper_music_demo.c" new file mode 100644 index 0000000000000000000000000000000000000000..7493c8bf2e7e5b8624714b1263805184021a8a9e --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_03/beeper_music_demo.c" @@ -0,0 +1,84 @@ +#include +#include +#include "ohos_init.h" +#include "cmsis_os2.h" +#include "wifiiot_gpio.h" +#include "wifiiot_gpio_ex.h" +#include "wifiiot_watchdog.h" +#include "wifiiot_pwm.h" +#include "hi_pwm.h" + +static const uint16_t g_tuneFreqs[] = { + 0, // 40M Hz 对应的分频系数: + 38223, // 1046.5 + 34052, // 1174.7 + 30338, // 1318.5 + 28635, // 1396.9 + 25511, // 1568 + 22728, // 1760 + 20249, // 1975.5 + 51021 // 5_ 783.99 // 第一个八度的 5 +}; + +// 曲谱音符 +static const uint8_t g_scoreNotes[] = { + // 《两只老虎》简谱:http://www.jianpu.cn/pu/33/33945.htm + 1, 2, 3, 1, 1, 2, 3, 1, 3, 4, 5, 3, 4, 5, + 5, 6, 5, 4, 3, 1, 5, 6, 5, 4, 3, 1, 1, 8, 1, 1, 8, 1, +}; + +// 曲谱时值 +static const uint8_t g_scoreDurations[] = { + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 4, 4, 8, + 3, 1, 3, 1, 4, 4, 3, 1, 3, 1, 4, 4, 4, 4, 8, 4, 4, 8, +}; + +static void *BeeperMusicTask(const char *arg) +{ + (void)arg; + printf("BeeperMusicTask start!\r\n"); + hi_pwm_set_clock(PWM_CLK_XTAL); // 设置时钟源为晶体时钟(40MHz,默认时钟源160MHz) + while (1) + { + for (size_t i = 0; i < sizeof(g_scoreNotes) / sizeof(g_scoreNotes[0]); i++) + { + uint32_t tune = g_scoreNotes[i]; // 音符 + uint16_t freqDivisor = g_tuneFreqs[tune]; //获取 频率 + uint32_t tuneInterval = g_scoreDurations[i] * (125 * 1000); // 音符时间 ,节拍, + printf("%d %d %d %d\r\n", tune, (40 * 1000 * 1000) / freqDivisor, freqDivisor, tuneInterval); + PwmStart(WIFI_IOT_PWM_PORT_PWM0, freqDivisor / 2, freqDivisor); + usleep(tuneInterval);//持续响tuneInterval时间, + PwmStop(WIFI_IOT_PWM_PORT_PWM0); + } + usleep(2000*1000); + } + return NULL; +} + + +static void StartBeepMusicTask(void) +{ + osThreadAttr_t attr; + GpioInit(); + // 蜂鸣器引脚 设置为 PWM功能 + IoSetFunc(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_IO_FUNC_GPIO_9_PWM0_OUT); + PwmInit(WIFI_IOT_PWM_PORT_PWM0); + WatchDogDisable(); + + //设置新线程属性 + attr.name = "BeeperMusicTask"; + attr.attr_bits = 0U; + attr.cb_mem = NULL; + attr.cb_size = 0U; + attr.stack_mem = NULL; + attr.stack_size = 1024; + attr.priority = osPriorityNormal; + + //创建线程,在新的线程中运行BeeperMusicTask函数 + if (osThreadNew((osThreadFunc_t)BeeperMusicTask, NULL, &attr) == NULL) + { + printf("[BeeperMusicTask] Falied to create BeeperMusicTask!\n"); + } +} + +SYS_RUN(StartBeepMusicTask); \ No newline at end of file diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_03/led_demo.c" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_03/led_demo.c" new file mode 100644 index 0000000000000000000000000000000000000000..1955f17ead121427d912194666f6bcedba63c662 --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_03/led_demo.c" @@ -0,0 +1,27 @@ +#include +#include "ohos_init.h" +#include +#include "wifiiot_gpio.h" +#include "wifiiot_gpio_ex.h" +#include "ohos_types.h" +void leddemo(void) +{ + GpioInit(); //初始化GPIO设备,使用GPIO管脚前调用 + + //设置GPIO_9功能为IO + IoSetFunc(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_IO_FUNC_GPIO_9_GPIO); + //设置GPIO_9的为输出 + GpioSetDir(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_GPIO_DIR_OUT); + //设置GPIO_9输出值为低电平。值为0 + GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW); + + printf("led 点亮\n");//在终端打印LED状态 + + usleep(4000000);//等待4秒 + + //设置GPIO_9输出值为高电平,LED熄灭 + GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_GPIO_EDGE_RISE_LEVEL_HIGH); + printf("led 熄灭\n"); + +} +SYS_RUN(leddemo);//注册leddemo函数在启动阶段运行 \ No newline at end of file diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_03/traffic_light_demo.c" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_03/traffic_light_demo.c" new file mode 100644 index 0000000000000000000000000000000000000000..8a342320f7e5b777897ead861f74d54151f200aa --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_03/traffic_light_demo.c" @@ -0,0 +1,43 @@ +#include +#include + +#include "ohos_init.h" +#include "cmsis_os2.h" +#include "wifiiot_gpio.h" +#include "wifiiot_gpio_ex.h" + +//对GPIO管脚进行初始化 +void init(void) +{ + GpioInit(); + IoSetFunc(WIFI_IOT_IO_NAME_GPIO_10, WIFI_IOT_IO_FUNC_GPIO_10_GPIO); + GpioSetDir(WIFI_IOT_IO_NAME_GPIO_10, WIFI_IOT_GPIO_DIR_OUT); + + IoSetFunc(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_IO_FUNC_GPIO_11_GPIO); + GpioSetDir(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_GPIO_DIR_OUT); + + IoSetFunc(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_IO_FUNC_GPIO_12_GPIO); + GpioSetDir(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_GPIO_DIR_OUT); +} + +static void StartTrafficLightTask(void) +{ + init(); + while (1) + { + //绿灯亮 + GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_GPIO_VALUE1); + usleep(3000 * 1000); + //绿灯灭、黄灯亮 + GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_GPIO_VALUE0); + GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_GPIO_VALUE1); + usleep(1000 * 1000); + //黄灯灭、红灯这 + GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_GPIO_VALUE0); + GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_10, WIFI_IOT_GPIO_VALUE1); + usleep(3000 * 1000);//10红灯 + GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_10, WIFI_IOT_GPIO_VALUE0); + } +} + +APP_FEATURE_INIT(StartTrafficLightTask); //注册StartTrafficLightTask函数为系统启动后运行 \ No newline at end of file diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_04/BUILD.gn" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_04/BUILD.gn" new file mode 100644 index 0000000000000000000000000000000000000000..8616076a00888599c0841f1129a7d148a13f6f87 --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_04/BUILD.gn" @@ -0,0 +1,15 @@ + +static_library("sensing_demo") { + sources = [ + # "aht20_demo.c","aht20.c", + # "mq2_demo.c" + # "oled_demo.c", "oled_ssd1306.c", + "enrionment_demo.c", "oled_ssd1306.c", "aht20.c" + ] + + include_dirs = [ + "//utils/native/lite/include", + "//kernel/liteos_m/components/cmsis/2.0", + "//base/iot_hardware/interfaces/kits/wifiiot_lite", + ] +} diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_04/README.md" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_04/README.md" new file mode 100644 index 0000000000000000000000000000000000000000..242a5a78092664a6df967ae5df14fb94a10b6dd8 --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_04/README.md" @@ -0,0 +1,106 @@ +# Harmony OS物联网应用开发实战 第三章 + +**本节课程中主要介绍如何在HiSpark WiFi IoT套件上使用Hamony OS的传感器相关编程接口,即环境感知能力。** + + + +## 相关知识点 + +本节涉及Harmony OS外设相关接口: + +* ADC接口 + + * `wifiiot_adc.h` 头文件 + + * ```c + unsigned int AdcRead(WifiIotAdcChannelIndex channel, unsigned short *data, WifiIotAdcEquModelSel equModel, + WifiIotAdcCurBais curBais, unsigned short rstCnt); + ``` + +* I2C接口 + + * `wifiiot_i2c.h`头文件 + * `I2cInit`/`I2cDeinit`/`I2cRead`/`I2cWrite` + + + +## 外设与主控芯片引脚的对应关系 + +* HiSpark Wi-Fi IoT 开发套件 炫彩灯板 + + * 三色LED——PWM占空比控制颜色 + * GPIO10: 红 + * GPIO11: 绿 + * GPIO12: 蓝 + * 光敏电阻——ADC采集值和电压对应,电压和电阻对应,电阻和光照强度对应 + * GPIO09: ADC4 + * 人体红外传感器——ADC采集值反应是否有人靠近 + * GPIO07: ADC3 + +* HiSpark Wi-Fi IoT 开发套件 OLED屏板 + + * OLED屏——SSD1306芯片,I2C总线, 地址 0x78 + * GPIO13: I2C0_SDA + * GPIO14: I2C0_SCL + * 两个按键——ADC,接在同一个GPIO引脚上,通过电压区分 + * GPIO05: ADC2 + +* HiSpark Wi-Fi IoT 开发套件 环境检测板 + + * 蜂鸣器——PWM控制声音的评率和音量 + + * GPIO09: PWM0 + + * MQ2 燃气传感器——ADC读取模拟值 + + * GPIO11: ADC5 + + * AHT20 温湿度传感器——I2C接口通信,地址 0x38 + + * GPIO13: I2C0_SDA + * GPIO14: I2C0_SCL + + + +## 如何编译 + +1. 将此目录下的所有`.c`文件和`BUILD.gn`文件拷贝到,openharmony源码的`applications\sample\wifi-iot\app\iothardware`目录下, + +2. 修改openharmony源码的`applications\sample\wifi-iot\app\BUILD.gn`文件,将其中的 `features` 改为: + + ```python + features = [ + "iothardware:sensing_demo", + ] + ``` + +3. 修改`applications\sample\wifi-iot\app\iothardware\BUILD.gn` 中的`solo_demo`下的`sources`属性,放开其中的的一个文件的注释,以开启选择对应功能文件; + + ```python + sources = [ + "colorful_light_demo.c", + # "enrionment_demo.c", + # "oled_demo.c", "oled_ssd1306.c" + ] + ``` + +4. 在openharmony源码顶层目录执行:`python build.py wifiiot` + + + +### 报错解决 + +1. 编译过程中报错:undefined reference to `hi_pwm_init` 等几个 `hi_pwm_`开头的函数, + * **原因:** 因为默认情况下,hi3861_sdk中,PWM的CONFIG选项没有打开 + * **解决:** 修改`vendor\hisi\hi3861\hi3861\build\config\usr_config.mk`文件中的`CONFIG_PWM_SUPPORT`行: + * `# CONFIG_PWM_SUPPORT is not set`修改为`CONFIG_PWM_SUPPORT=y` + +2. 编译过程中报错:undefined reference to `hi_i2c_init` 等几个 `hi_i2c_`开头的函数, + + * **原因:** 因为默认情况下,hi3861_sdk中,I2C的CONFIG选项没有打开 + + * **解决:** 修改`vendor\hisi\hi3861\hi3861\build\config\usr_config.mk`文件中的`CONFIG_I2C_SUPPORT`行: + * `# CONFIG_I2C_SUPPORT is not set`修改为`CONFIG_I2C_SUPPORT=y` + + + diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_04/aht20.c" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_04/aht20.c" new file mode 100644 index 0000000000000000000000000000000000000000..40bc6efb141423d80705a21e505c86e77a69cc1a --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_04/aht20.c" @@ -0,0 +1,189 @@ +#include "aht20.h" + +#include +#include +#include + +#include "wifiiot_i2c.h" +#include "wifiiot_errno.h" + +#define AHT20_I2C_IDX WIFI_IOT_I2C_IDX_0 + +#define AHT20_STARTUP_TIME 20*1000 // 上电启动时间 +#define AHT20_CALIBRATION_TIME 40*1000 // 初始化(校准)时间 +#define AHT20_MEASURE_TIME 75*1000 // 测量时间 + +#define AHT20_DEVICE_ADDR 0x38 +#define AHT20_READ_ADDR ((0x38<<1)|0x1) +#define AHT20_WRITE_ADDR ((0x38<<1)|0x0) + +#define AHT20_CMD_CALIBRATION 0xBE // 初始化(校准)命令 +#define AHT20_CMD_CALIBRATION_ARG0 0x08 +#define AHT20_CMD_CALIBRATION_ARG1 0x00 + +/** + * 传感器在采集时需要时间,主机发出测量指令(0xAC)后,延时75毫秒以上再读取转换后的数据并判断返回的状态位是否正常。 + * 若状态比特位[Bit7]为0代表数据可正常读取,为1时传感器为忙状态,主机需要等待数据处理完成。 + **/ +#define AHT20_CMD_TRIGGER 0xAC // 触发测量命令 +#define AHT20_CMD_TRIGGER_ARG0 0x33 +#define AHT20_CMD_TRIGGER_ARG1 0x00 + +// 用于在无需关闭和再次打开电源的情况下,重新启动传感器系统,软复位所需时间不超过20 毫秒 +#define AHT20_CMD_RESET 0xBA // 软复位命令 + +#define AHT20_CMD_STATUS 0x71 // 获取状态命令 + +/** + * STATUS 命令回复: + * 1. 初始化后触发测量之前,STATUS 只回复 1B 状态值; + * 2. 触发测量之后,STATUS 回复6B: 1B 状态值 + 2B 湿度 + 4b湿度 + 4b温度 + 2B 温度 + * RH = Srh / 2^20 * 100% + * T = St / 2^20 * 200 - 50 + **/ +#define AHT20_STATUS_BUSY_SHIFT 7 // bit[7] Busy indication +#define AHT20_STATUS_BUSY_MASK (0x1<> AHT20_STATUS_BUSY_SHIFT) + +#define AHT20_STATUS_MODE_SHIFT 5 // bit[6:5] Mode Status +#define AHT20_STATUS_MODE_MASK (0x3<> AHT20_STATUS_MODE_SHIFT) + + // bit[4] Reserved +#define AHT20_STATUS_CALI_SHIFT 3 // bit[3] CAL Enable +#define AHT20_STATUS_CALI_MASK (0x1<> AHT20_STATUS_CALI_SHIFT) + // bit[2:0] Reserved + +#define AHT20_STATUS_RESPONSE_MAX 6 + +#define AHT20_RESLUTION (1<<20) // 2^20 + +#define AHT20_MAX_RETRY 10 + +static uint32_t AHT20_Read(uint8_t* buffer, uint32_t buffLen) +{ + WifiIotI2cData data = { 0 }; + data.receiveBuf = buffer; + data.receiveLen = buffLen; + uint32_t retval = I2cRead(AHT20_I2C_IDX, AHT20_READ_ADDR, &data); + if (retval != WIFI_IOT_SUCCESS) { + printf("I2cRead() failed, %0X!\n", retval); + return retval; + } + return WIFI_IOT_SUCCESS; +} + +static uint32_t AHT20_Write(uint8_t* buffer, uint32_t buffLen) +{ + WifiIotI2cData data = { 0 }; + data.sendBuf = buffer; + data.sendLen = buffLen; + uint32_t retval = I2cWrite(AHT20_I2C_IDX, AHT20_WRITE_ADDR, &data); + if (retval != WIFI_IOT_SUCCESS) { + printf("I2cWrite(%02X) failed, %0X!\n", buffer[0], retval); + return retval; + } + return WIFI_IOT_SUCCESS; +} + +// 发送获取状态命令 +static uint32_t AHT20_StatusCommand(void) +{ + uint8_t statusCmd[] = { AHT20_CMD_STATUS }; + return AHT20_Write(statusCmd, sizeof(statusCmd)); +} + +// 发送软复位命令 +static uint32_t AHT20_ResetCommand(void) +{ + uint8_t resetCmd[] = {AHT20_CMD_RESET}; + return AHT20_Write(resetCmd, sizeof(resetCmd)); +} + +// 发送初始化校准命令 +static uint32_t AHT20_CalibrateCommand(void) +{ + uint8_t clibrateCmd[] = {AHT20_CMD_CALIBRATION, AHT20_CMD_CALIBRATION_ARG0, AHT20_CMD_CALIBRATION_ARG1}; + return AHT20_Write(clibrateCmd, sizeof(clibrateCmd)); +} + +// 读取温湿度值之前, 首先要看状态字的校准使能位Bit[3]是否为 1(通过发送0x71可以获取一个字节的状态字), +// 如果不为1,要发送0xBE命令(初始化),此命令参数有两个字节, 第一个字节为0x08,第二个字节为0x00。 +uint32_t AHT20_Calibrate(void) +{ + uint32_t retval = 0; + uint8_t buffer[AHT20_STATUS_RESPONSE_MAX] = { AHT20_CMD_STATUS }; + memset(&buffer, 0x0, sizeof(buffer)); + + retval = AHT20_StatusCommand(); + if (retval != WIFI_IOT_SUCCESS) { + return retval; + } + + retval = AHT20_Read(buffer, sizeof(buffer)); + if (retval != WIFI_IOT_SUCCESS) { + return retval; + } + + if (AHT20_STATUS_BUSY(buffer[0]) || !AHT20_STATUS_CALI(buffer[0])) { + retval = AHT20_ResetCommand(); + if (retval != WIFI_IOT_SUCCESS) { + return retval; + } + usleep(AHT20_STARTUP_TIME); + retval = AHT20_CalibrateCommand(); + usleep(AHT20_CALIBRATION_TIME); + return retval; + } + + return WIFI_IOT_SUCCESS; +} + +// 发送 触发测量 命令,开始测量 +uint32_t AHT20_StartMeasure(void) +{ + uint8_t triggerCmd[] = {AHT20_CMD_TRIGGER, AHT20_CMD_TRIGGER_ARG0, AHT20_CMD_TRIGGER_ARG1}; + return AHT20_Write(triggerCmd, sizeof(triggerCmd)); +} + +// 接收测量结果,拼接转换为标准值 +uint32_t AHT20_GetMeasureResult(float* temp, float* humi) +{ + uint32_t retval = 0, i = 0; + if (temp == NULL || humi == NULL) { + return WIFI_IOT_FAILURE; + } + + uint8_t buffer[AHT20_STATUS_RESPONSE_MAX] = { 0 }; + memset(&buffer, 0x0, sizeof(buffer)); + retval = AHT20_Read(buffer, sizeof(buffer)); // recv status command result + if (retval != WIFI_IOT_SUCCESS) { + return retval; + } + + for (i = 0; AHT20_STATUS_BUSY(buffer[0]) && i < AHT20_MAX_RETRY; i++) { + // printf("AHT20 device busy, retry %d/%d!\r\n", i, AHT20_MAX_RETRY); + usleep(AHT20_MEASURE_TIME); + retval = AHT20_Read(buffer, sizeof(buffer)); // recv status command result + if (retval != WIFI_IOT_SUCCESS) { + return retval; + } + } + if (i >= AHT20_MAX_RETRY) { + printf("AHT20 device always busy!\r\n"); + return WIFI_IOT_FAILURE; + } + + uint32_t humiRaw = buffer[1]; + humiRaw = (humiRaw << 8) | buffer[2]; + humiRaw = (humiRaw << 4) | ((buffer[3] & 0xF0) >> 4); + *humi = humiRaw / (float)AHT20_RESLUTION * 100; + + uint32_t tempRaw = buffer[3] & 0x0F; + tempRaw = (tempRaw << 8) | buffer[4]; + tempRaw = (tempRaw << 8) | buffer[5]; + *temp = tempRaw / (float)AHT20_RESLUTION * 200 - 50; + // printf("humi = %05X, %f, temp= %05X, %f\r\n", humiRaw, *humi, tempRaw, *temp); + return WIFI_IOT_SUCCESS; +} diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_04/aht20.h" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_04/aht20.h" new file mode 100644 index 0000000000000000000000000000000000000000..5ee89f2ecb4daf6cd3541aa01c6eebae2db88423 --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_04/aht20.h" @@ -0,0 +1,14 @@ + + +#ifndef AHT20_H +#define AHT20_H + +#include +// 发送初始化校准命令 +uint32_t AHT20_Calibrate(void); +// 发送 触发测量 命令,开始测量 +uint32_t AHT20_StartMeasure(void); +// 接收测量结果,拼接转换为标准值 +uint32_t AHT20_GetMeasureResult(float* temp, float* humi); + +#endif // AHT20_H \ No newline at end of file diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_04/aht20_demo.c" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_04/aht20_demo.c" new file mode 100644 index 0000000000000000000000000000000000000000..8c76b0a8a4805e52dc690aceb3c98c14459a46c1 --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_04/aht20_demo.c" @@ -0,0 +1,67 @@ +#include +#include +#include +#include + +#include "ohos_init.h" +#include "cmsis_os2.h" +#include "wifiiot_i2c.h" +#include "wifiiot_gpio.h" +#include "wifiiot_gpio_ex.h" +#include "wifiiot_errno.h" +#include "aht20.h" + +#define AHT20_BAUDRATE 400 * 1000 +#define AHT20_I2C_IDX WIFI_IOT_I2C_IDX_0 + +void init(void) +{ + GpioInit(); + IoSetFunc(WIFI_IOT_IO_NAME_GPIO_13, WIFI_IOT_IO_FUNC_GPIO_13_I2C0_SDA); + IoSetFunc(WIFI_IOT_IO_NAME_GPIO_14, WIFI_IOT_IO_FUNC_GPIO_14_I2C0_SCL); + I2cInit(AHT20_I2C_IDX, AHT20_BAUDRATE); +} + +static void AhtDemoTask(void *arg){ + (void)arg; + init();//初始化IIC + uint32_t retval = 0; + float humidity = 0.0f; + float temperature = 0.0f; + // 发送初始化校准命令 + while (WIFI_IOT_SUCCESS != AHT20_Calibrate()){ + printf("AHT20 sensor init failed!\r\n"); + usleep(1000); + } + while (1){ + // 发送 触发测量 命令,开始测量 + retval = AHT20_StartMeasure(); + if (retval != WIFI_IOT_SUCCESS){ + printf("trigger measure failed!\r\n"); + }else { + // 接收测量结果,拼接转换为标准值 + retval = AHT20_GetMeasureResult(&temperature, &humidity); + printf("temp: %.2f, humi: %.2f\n", temperature, humidity); + } + sleep(1); + } +} + +static void AhtDemo(void){ + osThreadAttr_t attr; + + attr.name = "AhtDemoTask"; + attr.attr_bits = 0U; + attr.cb_mem = NULL; + attr.cb_size = 0U; + attr.stack_mem = NULL; + attr.stack_size = 4096; + attr.priority = osPriorityNormal; + + if (osThreadNew(AhtDemoTask, NULL, &attr) == NULL) + { + printf("[EnvironmentDemo] Falied to create AhtDemoTask!\n"); + } +} + +APP_FEATURE_INIT(AhtDemo); \ No newline at end of file diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_04/enrionment_demo.c" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_04/enrionment_demo.c" new file mode 100644 index 0000000000000000000000000000000000000000..abfd0c322ff2ec0a6affbc63543fb017e1679c07 --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_04/enrionment_demo.c" @@ -0,0 +1,95 @@ +#include +#include +#include +#include + +#include "ohos_init.h" +#include "cmsis_os2.h" +#include "wifiiot_i2c.h" +#include "wifiiot_gpio.h" +#include "wifiiot_gpio_ex.h" +#include "wifiiot_adc.h" +#include "wifiiot_errno.h" + +#include "aht20.h" +#include "oled_ssd1306.h" + +#define GAS_SENSOR_CHAN_NAME WIFI_IOT_ADC_CHANNEL_5 +#define AHT20_BAUDRATE 400 * 1000 +#define AHT20_I2C_IDX WIFI_IOT_I2C_IDX_0 +#define ADC_RESOLUTION 2048 + +uint32_t retval = 0; +float humidity = 0.0f; //湿度 +float temperature = 0.0f; //温度 +unsigned short data = 0; +static char line[32] = {0}; + +//初始化函数 +void init(void) +{ + GpioInit(); + OledInit(); + OledFillScreen(0); + I2cInit(AHT20_I2C_IDX, AHT20_BAUDRATE); + + while (WIFI_IOT_SUCCESS != AHT20_Calibrate()) + { + printf("AHT20 sensor init failed!\r\n"); + usleep(1000); + } +} + +//综合业务函数 +static void EnvironmentTask(void *arg) +{ + (void)arg; + init(); + while (1){ + // 发送 触发测量 命令,开始测量 + if (AHT20_StartMeasure() != WIFI_IOT_SUCCESS){ + printf("trigger measure failed!\r\n"); + } + // 接收测量结果,拼接转换为标准值 + if (AHT20_GetMeasureResult(&temperature, &humidity) != WIFI_IOT_SUCCESS){ + printf("get humidity data failed!\r\n"); + } + //读取可燃气体值 + AdcRead(GAS_SENSOR_CHAN_NAME, &data, WIFI_IOT_ADC_EQU_MODEL_4, WIFI_IOT_ADC_CUR_BAIS_DEFAULT, 0); + //在(0,0)位置显示字符串"Sensor values:",字体为6x8点阵 + OledShowString(0, 0, "Sensor values:", 1); + + //组装显示温度的字符串 + snprintf(line, sizeof(line), "temp: %.2f", temperature); + OledShowString(0, 1, line, 1);//在(0,1)位置显示组装后的温度字符串 + + //组装显示湿度的字符串 + snprintf(line, sizeof(line), "humi: %.2f", humidity); + OledShowString(0, 2, line, 1);//在(0,2)位置显示组装后的湿度字符串 + + //组装显示气体的字符串 + snprintf(line, sizeof(line), "gas: %d", data); + OledShowString(0, 3, line, 1);//在(0,3)位置显示组装后的气体字符串 + sleep(1);//睡眠1秒 + } +} + +//创建新线程运行EnvironmentTask函数 +static void EnvironmentDemo(void) +{ + osThreadAttr_t attr; + attr.name = "EnvironmentTask"; + attr.attr_bits = 0U; + attr.cb_mem = NULL; + attr.cb_size = 0U; + attr.stack_mem = NULL; + attr.stack_size = 4096; + attr.priority = osPriorityNormal; + + if (osThreadNew(EnvironmentTask, NULL, &attr) == NULL) + { + printf("[EnvironmentDemo] Falied to create EnvironmentTask!\n"); + } +} + +APP_FEATURE_INIT(EnvironmentDemo);//注册为自启动运行 \ No newline at end of file diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_04/mq2_demo.c" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_04/mq2_demo.c" new file mode 100644 index 0000000000000000000000000000000000000000..8a5362ea45fd6b00deabfe54ef70d2e54ed47451 --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_04/mq2_demo.c" @@ -0,0 +1,50 @@ +#include +#include +#include +#include + +#include "ohos_init.h" +#include "cmsis_os2.h" +#include "wifiiot_gpio.h" +#include "wifiiot_gpio_ex.h" +#include "wifiiot_adc.h" +#include "wifiiot_errno.h" + +#define GAS_SENSOR_CHAN_NAME WIFI_IOT_ADC_CHANNEL_5 +// #define GAS_SENSOR_PIN_NAME WIFI_IOT_IO_NAME_GPIO_11 + +static void Mq2DemoTask(void *arg) +{ + (void)arg; + GpioInit(); + while (1) + { + + unsigned short data = 0; + + if (AdcRead(GAS_SENSOR_CHAN_NAME, &data, WIFI_IOT_ADC_EQU_MODEL_4, WIFI_IOT_ADC_CUR_BAIS_DEFAULT, 0) == WIFI_IOT_SUCCESS) + { + printf("gas:%d ppm\n", data); + } + + sleep(1); + } +} + +static void Mq2Demo(void) +{ + osThreadAttr_t attr; + attr.name = "Mq2DemoTask"; + attr.attr_bits = 0U; + attr.cb_mem = NULL; + attr.cb_size = 0U; + attr.stack_mem = NULL; + attr.stack_size = 4096; + attr.priority = osPriorityNormal; + if (osThreadNew(Mq2DemoTask, NULL, &attr) == NULL) + { + printf("[EnvironmentDemo] Falied to create EnvironmentTask!\n"); + } +} + +APP_FEATURE_INIT(Mq2Demo); \ No newline at end of file diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_04/oled_demo.c" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_04/oled_demo.c" new file mode 100644 index 0000000000000000000000000000000000000000..ad48d0cb845c96fdaf33b78632b01df2a8704b91 --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_04/oled_demo.c" @@ -0,0 +1,55 @@ +#include +#include +#include "ohos_init.h" +#include "cmsis_os2.h" +#include "wifiiot_gpio.h" +#include "wifiiot_gpio_ex.h" +#include "oled_ssd1306.h" + +//该函数对GPIO管脚及OLED进行初始化 +void init(void) +{ + GpioInit(); + OledInit(); +} + +//业务函数,完成数据在OLED上显示 +static void OledTask(void *arg) +{ + (void)arg; + init(); //初始化 + OledFillScreen(0x00);//清屏, + //在左上角位置显示字符串Hello, HarmonyOS + OledShowString(0, 0, "Hello, HarmonyOS", 1); + sleep(1);//等待1秒 + + //循环3次显示 + for (int i = 0; i < 3; i++) + { + OledFillScreen(0x00);//清屏 + for (int y = 0; y < 8; y++) + {//设置每行显示的字符串 + static const char text[] = "ABCDEFGHIJKLMNOP"; // QRSTUVWXYZ + OledShowString(0, y, text, 1);//在(0,y)位置显示字符串 + } + sleep(1);//等待1秒, + } +} + +//创建新线程运行OledTask函数 +static void OledDemo(void) +{ + osThreadAttr_t attr; + attr.name = "OledTask"; + attr.attr_bits = 0U; + attr.cb_mem = NULL; + attr.cb_size = 0U; + attr.stack_mem = NULL; + attr.stack_size = 4096; + attr.priority = osPriorityNormal; + if (osThreadNew(OledTask, NULL, &attr) == NULL) + { + printf("[OledDemo] Falied to create OledTask!\n"); + } +} +APP_FEATURE_INIT(OledDemo); diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_04/oled_fonts.h" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_04/oled_fonts.h" new file mode 100644 index 0000000000000000000000000000000000000000..f0b342bae64184e20eea8af14c4c357e6cb71445 --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_04/oled_fonts.h" @@ -0,0 +1,201 @@ +#ifndef OLOED_FONTS_H +#define OLOED_FONTS_H + +/*****************6*8的点阵********************/ +static unsigned char F6x8[][6] = +{ + { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // sp + { 0x00, 0x00, 0x00, 0x2f, 0x00, 0x00 }, // ! + { 0x00, 0x00, 0x07, 0x00, 0x07, 0x00 }, // " + { 0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14 }, // # + { 0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12 }, // $ + { 0x00, 0x62, 0x64, 0x08, 0x13, 0x23 }, // % + { 0x00, 0x36, 0x49, 0x55, 0x22, 0x50 }, // & + { 0x00, 0x00, 0x05, 0x03, 0x00, 0x00 }, // ' + { 0x00, 0x00, 0x1c, 0x22, 0x41, 0x00 }, // ( + { 0x00, 0x00, 0x41, 0x22, 0x1c, 0x00 }, // ) + { 0x00, 0x14, 0x08, 0x3E, 0x08, 0x14 }, // * + { 0x00, 0x08, 0x08, 0x3E, 0x08, 0x08 }, // + + { 0x00, 0x00, 0x00, 0xA0, 0x60, 0x00 }, // , + { 0x00, 0x08, 0x08, 0x08, 0x08, 0x08 }, // - + { 0x00, 0x00, 0x60, 0x60, 0x00, 0x00 }, // . + { 0x00, 0x20, 0x10, 0x08, 0x04, 0x02 }, // / + { 0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E }, // 0 + { 0x00, 0x00, 0x42, 0x7F, 0x40, 0x00 }, // 1 + { 0x00, 0x42, 0x61, 0x51, 0x49, 0x46 }, // 2 + { 0x00, 0x21, 0x41, 0x45, 0x4B, 0x31 }, // 3 + { 0x00, 0x18, 0x14, 0x12, 0x7F, 0x10 }, // 4 + { 0x00, 0x27, 0x45, 0x45, 0x45, 0x39 }, // 5 + { 0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30 }, // 6 + { 0x00, 0x01, 0x71, 0x09, 0x05, 0x03 }, // 7 + { 0x00, 0x36, 0x49, 0x49, 0x49, 0x36 }, // 8 + { 0x00, 0x06, 0x49, 0x49, 0x29, 0x1E }, // 9 + { 0x00, 0x00, 0x36, 0x36, 0x00, 0x00 }, // : + { 0x00, 0x00, 0x56, 0x36, 0x00, 0x00 }, // ; + { 0x00, 0x08, 0x14, 0x22, 0x41, 0x00 }, // < + { 0x00, 0x14, 0x14, 0x14, 0x14, 0x14 }, // = + { 0x00, 0x00, 0x41, 0x22, 0x14, 0x08 }, // > + { 0x00, 0x02, 0x01, 0x51, 0x09, 0x06 }, // ? + { 0x00, 0x32, 0x49, 0x59, 0x51, 0x3E }, // @ + { 0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C }, // A + { 0x00, 0x7F, 0x49, 0x49, 0x49, 0x36 }, // B + { 0x00, 0x3E, 0x41, 0x41, 0x41, 0x22 }, // C + { 0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C }, // D + { 0x00, 0x7F, 0x49, 0x49, 0x49, 0x41 }, // E + { 0x00, 0x7F, 0x09, 0x09, 0x09, 0x01 }, // F + { 0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A }, // G + { 0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F }, // H + { 0x00, 0x00, 0x41, 0x7F, 0x41, 0x00 }, // I + { 0x00, 0x20, 0x40, 0x41, 0x3F, 0x01 }, // J + { 0x00, 0x7F, 0x08, 0x14, 0x22, 0x41 }, // K + { 0x00, 0x7F, 0x40, 0x40, 0x40, 0x40 }, // L + { 0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F }, // M + { 0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F }, // N + { 0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E }, // O + { 0x00, 0x7F, 0x09, 0x09, 0x09, 0x06 }, // P + { 0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E }, // Q + { 0x00, 0x7F, 0x09, 0x19, 0x29, 0x46 }, // R + { 0x00, 0x46, 0x49, 0x49, 0x49, 0x31 }, // S + { 0x00, 0x01, 0x01, 0x7F, 0x01, 0x01 }, // T + { 0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F }, // U + { 0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F }, // V + { 0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F }, // W + { 0x00, 0x63, 0x14, 0x08, 0x14, 0x63 }, // X + { 0x00, 0x07, 0x08, 0x70, 0x08, 0x07 }, // Y + { 0x00, 0x61, 0x51, 0x49, 0x45, 0x43 }, // Z + { 0x00, 0x00, 0x7F, 0x41, 0x41, 0x00 }, // [ + { 0x00, 0x55, 0x2A, 0x55, 0x2A, 0x55 }, // 55 + { 0x00, 0x00, 0x41, 0x41, 0x7F, 0x00 }, // ] + { 0x00, 0x04, 0x02, 0x01, 0x02, 0x04 }, // ^ + { 0x00, 0x40, 0x40, 0x40, 0x40, 0x40 }, // _ + { 0x00, 0x00, 0x01, 0x02, 0x04, 0x00 }, // ' + { 0x00, 0x20, 0x54, 0x54, 0x54, 0x78 }, // a + { 0x00, 0x7F, 0x48, 0x44, 0x44, 0x38 }, // b + { 0x00, 0x38, 0x44, 0x44, 0x44, 0x20 }, // c + { 0x00, 0x38, 0x44, 0x44, 0x48, 0x7F }, // d + { 0x00, 0x38, 0x54, 0x54, 0x54, 0x18 }, // e + { 0x00, 0x08, 0x7E, 0x09, 0x01, 0x02 }, // f + { 0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C }, // g + { 0x00, 0x7F, 0x08, 0x04, 0x04, 0x78 }, // h + { 0x00, 0x00, 0x44, 0x7D, 0x40, 0x00 }, // i + { 0x00, 0x40, 0x80, 0x84, 0x7D, 0x00 }, // j + { 0x00, 0x7F, 0x10, 0x28, 0x44, 0x00 }, // k + { 0x00, 0x00, 0x41, 0x7F, 0x40, 0x00 }, // l + { 0x00, 0x7C, 0x04, 0x18, 0x04, 0x78 }, // m + { 0x00, 0x7C, 0x08, 0x04, 0x04, 0x78 }, // n + { 0x00, 0x38, 0x44, 0x44, 0x44, 0x38 }, // o + { 0x00, 0xFC, 0x24, 0x24, 0x24, 0x18 }, // p + { 0x00, 0x18, 0x24, 0x24, 0x18, 0xFC }, // q + { 0x00, 0x7C, 0x08, 0x04, 0x04, 0x08 }, // r + { 0x00, 0x48, 0x54, 0x54, 0x54, 0x20 }, // s + { 0x00, 0x04, 0x3F, 0x44, 0x40, 0x20 }, // t + { 0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C }, // u + { 0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C }, // v + { 0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C }, // w + { 0x00, 0x44, 0x28, 0x10, 0x28, 0x44 }, // x + { 0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C }, // y + { 0x00, 0x44, 0x64, 0x54, 0x4C, 0x44 }, // z + { 0x14, 0x14, 0x14, 0x14, 0x14, 0x14 }, // horiz lines +}; + +/****************************************8*16的点阵************************************/ +static const unsigned char F8X16[]= +{ + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 0 + 0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,//! 1 + 0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//" 2 + 0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,//# 3 + 0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,//$ 4 + 0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,//% 5 + 0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,//& 6 + 0x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' 7 + 0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,//( 8 + 0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,//) 9 + 0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,//* 10 + 0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,//+ 11 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,//, 12 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//- 13 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//. 14 + 0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,/// 15 + 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//0 16 + 0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//1 17 + 0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//2 18 + 0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//3 19 + 0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//4 20 + 0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//5 21 + 0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//6 22 + 0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//7 23 + 0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//8 24 + 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//9 25 + 0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//: 26 + 0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,//; 27 + 0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,//< 28 + 0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//= 29 + 0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,//> 30 + 0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//? 31 + 0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@ 32 + 0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A 33 + 0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B 34 + 0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C 35 + 0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D 36 + 0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E 37 + 0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F 38 + 0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G 39 + 0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H 40 + 0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I 41 + 0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J 42 + 0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K 43 + 0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L 44 + 0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M 45 + 0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N 46 + 0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O 47 + 0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P 48 + 0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q 49 + 0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R 50 + 0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S 51 + 0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T 52 + 0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U 53 + 0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V 54 + 0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W 55 + 0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X 56 + 0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y 57 + 0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z 58 + 0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[ 59 + 0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//\ 60 + 0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//] 61 + 0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 62 + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_ 63 + 0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 64 + 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a 65 + 0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b 66 + 0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c 67 + 0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d 68 + 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e 69 + 0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f 70 + 0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g 71 + 0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h 72 + 0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i 73 + 0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,//j 74 + 0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k 75 + 0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l 76 + 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m 77 + 0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n 78 + 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o 79 + 0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p 80 + 0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q 81 + 0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r 82 + 0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s 83 + 0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t 84 + 0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u 85 + 0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v 86 + 0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w 87 + 0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x 88 + 0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y 89 + 0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z 90 + 0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{ 91 + 0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//| 92 + 0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//} 93 + 0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94 +}; + +#endif \ No newline at end of file diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_04/oled_ssd1306.c" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_04/oled_ssd1306.c" new file mode 100644 index 0000000000000000000000000000000000000000..44da6749df28b499c170185e44d4fb9f428c1b46 --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_04/oled_ssd1306.c" @@ -0,0 +1,187 @@ +#include +#include +#include "oled_ssd1306.h" +#include "wifiiot_gpio.h" +#include "wifiiot_gpio_ex.h" +#include "wifiiot_i2c.h" +#include "wifiiot_errno.h" +#include "oled_fonts.h" + +#define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0]) + +#define OLED_I2C_IDX WIFI_IOT_I2C_IDX_0 + +#define OLED_WIDTH (128) +#define OLED_I2C_ADDR 0x78 // 默认地址为 0x78 +#define OLED_I2C_CMD 0x00 // 0000 0000 写命令 +#define OLED_I2C_DATA 0x40 // 0100 0000(0x40) 写数据 +#define OLED_I2C_BAUDRATE (400*1000) // 400k + +#define DELAY_100_MS (100*1000) + +// unsigned int I2cSetBaudrate(WifiIotI2cIdx id, unsigned int baudrate); + +static uint32_t I2cWiteByte(uint8_t regAddr, uint8_t byte) +{ + WifiIotI2cIdx id = OLED_I2C_IDX; + uint8_t buffer[] = {regAddr, byte}; + WifiIotI2cData i2cData = {0}; + + i2cData.sendBuf = buffer; + i2cData.sendLen = sizeof(buffer)/sizeof(buffer[0]); + + return I2cWrite(id, OLED_I2C_ADDR, &i2cData); +} + +/** + * @brief Write a command byte to OLED device. + * + * @param cmd the commnad byte to be writen. + * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link wifiiot_errno.h} otherwise. + */ +static uint32_t WriteCmd(uint8_t cmd) +{ + return I2cWiteByte(OLED_I2C_CMD, cmd); +} + +/** + * @brief Write a data byte to OLED device. + * + * @param cmd the data byte to be writen. + * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link wifiiot_errno.h} otherwise. + */ +static uint32_t WriteData(uint8_t data) +{ + return I2cWiteByte(OLED_I2C_DATA, data); +} + +/** + * @brief ssd1306 OLED Initialize. + */ +uint32_t OledInit(void) +{ + static const uint8_t initCmds[] = { + 0xAE, // --display off + 0x00, // ---set low column address + 0x10, // ---set high column address + 0x40, // --set start line address + 0xB0, // --set page address + 0x81, // contract control + 0xFF, // --128 + 0xA1, // set segment remap + 0xA6, // --normal / reverse + 0xA8, // --set multiplex ratio(1 to 64) + 0x3F, // --1/32 duty + 0xC8, // Com scan direction + 0xD3, // -set display offset + 0x00, // + 0xD5, // set osc division + 0x80, // + 0xD8, // set area color mode off + 0x05, // + 0xD9, // Set Pre-Charge Period + 0xF1, // + 0xDA, // set com pin configuartion + 0x12, // + 0xDB, // set Vcomh + 0x30, // + 0x8D, // set charge pump enable + 0x14, // + 0xAF, // --turn on oled panel + }; + + IoSetFunc(WIFI_IOT_IO_NAME_GPIO_13, WIFI_IOT_IO_FUNC_GPIO_13_I2C0_SDA); + IoSetFunc(WIFI_IOT_IO_NAME_GPIO_14, WIFI_IOT_IO_FUNC_GPIO_14_I2C0_SCL); + + I2cInit(WIFI_IOT_I2C_IDX_0, OLED_I2C_BAUDRATE); + // I2cSetBaudrate(WIFI_IOT_I2C_IDX_0, OLED_I2C_BAUDRATE); + + for (size_t i = 0; i < ARRAY_SIZE(initCmds); i++) { + uint32_t status = WriteCmd(initCmds[i]); + if (status != WIFI_IOT_SUCCESS) { + return status; + } + } + return WIFI_IOT_SUCCESS; +} + +void OledSetPosition(uint8_t x, uint8_t y) +{ + WriteCmd(0xb0 + y); + WriteCmd(((x & 0xf0) >> 4) | 0x10); + WriteCmd(x & 0x0f); +} + +/*全屏填充*/ +void OledFillScreen(uint8_t fillData) +{ + uint8_t m = 0; + uint8_t n = 0; + + for (m=0; m < 8; m++) { + WriteCmd(0xb0 + m); + WriteCmd(0x00); + WriteCmd(0x10); + + for (n=0; n < 128; n++) { + WriteData(fillData); + } + } +} + +/** + * @brief 8*16 typeface + * @param x: write positon start from x axis + * @param y: write positon start from y axis + * @param ch: write data + * @param font: selected font + */ +void OledShowChar(uint8_t x, uint8_t y, uint8_t ch, Font font) +{ + uint8_t c = 0; + uint8_t i = 0; + + c = ch - ' '; //得到偏移后的值 + if (x > OLED_WIDTH - 1) { + x = 0; + y = y + 2; + } + + if (font == FONT8x16) { + OledSetPosition(x, y); + for (i = 0; i < 8; i++){ + WriteData(F8X16[c*16 + i]); + } + + OledSetPosition(x, y+1); + for (i = 0; i < 8; i++) { + WriteData(F8X16[c*16 + i + 8]); + } + } else { + OledSetPosition(x, y); + for (i = 0; i < 6; i++) { + WriteData(F6x8[c][i]); + } + } +} + +void OledShowString(uint8_t x, uint8_t y, const char* str, Font font) +{ + uint8_t j = 0; + if (str == NULL) { + printf("param is NULL,Please check!!!\r\n"); + return; + } + + while (str[j]) { + OledShowChar(x, y, str[j], font); + x += 8; + if (x > 120) { + x = 0; + y += 2; + } + j++; + } +} diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_04/oled_ssd1306.h" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_04/oled_ssd1306.h" new file mode 100644 index 0000000000000000000000000000000000000000..4dfaaf430c9155a82be5dcdec998902ff99f59cd --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_04/oled_ssd1306.h" @@ -0,0 +1,32 @@ +#ifndef OLED_SSD1306_H +#define OLED_SSD1306_H + +#include + +/** + * @brief ssd1306 OLED Initialize. + */ +uint32_t OledInit(void); + +/** + * @brief Set cursor position + * + * @param x the horizontal posistion of cursor + * @param y the vertical position of cursor + * @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful; + * returns an error code defined in {@link wifiiot_errno.h} otherwise. + */ +void OledSetPosition(uint8_t x, uint8_t y); + +void OledFillScreen(uint8_t fillData); + +enum Font { + FONT6x8 = 1, + FONT8x16 +}; +typedef enum Font Font; + +void OledShowChar(uint8_t x, uint8_t y, uint8_t ch, Font font); +void OledShowString(uint8_t x, uint8_t y, const char* str, Font font); + +#endif // OLED_SSD1306_H diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_05/BUILD.gn" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_05/BUILD.gn" new file mode 100644 index 0000000000000000000000000000000000000000..874087706f48f16b1f8d4c49f6a933f953cab5ab --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_05/BUILD.gn" @@ -0,0 +1,13 @@ + +static_library("at_demo") { + sources = [ + "at_demo.c", + ] + + include_dirs = [ + "//utils/native/lite/include", + "//kernel/liteos_m/components/cmsis/2.0", + "//base/iot_hardware/interfaces/kits/wifiiot_lite", + "//vendor\hisi\hi3861\hi3861\components\at\src", + ] +} diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_05/at_demo.c" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_05/at_demo.c" new file mode 100644 index 0000000000000000000000000000000000000000..5f046f70f9b6db9e204a416c5edeb5a5bdb366c9 --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_05/at_demo.c" @@ -0,0 +1,23 @@ +#include +#include +#include "ohos_init.h" +#include "cmsis_os2.h" +#include +#include +hi_u32 at_exe_myproject_cmd(void) +{ + + hi_at_printf("at_exe_myproject_cmd\r\n"); + return HI_ERR_SUCCESS; +} + +const at_cmd_func g_at_test_func_tbl[] = { + {"+MYPROJECT", 10, HI_NULL, HI_NULL, HI_NULL, (at_call_back_func)at_exe_myproject_cmd}, +}; + +void AtExampleEntry(void) +{ + hi_at_register_cmd(g_at_test_func_tbl, sizeof(g_at_test_func_tbl) / sizeof(g_at_test_func_tbl[0])); +} + +SYS_RUN(AtExampleEntry); diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_06/BUILD.gn" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_06/BUILD.gn" new file mode 100644 index 0000000000000000000000000000000000000000..83dc0d8e207d64b70e63351ec7c4ae593eca44b4 --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_06/BUILD.gn" @@ -0,0 +1,18 @@ + +static_library("wifi_demo") { + sources = [ + # "wifi_sta_demo.c", + "wifi_ap_demo.c", + ] + + include_dirs = [ + "//utils/native/lite/include", + "//kernel/liteos_m/components/cmsis/2.0", + "//base/iot_hardware/interfaces/kits/wifiiot_lite", + "//foundation/communication/interfaces/kits/wifi_lite/wifiservice", + # "//third_party/lwip/src/include", + "//vendor/hisi/hi3861/hi3861/third_party/lwip_sack/include/", + ] +} + + diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_06/wifi_ap_demo.c" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_06/wifi_ap_demo.c" new file mode 100644 index 0000000000000000000000000000000000000000..2c516ebcb6b7cdeb0f38d18900a3eec38afd44fd --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_06/wifi_ap_demo.c" @@ -0,0 +1,86 @@ + +#include "hi_wifi_api.h" +#include "lwip/ip_addr.h" +#include "lwip/netifapi.h" +#include + +#include +#include "ohos_init.h" +#include "cmsis_os2.h" + +#define APP_INIT_VAP_NUM 2 +#define APP_INIT_USR_NUM 2 + +static struct netif *g_lwip_netif = NULL; + +static void WifiAPTask(void *arg) +{ + arg = arg; + int ret; + errno_t rc; + char ifname[WIFI_IFNAME_MAX_SIZE + 1] = {0}; //存放ssid + int len = sizeof(ifname); + hi_wifi_softap_config hapd_conf = {0}; + ip4_addr_t st_gw; + ip4_addr_t st_ipaddr; + ip4_addr_t st_netmask; + + rc = memcpy_s(hapd_conf.ssid, HI_WIFI_MAX_SSID_LEN + 1, "Hispark-WiFi-IoT", 16); + if (rc != EOK) + { + return; + } + + //gyz add 2020.11.04 设置密码 + rc = memcpy_s(hapd_conf.key, HI_WIFI_MAX_KEY_LEN + 1, "12345678", 8); + if (rc != EOK) + { + return; + } + // hapd_conf.authmode = HI_WIFI_SECURITY_OPEN; + //gyz add 2020.11.04 设置加密模式 + hapd_conf.authmode = HI_WIFI_SECURITY_WPA2PSK; // + hapd_conf.channel_num = 1; + + ret = hi_wifi_softap_start(&hapd_conf, ifname, &len); + if (ret != HISI_OK) + { + printf("hi_wifi_softap_start\n"); + return; + } + + /* acquire netif for IP operation */ + g_lwip_netif = netifapi_netif_find(ifname); + if (g_lwip_netif == NULL) + { + printf("%s: get netif failed\n", __FUNCTION__); + return; + } + + IP4_ADDR(&st_gw, 192, 168, 10, 1); /* input your IP for example: 192.168.1.1 */ + IP4_ADDR(&st_ipaddr, 192, 168, 10, 1); /* input your netmask for example: 192.168.1.1 */ + IP4_ADDR(&st_netmask, 255, 255, 255, 0); /* input your gateway for example: 255.255.255.0 */ + netifapi_netif_set_addr(g_lwip_netif, &st_ipaddr, &st_netmask, &st_gw); + + netifapi_dhcps_start(g_lwip_netif, 0, 0); +} + +static void WifiApDemo(void) +{ + osThreadAttr_t attr; + + attr.name = "WifiAPTask"; + attr.attr_bits = 0U; + attr.cb_mem = NULL; + attr.cb_size = 0U; + attr.stack_mem = NULL; + attr.stack_size = 10240; + attr.priority = osPriorityNormal; + + if (osThreadNew(WifiAPTask, NULL, &attr) == NULL) + { + printf("[WifiAPTask] Falied to create WifiConnectTask!\n"); + } +} + +APP_FEATURE_INIT(WifiApDemo); \ No newline at end of file diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_06/wifi_sta_demo.c" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_06/wifi_sta_demo.c" new file mode 100644 index 0000000000000000000000000000000000000000..c95185e2e773f66987010d2208740571a4e611ce --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_06/wifi_sta_demo.c" @@ -0,0 +1,73 @@ +#include +#include +#include + +#include "ohos_init.h" +#include "cmsis_os2.h" +#include "wifi_device.h" +#include "lwip/netifapi.h" +#include "lwip/api_shell.h" + + + +static void WifiStaTask(void *arg) +{ + (void)arg; + WifiErrorCode errCode; + WifiDeviceConfig apConfig = {}; + int netId = -1; + + // 设置将要连接的AP属性(wifi名称、密码、加密方式), + // strcpy(apConfig.ssid, "TP-LINK_A673");// + // strcpy(apConfig.preSharedKey, "aaaaaaaaaa");//密码 + strcpy(apConfig.ssid, "TP-LINK_D49F");// + strcpy(apConfig.preSharedKey, "wyz315ao");//密码 + apConfig.securityType = WIFI_SEC_TYPE_PSK;//加密类型 + + errCode = EnableWifi();//使能wifi功能 + errCode = AddDeviceConfig(&apConfig, &netId);//生成netId + printf("AddDeviceConfig: %d\r\n", errCode); + + errCode = ConnectTo(netId);//连接到与指定的网络ID匹配的热点 + printf("ConnectTo(%d): %d\r\n", netId, errCode); + usleep(3000 * 1000);//等待连接 + // 联网业务开始 + // 这里是网络业务代码... + struct netif *iface = netifapi_netif_find("wlan0"); + if (iface) + { + err_t ret = netifapi_dhcp_start(iface); + printf("netifapi_dhcp_start: %d\r\n", ret); + + usleep(2000 * 1000); + ; // wait DHCP server give me IP + ret = netifapi_netif_common(iface, dhcp_clients_info_show, NULL); + printf("netifapi_netif_common: %d\r\n", ret); + } + usleep(5000*1000); + // 联网业务结束 + Disconnect(); // disconnect with your AP + RemoveDevice(netId); // remove AP config + errCode = DisableWifi(); + printf("DisableWifi: %d\r\n", errCode); +} + +static void WifiStaDemo(void) +{ + osThreadAttr_t attr; + + attr.name = "WifiStaTask"; + attr.attr_bits = 0U; + attr.cb_mem = NULL; + attr.cb_size = 0U; + attr.stack_mem = NULL; + attr.stack_size = 10240; + attr.priority = osPriorityNormal; + + if (osThreadNew(WifiStaTask, NULL, &attr) == NULL) + { + printf("[WifiConnectDemo] Falied to create WifiConnectTask!\n"); + } +} + +APP_FEATURE_INIT(WifiStaDemo); diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/BUILD.gn" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/BUILD.gn" new file mode 100644 index 0000000000000000000000000000000000000000..6ecb18367e9f9f2a5a51ef2daefe59743b4ec90c --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/BUILD.gn" @@ -0,0 +1,18 @@ +static_library("net_demo") { + sources = [ + "demo.c", + "wifi_utils.c", + "tcp_server.c", + "tcp_client.c", + "udp_server.c", + "udp_client.c", + ] + + include_dirs = [ + "//utils/native/lite/include", + "//kernel/liteos_m/components/cmsis/2.0", + "//base/iot_hardware/interfaces/kits/wifiiot_lite", + "//vendor/hisi/hi3861/hi3861/third_party/lwip_sack/include", + "//foundation/communication/interfaces/kits/wifi_lite/wifiservice", + ] +} diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/demo.c" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/demo.c" new file mode 100644 index 0000000000000000000000000000000000000000..9e1e7b86ede9347d1b76ed95e5d203aa3e305dcc --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/demo.c" @@ -0,0 +1,52 @@ +#include +#include +#include + +#include "ohos_init.h" +#include "cmsis_os2.h" +#include "wifi_device.h" +#include "lwip/netifapi.h" +#include "lwip/api_shell.h" +#include "wifi_utils.h" +#include "tcp_server.h" +#include "tcp_client.h" +#include "udp_server.h" +#include "udp_client.h" + +static void NetWorkTask(void *arg){ + (void)arg; + connect_wifi(); + printf("begin demo\n"); + + // unsigned short port = 5001; + // tcp_server(port); + + // unsigned short port = 5002; + // conent_tcp_server("192.168.0.104",port); + + // unsigned short port = 5001; + // udp_server(port); + + unsigned short port = 5002; + send_to_udp_server("192.168.0.104", port); +} + +static void NetWorkDemo(void) +{ + osThreadAttr_t attr; + + attr.name = "NetWorkTask"; + attr.attr_bits = 0U; + attr.cb_mem = NULL; + attr.cb_size = 0U; + attr.stack_mem = NULL; + attr.stack_size = 10240; + attr.priority = osPriorityNormal; + + if (osThreadNew(NetWorkTask, NULL, &attr) == NULL) + { + printf("[NetWorkDemo] Falied to create WifiConnectTask!\n"); + } +} + +APP_FEATURE_INIT(NetWorkDemo); \ No newline at end of file diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/tcp_client.c" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/tcp_client.c" new file mode 100644 index 0000000000000000000000000000000000000000..d22fa265997a406d5fa1d3789965949a03f64793 --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/tcp_client.c" @@ -0,0 +1,53 @@ +#include +#include +#include "ohos_init.h" +#include "cmsis_os2.h" +#include "hi_wifi_api.h" +#include "lwip/ip_addr.h" +#include "lwip/netifapi.h" +#include "lwip/sockets.h" + +static char request[] = "Hello"; +static char response[128] = ""; + +void conent_tcp_server(const char* host, unsigned short port) +{ + ssize_t retval = 0; + int sockfd = socket(AF_INET, SOCK_STREAM, 0); // TCP socket + + struct sockaddr_in serverAddr = {0}; + serverAddr.sin_family = AF_INET; // AF_INET表示IPv4协议 + serverAddr.sin_port = htons(port); // 端口号,从主机字节序转为网络字节序 + if (inet_pton(AF_INET, host, &serverAddr.sin_addr) <= 0) { // 将主机IP地址从“点分十进制”字符串 转化为 标准格式(32位整数) + printf("inet_pton failed!\r\n"); + goto do_cleanup; + } + + // 尝试和目标主机建立连接,连接成功会返回0 ,失败返回 -1 + if (connect(sockfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0) { + printf("connect failed!\r\n"); + goto do_cleanup; + } + printf("connect to server %s success!\r\n", host); + + // 建立连接成功之后,这个TCP socket描述符 —— sockfd 就具有了 “连接状态”,发送、接收 对端都是 connect 参数指定的目标主机和端口 + retval = send(sockfd, request, sizeof(request), 0); + if (retval < 0) { + printf("send request failed!\r\n"); + goto do_cleanup; + } + printf("send request{%s} %ld to server done!\r\n", request, retval); + + retval = recv(sockfd, &response, sizeof(response), 0); + if (retval <= 0) { + printf("send response from server failed or done, %ld!\r\n", retval); + goto do_cleanup; + } + response[retval] = '\0'; + printf("recv response{%s} %ld from server done!\r\n", response, retval); + +do_cleanup: + printf("do_cleanup...\r\n"); + lwip_close(sockfd); +} + diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/tcp_client.h" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/tcp_client.h" new file mode 100644 index 0000000000000000000000000000000000000000..d23f223b4fddcdc98f7f11643b770230c8000d4b --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/tcp_client.h" @@ -0,0 +1,4 @@ +#ifndef TCP_CLIENT +#define TCP_CLIENT +void conent_tcp_server(const char* host, unsigned short port); +#endif \ No newline at end of file diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/tcp_server.c" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/tcp_server.c" new file mode 100644 index 0000000000000000000000000000000000000000..bc9f22c9950476d0b08df25826e69bc48cc3bf3e --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/tcp_server.c" @@ -0,0 +1,80 @@ +#include +#include +#include "ohos_init.h" +#include "cmsis_os2.h" +#include "hi_wifi_api.h" +#include "lwip/ip_addr.h" +#include "lwip/netifapi.h" +#include "lwip/sockets.h" + + +static char request[128] = ""; +void tcp_server(unsigned short port) +{ + ssize_t retval = 0; + int backlog = 1; + int sockfd = socket(AF_INET, SOCK_STREAM, 0); // TCP socket + int connfd = -1; + + struct sockaddr_in clientAddr = {0}; + socklen_t clientAddrLen = sizeof(clientAddr); + struct sockaddr_in serverAddr = {0}; + serverAddr.sin_family = AF_INET; + serverAddr.sin_port = htons(port); // 端口号,从主机字节序转为网络字节序 + serverAddr.sin_addr.s_addr = htonl(INADDR_ANY); // 允许任意主机接入, 0.0.0.0 + + retval = bind(sockfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)); // 绑定端口 + if (retval < 0) + { + printf("bind failed, %ld!\r\n", retval); + goto do_cleanup; + } + printf("bind to port %d success!\r\n", port); + + retval = listen(sockfd, backlog); // 开始监听 + if (retval < 0) + { + printf("listen failed!\r\n"); + goto do_cleanup; + } + printf("listen with %d backlog success!\r\n", backlog); + + // 接受客户端连接,成功会返回一个表示连接的 socket , clientAddr 参数将会携带客户端主机和端口信息 ;失败返回 -1 + // 此后的 收、发 都在 表示连接的 socket 上进行;之后 sockfd 依然可以继续接受其他客户端的连接, + // UNIX系统上经典的并发模型是“每个连接一个进程”——创建子进程处理连接,父进程继续接受其他客户端的连接 + // 鸿蒙liteos-a内核之上,可以使用UNIX的“每个连接一个进程”的并发模型 + // liteos-m内核之上,可以使用“每个连接一个线程”的并发模型 + connfd = accept(sockfd, (struct sockaddr *)&clientAddr, &clientAddrLen); + if (connfd < 0) + { + printf("accept failed, %d, %d\r\n", connfd, errno); + goto do_cleanup; + } + printf("accept success, connfd = %d!\r\n", connfd); + printf("client addr info: host = %s, port = %d\r\n", inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port)); + + // 后续 收、发 都在 表示连接的 socket 上进行; + retval = recv(connfd, request, sizeof(request), 0); + if (retval < 0) + { + printf("recv request failed, %ld!\r\n", retval); + goto do_disconnect; + } + printf("recv request{%s} from client done!\r\n", request); + + retval = send(connfd, request, strlen(request), 0); + if (retval <= 0) + { + printf("send response failed, %ld!\r\n", retval); + goto do_disconnect; + } + printf("send response{%s} to client done!\r\n", request); + +do_disconnect: + lwip_close(connfd); + +do_cleanup: + printf("do_cleanup...\r\n"); + + lwip_close(sockfd); +} diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/tcp_server.h" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/tcp_server.h" new file mode 100644 index 0000000000000000000000000000000000000000..70df2760fc0676923935dcd3f81f4991fd362167 --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/tcp_server.h" @@ -0,0 +1,4 @@ +#ifndef TCP_SERVER +#define TCP_SERVER +void tcp_server(unsigned short port); +#endif \ No newline at end of file diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/udp_client.c" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/udp_client.c" new file mode 100644 index 0000000000000000000000000000000000000000..4bddae4a32d1ad90a18833fdc57d7f07e1ea7f12 --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/udp_client.c" @@ -0,0 +1,51 @@ +#include +#include +#include "ohos_init.h" +#include "cmsis_os2.h" +#include "hi_wifi_api.h" +#include "lwip/ip_addr.h" +#include "lwip/netifapi.h" +#include "lwip/sockets.h" + +static char request[] = "Hello. I am from chinasoft"; +static char response[128] = ""; + +void send_to_udp_server(const char* host, unsigned short port) +{ + ssize_t retval = 0; + int sockfd = socket(AF_INET, SOCK_DGRAM, 0); // UDP socket + + struct sockaddr_in toAddr = {0}; + toAddr.sin_family = AF_INET; + toAddr.sin_port = htons(port); // 端口号,从主机字节序转为网络字节序 + if (inet_pton(AF_INET, host, &toAddr.sin_addr) <= 0) { // 将主机IP地址从“点分十进制”字符串 转化为 标准格式(32位整数) + printf("inet_pton failed!\r\n"); + goto do_cleanup; + } + + // UDP socket 是 “无连接的” ,因此每次发送都必须先指定目标主机和端口,主机可以是多播地址 + retval = sendto(sockfd, request, sizeof(request), 0, (struct sockaddr *)&toAddr, sizeof(toAddr)); + if (retval < 0) { + printf("sendto failed!\r\n"); + goto do_cleanup; + } + printf("send UDP message {%s} %ld done!\r\n", request, retval); + + struct sockaddr_in fromAddr = {0}; + socklen_t fromLen = sizeof(fromAddr); + + // UDP socket 是 “无连接的” ,因此每次接收时前并不知道消息来自何处,通过 fromAddr 参数可以得到发送方的信息(主机、端口号) + retval = recvfrom(sockfd, &response, sizeof(response), 0, (struct sockaddr *)&fromAddr, &fromLen); + if (retval <= 0) { + printf("recvfrom failed or abort, %ld, %d!\r\n", retval, errno); + goto do_cleanup; + } + response[retval] = '\0'; + printf("recv UDP message {%s} %ld done!\r\n", response, retval); + printf("peer info: ipaddr = %s, port = %d\r\n", inet_ntoa(fromAddr.sin_addr), ntohs(fromAddr.sin_port)); + +do_cleanup: + printf("do_cleanup...\r\n"); + lwip_close(sockfd); +} + diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/udp_client.h" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/udp_client.h" new file mode 100644 index 0000000000000000000000000000000000000000..39f07312845a4e2845c1216a8c56ac8139699582 --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/udp_client.h" @@ -0,0 +1,4 @@ +#ifndef UDP_CLIENT +#define UDP_CLIENT +void send_to_udp_server(const char* host, unsigned short port); +#endif \ No newline at end of file diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/udp_server.c" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/udp_server.c" new file mode 100644 index 0000000000000000000000000000000000000000..cfa7441b4437a3504366e63f9d17cfee3c2dc261 --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/udp_server.c" @@ -0,0 +1,51 @@ +#include +#include +#include "ohos_init.h" +#include "cmsis_os2.h" +#include "hi_wifi_api.h" +#include "lwip/ip_addr.h" +#include "lwip/netifapi.h" +#include "lwip/sockets.h" + +static char message[128] = ""; +void udp_server(unsigned short port) +{ + ssize_t retval = 0; + int sockfd = socket(AF_INET, SOCK_DGRAM, 0); // UDP socket + + struct sockaddr_in clientAddr = {0}; + socklen_t clientAddrLen = sizeof(clientAddr); + struct sockaddr_in serverAddr = {0}; + serverAddr.sin_family = AF_INET; + serverAddr.sin_port = htons(port); + serverAddr.sin_addr.s_addr = htonl(INADDR_ANY); + + retval = bind(sockfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)); + if (retval < 0) { + printf("bind failed, %ld!\r\n", retval); + goto do_cleanup; + } + printf("bind to port %d success!\r\n", port); + + retval = recvfrom(sockfd, message, sizeof(message), 0, (struct sockaddr *)&clientAddr, &clientAddrLen); + if (retval < 0) { + printf("recvfrom failed, %ld!\r\n", retval); + goto do_cleanup; + } + printf("recv message {%s} %ld done!\r\n", message, retval); + printf("peer info: ipaddr = %s, port = %d\r\n", inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port)); + + retval = sendto(sockfd, message, strlen(message), 0, (struct sockaddr *)&clientAddr, sizeof(clientAddr)); + if (retval <= 0) { + printf("send failed, %ld!\r\n", retval); + goto do_cleanup; + } + printf("send message {%s} %ld done!\r\n", message, retval); + +do_cleanup: + printf("do_cleanup...\r\n"); + + lwip_close(sockfd); +} + + diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/udp_server.h" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/udp_server.h" new file mode 100644 index 0000000000000000000000000000000000000000..3bc50c6b9748b9d45650cfe0734e4e966bd197aa --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/udp_server.h" @@ -0,0 +1,4 @@ +#ifndef UDP_SERVER +#define UDP_SERVER +void udp_server(unsigned short port); +#endif \ No newline at end of file diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/wifi_utils.c" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/wifi_utils.c" new file mode 100644 index 0000000000000000000000000000000000000000..b0920a4e915916fbf6ede46dde141dd279875685 --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/wifi_utils.c" @@ -0,0 +1,46 @@ +#include +#include +#include + +#include "ohos_init.h" +#include "cmsis_os2.h" +#include "wifi_device.h" +#include "lwip/netifapi.h" +#include "lwip/api_shell.h" + + + + void connect_wifi(void) +{ + + WifiErrorCode errCode; + WifiDeviceConfig apConfig = {}; + int netId = -1; + + // setup your AP params + strcpy(apConfig.ssid, "TP-LINK_D49F");// + strcpy(apConfig.preSharedKey, "wyz315ao");//密码 + apConfig.securityType = WIFI_SEC_TYPE_PSK; + + errCode = EnableWifi(); + errCode = AddDeviceConfig(&apConfig, &netId); + + errCode = ConnectTo(netId); + printf("ConnectTo(%d): %d\r\n", netId, errCode); + usleep(1000 * 1000); + // 联网业务开始 + // 这里是网络业务代码... + struct netif *iface = netifapi_netif_find("wlan0"); + if (iface) + { + err_t ret = netifapi_dhcp_start(iface); + printf("netifapi_dhcp_start: %d\r\n", ret); + + usleep(2000 * 1000); + ; // wait DHCP server give me IP + ret = netifapi_netif_common(iface, dhcp_clients_info_show, NULL); + printf("netifapi_netif_common: %d\r\n", ret); + } + +} + diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/wifi_utils.h" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/wifi_utils.h" new file mode 100644 index 0000000000000000000000000000000000000000..dc016576a777adbb009a7f834dae56ecda7ff2e8 --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_07/wifi_utils.h" @@ -0,0 +1,5 @@ + +#ifndef WIFI_UTILS +#define WIFI_UTILS +void connect_wifi(void); +#endif \ No newline at end of file diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_08/BUILD.gn" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_08/BUILD.gn" new file mode 100644 index 0000000000000000000000000000000000000000..73534d299ec976c9f0ba842e8d30b9d5a78ed780 --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_08/BUILD.gn" @@ -0,0 +1,18 @@ +static_library("mqtt_demo") { + sources = [ + "wifi_utils.c","demo.c","mqtt_test.c", + + ] + + include_dirs = [ + + "//utils/native/lite/include", + "//kernel/liteos_m/components/cmsis/2.0", + "//base/iot_hardware/interfaces/kits/wifiiot_lite", + "//vendor/hisi/hi3861/hi3861/third_party/lwip_sack/include", + "//foundation/communication/interfaces/kits/wifi_lite/wifiservice", + "//third_party/pahomqtt/MQTTPacket/src", + "//third_party/pahomqtt/MQTTPacket/samples", + "//vendor\hisi\hi3861\hi3861\components\at\src" + ] +} diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_08/demo.c" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_08/demo.c" new file mode 100644 index 0000000000000000000000000000000000000000..c6dbe875d3dd7f5026185eeb25231a2313ddcc29 --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_08/demo.c" @@ -0,0 +1,55 @@ +#include +#include +#include + +#include "ohos_init.h" +#include "cmsis_os2.h" +#include "wifi_device.h" +#include "lwip/netifapi.h" +#include "lwip/api_shell.h" +#include "wifi_utils.h" +#include "mqtt_test.h" +#include +#include + +static void mqtt_test_thread(void *arg) +{ + (void)arg; + connect_wifi();//连接AP, + printf("begin mqtt demo\n"); + + mqtt_test();//运行mqtt测试程序 +} + +//AT指令回调函数,在函数中创建新线程 +hi_u32 at_exe_mqtt_test_cmd(void) +{ + osThreadAttr_t attr; + + attr.name = "wifi_config_thread"; + attr.attr_bits = 0U; + attr.cb_mem = NULL; + attr.cb_size = 0U; + attr.stack_mem = NULL; + attr.stack_size = 4096; + attr.priority = 36; + //在新线程中执行函数mqtt_test_thread + if (osThreadNew((osThreadFunc_t)mqtt_test_thread, NULL, &attr) == NULL) + { + printf("[LedExample] Falied to create LedTask!\n"); + } + + AT_RESPONSE_OK; + return HI_ERR_SUCCESS; +} + +const at_cmd_func g_at_mqtt_func_tbl[] = { + {"+MQTTTEST", 9, HI_NULL, HI_NULL, HI_NULL, (at_call_back_func)at_exe_mqtt_test_cmd}, +}; + +//注册AT指令 +void AtMqttTestEntry(void) +{ + hi_at_register_cmd(g_at_mqtt_func_tbl, sizeof(g_at_mqtt_func_tbl) / sizeof(g_at_mqtt_func_tbl[0])); +} +SYS_RUN(AtMqttTestEntry); \ No newline at end of file diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_08/mqtt_test.c" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_08/mqtt_test.c" new file mode 100644 index 0000000000000000000000000000000000000000..9c52bcbc5395f7b522e6c71d9b3aaf23d2a8603e --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_08/mqtt_test.c" @@ -0,0 +1,134 @@ +#include + +#include + +#include "ohos_init.h" +#include "cmsis_os2.h" + +#include +#include "hi_wifi_api.h" +//#include "wifi_sta.h" +#include "lwip/ip_addr.h" +#include "lwip/netifapi.h" + +#include "lwip/sockets.h" + +#include "MQTTPacket.h" +#include "transport.h" + +int toStop = 0; + +int mqtt_connect(void) +{ + MQTTPacket_connectData data = MQTTPacket_connectData_initializer; + int rc = 0; + int mysock = 0; + unsigned char buf[200]; + int buflen = sizeof(buf); + int msgid = 1; + MQTTString topicString = MQTTString_initializer; + int req_qos = 0; + char* payload = "hello HarmonyOS"; + int payloadlen = strlen(payload); + int len = 0; + + char *host = "121.36.35.193"; //mqtt服务器ip地址 + int port = 1883;////mqtt服务器端口 + + + mysock = transport_open(host, port); + if(mysock < 0) + return mysock; + + printf("Sending to hostname %s port %d\n", host, port); + + data.clientID.cstring = "hi3861"; //设置客户端ID + data.keepAliveInterval = 20; //设置心跳间隔 + data.cleansession = 1; + data.username.cstring = "hi3861"; //用户名 + data.password.cstring = "testpassword"; //密码 + + len = MQTTSerialize_connect(buf, buflen, &data);//开始连接 + rc = transport_sendPacketBuffer(mysock, buf, len);//发送缓冲区 + + /* wait for connack */ + if (MQTTPacket_read(buf, buflen, transport_getdata) == CONNACK)//等待连接返回 + { + unsigned char sessionPresent, connack_rc; + + if (MQTTDeserialize_connack(&sessionPresent, &connack_rc, buf, buflen) != 1 || connack_rc != 0) + { + printf("Unable to connect, return code %d\n", connack_rc); + goto exit; + } + } + else + goto exit; + + /* subscribe */ + topicString.cstring = "subtopic"; //设置订阅主题 + // topicString.cstring = "substopic"; + len = MQTTSerialize_subscribe(buf, buflen, 0, msgid, 1, &topicString, &req_qos); + rc = transport_sendPacketBuffer(mysock, buf, len); + if (MQTTPacket_read(buf, buflen, transport_getdata) == SUBACK) /* wait for suback */ + { + unsigned short submsgid; + int subcount; + int granted_qos; + + rc = MQTTDeserialize_suback(&submsgid, 1, &subcount, &granted_qos, buf, buflen); + if (granted_qos != 0) + { + printf("granted qos != 0, %d\n", granted_qos); + goto exit; + } + } + else + goto exit; + + /* loop getting msgs on subscribed topic */ + + while (!toStop) + { + /* transport_getdata() has a built-in 1 second timeout, + your mileage will vary */ + if (MQTTPacket_read(buf, buflen, transport_getdata) == PUBLISH) + { + unsigned char dup; + int qos; + unsigned char retained; + unsigned short msgid; + int payloadlen_in; + unsigned char* payload_in; + int rc; + MQTTString receivedTopic; + rc = MQTTDeserialize_publish(&dup, &qos, &retained, &msgid, &receivedTopic, + &payload_in, &payloadlen_in, buf, buflen); + printf("message arrived %.*s\n", payloadlen_in, payload_in); + + rc = rc; + } + + topicString.cstring = "pubtopic"; //发布主题 + // printf("publishing reading\n"); + //发布接收到的消息 + len = MQTTSerialize_publish(buf, buflen, 0, 0, 0, 0, topicString, (unsigned char*)payload, payloadlen); + rc = transport_sendPacketBuffer(mysock, buf, len); + } + + printf("disconnecting\n"); + len = MQTTSerialize_disconnect(buf, buflen); + rc = transport_sendPacketBuffer(mysock, buf, len); +exit: + transport_close(mysock); + + rc = rc; + + return 0; +} + + +void mqtt_test(void) +{ + mqtt_connect(); +} \ No newline at end of file diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_08/mqtt_test.h" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_08/mqtt_test.h" new file mode 100644 index 0000000000000000000000000000000000000000..57f01fe6faa70e11bed46b07b378e4c10e51e441 --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_08/mqtt_test.h" @@ -0,0 +1,6 @@ +#ifndef __MQTT_TEST_H__ +#define __MQTT_TEST_H__ + +void mqtt_test(void);//mqtt测试函数 + +#endif /* __MQTT_TEST_H__ */ \ No newline at end of file diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_08/wifi_utils.c" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_08/wifi_utils.c" new file mode 100644 index 0000000000000000000000000000000000000000..b0920a4e915916fbf6ede46dde141dd279875685 --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_08/wifi_utils.c" @@ -0,0 +1,46 @@ +#include +#include +#include + +#include "ohos_init.h" +#include "cmsis_os2.h" +#include "wifi_device.h" +#include "lwip/netifapi.h" +#include "lwip/api_shell.h" + + + + void connect_wifi(void) +{ + + WifiErrorCode errCode; + WifiDeviceConfig apConfig = {}; + int netId = -1; + + // setup your AP params + strcpy(apConfig.ssid, "TP-LINK_D49F");// + strcpy(apConfig.preSharedKey, "wyz315ao");//密码 + apConfig.securityType = WIFI_SEC_TYPE_PSK; + + errCode = EnableWifi(); + errCode = AddDeviceConfig(&apConfig, &netId); + + errCode = ConnectTo(netId); + printf("ConnectTo(%d): %d\r\n", netId, errCode); + usleep(1000 * 1000); + // 联网业务开始 + // 这里是网络业务代码... + struct netif *iface = netifapi_netif_find("wlan0"); + if (iface) + { + err_t ret = netifapi_dhcp_start(iface); + printf("netifapi_dhcp_start: %d\r\n", ret); + + usleep(2000 * 1000); + ; // wait DHCP server give me IP + ret = netifapi_netif_common(iface, dhcp_clients_info_show, NULL); + printf("netifapi_netif_common: %d\r\n", ret); + } + +} + diff --git "a/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_08/wifi_utils.h" "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_08/wifi_utils.h" new file mode 100644 index 0000000000000000000000000000000000000000..7b6e5c298083b84ea1b5cd3b8037a1db56716c98 --- /dev/null +++ "b/OpenHarmony\347\241\254\344\273\266\345\274\200\345\217\221\345\237\272\347\241\200/02.\346\225\231\345\255\246\346\241\210\344\276\213\346\272\220\344\273\243\347\240\201/chapter_08/wifi_utils.h" @@ -0,0 +1,5 @@ + +#ifndef WIFI_UTILS +#define WIFI_UTILS +void connect_wifi(void);//连接wifi +#endif \ No newline at end of file