Copy vector to struct - vector

I have a structure
struct
{
unsigned char data[6]; // switches
unsigned char name[12]; // entry name
unsigned char desc[16]; // entry description
} TOC; // table of contents
and a vector <unsigned char> midiData of 41 bytes, 34 of which represent the values that should fill the above struct TOC, starting from byte n. 6 (0-index).
So I do:
memcpy(&TOC, &midiData[6], 34);
This compiles, but what I get is the expected characters plus unwanted ones. Where's the problem?
EDIT:
vector midiData contains:
240 117 38 9 85 0 1 0 0 0 0 0 84 101 115 116 32 78 97 109 101 32 49 32 84 101 115 116 32 68 101 115 99 114 105 112 116 105 111 110 247
TOC.name contains: Test Name 1
TOC.desc contains: Test Description5èBVÄÿ5¤¦l

Related

Count values greater than x within subsets of a matrix?

I have a matrix (49 rows x 533 columns) and the columns are subsetted into 5 "subtypes".
For each row, I want to count how many values are greater than 1 within each subtype
e.g. if I have subsets A,B,C,D,E: "In row (i) how many of the values in subset A are greater than 1?" and the same for b,c,d and e for every row.
Using tapply() and length() I am able to count the values for each row by subtype:
lengthBySubtype <- function(x) {tapply(x,subtypes,length)}
apply(dataMatrix,1,lengthBySubtype)
My code returns, for each row, the number of values in each subset. Here's a small chunk of the results:
r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r13 r14 r15 r16 r17
A 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111
B 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74
C 195 195 195 195 195 195 195 195 195 195 195 195 195 195 195 195 195
D 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128
E 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25
It's in the exact format I want, but what if I only want to count values that meet a certain condition? (e.g. are greater than 1 in my case). Is there a different function that would work with the apply family for this?
M <- data.matrix( read.table(text=" r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r13 r14 r15 r16 r17
A 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111 111
B 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74 74
C 195 195 195 195 195 195 195 195 195 195 195 195 195 195 195 195 195
D 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128 128
E 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25", head=TRUE))
x=50
Assuming the use of the word "matrix" is referring to an R matrix. then this demonstrates the comment suggestion on the example.
rowSums(M > x)
# A B C D E
#17 17 17 17 0

Send buffer of 288 Uint16 values from One arduino to another using SPI

I am trying to send a buffer of 288 Uint16 values from one Arduino (the main) to a secondary Arduino using SPI between the two. I am trying to send over a buffer of data and then read it on the secondary Arduino and serial print the buffer that I received but I seem to get random values (every time it serial prints they are different values from the buffer). Not sure if it is splitting the uint16 into to separate bytes when it sends over or if I am not reading or writing the SPI values correctly. Is there a way to send over an array of 16 bit values all at once with SPI and then read into a buffer on the second Arduino? I am new to SPI on Arduino so any advice would be helpful.
Primary Arduino Code:
#include<SPI.h>
uint16_t buffer_data[288];
void setup()
{
pinMode(SS, OUTPUT);
digitalWrite(SS, HIGH);
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV32); //2^n = 2, 4, 8, 16, 32
}
void loop()
{
digitalWrite(SS, LOW);
for (uint16_t x = 0; x <sizeof(buffer_data); x++) {
buffer_data[x] = x;
}
SPI.transfer(buffer_data, 288);
digitalWrite(SS, HIGH);
delay(10000);
}
Secondary Arduino:
#include<SPI.h>
uint16_t buffer_new[288];
int pos = 0;
bool flag = false;
void setup()
{
Serial.begin(115200);
SPCR |= _BV(SPE);
SPI.attachInterrupt();
}
ISR (SPI_STC_vect)
{
volatile byte data_new = SPDR;
if (pos < sizeof(buffer_new)) {
buffer_new[pos++] = uint16_t(data_new);
}
else {
flag = true;
}
}
void loop()
{
if (flag) {
for (int x = 0; x <288; x++) {
Serial.println(buffer_new[x]);
}
pos = 0;
flag = false;
}
}
Output:
0
0
1
0
2
0
3
0
4
0
5
0
6
0
7
0
8
0
9
0
10
0
11
0
12
0
13
0
14
0
15
0
16
0
16
1
32
1
48
1
64
1
80
1
96
1
112
1
128
1
144
1
160
1
176
1
192
1
208
1
224
1
240
2
0
2
16
16
1
24
1
32
1
40
1
48
1
56
1
64
1
72
1
80
1
88
1
96
1
64
11
128
11
192
12
0
12
64
12
128
12
192
13
0
13
64
13
128
13
192
0
7
32
7
64
7
96
7
128
7
160
7
192
7
224
8
0
8
32
8
64
8
96
8
128
8
160
8
192
8
224
9
0
36
1
40
1
44
1
48
1
52
1
56
1
60
1
64
1
68
1
72
1
76
1
64
5
80
5
96
5
112
5
128
5
144
5
160
5
176
5
192
5
208
5
224
5
240
6
0
6
16
6
32
6
48
6
64
6
80
6
96
6
112
6
128
6
144
6
160
6
176
6
192
6
208
6
224
6
240
7
0
7
16
7
32
7
48
7
64
7
80
7
96
7
112
7
128
7
144
7
160
7
96
15
128
15
160
15
192
15
224
16
0
16
32
16
64
16
96
16
128
16
160
16
0
67
128
68
0
68
128
69
0
69
128
70
0
70
128
71
0
71
128
72
0
72
128

How do I create SHA256 HMAC using Ironclad in Common Lisp?

There is a python function I am trying to port to Common Lisp:
HEX(HMAC_SHA256(apiSecret, 'stupidstupid'))
How do I go about this with Ironclad?
The closest I've come is:
(ironclad:make-hmac apiSecret :sha256)
But it's not working; it says that apiSecret
The value "V0mn1LLQIc6GNSiBpDfDmRo3Ji8leBZWqMIolNBsnaklScgI"
is not of type
(SIMPLE-ARRAY (UNSIGNED-BYTE 8) (*)).
Ironclad internally works with arrays of bytes.
But it provides tools to convert from ascii strings to such arrays and from bytes to "hex" strings. Here is an interactive session (note that I don't know much about crypto algorithms):
CL-USER> (in-package :ironclad)
#<PACKAGE "IRONCLAD">
Converting the secret:
CRYPTO> (ascii-string-to-byte-array "V0mn1LLQIc6GNSiBpDfDmRo3Ji8leBZWqMIolNBsnaklScgI")
#(86 48 109 110 49 76 76 81 73 99 54 71 78 83 105 66 112 68 102 68 109 82 111
51 74 105 56 108 101 66 90 87 113 77 73 111 108 78 66 115 110 97 107 108 83
99 103 73)
Building the HMAC from previous value:
CRYPTO> (make-hmac * :sha256)
#<HMAC(SHA256) {1006214D93}>
Now, I am not sure this is what you want, but according to the documentation, you are supposed to update the hmac with one or more sequences:
CRYPTO> (update-hmac * (ascii-string-to-byte-array "stupidstupid"))
#<HMAC(SHA256) {1006214D93}>
... and then compute a digest:
CRYPTO> (hmac-digest *)
#(178 90 228 244 244 45 109 163 51 222 77 235 244 173 249 208 144 43 116 130
210 188 62 247 145 153 100 198 119 86 207 163)
The resulting array can be converted to an hex string:
CRYPTO> (byte-array-to-hex-string *)
"b25ae4f4f42d6da333de4debf4adf9d0902b7482d2bc3ef7919964c67756cfa3"
For completeness, here is how you could wrap those functions to replicate the original code, assuming you are in a package that imports the right symbols:
(defun hex (bytes)
(byte-array-to-hex-string bytes))
(defun hmac_sha256 (secret text)
(let ((hmac (make-hmac (ascii-string-to-byte-array secret) :sha256)))
(update-hmac hmac (ascii-string-to-byte-array text))
(hmac-digest hmac)))
Finally:
(HEX (HMAC_SHA256 "V0mn1LLQIc6GNSiBpDfDmRo3Ji8leBZWqMIolNBsnaklScgI"
"stupidstupid"))
=> "b25ae4f4f42d6da333de4debf4adf9d0902b7482d2bc3ef7919964c67756cfa3"

R2OpenBUGS - several problems with matrices, list and vectors

I am new to R2OpenBUGS and the very enigmatic errors are quite frustrating.
I try to run a model that is quite simple. I had success running similar models before.
Are my problems from the fact that I have a 2-dimensional array (matrix) ?
I tried simplifying the model without success.
Here are the errors:
model is syntactically correct
expected the collection operator c error pos 11
model compiled
expected a number or an NA error pos 1449
initial values generated, model initialized
model is updating
200 updates took 0 s
tau.0 is not a variable in the model
tau.1 is not a variable in the model
model is updating
****** Sorry something went wrong in procedure StdMonitor.Update in module DeviancePlugin ******
And here is the code I use
rm(list=ls(all=TRUE))
cat("\014")
library(R2OpenBUGS)
rat.dat<- read.table("BigRatDat.txt",header=FALSE);
dose = data.matrix(rat.dat[1])
weight = data.matrix(rat.dat[3:13])
N<- length(dose);
cat("
model{
for(i in 1:50){
for(j in 1:11){
weight[i,j]~dnorm(mu[i,j],tau[i])
mu[i,j]<-b.0[i]+b.1[i]*j
}
b.0[i]~dnorm(mu.0[i],tau.0)
b.1[i]~dnorm(mu.1[i],tau.1)
mu.0[i] <-b.00+b.01*dose[i]
mu.1[i] <-b.00+b.01*dose[i]
tau[i]~dgamma(0.01,0.01)
dose[i]~dnorm(0,1)
}
b.00~dnorm(0,0.001)
b.01~dnorm(0,0.001)
b.10~dnorm(0,0.001)
b.11~dnorm(0,0.001)
tau.0~dgamma(0.01,0.01)
tau.1~dgamma(0.01,0.01)
}
",file="Rats2OpenBugs.txt")
data <- list("dose","weight")
inits <- function(){
b.0<-rnorm(n=N,0);
b.1<-rnorm(n=N,0);
b.00<-rnorm(1,0);
b.01<-rnorm(1,0);
b.10<-rnorm(1,0);
b.11<-rnorm(1,0);
tau = rep(1,N);
tau.0 = 1;
tau.1 = 1;
list(b.0=b.0,b.1=b.1,b.00=b.00,b.01=b.01,b.10=b.10,b.11=b.11,tau=tau,tau.0=1,tau.1=1)
}
params <- c("b.0","b.1","b.00","b.01","b.10","b.11","tau","tau.0","tau.1");
output.sim <- bugs(data,inits,params,model.file="Rats2OpenBugs.txt",
n.chains=1, n.iter=5000, n.burnin=200, n.thin=1
,debug=TRUE)
A Datafile:
0 1 54 60 63 74 77 89 93 100 108 114 124
0 2 69 75 81 90 97 120 114 119 126 138 143
0 3 77 81 87 94 101 110 117 124 134 141 151
0 4 64 69 77 83 88 96 104 109 120 123 131
0 5 51 58 62 71 74 81 88 93 99 103 113
0 6 64 71 77 89 90 100 106 114 122 134 139
0 7 80 91 97 101 111 119 129 131 137 147 154
0 8 79 85 89 99 104 105 116 121 132 139 147
0 9 77 82 88 92 101 109 119 127 135 144 158
0 10 79 84 91 98 107 114 119 131 137 146 155
.5 1 62 71 75 79 87 91 100 105 111 121 124
.5 2 68 73 81 89 94 101 110 114 123 132 139
.5 3 94 102 109 110 128 133 147 151 153 171 184
.5 4 81 90 95 102 109 120 128 137 141 154 160
.5 5 64 69 72 76 84 89 97 103 108 114 124
.5 6 67 74 81 81 84 95 100 109 119 128 130
.5 7 73 80 86 89 97 101 110 116 117 135 141
.5 8 71 74 82 84 93 97 102 113 119 124 131
.5 9 69 74 79 89 94 100 107 113 124 134 139
.5 10 60 62 67 74 78 85 92 103 112 121 130
1 1 59 63 66 75 80 87 99 104 110 115 124
1 2 56 66 70 81 77 88 96 100 113 120 130
1 3 71 77 84 80 97 106 111 109 128 133 140
1 4 59 64 69 76 85 88 96 104 110 119 126
1 5 65 70 73 77 85 92 96 101 111 118 121
1 6 61 69 77 81 89 92 107 111 118 127 132
1 7 80 86 95 99 106 113 127 131 142 150 160
1 8 74 80 84 90 99 101 108 117 126 133 140
1 9 71 79 88 90 98 102 116 121 127 139 142
1 10 69 75 80 86 96 97 104 113 122 129 138
The problem was that I was trying to use a matrix with only one column as a vector. R has no problem with that but it does not work when exporting the data to OpenBUGS. The program expects references to a matrix to have 2 indices (for line and column).
I just had to replace:
dose = data.matrix(rat.dat[1])
with:
dose = unlist(as.vector(rat.dat[1]))

Create a for loop which prints every number that is x%%3=0 between 1-200

Like the title says I need a for loop which will write every number from 1 to 200 that is evenly divided by 3.
Every other method posted so far generates the 1:200 vector then throws away two thirds of it. What a waste. In an attempt to be eco-conscious, this method does not waste any electrons:
seq(3,200,by=3)
You don't need a for loop, use match function instead, as in:
which(1:200 %% 3 == 0)
[1] 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81
[28] 84 87 90 93 96 99 102 105 108 111 114 117 120 123 126 129 132 135 138 141 144 147 150 153 156 159 162
[55] 165 168 171 174 177 180 183 186 189 192 195 198
Two other alternatives:
c(1:200)[c(F, F, T)]
c(1:200)[1:200 %% 3 == 0]

Resources