• 嵌入式Linux Zgt300 API动态库的开发与调用


    嵌入式Linux Zgt300 API动态库的开发与调用

    BY: zsz @ 2012-6-19

    1、 库文件头文件

    zgt300API.h:

    /*

    ZGT300 user APIs

    auth:         zsz

    begin:       2012.06.19

    */

    #ifndef _ZGT300API_H

    #define _ZGT300API_H

    /*

    导通/关断热敏打印机电源

    */

    void PrinterPowerSwitch(int isPowerOn);

    /*

    导通/关断GSM模块电源

    */

    void GSMPowerSwitch(int isPowerOn);

    /*

    查询GSM模块是在运行状态还是关机状态

    */

    int IsGSMOn(void);

    /*

    控制GSM模块开机或关机

    */

    int GSMTurnOnOff(int isTurnOnGSM);

    /*

    打开/关闭WIFI

    */

    void WIFITurnOnOff(int isTurnOn);

    /*

    导通/断开3G模块电源

    */

    void CDMA3GPowerSwitch(int isPowerOn);

    /*

    查询锂电池是否在充电

    */

    int IsBatteryCharging(void);

    /*

    打开蜂鸣器,可设定打开时间长度(毫秒)

    */

    void BuzBeep(int keepMilliseconds);

    /*

    打开钱箱:打开钱箱的脉冲宽度(延时时间,单位毫秒),根据具体钱箱设定

    */

    void OpenCashbox(int keepMilliseconds);

    #endif 

    2、 库文件源代码

    Zgt300API.c

    /*

    ZGT300 user APIs

    auth:         zsz

    begin:       2012.06.19

    */

    #include <stdio.h>

    #include <stdlib.h>

    #include <sys/types.h>

    #include <sys/stat.h>

    #include <fcntl.h>

    #include <unistd.h>

    #include "zgt300API.h"

    #define msleep(x) usleep(x*1000)

    /*

    set GPIO output high or low

    */

    void setGpio(char portName,char portIndex,int isHigh)

    {

             int fd;

             char buf[3];

             buf[0] = portName;

             buf[1] = portIndex;

             buf[2] = isHigh;

             fd = open("/dev/zgt300_gpio",O_RDWR);

             if(fd==-1)

             {

                       printf("can not open zgt300_gpio device\n");

    //               return -1;

             } 

             else

             {

                       write(fd,buf,3);

             }

             close(fd);

    }

    /*

    get GPIO state is high(1) or low(0)

    */

    int getGpio(char portName,char portIndex)

    {

             int fd;

             char buf[3];

             buf[0] = portName;

             buf[1] = portIndex;

             buf[2] = 0;//don't care

             fd = open("/dev/zgt300_gpio",O_RDWR);

             if(fd==-1)

             {

                       printf("can not open zgt300_gpio device\n");

                       return -1;

             } 

             else

             {

                       read(fd,buf,3);

             }

             close(fd);

             return buf[2];

    }

    /*

    导通/关断热敏打印机电源

    */

    void PrinterPowerSwitch(int isPowerOn)

    {

             setGpio('A',6,isPowerOn);

    }

    /*

    导通/关断GSM模块电源

    */

    void GSMPowerSwitch(int isPowerOn)

    {

             setGpio('N',9,(isPowerOn == 1) ? 0 : 1);

    }

    /*

    查询GSM模块是在运行状态还是关机状态

    */

    int IsGSMOn(void)

    {

             return getGpio('L',8);

    }

    /*

    控制GSM模块开机或关机,按照特定GSM模块设定时间(毫秒:keepTimeInMS)。

    执行此操作将使GSM模块的开关机状态倒转,即在关机状态下会使模块开机,在开机

    状态下使模块关机

    */

    void GSMControl(int keepTimeInMS)

    {

             //GPN10 output high

             setGpio('N',10,1);

             msleep(keepTimeInMS);

             //GPN10 output low

             setGpio('N',10,0);

    }

    /*

    控制GSM模块开机或关机

    return the GSM status:

    1 - ON

    0 - OFF

    */

    int GSMTurnOnOff(int isTurnOnGSM)

    {

             if (isTurnOnGSM)

        {

            if (!IsGSMOn())

            {

                GSMControl(2500);//2.5s

                return IsGSMOn();

            }

            else

            {

                return 1;

            }

        }

        else

        {

            if (IsGSMOn())

            {

                GSMControl(1500);//1.5s

                return IsGSMOn();

            }

            else

                return 0;

        }

    }

    /*

    打开/关闭WIFI

    */

    void WIFITurnOnOff(int isTurnOn)

    {

             if(isTurnOn)

             {

                       setGpio('C',7,0);

             }

             else

             {

                       setGpio('C',7,1);

             }

    }

    /*

    导通/断开3G模块电源

    */

    void CDMA3GPowerSwitch(int isPowerOn)

    {

             if (isPowerOn)

        {

            setGpio('L', 12, 0);

        }

        else

        {

            setGpio('L', 12, 1);

        }

    }

    /*

    查询锂电池是否在充电

    */

    int IsBatteryCharging(void)

    {

             return getGpio('L',10);

    }

    /*

    打开蜂鸣器,可设定打开时间长度(毫秒)

    */

    void BuzBeep(int keepMilliseconds)

    {

             setGpio('L', 9, 1);

             msleep(keepMilliseconds);

             setGpio('L', 9, 0);

    }

    /*

    打开钱箱:打开钱箱的脉冲宽度(延时时间,单位毫秒),根据具体钱箱设定

    */

    void OpenCashbox(int keepMilliseconds)

    {

             setGpio('N', 6, 1);

             msleep(keepMilliseconds);

             setGpio('N', 6, 0);

    }

    3、 编译库文件

    arm-linux-gcc zgt300API.c -fPIC -shared -o libzgt300API.so

    生成libzgt300API.so动态库文件。

    4、 调用库文件

    zgt300_gpio_test.c:

    #include <stdio.h>

    #include <stdlib.h>

    #include <sys/types.h>

    #include <sys/stat.h>

    #include <fcntl.h>

    #include <unistd.h>

    #include "zgt300API.h"

    int main(int argc,char *argv[])

    {

             int i,fd;

             char buf[3];

             char *p;

             if(argc == 2)

             {

                       if(argv[1][0] == 'B')

                       {

                                p = argv[1];

                                while(p && *p == 'B')

                                {

                                         p++;

                                         BuzBeep(200);

                                         usleep(500*1000);

                                }

                       }

                       else

                       {

                                printf("param error! argc = %d\n",argc);

                           return -1;

                       }

             }

             else

             {

                       printf("param error! argc = %d\n",argc);

                       return -1;

             }

             return 0;

    }

    zgt300API.hlibzgt300API.so拷贝到Zgt300_gpio_test.c所在目录,编译:

    arm-linux-gcc zgt300_gpio_test.c -o zgt300_gpio_test ./libzgt300API.so

    得到zgt300_gpio_test执行文件。

    5、 测试

    libzgt300API.so拷贝到开发板的/lib目录下,在开发板里运行

    ./zgt300_gpio_test BB

    蜂鸣器连响两声,测试通过。

  • 相关阅读:
    恭喜,贺喜,同喜
    IIS 原理学习
    ASP.NET Ajax 学习(一)服务器端部分
    一张图片引发的血案
    poj 2828 Buy Tickets
    hdu 1556 Color the ball
    nyoj 求余数
    hdu 1358Period
    hdu 3577Fast Arrangement
    poj2752 Seek the Name, Seek the Fame
  • 原文地址:https://www.cnblogs.com/Shangzhi/p/2666174.html
Copyright © 2020-2023  润新知