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 6025241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:16:21+00:00 2026-05-23T04:16:21+00:00

I have a device driver that uses the following algorithm to convert a value

  • 0

I have a device driver that uses the following algorithm to convert a value received from a sensor (no bound range) to a different value (bound range).

The sensor’s values are usually within the 0-200 range but can exceed it, the maximum being about 4000 (this only happens when you use the sensor in an extreme way). I basically need a function that can do the following but without the giant if so it is more flexible.

It needs to take in the value, the step (in this case 20.0f) and the max output (in this case 10).

/* disregard the floating point numbers, I can cast them to int */
if (value <= 20.0f)
    return 0;
else if (value <= 40.0f)
    return 1;
else if (value <= 60.0f)
    return 2;
else if (value <= 80.0f)
    return 3;
else if (value <= 100.0f)
    return 4;
else if (value <= 120.0f)
    return 5;
else if (value <= 140.0f)
    return 6;
else if (value <= 160.0f)
    return 7;
else if (value <= 180.0f)
    return 8;
else if (value <= 190.0f)
    return 9;
else if (value >= 200.0f)
    return 10;

return 0;
  • 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-05-23T04:16:22+00:00Added an answer on May 23, 2026 at 4:16 am
    int step(double value, int step, int maximum) { 
        return min(int(value / step), maximum);
    }
    

    Edit: As @DSM noted, this has a fencepost error, and should be something like:

    int step(double value, int step, int maximum) { 
        return min(int((value-1) / step), maximum);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider a Linux driver that uses get_user_pages (or get_page ) to map pages from
I have a device driver I want to patch. This device driver calls IOLog
I'm developing a Linux driver loadable module and I have to use another device
I have a device that supports 4-color graphics (much like CGA in the old
We have an embedded device that needs to interact with an enterprise software system.
I have a Windows CE device that we are deploying, but we have complete
I have the following code: for attribute in site.device_attributes device.attribute end where I would
I have a .mbm file that I copy to my device using this line
Windows Mobile devices have different behaviour for suspending when the device is on battery
I have been tasked to write a device driver for an embedded device which

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.