Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

Working with Sensors, Devices and Platform Services in your C++ applications

All of our modern PCs, Smartphones, Tablets and other devices include more than just a screen, keyboard, mouse, memory, and storage. The devices We use include sensors for location, orientation, acceleration, ambient light, etc. My Samsung Slate Series 7 is a hybrid or “2 for 1” device that is both a desktop PC and a Tablet. It includes two cameras and accelerometer. Even if they don’t include a GPS or other hardware sensor, our programs can use APIs to find the location from known Internet access points.  Some smartphones and tablets include two camera devices.  PCs for some time have had both a built-in microphone and a line-input jack. Different operating systems and hardware platforms provide different services:  Windows and Mac support menus, dialogs and mice, most include support for a canvas for low level drawing, a GPU (at least one or more) is available for higher performance 2D and 3D graphics, earlier versions of iOS did not provide a cut/copy/paste clipboard, server side systems may not provide a screen service, and there are many other services that we may or not have access to for C++ applications we build that need to run on different platforms.  What is a C++ developer supposed to do build applications that can run on a variety of systems and platforms?  The choices are varied and include using platform APIs, querying the hardware directly, adding conditional compilation to source code, and other methods.

Having access to a robust component framework that provides start-of-the-art components, classes and interfaces that do the work for you, greatly improves your ability to build applications for a wider arrange of systems.  C++Builder XE4 allows you to query for services, devices and sensors to know what your application can use.  FM, the component framework from Embarcadero Technologies, provides everything a C++ developer needs to query devices, count the number of sensors, and know which services are available for application use.  FM itself uses the same sensor, device and platform services to know how to initialize itself on each of the target platforms.  What sensors, device and services are supported for C++ application development? Let’s take a look at each of the three in more detail.

Sensors

FM’s support for sensors is organized into sensor categories:

 

 

 

 

 

 

 

 

 

The sensor category definition in System.Sensors.hpp is:

TSensorCategory : unsigned char { Location, Environmental, Motion, Orientation, Mechanical, Electrical, Biometric, Light, Scanner }

For each sensor category there are many possible sensor types including:

 

 

 

 

 

 

 

 

 

 

In your C++ code you can get a count of the identified sensors, sensor name, sensor category and type, sensor state and more.

 

[crayon-675010a3153eb006250651/]

 

Once you know which sensors are available in your application you can chose to use the FM components and classes directly in your applications.  You can also query to ensure that a specific sensor is available for your application to use.  When you build your applications for Windows and Mac in C++Builder XE4 you’ll have easy access to all of the sensors available on each of your target systems.

In this next article, learn about how you can convert int to char C++ in this article about String Operations.

Devices

How many cameras do your PCs, Smartphones and Tablets have?  How many audio inputs does you application have access to?  You use use FM’s support for device managers to be able to use them in your applications.  Each FM based application includes a default device manager.  The Device Manager interfaces will give you the device name, media type of the device (audio/video), the device state and other information about specific devices.  You can query and filter for specific device media types or iterate through the collection of devices to find the ones your application is looking for.

 

[crayon-675010a3153f5539106556/]

 

The following C++ code snippet lists the audio and video capture devices available to your application.

 

[crayon-675010a3153f8295477959/]

 

The following C++ code snippet shows you how to support a system with one or two video cameras (like my Samsung Slate Series 7).

 

[crayon-675010a3153fa482720127/]

 

Platform Services

Each target platform comes with its support for different application and user interface paradigms.  These platform services can include, but are not limited to, the following types of services:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

In your C++ applications you can query whether a service is available on a target platform. If the service is available, use the native platform service or provide your own or a third party alternative.  For Windows and Mac development it is safe to assume that there is a mouse, menu, dialog and clipboard.  As your C++ applications move beyond the desktop, you may not have a dialog or menu service, but you can probably assume that there will be a virtual keyboard service.

Using the SupportsPlatformService method will return true or false if the service is available to your application.  The second method will return the service so that you can use it in your code.

 

[crayon-675010a3153fc500214907/]

 

Here is a FM based C++ code snippet that lets you test to see if there is a virtual keyboard service available to your application and uses it:

 

[crayon-675010a3153fe120756203/]

 

Using Devices, Sensors and Platform Services in your Applications

I’ve written blog posts and created videos that show C++ developers how to work with sensors, devices and platform services.  Take a look at the following links for additional information.

 

 

 

 

 

 

 

 

 

 

As C++ developers move beyond the desktop, the same component framework will take you and your code to new platforms, devices, embedded applications and beyond.

From Scooter Software, this Beyond Compare download on the next article will allow you and your organization to compare and reconcile files or system drives in a more agile manner.

Next, find out how you can convert String to Int C++ in this article about Modern C++ Software.
Exit mobile version