Brian Pearson
/
LPC1768OpacityMeterUSB
Opacity meter with USB interface
ByteOperations.cpp@0:8ce24e6c4de1, 2016-01-05 (annotated)
- Committer:
- BPPearson
- Date:
- Tue Jan 05 16:44:57 2016 +0000
- Revision:
- 0:8ce24e6c4de1
Opacity meter with USB interface
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
BPPearson | 0:8ce24e6c4de1 | 1 | #include "ByteOperations.h" |
BPPearson | 0:8ce24e6c4de1 | 2 | #include <stdint.h> |
BPPearson | 0:8ce24e6c4de1 | 3 | |
BPPearson | 0:8ce24e6c4de1 | 4 | |
BPPearson | 0:8ce24e6c4de1 | 5 | /* low index equals low nibble */ |
BPPearson | 0:8ce24e6c4de1 | 6 | /* Write 32 bit var to 4 bytes */ |
BPPearson | 0:8ce24e6c4de1 | 7 | void write_32_to_8(int *i, uint8_t dst[], uint32_t src){ |
BPPearson | 0:8ce24e6c4de1 | 8 | dst[(*i)++] = (src >> 0)&0xFF ; |
BPPearson | 0:8ce24e6c4de1 | 9 | dst[(*i)++] = (src >> 8)&0xFF ; |
BPPearson | 0:8ce24e6c4de1 | 10 | dst[(*i)++] = (src >> 16)&0xFF; |
BPPearson | 0:8ce24e6c4de1 | 11 | dst[(*i)++] = (src >> 24)&0xFF; |
BPPearson | 0:8ce24e6c4de1 | 12 | } |
BPPearson | 0:8ce24e6c4de1 | 13 | |
BPPearson | 0:8ce24e6c4de1 | 14 | /* Write 16 bit var to 2 bytes */ |
BPPearson | 0:8ce24e6c4de1 | 15 | void write_16_to_8(int *i, uint8_t dst[], uint16_t src){ |
BPPearson | 0:8ce24e6c4de1 | 16 | dst[(*i)++] = (src >> 0)&0xFF ; |
BPPearson | 0:8ce24e6c4de1 | 17 | dst[(*i)++] = (src >> 8)&0xFF ; |
BPPearson | 0:8ce24e6c4de1 | 18 | } |
BPPearson | 0:8ce24e6c4de1 | 19 | |
BPPearson | 0:8ce24e6c4de1 | 20 | /* Write 4 bytes to 32 bit var*/ |
BPPearson | 0:8ce24e6c4de1 | 21 | uint32_t read_8_to_32(int *i, uint8_t *src){ |
BPPearson | 0:8ce24e6c4de1 | 22 | uint32_t data = 0; |
BPPearson | 0:8ce24e6c4de1 | 23 | data |= (src[(*i)++] << 0) ; |
BPPearson | 0:8ce24e6c4de1 | 24 | data |= (src[(*i)++] << 8) ; |
BPPearson | 0:8ce24e6c4de1 | 25 | data |= (src[(*i)++] << 16); |
BPPearson | 0:8ce24e6c4de1 | 26 | data |= (src[(*i)++] << 24); |
BPPearson | 0:8ce24e6c4de1 | 27 | return data; |
BPPearson | 0:8ce24e6c4de1 | 28 | } |
BPPearson | 0:8ce24e6c4de1 | 29 | |
BPPearson | 0:8ce24e6c4de1 | 30 | /* Write 2 bytes to 16 bit var*/ |
BPPearson | 0:8ce24e6c4de1 | 31 | uint16_t read_8_to_16(int *i, uint8_t *src){ |
BPPearson | 0:8ce24e6c4de1 | 32 | uint16_t data = 0; |
BPPearson | 0:8ce24e6c4de1 | 33 | data |= (src[(*i)++] << 0) ; |
BPPearson | 0:8ce24e6c4de1 | 34 | data |= (src[(*i)++] << 8) ; |
BPPearson | 0:8ce24e6c4de1 | 35 | return data; |
BPPearson | 0:8ce24e6c4de1 | 36 | } |
BPPearson | 0:8ce24e6c4de1 | 37 |