Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8453961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:54:05+00:00 2026-06-10T11:54:05+00:00

I am a complete code noob and I’m trying to implement a HAL (specifically

  • 0

I am a complete code noob and I’m trying to implement a HAL (specifically the BMA222 Sensor) to my Android device.

When I try this code (BMA222.cpp):

#include <fcntl.h>
#include <errno.h>
#include <math.h>
#include <poll.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/select.h>
#include <cutils/log.h>

#include "BMA222Sensor.h"

#define CONVERT                     (GRAVITY_EARTH / 256)
//#define CONVERT_GRAVITY_X           (CONVERT)
//#define CONVERT_GRAVITY_Y           (-1 * CONVERT)
//#define CONVERT_GRAVITY_Z           (-1 * CONVERT)
#define SENSOR_NAME                 "bma222"

/*****************************************************************************/

BMA222Sensor::BMA222Sensor()
    : SensorBase(NULL, SENSOR_NAME),
      mEnabled(0),
      mInputReader(4),
      mHasPendingEvent(false),
      mEnabledTime(0)
{
    mPendingEvent.version = sizeof(sensors_event_t);
    mPendingEvent.sensor = ID_GY;
    mPendingEvent.type = SENSOR_TYPE_ACCELEROMETER;
    memset(mPendingEvent.data, 0, sizeof(mPendingEvent.data));

    if (sensor_get_class_path() < 0)
        LOGD("Failed to get class path\n");
}

BMA222Sensor::~BMA222Sensor() {
    if (mEnabled) {
        enable(0, 0);
    }
}

int BMA222Sensor::enable(int32_t, int en) {
    int flags = en ? 1 : 0;
    char buf[2];
    int count = 0;;

    if (flags != mEnabled) {
            buf[1] = 0;
            if (flags) {
                buf[0] = '1';
                mEnabledTime = getTimestamp();
            } else {
                buf[0] = '0';
            }
        set_sysfs_input_attr(class_path, "enable", buf, 1);
            mEnabled = flags;
    }
    return 0;
}

bool BMA222Sensor::hasPendingEvents() const {
    return mHasPendingEvent;
}

int BMA222Sensor::setDelay(int32_t handle, int64_t delay_ns)
{
    char buf[80];
    int count = 0;;
    int64_t delay_ms = delay_ns / 1000000;

    count = sprintf(buf, "%lld", delay_ms);
    set_sysfs_input_attr(class_path, "delay", buf, count);

    return 0;
}

int BMA222Sensor::readEvents(sensors_event_t* data, int count)
{
    if (count < 1)
        return -EINVAL;

    if (mHasPendingEvent) {
        mHasPendingEvent = false;
        mPendingEvent.timestamp = getTimestamp();
        *data = mPendingEvent;
        return mEnabled ? 1 : 0;
    }

    ssize_t n = mInputReader.fill(data_fd);
    if (n < 0)
        return n;

    int numEventReceived = 0;
    input_event const* event;

    while (count && mInputReader.readEvent(&event)) {
        int type = event->type;
        if (type == EV_ABS) {
            float value = event->value;
            if (event->code == ABS_X) {
                //mPendingEvent.data[1] = value * CONVERT_GRAVITY_Y;
                mPendingEvent.data[0] = value * CONVERT;
            } else if (event->code == ABS_Y) {
                //mPendingEvent.data[0] = value * CONVERT_GRAVITY_X;
                mPendingEvent.data[1] = value * CONVERT;
            } else if (event->code == ABS_Z) {
                //mPendingEvent.data[2] = value * CONVERT_GRAVITY_Z;
                mPendingEvent.data[2] = value * CONVERT;
            }
        } else if (type == EV_SYN) {
            mPendingEvent.version = sizeof(sensors_event_t);
            mPendingEvent.sensor = ID_A;
            mPendingEvent.type = SENSOR_TYPE_ACCELEROMETER;
            mPendingEvent.timestamp = timevalToNano(event->time);
            *data++ = mPendingEvent;
            numEventReceived++;
            count--;
        } else {
            LOGE("BMA222Sensor: unknown event (type=%d, code=%d)",
                    type, event->code);
        }
        mInputReader.next();
    }

    return numEventReceived;
}

bool BMA222Sensor::isEnabled(int32_t handle) {
    return (0 != (mEnabled)) ? true : false;
}

int BMA222Sensor::sensor_get_class_path()
{
    char dirname[] = "/sys/class/input";
    char buf[256];
    int res;
    DIR *dir;
    struct dirent *de;
    int fd = -1;
    int found = 0;

    dir = opendir(dirname);
    if (dir == NULL)
        return -1;

    while((de = readdir(dir))) {
        if (strncmp(de->d_name, "input", strlen("input")) != 0) {
                continue;
        }

        sprintf(class_path, "%s/%s", dirname, de->d_name);
        snprintf(buf, sizeof(buf), "%s/name", class_path);

        fd = open(buf, O_RDONLY);
        if (fd < 0) {
                continue;
        }
        if ((res = read(fd, buf, sizeof(buf))) < 0) {
                close(fd);
                continue;
        }
        buf[res - 1] = '\0';
        if (strcmp(buf, SENSOR_NAME) == 0) {
                found = 1;
                close(fd);
                break;
        }

        close(fd);
        fd = -1;
    }
    closedir(dir);

    if (found) {
        return 0;
    }else {
        *class_path = '\0';
        return -1;
    }
}

int BMA222Sensor:: set_sysfs_input_attr(char *class_path,
                const char *attr, char *value, int len)
{
    char path[256];
    int fd;

    if (class_path == NULL || *class_path == '\0'
            || attr == NULL || value == NULL || len < 1) {
        return -EINVAL;
    }
    snprintf(path, sizeof(path), "%s/%s", class_path, attr);
    path[sizeof(path) - 1] = '\0';
    fd = open(path, O_RDWR);
    if (fd < 0) {
            return -errno;
    }
    if (write(fd, value, len) < 0) {
        close(fd);
        return -errno;
    }
    close(fd);
    return 0;
}

And this (BMA222Sensor.h):

#ifndef ANDROID_BMA222_SENSOR_H
#define ANDROID_BMA222_SENSOR_H

#include <stdint.h>
#include <errno.h>
#include <sys/cdefs.h>
#include <sys/types.h>

#include "sensors.h"
#include "SensorBase.h"
#include "InputEventReader.h"

/*****************************************************************************/

struct input_event;

class BMA222Sensor : public SensorBase {
    int mEnabled;
    InputEventCircularReader mInputReader;
    sensors_event_t mPendingEvent;
    bool mHasPendingEvent;
    char input_sysfs_path[PATH_MAX];
    int input_sysfs_path_len;
    int64_t mEnabledTime;

    char class_path[256];
    int sensor_get_class_path();
    int set_sysfs_input_attr(char *class_path,
                             const char *attr, char *value, int len);
public:
            BMA222Sensor();
    virtual ~BMA222Sensor();
    virtual int readEvents(sensors_event_t* data, int count);
    virtual bool hasPendingEvents() const;
    virtual int setDelay(int32_t handle, int64_t ns);
    virtual int enable(int32_t handle, int enabled);
    virtual bool isEnabled(int32_t handle);     //rockie
};

/*****************************************************************************/

#endif  // ANDROID_BMA222_SENSOR_H

I get this (shortened):

In constructor 'BMA222Sensor::BMA222Sensor()':
error: 'ID_GY' was not declared in this scope
make: *** [BMA222Sensor.o] Error 1

I don’t understand where I’m going wrong, any help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:54:06+00:00Added an answer on June 10, 2026 at 11:54 am

    Unfortunately I know nothing about android only C++. I would guess that ID_GY is a integer value that identifies the sensor. One way to define that would be to write

    #define ID_GY 100
    

    just after where you wrote

    #define SENSOR_NAME "bma222"
    

    The value of 100 is arbitrary, it might not matter too much what the actual value is, but I could easily be wrong about that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to do this: List<Parent> test = new List<Child>(); The complete code
This is a question from a complete noob. I have the following code: nb='([-
I'm a complete noob in java. I still really don't know how to code.
I am trying to submit a form with ajax. Here is my complete code
This is the complete source code: http://www.sendspace.com/file/lwxpyf I have a problem with a JSF
With regards this example from Code Complete: Comparison Compare(int value1, int value2) { if
I figured this would be an issue eventually and I'm a complete linux noob.
I am trying to get a stringArray resource with this code: String continent =
The complete code can be found here: http://www.webdeveloper.com/forum/showthread.php?t=224180#6 This emulates jquery like functionality. The
I am a complete noob to c. I have downloaded a code from internet.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.