一、前言
像我们学习一门编语言那样,从码一个hello world应用程序开始我们的Zeta Linux之旅!嵌入式产品常伴有带显示与不带显示之分,即使同一类产品,也会存在是否有显示的形态差异。比如行车记录仪,有带显示屏的小机及后视镜,也有不带显示屏的口红机及隐藏式记录仪。下文将展示如何编写一个不含GUI的在终端窗口打印hello world的程序,以及一个包含嵌入式GUI的在显示屏上通过GUI控制显示hello world的程序。
二、终端程序
一个Zeta Linux程序/模块至少包含两部分,Makefile文件zeta.in以及源码文件。本例中,应用程序名字为demo_terminal_helloworld,创建相应的文件:
cd ZetaLinux/appmkdir demo_terminal_helloworld/touch zeta.in main.c
编写Makefile文件:
TARGET_PATH:= $(call my-dir)include $(CLEAR_VARS)TARGET_INC:=TARGET_SRC:= main.cTARGET_CPPFLAGS += -fPIC -WallTARGET_CFLAGS += -fPIC -WallTARGET_MODULE := demo_terminal_helloworldinclude $(BUILD_BIN)
编写源码文件:
#include <stdio.h>#include<stdlib.h>int main(){printf("hello world.\n");}
编译应用程序:
cd ZetaLinux/app/demo_terminal_helloworldzmake zeta.in
将编译生成的可执行文件通过adb推送到设备里面运行,可在终端输出”hello world.”。
三、GUI程序
Zeta Linux支持多种GUI引擎,目前在Zeta上顺利运行使用的GUI有MiniGUI、DireceFB、QT、LittlevGL。本例中使用MiniGUI。使用MiniGUI需要在zeta.in中显式指定程序所用的MiniGUI动态库,zeta.in如下:
TARGET_PATH :=$(call my-dir)include $(ENV_CLEAR)TARGET_SRC := ./main.cTARGET_SHARED_LIB += libminigui_thsTARGET_MODULE := demo_gui_helloworldinclude $(BUILD_BIN)
编写源码文件,在窗口中输出Hello world!
#include <minigui/common.h>#include <minigui/minigui.h>#include <minigui/gdi.h>#include <minigui/window.h>#include <sys/time.h>static BITMAP bmp_bkgnd;static int HelloWinProc (HWND hWnd, int message, WPARAM wParam, LPARAM lParam){HDC hdc;static HWND hwnd;static int i = 0;switch (message){case MSG_PAINT:{hdc = BeginPaint (hWnd);TextOut (hdc, 320 / 2 - 50, 240 / 2 - 10, "Hello world!");EndPaint (hWnd, hdc);return 0;}case MSG_CREATE:{hwnd = CreateWindowEx("static", "", WS_CHILD | WS_VISIBLE,WS_EX_NONE, 123, 0, 0, 50, 50, hWnd, NULL);SetWindowBkColor(hwnd, RGBA2Pixel(HDC_SCREEN, 0x00, 0x00, 0xff, 0x10));break;}case MSG_CLOSE:{DestroyMainWindow (hWnd);PostQuitMessage (hWnd);return 0;}}return DefaultMainWinProc (hWnd, message, wParam, lParam);}int MiniGUIMain(int argc, const char* argv[]){MSG Msg;HWND hMainWnd;MAINWINCREATE CreateInfo;#ifdef _MGRM_PROCESSESJoinLayer (NAME_DEF_LAYER , "helloworld" , 0 , 0);#endifCreateInfo.dwStyle = WS_VISIBLE | WS_BORDER | WS_CAPTION;CreateInfo.dwExStyle = WS_EX_NONE;CreateInfo.spCaption = "HelloWorld";CreateInfo.hMenu = 0;CreateInfo.hCursor = GetSystemCursor (0);CreateInfo.hIcon = 0;CreateInfo.MainWindowProc = HelloWinProc;CreateInfo.lx = 0;CreateInfo.ty = 0;CreateInfo.rx = 320;CreateInfo.by = 240;CreateInfo.iBkColor = RGBA2Pixel(HDC_SCREEN, 0xff, 0xff, 0xff, 0x00);CreateInfo.dwAddData = 0;CreateInfo.hHosting = HWND_DESKTOP;hMainWnd = CreateMainWindow (&CreateInfo);if(hMainWnd == HWND_INVALID)return -1;ShowWindow (hMainWnd, SW_SHOWNORMAL);while (GetMessage (&Msg, hMainWnd)){fprintf(stderr, "msg\n");TranslateMessage (&Msg);DispatchMessage (&Msg);}UnloadBitmap(&bmp_bkgnd);MainWindowThreadCleanup (hMainWnd);return 0;}#ifndef _MGRM_PROCESSES#include <minigui/dti.c>#endif
将编译生成的可执行文件通过adb推送到设备里面运行,可在显示屏上显示”hello world.”。
