1. 程式人生 > >linux arm 使用cmake 進行編譯的一個播放音樂的例子

linux arm 使用cmake 進行編譯的一個播放音樂的例子

搭建 arm linux 環境一般是這樣:
1 下載交叉編譯環境
2 下載cmake,如果版本低,那麼下載原始碼自己編譯
3 下載gdb新版本,我下的是7.5
4 編譯 gdb ,然後配合 gdbserver可以遠端除錯,或者使用 idapro 進行除錯
下面就是cmake的編譯設定

指定 c編譯器 , c++編譯器, include 目錄和library 目錄就可以了.

set(CMAKE_SYSTEM_NAME Linux)
set(TOOLCHAIN_DIR "/usr/local/arm/cross/am335xt3/devkit/arm-arago-linux-gnueabi")
set
(CMAKE_C_COMPILER "${TOOLCHAIN_DIR}/bin/gcc") set(CMAKE_CXX_COMPILER "${TOOLCHAIN_DIR}/bin/g++") set(CMAKE_FIND_ROOT_PATH ${TOOLCHAIN_DIR} ${TOOLCHAIN_DIR}/include ${TOOLCHAIN_DIR}/lib) include_directories("/usr/local/arm/cross/am335xt3/devkit/arm-arago-linux-gnueabi/usr/include") include_directories("/usr/local/arm/cross/am335xt3/devkit/arm-arago-linux-gnueabi/usr/include"
) link_directories("/usr/local/arm/cross/am335xt3/devkit/arm-arago-linux-gnueabi/lib") link_directories("/usr/local/arm/cross/am335xt3/devkit/arm-arago-linux-gnueabi/usr/lib") project(hal) cmake_minimum_required(VERSION 3.5) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/bin) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}
/bin) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO ${CMAKE_BINARY_DIR}/bin) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR}/lib/debug) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR}/lib/release) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELWITHDEBINFO ${CMAKE_BINARY_DIR}/lib/Release) message("cmake_binary_dir=${CMAKE_BINARY_DIR}") message("CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}") add_subdirectory(buzzer)

在buzzer目錄的CMakeLists.txt 的內容

set(src interface_buzzer.c
        test/main.c)
add_executable(test_buzzer ${src})

在 buzzer 中的 interface_buzzer.h 檔案內容

#ifndef _interface_buzzer_h_20180806_
#define _interface_buzzer_h_20180806_

#ifdef __cplusplus
extern "C" {
#endif

int virbox_open_buzzer();
int virbox_close_buzzer(int fd);
int virbox_sound_frequency(int fd,int freq,long keep_time);
int virbox_keep_buzzer_frequency(int freq,long keep_time);

#ifdef __cplusplus
}
#endif

#endif

在 buzzer 中的 interface_buzzer.c 檔案內容

#include <stdlib.h> //system
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <sys/ioctl.h>
#define BUZZER_DEVICE "/dev/buzzer"
/*
 * virbox_open_buzzer
 * parameters: -
 * return : 成功返回檔案描述符;否則返回errno
 * desc:
 *  開啟buzzer 裝置.
 */
int virbox_open_buzzer()
{
    return open(BUZZER_DEVICE,O_RDWR);
}
/*
 * virbox_close_buzzer
 * parameters: fd - buzzer 裝置描述符(檔案描述符)
 * return 0 - 成功 ;其他 - 失敗
 */
int virbox_close_buzzer(int fd)
{
    return close(fd);
}
/*
 * virbox_sound_frequency
 * parameters: fd - buzzer 裝置描述符(檔案描述符)
 * freq - 聲音的頻率
 * keep_time - 保持多久
 * return 0 - 成功 ;其他 - 失敗
 * desc:
 *  使用開啟的裝置發出一定頻率的聲音,並且保持一定時間。
 */
int virbox_sound_frequency(int fd,int freq,long keep_time)
{
    int ret = ioctl(fd, 1, freq);
    if(ret<0)
    {
        return ret;
    }
    usleep(keep_time);
    return ret;
}

/*
 * virbox_keep_buzzer_frequency
 * parameters:
 *  
 * desc:
 *  發出某個頻率聲音,並且保持多久
 */
int virbox_keep_buzzer_frequency(int freq,long keep_time)
{
    int fd = virbox_open_buzzer();
    int ret = virbox_sound_frequency(fd,freq,keep_time);
    virbox_close_buzzer(fd);
    return ret;
}



在 test 目錄下的main.c 檔案
這裡是小蜜蜂的樂譜:
小蜜蜂

#include <stdio.h>
#include "../interface_buzzer.h"
typedef struct sound_data
{
    int freq_index;
    int delay_time;
}sound_data;

#define BEAT_1 3200000/8
#define BEAT_2 3200000/4

int mid[]= {0, 523,578,659,698,784,880,988};
sound_data bee[]={{5,1},{3,1},{3,2},{4,1},{2,1},{2,2},{1,1},{2,1},{3,1},{4,1},{5,1},{5,1},{5,2},
    {5,1},{3,1},{3,2},{4,1},{2,1},{2,2},{1,1},{3,1},{5,1},{5,1},{3,4},
    {2,1},{2,1},{2,1},{2,1},{2,1},{3,1},{4,2},{3,1},{3,1},{3,1},{3,1},{3,1},{4,1},{5,2},
    {5,1},{3,1},{3,2},{4,1},{2,1},{2,2},{1,1},{3,1},{5,1},{5,1},{1,4}};
int main()
{
    int i;
    int fd = virbox_open_buzzer();
    for(i=0;i<sizeof(bee)/sizeof(bee[0]);i++)
    {
        virbox_sound_frequency(fd,mid[bee[i].freq_index],BEAT_1*bee[i].delay_time);
    }
    virbox_close_buzzer(fd);
    return 0;
}