Brian Pearson
/
LPC1768OpacityMeterUSB
Opacity meter with USB interface
Diff: ByteOperations.cpp
- Revision:
- 0:8ce24e6c4de1
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ByteOperations.cpp Tue Jan 05 16:44:57 2016 +0000 @@ -0,0 +1,37 @@ +#include "ByteOperations.h" +#include <stdint.h> + + +/* low index equals low nibble */ +/* Write 32 bit var to 4 bytes */ +void write_32_to_8(int *i, uint8_t dst[], uint32_t src){ + dst[(*i)++] = (src >> 0)&0xFF ; + dst[(*i)++] = (src >> 8)&0xFF ; + dst[(*i)++] = (src >> 16)&0xFF; + dst[(*i)++] = (src >> 24)&0xFF; +} + +/* Write 16 bit var to 2 bytes */ +void write_16_to_8(int *i, uint8_t dst[], uint16_t src){ + dst[(*i)++] = (src >> 0)&0xFF ; + dst[(*i)++] = (src >> 8)&0xFF ; +} + +/* Write 4 bytes to 32 bit var*/ +uint32_t read_8_to_32(int *i, uint8_t *src){ + uint32_t data = 0; + data |= (src[(*i)++] << 0) ; + data |= (src[(*i)++] << 8) ; + data |= (src[(*i)++] << 16); + data |= (src[(*i)++] << 24); + return data; +} + +/* Write 2 bytes to 16 bit var*/ +uint16_t read_8_to_16(int *i, uint8_t *src){ + uint16_t data = 0; + data |= (src[(*i)++] << 0) ; + data |= (src[(*i)++] << 8) ; + return data; +} +