Skip to content

Commit d47bc1a

Browse files
zelenskiCS107E BOT
authored andcommitted
wip sensor lecture
commit 6c666d9a6941fbba922c9710cedd66d7a3b0729b Author: Julie Zelenski <[email protected]> Date: Sat Nov 16 22:13:14 2024 -0800 wip sensor lecture
1 parent 86fa1d6 commit d47bc1a

File tree

5 files changed

+47
-16
lines changed

5 files changed

+47
-16
lines changed

cs107e/include/rand.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,26 @@
44
/*
55
* Module to generate random numbers.
66
*
7-
* Author: Philip Levis <[email protected]>
87
* Author: Pat Hanrahan <[email protected]>
98
*/
109

1110
/*
1211
* `rand`
1312
*
1413
* Generate a 32-bit random number. Uses a psuedo-random
15-
* generator with a fixed seed.
14+
* generator with a fixed seed by default.
1615
*
1716
* @return pseudo-random value in the range 0 to UINT_MAX
1817
*/
1918
unsigned int rand(void);
2019

20+
/*
21+
* `srand`
22+
*
23+
* Use argument as seed for sequence of pseudo-random numbers to be
24+
* returned by rand(). Sequence is repeatable by calling srand()
25+
* with the same seed value.
26+
*/
27+
void srand(unsigned int seed);
28+
2129
#endif

cs107e/lib/libmango.a

4.31 KB
Binary file not shown.

cs107e/src/rand.c

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,47 @@
11
/*
22
* Pseudorandom number generator
33
*
4-
* Author: Philip Levis <[email protected]>
54
* Author: Pat Hanrahan <[email protected]>
5+
* Author: Julie Zelenski <[email protected]>
66
*/
77

88
#include "rand.h"
9+
#include <stdbool.h>
10+
#include <stdint.h>
911

10-
// From http://stackoverflow.com/questions/1167253/implementation-of-rand
11-
unsigned int rand(void) {
12-
static unsigned int z1 = 12345, z2 = 12345, z3 = 12345, z4 = 12345;
13-
unsigned int b;
12+
static struct {
13+
bool initialized;
14+
uint32_t z1, z2, z3, z4;
15+
} module = {
16+
.initialized = false,
17+
};
1418

15-
b = ((z1 << 6) ^ z1) >> 13;
16-
z1 = ((z1 & 4294967294U) << 18) ^ b;
17-
b = ((z2 << 2) ^ z2) >> 27;
18-
z2 = ((z2 & 4294967288U) << 2) ^ b;
19-
b = ((z3 << 13) ^ z3) >> 21;
20-
z3 = ((z3 & 4294967280U) << 7) ^ b;
21-
b = ((z4 << 3) ^ z4) >> 12;
22-
z4 = ((z4 & 4294967168U) << 13) ^ b;
23-
return (z1 ^ z2 ^ z3 ^ z4);
19+
unsigned int rand(void) {
20+
if (!module.initialized) {
21+
srand(12345);
22+
}
23+
// http://stackoverflow.com/questions/1167253/implementation-of-rand
24+
// LFSR113 from L'écuyer
25+
uint32_t b;
26+
b = ((module.z1 << 6) ^ module.z1) >> 13;
27+
module.z1 = ((module.z1 & 4294967294U) << 18) ^ b;
28+
b = ((module.z2 << 2) ^ module.z2) >> 27;
29+
module.z2 = ((module.z2 & 4294967288U) << 2) ^ b;
30+
b = ((module.z3 << 13) ^ module.z3) >> 21;
31+
module.z3 = ((module.z3 & 4294967280U) << 7) ^ b;
32+
b = ((module.z4 << 3) ^ module.z4) >> 12;
33+
module.z4 = ((module.z4 & 4294967168U) << 13) ^ b;
34+
return (module.z1 ^ module.z2 ^ module.z3 ^ module.z4);
2435
}
36+
37+
void srand(unsigned int seed) {
38+
// initial factors must be >= 2, 8, 16, and 128 respectively
39+
// this just lifts all to at least 128 to satisfy requirement
40+
seed <<= 1;
41+
if (seed < 128) seed += 128;
42+
module.z1 = seed;
43+
module.z2 = seed;
44+
module.z3 = seed;
45+
module.z4 = seed;
46+
module.initialized = true;
47+
}

readings/i2c.pdf

1.33 MB
Binary file not shown.

readings/spi.pdf

447 KB
Binary file not shown.

0 commit comments

Comments
 (0)