Swift Sensortag 2.0 Write Bits to BLE - swift

Pretty basic question here:
I'm currently trying to control a sensortag 2.0 via Swift 3.0.
I'm trying to simultaneously turn on the acc, gyro, and magnetometer.
According to Texas Instruments documentation, the following applies for the IMU:
Axis enable bits:gyro-z=0,gyro-y,gyro-x,acc-z=3,acc-y,acc-x,mag=6 Range: bit 8,9
I have written "0x023F" in the following manner, which turns on the gyro and the accelerometer with great success.
let value = OperationDataHolder(data:[0x023F])
var parameter = 0x023F
let data = NSData(bytes: &parameter, length: 2)
self.sensorTagPeripheral.writeValue(data as Data, for: thisCharacteristic, type: CBCharacteristicWriteType.withResponse)
However, I'm not able to figure out the value to write to turn turn on all 3 units simultaneously. Would someone be able to provide me with this value?
Thanks!

When you convert the current value you are using (0x023F) to binary, you get 0b1000111111. Each of the bits represents the on/off (on=1/off=0) state of a given sensor component.
If you read the binary number from right to left, and map each bit by referencing the table below, you will see that the gyro z/y/x and accelerometer z/y/x are all enabled. If you want to enable the magnetometer, simply change 'bit 6' to a '1' and convert that binary number to a hexadecimal number.
So, Google says: 0b1001111111 is 0x027F in hexadecimal
Mapping:
bit 0 => gyro z-axis
bit 1 => gyro y-axis
bit 2 => gyro x-axis
bit 3 => acc z-axis
bit 4 => acc y-axis
bit 5 => acc x-axis
bit 6 => mag (enables all axes)
bit 7 => Wake-on-motion enable
bits 8 & 9 => Accelerometer range
bits 10-15 => not used
For more info about the mapping (i.e. what bits 8 & 9 do), see the Sensor Tag Wiki Page

Building on peters answer, the swifty way to do this is with an option set, which is admittedly more work up front, but much more readable than hex:
struct SensorOptions: OptionSet {
let rawValue: Int32
static let gyroZ = SensorOptions(rawValue: 1 << 0)
static let gyroY = SensorOptions(rawValue: 1 << 1)
static let gyroX = SensorOptions(rawValue: 1 << 2)
static let accZ = SensorOptions(rawValue: 1 << 3)
static let accY = SensorOptions(rawValue: 1 << 4)
static let accX = SensorOptions(rawValue: 1 << 5)
static let mag = SensorOptions(rawValue: 1 << 6)
static let wakeOnMotion = SensorOptions(rawValue: 1 << 7)
static let accelerometerRange1 = SensorOptions(rawValue: 1 << 8)
static let accelerometerRange2 = SensorOptions(rawValue: 1 << 9)
}
let options: SensorOptions = [.gyroZ, .gyroY, .gyroX, .accZ, .accY, .accX, .mag, .accelerometerRange2]
var parameter = options.rawValue
let data = NSData(bytes: &parameter, length: 2)
self.sensorTagPeripheral.writeValue(data as Data, for: thisCharacteristic, type: CBCharacteristicWriteType.withResponse)
Also if you don't like the option set swift has binary literals so you can write var parameter = 0b1001111111 in your code instead of 0x027F

Related

How to get the binary inverse of a number in Swift?

If we have a given number, say 9 (binary representation is 1001). How can we most efficiently get it's inverse 6 (binary representation is 0110)? i.e replacing 0 with 1 and 1 with 0.
I have written a code of order O(1) complexity? But can there be a better way? Does Swift provide an elegant way of handling this?
Note negate function ~9 results in -10. This is not what I am seeking.
func inverse(of givenNumber: Int) -> Int // eg. 9
{
let binaryRepresentation = String(givenNumber, radix: 2) // "1001"
let binaryRepresentationLength = binaryRepresentation.count // 4
let maxValueInLength = (1 << binaryRepresentationLength) - 1 // 15, i.e., 1111
let answer = givenNumber ^ maxValueInLength // 6, i.e., 0110
return answer
}
Edit 1: givenNumber > 0
For positive numbers you can use the following:
func intInverse<T: FixedWidthInteger>(of givenNumber: T) -> T
{
assert(!T.isSigned || givenNumber & (T(1) << (givenNumber.bitWidth - 1)) == 0)
let binaryRepresentationLength = givenNumber.bitWidth - givenNumber.leadingZeroBitCount
let maxValueInLength = givenNumber.leadingZeroBitCount > 0 ? (~(~T(0) << binaryRepresentationLength)) : ~0
let answer = givenNumber ^ maxValueInLength
return answer
}
Which is identical to your algorithm but doesn't require stringifying the number. It doesn't work for negative numbers, but then neither does your algorithm because your algorithm sticks a - on the front of the number.
Probably the easiest way to extend this to cover negative numbers is to invert all the bits to get the binaryRepresentationLength
EDIT
I changed the way the exclusive or mask is created because the old one crashed for unsigned values with the top bit set and for signed values with the second highest bit set.
The code becomes much simpler using the property binade of a floating-point value.
func inverse(of givenNumber: Int) -> Int // eg. 9
{
let maxValueInLength = Int((Double(givenNumber).binade * 2) - 1) // 15, i.e., 1111
let answer = givenNumber ^ maxValueInLength // 6, i.e., 0110
return answer
}

Decomposing a number into powers of 2

Hi I need to decompose a number into powers of 2 in swift 5 for an iOS app I'm writing fro a click and collect system.
The backend of this system is written in c# and uses the following to save a multi-pick list of options as a single number in the database eg:
choosing salads for a filled roll on an order system works thus:
lettuce = 1
cucumber = 2
tomato = 4
sweetcorn = 8
onion = 16
by using this method it saves the options into the database for the choice made as (lettuce + tomato + onion) = 21 (1+4+16)
at the other end I use a c# function to do this thus:
for(int j = 0; j < 32; j++)
{
int mask = 1 << j;
}
I need to convert this function into a swift 5 format to integrate the decoder into my iOS app
any help would be greatly appreciated
In Swift, these bit fields are expressed as option sets, which are types that conform to the OptionSet protocol. Here is an example for your use case:
struct Veggies: OptionSet {
let rawValue: UInt32
static let lettuce = Veggies(rawValue: 1 << 0)
static let cucumber = Veggies(rawValue: 1 << 1)
static let tomato = Veggies(rawValue: 1 << 2)
static let sweetcorn = Veggies(rawValue: 1 << 3)
static let onion = Veggies(rawValue: 1 << 4)
}
let someVeggies: Veggies = [.lettuce, .tomato]
print(someVeggies) // => Veggies(rawValue: 5)
print(Veggies.onion.rawValue) // => 16
OptionSets are better than just using their raw values, for two reasons:
1) They standardize the names of the cases, and gives a consistent and easy way to interact with these values
2) OptionSet derives from the SetAlgebra protocol, and provides defaulted implementations for many useful methods like union, intersection, subtract, contains, etc.
I would caution against this design, however. Option sets are useful only when there's a really small number of flags (less than 64), that you can't forsee expanding. They're really basic, can't store any payload besides "x exists, or it doesn't", and they're primarily intended for use cases that have very high sensitivity for performance and memory use, which quick rare these days. I would recommend using regular objects (Veggie class, storing a name, and any other relevant data) instead.
You can just use a while loop, like this :
var j = 0
while j < 32 {
var mask = 1 << j
j += 1
}
Here is a link about loops and control flow in Swift 5.
Hi I figured it out this is my final solution:
var salads = "" as String
let value = 127
var j=0
while j < 256 {
let mask=1 << j
if((value & mask) != 0) {
salads.append(String(mask) + ",")
}
j += 1
}
salads = String(salads.dropLast()) // removes the final ","
print(salads)
This now feeds nicely into the in clause in my SQL query, thanks you all for your help! :)

Find most significant bit in Swift

I need to find the value (or position) of the most significant bit (MSB) of an integer in Swift.
Eg:
Input number: 9
Input as binary: 1001
MS value as binary: 1000 -> (which is 8 in decimal)
MS position as decimal: 3 (because 1<<3 == 1000)
Many processors (Intel, AMD, ARM) have instructions for this. In c, these are exposed. Are these instructions similarly available in Swift through a library function, or would I need to implement some bit twiddling?
The value is more useful in my case.
If a position is returned, then the value can be easily derived by a single shift.
Conversely, computing position from value is not so easy unless a fast Hamming Weight / pop count function is available.
You can use the flsl() function ("find last set bit, long"):
let x = 9
let p = flsl(x)
print(p) // 4
The result is 4 because flsl() and the related functions number the bits starting at 1, the least significant bit.
On Intel platforms you can use the _bit_scan_reverse intrinsic,
in my test in a macOS application this translated to a BSR
instruction.
import _Builtin_intrinsics.intel
let x: Int32 = 9
let p = _bit_scan_reverse(x)
print(p) // 3
You can use the the properties leadingZeroBitCount and trailingZeroBitCount to find the Most Significant Bit and Least Significant Bit.
For example,
let i: Int = 95
let lsb = i.trailingZeroBitCount
let msb = Int.bitWidth - 1 - i.leadingZeroBitCount
print("i: \(i) = \(String(i, radix: 2))") // i: 95 = 1011111
print("lsb: \(lsb) = \(String(1 << lsb, radix: 2))") // lsb: 0 = 1
print("msb: \(msb) = \(String(1 << msb, radix: 2))") // msb: 6 = 1000000
If you look at the disassembly(ARM Mac) in LLDB for the Least Significant Bit code, it uses a single instruction, clz, to count the zeroed bits. (ARM Reference)
** 15 let lsb = i.trailingZeroBitCount
0x100ed947c <+188>: rbit x9, x8
0x100ed9480 <+192>: clz x9, x9
0x100ed9484 <+196>: mov x10, x9
0x100ed9488 <+200>: str x10, [sp, #0x2d8]

Bitwise operations in Swift, reading values from beacon data

I need some help interpreting a formula. This is from the documentation of a beacon I am experimenting with. I have written it in Swift but I can't get it to work. No matter the values the temperature variable ends up as 0.
From documentation:
*The major ID broadcasts the most significant 8 bits of the humidity and the most significant 8 bits of the temperature, and the
minor ID broadcasts the next 2 bits of temperature (for a total of the 10 most significant bits) and the 14 least significant bits
of the minor ID as the really Minor configured by user.
So the humidity is 8 bits in total, and the temperature is 10 bits in total.
Example:
So the humidity:
uint16_t Humidity = Major(As Hex value) & 0xFF00;
The temperature:
uint16_t temperature = ((Major(As Hex value) & 0x00FF) << 8 ) & ((Minor(As Hex value) &
0xC000) >> 8);
The really Minor:
uint16_t Real Minor = Minor(As Hex value) & 0x03FF;
This is what I came up with and it seems correct but the result from the last bitwise AND returns 0*
let majorAnd = UInt16(beacon.major) & 0x00FF
let majorShift = majorAnd << 8
let minorAnd = UInt16(beacon.minor) & 0xC000
let minorShift = minorAnd >> 8
let temperatureResult = majorShift & minorShift
Your problem is here:
let temperatureResult = majorShift & minorShift
replace it with:
let temperatureResult = majorShift | minorShift
Bitwise AND & is only going to give a result when there are bits in common between the two operands. In your case, they are mutually exclusive, You should combine them with bitwise OR |.
There is also a problem with the way you are shifting the values. Here is the corrected solution:
let majorAnd = UInt16(beacon.major) & 0x00FF
let majorShift = majorAnd << 2 // make space for the last 2 bits
let minorAnd = UInt16(beacon.minor) & 0xC000
let minorShift = minorAnd >> 14 // shift off the unwanted 14 bits
let temperatureResult = majorShift | minorShift
You'll need to shift your humidity as well:
let humidity = UInt16(beacon.major) & 0xFF00 >> 8
In the two shift right >> cases above, as a shortcut, you can skip the masking because those bits are being tossed anyway:
let minorShift = UInt16(beacon.minor) >> 14
let humidity = UInt16(beacon.major) >> 8

Efficiently writing Int16 data to memory in Swift?

I have a memory reference, mBuffers.mData (from an AudioUnit bufferList), declared in the OS X and iOS framework headers as an:
UnsafeMutablePointer<Void>
What is an efficient way to write lots of Int16 values into memory referenced by this pointer?
A disassembly of this Swift source code:
for i in 0..<count {
var x : Int16 = someFastCalculation()
let loByte : Int32 = Int32(x) & 0x00ff
let hiByte : Int32 = (Int32(x) >> 8) & 0x00ff
memset(mBuffers.mData + 2 * i , loByte, 1)
memset(mBuffers.mData + 2 * i + 1, hiByte, 1)
}
shows lots of instructions setting up the memset() function calls (far more instructions than in my someFastCalculation). This is a loop inside a real-time audio callback, so efficient code to minimize latency and battery consumption is important.
Is there a faster way?
This Swift source allows array assignment of individual audio samples to an Audio Unit (or AUAudioUnit) audio buffer, and compiles down to a faster result than using memset.
let mutableData = UnsafeMutablePointer<Int16>(mBuffers.mData)
let sampleArray = UnsafeMutableBufferPointer<Int16>(
start: mutableData,
count: Int(mBuffers.mDataByteSize)/sizeof(Int16))
for i in 0..<count {
let x : Int16 = mySampleSynthFunction(i)
sampleArray[i] = x
}
More complete Gist here .