MySQL for the Internet of Things

(Steven Felgate) #1
Chapter 4 ■ Data transformation

Code Implementation


While most experienced programmers would caution against converting data types unless absolutely
necessary,^4 there are a few cases where it is possible to justify if done carefully. For example, if you are
working with a language that has loose type checking and want to do some arithmetic resulting in a floating
point value but unintentionally use integers in the equation, you may get an integer result. To overcome
this, you may need to change the integers to floating-point numbers with a special operation called a cast
(also called casting). Casting in Arduino C is implemented by placing the new data type in parentheses. An
example is shown here:


int shift_value = 100;
val_shifted = (float)shift_value * 95.0675;


Another argument for using certain data type conversions is to save memory. This is most common
when working with microcontrollers and other processors that have limited memory for variables. Those
new to working with devices with limited memory may not think saving a byte here or there could make a
difference, but it really can make a huge difference.
For example, if your sketch uses a lot of variables for calculations, you may need to consider reducing
the types and therefore the sizes of your variables. For example, consider how many bytes a relatively small
matrix of 20×20 values would consume if each cell were a double. Yes, that’s 20×20×4 = 1,600 bytes. If your
Arduino has only 2Kb of memory, you’re likely to encounter problems.
If you encounter a problem like this and the range of values you are working with is smaller than the
data type you’ve chosen, you may be able to convert them. For example, if the values in the matrix never
exceed about +/-320 and precision permits the use of only 2 decimals, you can convert the values to integers
as follows:


int new_val;
new_val = (int)(orig_val * 100.00);


This code results in a value such as 222.66 to 22266, which is small enough to fit in an integer data type
and thus can save 800 bytes in memory. This is quite a large savings for an older Arduino board! However, I
should note here that the values would be truncated, not rounded. So while you can get the data back to a
float by dividing by 100, you will not recover any additional precision of the original value.
Another possible data type conversion you may encounter is converting bytes to characters or retrieving
larger numeric values from a series of bytes. This is likely to come about when communicating between
devices. That is, most communication mechanisms send data in a byte stream. In this case, we may need to
extract data such as integers, floating-point numbers, and even character strings (text) from the byte stream.
Converting byte to characters is simple since they are treated the same in code (each is one byte).
However, they can be interpreted quite differently. For example, an ASCII character uses only the first seven
bits, while a byte is treated as eight bits.
If you have to extract numbers from byte streams, you will need to know how the values are encoded.
For example, a small value that fits in a single byte will require, well, one byte. A larger value may be an
integer and require two bytes, and a long integer may require four bytes. Aside from the number of bytes,
you must also know in what order the individual bytes are stored. Most microprocessor and microcontroller
platforms store them with the smallest byte first.^5


(^4) My own experience and training force me to maintain strict type adherence, but I too admit there are some cases where
it may be necessary. That is, beyond the trivial.
(^5) Called little-endian. See endianness (https://en.wikipedia.org/wiki/Endianness).

Free download pdf