Stepping through Multi-dimensional Array - c

I have a piece of code that runs on an embedded system. Its job is to convert some ASCII characters into proprietary data. The data is stored in a multi-dimensional array and what appears to be the problem, although I am unable to confirm this with the hardware debugger, is that the bit variable is staying at value 2. This code works the first two times it is run, but on the third run it breaks and returns starts sending wrong data through the UART interface. I though maybe someone else analyzing this might be able to see what I'm missing. This is C99 which I am not too familiar with. Bleow is the entire function, but I think the problem is with the for statement?? Any help would be greatly appreciated!
void simple_uart_putstring(uint8_t *str, uint16_t length)
{
//send data bits
uint_fast8_t index = 0;
uint8_t ch = str[index++];
uint_fast8_t bitCount = 0;
int index2 = 0;
int bit = 0;
if (length > 1)
{
while (length >= index)
{
if (bitCount < 2)
{
if (length < 10)
{
//send sync bits
simple_uart_put(254);
simple_uart_put(223);
bitCount = 2;
} else {
//send sync bits and add scrolling
simple_uart_put(254);
simple_uart_put(222);
bitCount = 2;
}
}
//send each bit for each letter in the string
for (uint_fast8_t i = 0; i < 5; i++)
{
index2 = (int)ch;
bit = (int)i;
simple_uart_put(matrix[index2 - 32][bit]);
bitCount++;
}
ch = str[index++];
}
//the main controller is expecting 150 bits total to continue to send bit until 150
while (bitCount <= 150)
{
simple_uart_put(0);
bitCount++;
if(bitCount >= 150)
{
bitCount = 0;
break;
}
}
bitCount = 0;
}
}
And here is a sample of the array:
const uint8_t matrix[59][5] =
{
{ 0, 0, 0, 0, 0}, //space
{ 0, 125, 0, 0, 0}, //!
{ 0, 112, 0, 112, 0}, //"
{127, 127, 127, 127, 127 }, //#
{ 18, 42, 107, 36, 0}, //$
{ 50, 52, 22, 38, 0}, //%
{ 38, 89, 57, 6, 9}, //&
{ 64, 48, 0, 0, 0}, //'
{ 0, 0, 62, 65, 0}, //(
{ 0, 65, 62, 0, 0}, //)
{ 20, 8, 62, 8, 20}, //*
{ 0, 8, 28, 8, 0}, //+
{ 1, 6, 0, 0, 0}, //,
{ 0, 8, 8, 8, 0}, //-
{ 3, 3, 0, 0, 0}, //.
{ 2, 4, 8, 16, 32}, //
{ 62, 69, 73, 62, 0}, //0
{ 1, 33, 127, 1, 0}, //1
{ 35, 67, 69, 49, 0}, //2
{ 34, 73, 73, 54, 0}, //3
{ 12, 20, 36, 127, 0}, //4
{ 114, 81, 81, 78, 0}, //5
{ 30, 41, 73, 6, 0}, //6
{ 64, 71, 72, 112, 0}, //7
{ 54, 73, 73, 54, 0}, //8
{ 48, 73, 74, 60, 0}, //9
{ 0, 54, 54, 0, 0}, //:
{ 0, 1, 54, 0, 0}, //;
{ 0, 8, 20, 34, 0}, //<
{ 0, 20, 20, 20, 0}, //=
{ 0, 34, 20, 8, 0}, //>
{ 32, 64, 69, 72, 48}, //?
{ 62, 65, 93, 93, 112}, //#
{ 63, 72, 72, 63, 0 }, //a
{ 127, 73, 73, 54, 0 }, //b
{ 62, 65, 65, 34, 0}, //c
{ 127, 65, 34, 28, 0 }, //d
{ 127, 73, 73, 65, 0}, //e
{ 127, 72, 72, 64, 0}, //f
{ 62, 65, 73, 47, 0}, //g
{ 127, 8, 8, 127, 0}, //h
{ 0, 65, 127, 65, 0},//i
{ 6, 65, 126, 64, 0}, //j
{ 127, 8, 20, 99, 0}, //k
{ 127, 1, 1, 1, 0}, //l
{ 127, 32, 24, 32, 127}, //m
{ 127, 16, 8, 127, 0}, //n
{ 62, 65, 65, 62, 0}, //o
{ 127, 72, 72, 48, 0}, //p
{ 60, 70, 66, 61, 0}, //q
{ 127, 76, 74, 49, 0}, //r
{ 50, 73, 73, 38, 0}, //s
{ 0, 64, 127, 64, 0}, //t
{ 126, 1, 1, 126, 0},
{ 127, 1, 2, 124, 0},
{ 126, 1, 6, 1, 126},
{ 99, 28, 28, 99, 0},
{ 112, 8, 8, 127, 0},
{ 71, 73, 81, 97, 0}};
And the uart send method:
void simple_uart_put(uint8_t cr)
{
NRF_UART0->TXD = (uint8_t)cr;
while (NRF_UART0->EVENTS_TXDRDY!=1)
{
// Wait for TXD data to be sent
}
NRF_UART0->EVENTS_TXDRDY=0;
}
An example of this working would be if the input string is "AB" and the length = 2;
It should send the following bytes via UART:
{254, 223, 63, 72, 72, 63, 0, 127, 73, 73, 54, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, etc....}
The first two bytes are sync bytes, the next five come from the array matrix[33][0 to 4] because ASCII 'A' = 65 and 65-32 = 33. Then the next five come from ASCII 'B' = 66 and 66 - 32 = 34 so they are sent from matrix[34][0 to 4]. Then the next n = 150 - bitNumber are sent as 0 because the main controller is expecting 150 bytes always.

See edit at bottom
Without having definitions for all functions, I cannot completely analyze this, however there are a couple of suspicious things:
1) this declaration is odd:
uint_fast8_t index = 0;
uint8_t ch = str[index++]; //always sets ch to first character of input "str" then increments index.
NOTE: corrected comment on previous line.
2) although the comment indicates "each bit for each letter in the string" it only handle 5:
for (uint_fast8_t i = 0; i < 5; i++){...} //what if lenth of input is less than 5?
Suggest changing to:
for (uint_fast8_t i = 0; i < length; i++){...} //used second argument "length"
3) Finally, it seems that null terminating the string should follow the for loop, but: (see comments in line) (Also, input arguments used were "abc", 4)
for (uint_fast8_t i = 0; i < 5; i++)
{
index2 = (int)ch; //ch inits to first char in str, and indexes through
bit = (int)i;
//simple_uart_put(matrix[index2 - 32][bit]);
bitCount++;
}
ch = str[index++]; //terminates in second character of input "b' in this case
//I think this should null terminate with '\0' if it is to be treated as a C string,
//but because, as you say, this is "proprietary", I am not sure.
EDIT
I think the problem may be that you have declared the variable matrix with what looks like sufficient room, but only initialized it with three rows of data:
const uint8_t matrix[59][5] =
{
{ 0, 0, 0, 0, 0}, //space
{ 0, 125, 0, 0, 0}, //!
{ 0, 112, 0, 112, 0}, //"
}; //have only initialized matrix[0], matrix[1] and matrix[2]
The remaining 53 rows of data, although owned by you, have not been initialized that I can see. So, when you say:
the next five come from the array matrix[33][0 to 4] because ASCII 'A' = 65 and 65-32 = 33. Then the next five come from ASCII 'B' = 66 and 66 - 32 = 34 so they are sent from matrix[34][0 to 4]
That suggests what ever random number happens to be occupying those uninitialized memory locations, are what is being written out in the line:
simple_uart_put(matrix[index2 - 32][bit]);
EDIT 2 (See comment to explain)
Here is my main(), and commented simple_uart_putstring():
int main()
{
uint8_t *str;
int len=3;
str = malloc(3); //extra char for terminating null byte
strcpy(str, "AB");
simple_uart_putstring(str, 2);
free(str);
}
void simple_uart_putstring(uint8_t *str, uint16_t length)
{
//send data bits
uint_fast8_t index = 0;
uint8_t ch = str[index++]; //ch inits to first char in str, and indexes through
uint_fast8_t bitCount = 0;
int index2 = 0;
int bit = 0;
if (length > 1)
{
while (length >= index)
{
if (bitCount < 2)
{
if (length < 10)
{
//send sync bits
//simple_uart_put(254);
//simple_uart_put(223);
bitCount = 2;
} else {
//send sync bits and add scrolling
//simple_uart_put(254);
//simple_uart_put(222);
bitCount = 2;
}
}
//send each bit for each letter in the string
for (uint_fast8_t i = 0; i < 5; i++)
{
index2 = (int)ch; //
bit = (int)i;
/*simple_uart_put(*/matrix[index2 - 32][bit];//);break here to view "matrix[index2 - 32][bit]"
bitCount++;
}
ch = str[index++]; //
}
//the main controller is expecting 150 bits total to continue to send bit until 150
while (bitCount <= 150)
{
//simple_uart_put(0);
bitCount++;
if(bitCount >= 150)
{
bitCount = 0;
break;
}
}
bitCount = 0;
}
}

Related

how to extract data, convert it to ascii and push it in a list in javascript

I receive data from an external device in decimal values, that I need to convert in ascii and then push it in a list
data example:
#onData: 1,3,200,
78,69,84,71,69,65,82,45,71,117,101,115,116,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
70,114,105,103,111,109,97,116,45,49,50,51,52,53,54,55,56,45,87,73,70,73,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
77,73,67,82,79,45,83,89,83,84,69,77,83,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
77,73,67,82,79,45,83,89,83,84,69,77,83,45,71,85,69,83,84,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
77,73,67,82,79,45,83,89,83,84,69,77,83,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,64,123
The first 3 elements of #onData need to be ignored and the last 2 elements as well.
In ascii it would be:
NETGEAR-Guest
Frigomat-12345678-WIFI
MICRO-SYSTEMS
MICRO-SYSTEMS-GUEST
MICRO-SYSTEMS
I have a method to convert:
arrayBuffer2str(buf) {
var str = "";
var ui8 = new Uint8Array(buf);
for (var i = 0; i < ui8.length; i++) {
str = str + String.fromCharCode(ui8[i]);
}
return str;
}
I would need to get rid of the zeros and then push it in an array of strings:
this.wifiNetworks.push("Network-1");
Thank you
If you want to extract the buffer between two zeros and isolate the significant response, try this
let prevVal;
let phrase= [];
for (let i=0; i<array.length; i++) {
let val = array[i];
if (val == 0 && prevVal != 0) {
// end of response
process(phrase);
}
if (prevVal == 0 && val != 0) {
// start new response
phrase= [];
}
phrase.push(val);
prevVal = val;
}
function process(phrase) {
// TODO Process your phrase here and remove the inner zeros
}
Also, consider removing your zeros using the .filter function
Try this,
Assuming that we are getting the input as an array.
const inputArray = [1, 3, 200,
78, 69, 84, 71, 69, 65, 82, 45, 71, 117, 101, 115, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
70, 114, 105, 103, 111, 109, 97, 116, 45, 49, 50, 51, 52, 53, 54, 55, 56, 45, 87, 73, 70, 73, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77, 73, 67, 82, 79, 45, 83, 89, 83, 84, 69, 77, 83, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77, 73, 67, 82, 79, 45, 83, 89, 83, 84, 69, 77, 83, 45, 71, 85, 69, 83, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77, 73, 67, 82, 79, 45, 83, 89, 83, 84, 69, 77, 83, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 123];
const resultArray = [];
let stringIndex = 0;
for (i = 3; i <= inputArray.length - 3; i++) {
if (inputArray[i] !== 0) {
resultArray[stringIndex] = resultArray[stringIndex]
? resultArray[stringIndex].concat(String.fromCharCode(inputArray[i]))
: ''.concat(String.fromCharCode(inputArray[i]));
} else if (inputArray[i - 1] !== 0) {
stringIndex = stringIndex + 1
}
}
console.log(resultArray);
// Add code to push the result to server,
The output would be
[ 'NETGEAR-Guest', 'Frigomat-12345678-WIFI', 'MICRO-SYSTEMS',
'MICRO-SYSTEMS-GUEST', 'MICRO-SYSTEMS' ]

How to reshape a 2-D linearized Array in Scala without loops

I would like to know how to reshape a 2-D linearized Array in Scala without loops, i.e. by using functional programming.
Assume a 2-D Array of 6x12 = 72 elements. What I need is to resize this Array so that it has 8x16 = 128 elements, assuming that the new elements are initialized to zero.
This is how I do it with loops:
val a = (1 to 72).toArray
a: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72)
scala> val h = 6
h: Int = 6
scala> val w = 12
w: Int = 12
scala> val H = 8
H: Int = 8
scala> val W = 16
W: Int = 16
scala> val b = Array.ofDim[Int](H * W)
b: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
for (i <- 0 until h)
for (j <- 0 until w)
b(i * W + j) = a(i * w + j)
b: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 0, 0, 0, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 0, 0, 0, 0, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 0, 0, 0, 0, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 0, 0, 0, 0, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 0, 0, 0, 0, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
The question is: how to do this by using functional programming?
Thank you very much!
a.grouped(w).map(_.padTo(W, 0)).padTo(H, Array.fill(W)(0)).flatten.toArray
Result (and string copied from your code, for comparison):
result : Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 0, 0, 0, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 0, 0, 0, 0, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 0, 0, 0, 0, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 0, 0, 0, 0, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 0, 0, 0, 0, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
b: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, 0, 0, 0, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 0, 0, 0, 0, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 0, 0, 0, 0, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 0, 0, 0, 0, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 0, 0, 0, 0, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)

How to split byte array into 4 bytes and convert result to float?

I am currently developing a display application for F1 2017 on my iPhone using swift. I am able to read the UDP packets from my PS4 to my phone by using SwiftSockets.
I received the following data:
[25, 79, 3, 64, 176, 153, 15, 61, 244, 133, 165, 69, 0, 20, 145, 192, 223, 145, 218, 194, 165, 106, 64, 64, 15, 88, 234, 67, 220, 222, 109, 59, 168, 219, 67, 187, 42, 245, 4, 59, 200, 163, 186, 57, 65, 126, 46, 63, 22, 207, 77, 59, 13, 81, 59, 191, 127, 79, 59, 191, 0, 73, 203, 187, 141, 126, 46, 191, 180, 81, 37, 190, 5, 254, 41, 189, 251, 167, 151, 62, 145, 185, 39, 62, 7, 210, 92, 63, 203, 186, 128, 192, 218, 25, 99, 64, 112, 21, 0, 191, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 63, 24, 106, 72, 61, 239, 89, 49, 187, 0, 0, 192, 64, 191, 199, 133, 69, 0, 0, 0, 0, 0, 0, 144, 65, 0, 80, 195, 72, 0, 80, 195, 72, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 63, 75, 253, 209, 66, 0, 0, 210, 66, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 80, 182, 232, 65, 80, 182, 232, 65, 80, 182, 232, 65, 80, 182, 232, 65, 0, 0, 172, 65, 0, 0, 172, 65, 0, 0, 184, 65, 0, 0, 184, 65, 0, 0, 0, 64, 0, 0, 198, 66, 57, 170, 165, 69, 128, 93, 205, 66, 0, 128, 84, 70, 0, 96, 134, 69, 0, 0, 16, 65, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 252, 68, 240, 253, 176, 66, 131, 119, 113, 188, 93, 249, 95, 187, 28, 146, 43, 59, 116, 163, 23, 188, 87, 87, 88, 88, 0, 0, 0, 0, 5, 54, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 37, 200, 88, 12, 67, 0, 0, 255, 20, 0, 146, 115, 234, 194, 88, 249, 59, 64, 95, 94, 231, 67, 128, 93, 205, 66, 176, 153, 15, 61, 24, 134, 204, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 192, 122, 64, 100, 2, 18, 7, 5, 0, 0, 0, 0, 245, 73, 129, 193, 65, 222, 85, 64, 134, 169, 0, 68, 32, 234, 205, 66, 224, 188, 92, 67, 94, 87, 203, 66, 0, 0, 0, 0, 0, 0, 0, 0, 62, 112, 162, 69, 34, 2, 15, 3, 5, 1, 2, 0, 0, 134, 253, 181, 65, 69, 178, 83, 64, 5, 6, 10, 68, 136, 6, 200, 66, 70, 194, 141, 67, 180, 236, 199, 66, 0, 0, 0, 0, 0, 0, 0, 0, 17, 208, 160, 69, 16, 0, 5, 3, 5, 1, 2, 0, 0, 16, 137, 185, 195, 194, 6, 44, 64, 211, 20, 153, 66, 64, 15, 198, 66, 238, 232, 27, 61, 64, 15, 198, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 208, 253, 67, 0, 1, 1, 4, 5, 0, 0, 0, 0, 83, 13, 149, 65, 96, 78, 84, 64, 101, 6, 9, 68, 170, 16, 200, 66, 82, 86, 44, 68, 212, 184, 199, 66, 0, 0, 0, 0, 0, 0, 0, 0, 75, 252, 160, 69, 6, 1, 3, 3, 5, 1, 2, 0, 0, 247, 239, 27, 68, 227, 16, 124, 64, 33, 229, 220, 67, 171, 9, 202, 66, 0, 0, 0, 0, 171, 9, 202, 66, 0, 0, 0, 0, 0, 0, 0, 0, 86, 90, 110, 69, 7, 11, 11, 2, 5, 0, 2, 0, 0, 91, 113, 167, 67, 92, 188, 60, 64, 223, 10, 42, 68, 58, 73, 206, 66, 0, 0, 0, 0, 58, 73, 206, 66, 0, 0, 0, 0, 0, 0, 0, 0, 214, 218, 146, 69, 18, 5, 19, 3, 5, 0, 2, 0, 0, 235, 33, 44, 68, 26, 50, 118, 64, 22, 232, 74, 68, 64, 189, 200, 66, 0, 0, 0, 0, 64, 189, 200, 66, 0, 0, 0, 0, 0, 0, 0, 0, 190, 18, 132, 69, 5, 6, 7, 3, 5, 0, 2, 0, 0, 63, 156, 38, 68, 27, 127, 136, 64, 55, 132, 5, 68, 48, 100, 203, 66, 0, 0, 0, 0, 48, 100, 203, 66, 0, 0, 0, 0, 0, 0, 0, 0, 12, 190, 116, 69, 35, 7, 16, 3, 5, 0, 2, 0, 0, 214, 118, 54, 196, 239, 49, 70, 64, 111, 224, 185, 195, 176, 46, 202, 66, 200, 232, 13, 61, 176, 46, 202, 66, 0, 0, 0, 0, 0, 0, 0, 0, 128, 229, 135, 68, 3, 7, 13, 4, 5, 0, 0, 0, 0, 122, 220, 252, 67, 157, 82, 60, 64, 88, 205, 89, 68, 208, 106, 202, 66, 0, 0, 0, 0, 208, 106, 202, 66, 0, 0, 0, 0, 0, 0, 0, 0, 213, 35, 138, 69, 14, 11, 14, 2, 5, 0, 2, 0, 0, 43, 225, 15, 68, 97, 160, 103, 64, 244, 178, 187, 67, 216, 10, 202, 66, 0, 0, 0, 0, 216, 10, 202, 66, 0, 0, 0, 0, 0, 0, 0, 0, 64, 42, 105, 69, 1, 8, 12, 3, 5, 0, 2, 0, 0, 12, 73, 197, 67, 40, 119, 53, 64, 115, 141, 56, 68, 199, 157, 201, 66, 0, 0, 0, 0, 199, 157, 201, 66, 0, 0, 0, 0, 0, 0, 0, 0, 5, 231, 143, 69, 10, 3, 8, 2, 5, 0, 2, 0, 0, 66, 178, 177, 67, 10, 45, 66, 64, 88, 176, 42, 68, 168, 124, 203, 66, 0, 0, 0, 0, 168, 124, 203, 66, 0, 0, 0, 0, 0, 0, 0, 0, 230, 9, 146, 69, 20, 3, 17, 3, 5, 0, 2, 0, 0, 140, 235, 54, 68, 99, 244, 101, 64, 198, 155, 62, 68, 16, 171, 201, 66, 0, 0, 0, 0, 16, 171, 201, 66, 0, 0, 0, 0, 0, 0, 0, 0, 109, 212, 129, 69, 33, 6, 9, 2, 5, 0, 2, 0, 0, 21, 102, 86, 66, 112, 68, 96, 64, 60, 2, 135, 65, 128, 194, 199, 66, 164, 206, 103, 66, 128, 194, 199, 66, 96, 174, 4, 66, 0, 0, 0, 0, 236, 0, 63, 69, 22, 0, 4, 3, 5, 0, 1, 0, 0, 47, 20, 201, 67, 201, 159, 49, 64, 18, 130, 60, 68, 8, 44, 199, 66, 86, 200, 170, 66, 8, 44, 199, 66, 64, 16, 4, 66, 64, 253, 217, 65, 94, 90, 143, 69, 9, 4, 2, 3, 5, 0, 2, 0, 0, 32, 154, 227, 194, 27, 193, 93, 64, 243, 43, 77, 196, 251, 231, 201, 66, 0, 0, 0, 0, 251, 231, 201, 66, 0, 0, 0, 0, 0, 0, 0, 0, 112, 189, 255, 68, 23, 8, 10, 3, 5, 0, 1, 0, 0, 157, 12, 27, 66, 80, 210, 84, 64, 234, 220, 13, 68, 240, 20, 200, 66, 204, 189, 15, 67, 240, 20, 200, 66, 0, 0, 0, 0, 0, 0, 0, 0, 113, 36, 160, 69, 15, 4, 6, 2, 5, 1, 2, 0, 0, 70, 217, 139, 67, 81, 9, 51, 64, 70, 253, 57, 68, 81, 176, 206, 66, 204, 63, 149, 60, 81, 176, 206, 66, 0, 0, 0, 0, 0, 0, 0, 0, 71, 132, 149, 69, 31, 5, 20, 2, 5, 0, 2, 0, 0, 182, 135, 20, 192, 86, 227, 218, 187, 41, 207, 77, 187, 23, 39, 22, 59, 188, 55, 6, 59, 86, 40, 253, 58, 203, 2, 123, 66, 62, 192, 141, 194, 179, 242, 115, 66, 2, 233, 141, 194, 207, 30, 49, 191, 192, 105, 55, 62, 231, 125, 246, 190]
How would I be able to decode the data into a UDPPacket? In order to do this, I think I need to convert the data into four bytes and then somehow convert it to a float.
Here is my swift code:
import UIKit
import SwiftSocket
class ViewController: UIViewController {
var server: UDPServer!
var timer: Timer!
override func viewDidLoad() {
super.viewDidLoad()
server = UDPServer(address: "192.168.1.158", port: 20777)
timer = Timer.scheduledTimer(withTimeInterval: 0, repeats: true) { _ in
print("a")
let data = self.server.recv(1289).0
if let data = data {
print(data)
self.timer.invalidate()
}
}
timer.fire()
}
}
This is what the UDPPacket should look like:
struct UDPPacket
{
float m_time;
float m_lapTime;
float m_lapDistance;
float m_totalDistance;
float m_x; // World space position
float m_y; // World space position
float m_z; // World space position
float m_speed; // Speed of car in MPH
float m_xv; // Velocity in world space
float m_yv; // Velocity in world space
float m_zv; // Velocity in world space
float m_xr; // World space right direction
float m_yr; // World space right direction
float m_zr; // World space right direction
float m_xd; // World space forward direction
float m_yd; // World space forward direction
float m_zd; // World space forward direction
float m_susp_pos[4]; // Note: All wheel arrays have the order:
float m_susp_vel[4]; // RL, RR, FL, FR
float m_wheel_speed[4];
float m_throttle;
float m_steer;
float m_brake;
float m_clutch;
float m_gear;
float m_gforce_lat;
float m_gforce_lon;
float m_lap;
float m_engineRate;
float m_sli_pro_native_support; // SLI Pro support
float m_car_position; // car race position
float m_kers_level; // kers energy left
float m_kers_max_level; // kers maximum energy
float m_drs; // 0 = off, 1 = on
float m_traction_control; // 0 (off) - 2 (high)
float m_anti_lock_brakes; // 0 (off) - 1 (on)
float m_fuel_in_tank; // current fuel mass
float m_fuel_capacity; // fuel capacity
float m_in_pits; // 0 = none, 1 = pitting, 2 = in pit area
float m_sector; // 0 = sector1, 1 = sector2, 2 = sector3
float m_sector1_time; // time of sector1 (or 0)
float m_sector2_time; // time of sector2 (or 0)
float m_brakes_temp[4]; // brakes temperature (centigrade)
float m_tyres_pressure[4]; // tyres pressure PSI
float m_team_info; // team ID
float m_total_laps; // total number of laps in this race
float m_track_size; // track size meters
float m_last_lap_time; // last lap time
float m_max_rpm; // cars max RPM, at which point the rev limiter will kick in
float m_idle_rpm; // cars idle RPM
float m_max_gears; // maximum number of gears
float m_sessionType; // 0 = unknown, 1 = practice, 2 = qualifying, 3 = race
float m_drsAllowed; // 0 = not allowed, 1 = allowed, -1 = invalid / unknown
float m_track_number; // -1 for unknown, 0-21 for tracks
float m_vehicleFIAFlags; // -1 = invalid/unknown, 0 = none, 1 = green, 2 = blue, 3 = yellow, 4 = red
float m_era; // era, 2017 (modern) or 1980 (classic)
float m_engine_temperature; // engine temperature (centigrade)
float m_gforce_vert; // vertical g-force component
float m_ang_vel_x; // angular velocity x-component
float m_ang_vel_y; // angular velocity y-component
float m_ang_vel_z; // angular velocity z-component
byte m_tyres_temperature[4]; // tyres temperature (centigrade)
byte m_tyres_wear[4]; // tyre wear percentage
byte m_tyre_compound; // compound of tyre – 0 = ultra soft, 1 = super soft, 2 = soft, 3 = medium, 4 = hard, 5 = inter, 6 = wet
byte m_front_brake_bias; // front brake bias (percentage)
byte m_fuel_mix; // fuel mix - 0 = lean, 1 = standard, 2 = rich, 3 = max
byte m_currentLapInvalid; // current lap invalid - 0 = valid, 1 = invalid
byte m_tyres_damage[4]; // tyre damage (percentage)
byte m_front_left_wing_damage; // front left wing damage (percentage)
byte m_front_right_wing_damage; // front right wing damage (percentage)
byte m_rear_wing_damage; // rear wing damage (percentage)
byte m_engine_damage; // engine damage (percentage)
byte m_gear_box_damage; // gear box damage (percentage)
byte m_exhaust_damage; // exhaust damage (percentage)
byte m_pit_limiter_status; // pit limiter status – 0 = off, 1 = on
byte m_pit_speed_limit; // pit speed limit in mph
float m_session_time_left; // NEW: time left in session in seconds
byte m_rev_lights_percent; // NEW: rev lights indicator (percentage)
byte m_is_spectating; // NEW: whether the player is spectating
byte m_spectator_car_index; // NEW: index of the car being spectated
// Car data
byte m_num_cars; // number of cars in data
byte m_player_car_index; // index of player's car in the array
CarUDPData m_car_data[20]; // data for all cars on track
float m_yaw; // NEW (v1.8)
float m_pitch; // NEW (v1.8)
float m_roll; // NEW (v1.8)
float m_x_local_velocity; // NEW (v1.8) Velocity in local space
float m_y_local_velocity; // NEW (v1.8) Velocity in local space
float m_z_local_velocity; // NEW (v1.8) Velocity in local space
float m_susp_acceleration[4]; // NEW (v1.8) RL, RR, FL, FR
float m_ang_acc_x; // NEW (v1.8) angular acceleration x-component
float m_ang_acc_y; // NEW (v1.8) angular acceleration y-component
float m_ang_acc_z; // NEW (v1.8) angular acceleration z-component
};
CarUDPData:
struct CarUDPData
{
float m_worldPosition[3]; // world co-ordinates of vehicle
float m_lastLapTime;
float m_currentLapTime;
float m_bestLapTime;
float m_sector1Time;
float m_sector2Time;
float m_lapDistance;
byte m_driverId;
byte m_teamId;
byte m_carPosition; // UPDATED: track positions of vehicle
byte m_currentLapNum;
byte m_tyreCompound; // compound of tyre – 0 = ultra soft, 1 = super soft, 2 = soft, 3 = medium, 4 = hard, 5 = inter, 6 = wet
byte m_inPits; // 0 = none, 1 = pitting, 2 = in pit area
byte m_sector; // 0 = sector1, 1 = sector2, 2 = sector3
byte m_currentLapInvalid; // current lap invalid - 0 = valid, 1 = invalid
byte m_penalties; // NEW: accumulated time penalties in seconds to be added
};
If it helps, I am at the start of a Grand Prix (not career), racing as Lewis Hamilton at Australia and the starting lights have not gone off yet.
Here is a link to the UDP data specifications for F1 2017.

How to print the next index of the same element in array

I'm writing a function that finds two identical elements in an array and then prints the value of their index. I can't seem to get the right second index. What's my problem?
code:
the function I'm troubled with called 'couplesSearch' and it returns its
value to 'printCouples'.line 139
#include <stdio.h>
#include <stdlib.h>
#define NUM_GUESTS 200
#define FALSE 0
#define TRUE 1
#define KIDS_AGE 12
float averageAge(int ages[], int size);
int meals(int guestAges[], int size);
int search(int guestAges[], int size);
void print(int condition);
int coupleSearch(int guestAges[], int size);
void printCouples(int size, int condition2, int ageFriend);
int main(void)
{
int guestAges[NUM_GUESTS] = {42, 108, 95, 101, 90, 5, 79, 79, 83, 105, 66, 66, 2, 28, 2, 12, 116, 63, 28, 37,
112, 85, 63, 34, 53, 23, 22, 117, 39, 96, 48, 7, 12, 19, 70, 113, 108, 20, 116,
55, 24, 52, 3, 94, 34, 105, 22, 32, 54, 29, 108, 45, 23, 118, 118, 20, 84, 22,
50, 59, 77, 36, 111, 43, 49, 107, 41, 63, 65, 89, 87, 46, 51, 10, 11, 111, 7, 22,
34, 69, 70, 24, 85, 35, 37, 81, 47, 57, 12, 29, 25, 40, 27, 44, 18, 59, 39, 43,
10, 102, 34, 36, 80, 19, 25, 91, 100, 27, 114, 67, 102, 66, 45, 113, 31, 70, 18,
94, 58, 73, 107, 91, 42, 37, 36, 48, 16, 95, 72, 53, 111, 71, 22, 5, 47, 71, 28,
72, 8, 58, 98, 48, 34, 64, 66, 30, 50, 39, 102, 109, 63, 107, 27, 71, 94, 9,
61, 72, 43, 96, 11, 120, 25, 18, 69, 4, 116, 82, 3, 111, 92, 117, 15, 101, 37, 22,
109, 40, 109, 5, 2, 55, 54, 80, 19, 99, 61, 69, 8, 108, 9, 14, 49, 44, 48, 22,
31, 18, 14, 35};
int size = 0;
printf("average is: %.2f\n", averageAge(guestAges,size));
printf("Number of kids 12 and under: %d\n" , meals(guestAges, size));
print(search(guestAges,size));
coupleSearch(guestAges,size);
/*printf("Two guests with age 16 at index %d and index %d");
printf("Youngest guest age: ");
printf("Oldest guest age: ");*/
return 0;
}
/*
Function will return average age of party guests
input: age array, number of guests
output: average age
*/
float averageAge(int guestAges[], int size)
{
float sum = 0;
float avg = 0;
for(size = 0; size < NUM_GUESTS; ++size)
{
sum += guestAges[size];
}
avg = sum / NUM_GUESTS;
return avg;
}
int meals(int guestAges[], int size)
{
int kids = 0;
for (size = 0; size < NUM_GUESTS; size++)
{
if (guestAges[size] == KIDS_AGE || guestAges[size] < KIDS_AGE)
{
kids++;
}
}
return kids;
}
int search(int guestAges[], int size)
{
int condition = 0;
int age = 0;
printf("Enter age to search: ");
scanf("%d", &age);
while (size < NUM_GUESTS && age != guestAges[size])
{
size++;
}
if (size < NUM_GUESTS)
{
condition = TRUE;
} else
{
condition = FALSE;
}
return condition;
}
void print(int condition)
{
if (condition == TRUE)
{
printf("Guest found!\n");
}
else if (condition == FALSE)
{
printf("No guest this age.\n");
}
}
int coupleSearch(int guestAges[], int size)
{
int condition2 = 0;
int ageFriend = 0;
printf("Enter age of guest looking for friend: ");
scanf("%d", &ageFriend);
while (size < NUM_GUESTS && ageFriend != guestAges[size])
{
size++;
}
if (size < NUM_GUESTS)
{
condition2 = TRUE;
} else {
condition2 = FALSE;
}
printCouples(size,condition2,ageFriend);
return condition2;
}
void printCouples(int size, int condition2, int ageFriend)
{
if (condition2 == TRUE)
{
printf("Two guest with age %d at index %d and index %d\n", ageFriend, size+1, size+2);
}
else if (condition2 == FALSE)
{
printf("No guest this age.\n");
}
}
Thanks.
Your test assumes that there are two entries in the age array. That is, the age of the guest looking for a friend is not unique and that the array has been sorted. The array that you pass in is not sorted by age. Thus, size+2 will not contain the same age as size+1.
First sort your age array and then search for the first and last entry of that age in the array.
If you do not want to sort the array, then you would have to test every entry in the array and keep track of matching indexes.
You also exit coupleSearch() as soon as you have found the first match. You need to search for the second match separately if you do not want to sort and search for first and last match.
Since you print size+1, then your printed answer is one based.
You only search for the first match. In other words - you never search for a couple.
Maybe you should try something like:
void printCouples(int size, int condition2, int ageFriend)
{
int j = size+1; // Start after the first match
int found = 0;
while (j < NUM_GUESTS )
{
if (ageFriend == guestAges[j])
{
found = 1;
printf("Two guest with age %d at index %d and index %d\n", ageFriend, size, j);
}
}
if (!found)
{
printf("No guest this age.\n");
}
}
Notice that condition2 isn't used anymore.

How to decrease counter in for loop?

I'm using the array shuffle function from here: http://iosdevelopertips.com/swift-code/swift-shuffle-array-type.html.
On this line:
for var index = array.count - 1; index > 0; index -= 1
in the code below
func shuffleArray<T>( arrayparam: Array<T>) -> Array<T>
{
var array = arrayparam
for var index = array.count - 1; index > 0; index -= 1
{
// Random int from 0 to index-1
let j = Int(arc4random_uniform(UInt32(index-1)))
// Swap two array elements
// Notice '&' required as swap uses 'inout' parameters
swap(&array[index], &array[j])
}
return array
}
Swift throws this warning:
C-style for statement is deprecated and will be removed in a future
version of Swift
There isn't any recommendation of what should be used here. Any ideas what should replace it?
take a look at http://bjmiller.me/post/137624096422/on-c-style-for-loops-removed-from-swift-3
only decrease by 1:
for i in (0...n).reverse() {
}
or
decrease by more steps:
for i in someNum.stride(through: 0, by: -2) {
}
More info: there are two versions for stride: through and to. Difference is <= and >= for through, while < and > for to, depending on what you need.
func shuffle<T>(array: Array<T>) -> Array<T> {
var result = array
for index in array.indices.reverse() {
// generate random swapIndex and add a where clause
// to make sure it is not equal to index before swaping
guard
case let swapIndex = Int(arc4random_uniform(UInt32(array.count - index))) + index
where index != swapIndex
else { continue }
swap(&result[index], &result[swapIndex])
}
return result
}
var arrInt = Array(1...100)
shuffle(arrInt) // [28, 19, 25, 53, 35, 60, 14, 62, 34, 15, 81, 50, 59, 40, 89, 30, 2, 54, 27, 9, 82, 21, 11, 67, 84, 75, 44, 97, 66, 83, 36, 20, 26, 1, 76, 77, 8, 13, 72, 65, 64, 80, 88, 29, 98, 37, 33, 70, 52, 93, 100, 31, 4, 95, 45, 49, 61, 71, 24, 16, 12, 99, 94, 86, 46, 69, 63, 22, 48, 58, 51, 18, 43, 87, 41, 6, 92, 10, 38, 23, 68, 85, 42, 32, 55, 78, 56, 79, 3, 47, 39, 57, 90, 17, 5, 73, 7, 91, 74, 96]
for var index = array.count - 1; index > 0; index -= 1
Use a reverse range. Form the range and reverse it:
for index in (1..<array.count).reverse
However, as discussed in my answer here, there's a nicer way; I provide a >>> operator so you can say
for index in array.count>>>1
why don't you try:
for var index = array.count - 1; index > 0; index =index-1 ;

Resources