Posted on

为Intel Edison编写自己的UPM模块

UPM (Useful Packages and Modules)相当于第三方厂商基于MRAA为Intel Edison、树莓派等开发板做的电子模块库。
以下是在Intel Edison开发UPM模块的详细步骤。
* 安装和设置Git
第一次启动Edison,先执行 “configure_edison –setup”
添加opkg更新源,修改文件 “/etc/opkg/base-feeds.conf”

src/gz all http://repo.opkg.net/edison/repo/all
src/gz edison http://repo.opkg.net/edison/repo/edison
src/gz core2-32 http://repo.opkg.net/edison/repo/core2-32

执行

opkg update
opkg install git

* 编译UPM
如果mraa版本太低,先执行 opkg install mraa

cd ~
git clone https://github.com/intel-iot-devkit/upm
cd upm

设置环境变量

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:.../mraa/build/lib/pkgconfig

执行

mkdir build
cd build
cmake ..

编译过程比较漫长,至少1个小时

make
make install

* 创建模块必需资源

cd ../src
mkdir mymodule
cd mymodule

CMakeLists.txt

set (libname "mymodule")
set (libdescription "Test UPM module for my learning")
set (module_src mymodule.cxx)
set (module_h mymodule.h)
upm_module_init()

javaupm_mymodule.i(SWIG 接口文件 for Java)

%module javaupm_mymodule
 
%include "../upm.i"
 
%include "typemaps.i"
%{
    #include "mymodule.h"
%}
%include "mymodule.h"
%pragma(java) jniclasscode=%{
 
    static {
        try {
            System.loadLibrary("javaupm_mymodule");
        } catch (UnsatisfiedLinkError e) {
            System.err.println("Native code library failed to load. \n" + e);
            System.exit(1);
        }
    }
%}

jsupm_mymodule.i(SWIG 接口文件 for Javascript)

%module jsupm_mymodule
%include "../upm.i"
%include "mymodule.h"
%{
    #include "mymodule.h"
%}

pyupm_mymodule.i(SWIG 接口文件 for Python)

%include "pyupm_doxy2swig.i"
%module pyupm_mymodule
%include "../upm.i"
%feature("autodoc", "3");
%include "mymodule.h"
%{
    #include "mymodule.h"
%}

模块源文件
mymodule.h

#pragma once
#include <stdio.h>
#include <mraa/aio.h>
#include <mraa/gpio.h>
#define HIGH                   1
#define LOW                    0
namespace upm {
    class MyModule {
        public:
            MyModule();
            ~MyModule ();
            void test();
        private:
            bool m_doWork;
            mraa_gpio_context m_pinCtx;
    };
}

mymodule.cxx

#include "mymodule.h" 
#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <functional>
using namespace upm;
MyModule::MyModule() {
    mraa_init();
    m_doWork = false;
    // setup pin
    m_pinCtx = mraa_gpio_init(13);
    if (m_pinCtx == NULL) {
        fprintf (stderr, "Are you sure that pin%d you requested is valid on your platform?", 13);
        exit (1);
    }
    mraa_gpio_dir(m_pinCtx, MRAA_GPIO_OUT);
}
 
MyModule::~MyModule () {
    mraa_gpio_close (m_pinCtx);
}
void MyModule::test() {
    m_doWork = !m_doWork;
    mraa_gpio_write(m_pinCtx, m_doWork?HIGH:LOW);
}

所有文件都准备就绪

    mymodule

  • CMakeLists.txt
  • jsupm_mymodule.i
  • pyupm_mymodule.i
  • javaupm_mymodule.i
  • mymodule.cxx
  • mymodule.h

* 编译和安装自定义模块,如果make install报错,尝试执行make clean

cd ../../build
cmake ..
make mymodule
cd src/mymodule
make install

* NodeJS测试程序,test.js

var module = require('jsupm_mymodule');
var mod = new module.MyModule();
function loop(){
    mod.test();
    setTimeout(loop,500);
}
loop();

设置环境变量,NODE_PATH=$NODE_PATH:/usr/local/lib/node_modules/
执行node test.js

原文来自:http://shawnhymel.com/756/writing-your-own-upm-module-getting-started/

Print Friendly, PDF & Email

One thought on “为Intel Edison编写自己的UPM模块

  1. hi 北京浮生记 是你做的吗?希望能联系下。谢谢

发表评论

电子邮件地址不会被公开。 必填项已用*标注

This site uses Akismet to reduce spam. Learn how your comment data is processed.