Build Scripts

The final step in adding a platform to SensiML is to provide two scripts that help us build a binary and return a properly configured library.

Configuring Sensors

The first script is a script to configure sensors. When using Data Capture Lab to configure your device, a Sensor Configuration is created that stores the properties of the sensors used during data collection (Sample Rate, Sensor name, etc).

This configuration will be used to generate code to configure your sensors at build time or return a configuration file for setting up your device with a library.

config_sensor_files.sh
#!/bin/bash

inputs=$1
# Other sensor configurations can be added, these can be removed as well.

sensor_plugin_name=`$1 | jq -r '.["sensor_plugin_name"]'`
sample_rate=`$1 | jq -r '.["sample_rate"]'`
accel_range=`$1 | jq -r '.["accelerometer_sensor_range"]'`
gyro_range=`$1 | jq -r '.["gyroscope_sensor_range"]'`

# switch to recognition mode (add to mqtt app as well for backwards compatibility. The app config is for SDK versions > 1.8)
if [[ ! -z $sample_rate ]]; then
echo "Changing Sample rate"
echo "$SML_APP_CONFIG_FILE"
sed -i "s/#define SNSR_SAMPLE_RATE\b.*/#define SNSR_SAMPLE_RATE $sample_rate/" $SML_APP_CONFIG_FILE
fi

if [[ ! -z $accel_range ]]; then
echo "Changing Accel Range"
sed -i "s/#define SNSR_ACCEL_RANGE\b.*/#define SNSR_ACCEL_RANGE $accel_range/" $SML_APP_CONFIG_FILE
fi
if [[ ! -z $gyro_range ]]; then
echo "Changing Gyro Range"
sed -i "s/#define SNSR_GYRO_RANGE\b.*/#define SNSR_GYRO_RANGE $gyro_range/" $SML_APP_CONFIG_FILE
fi

The above script takes a sample_rate to configure your sensors to run at the right speed, as well as sensor-specific ranges to allow a motion sensor to be configured for the right sensitivity.

These work by replacing defines in the SML_APP_CONFIG_FILE specified in the codegen_parameters section of your hardware platform. It is highly suggested to have all of these configurations in one file.

Replacing the variable names of SNSR_SAMPLE_RATE with those that match your repository will allow SensiML to output proper configuration for your device.

Building a Binary

In order to build a binary on SensiML servers, we need to know the build steps required to build your project.

This will be needed for both a custom platform image and a platform using a supported compiler Docker image to properly build a binary.

build_binary.sh
#!/bin/bash

check_return_code() {
        #Check the return code of most recent calls.
        rc=$?; if [[ $rc != 0 ]]; then exit $rc; fi
}
# These are passed in by SensiML servers. Do not modify!
inputs=$1

mount_point=`$1 | jq -r '.["mount_directory"]'`
project=`$1 | jq -r '.["application"]'`
kp_id=`$1 | jq -r '.["uuid"]'`
user=`$1 | jq -r '.["user_id"]'`
debug=`$1 | jq -r '.["debug_flag"]'`
version=`$1 | jq -r '.["version"]'`
platform=`$1 | jq -r '.["platform"]'`
build_tensorflow=n

if [ "$BUCKET_NAME" = "NO S3" ]; then
        rename_output=$outfile
else
        rename_output="codegen-compiled-output"
fi

echo "Building Binary"

mkdir -p $SML_APP_LIB_DIR

cp $SML_KP_OUTPUT_DIR/kb.a $SML_APP_LIB_DIR/libsensiml.a
cp $SML_KP_OUTPUT_DIR/libsensiml.a $SML_APP_LIB_DIR/libsensiml.a
cp $SML_KP_DIR/libtensorflow-microlite.a $SML_APP_LIB_DIR/
cp $SML_KP_DIR/kb.h $SML_APP_LIB_DIR

cp $SML_KP_DIR/kb_defines.h  $SML_APP_LIB_DIR
cp $SML_KP_DIR/model_json.h  $SML_APP_LIB_DIR
cp $SML_KP_DIR/kb_debug.h  $SML_APP_LIB_DIR
cp $SML_KP_DIR/kb_typedefs.h  $SML_APP_LIB_DIR
cp $SML_KP_DIR/testdata.h $SML_APP_LIB_DIR
cp $SML_KP_DIR/model.json $mount_point/

pushd $SML_APP_BUILD_DIR
pwd
#
# HERE YOU WILL ADD YOUR BUILD STEPS.
#
#
# make all -j

check_return_code

mkdir return_files
cd return_files

#
# HERE YOU WILL ADD ALL FILES YOU WISH TO BE RETURNED WITH THE BINARY.
# SensiML typically returns bin/hex file, map, and other informational files.
# If all output files are in SML_APP_OUTPUT_BIN_DIR, no action is required.
#

echo $outfile

outfile=kp_"$kp_id"_"$platform"_"$lib_bin"_"$version"_"$debug".zip
cp -RT "$SML_APP_OUTPUT_BIN_DIR" .
cp $SML_KP_DIR/model.json .
zip -r $outfile ./
cp ./$outfile $mount_point/$outfile


exit $?

The build_binary script will reference many of the parameters in the codegen_parameters section of your hardware platform definition.

The commented sections of the build script should be the only items changed, so that your build steps perform, and the desired output files are included in a downloaded zip file.