Tesla's 90 minute embedded C / firmware coding challenge:
- Macro for incrementing based on a condition
#define INC_COND(COND, VAR) (COND ? (VAR + 1) : VAR)
- Swap Nibbles in a byte
unsigned char swapNibbles(unsigned char x) {
unsigned char res = (x & 0x0F) << 4 | (x & 0xF0) >> 4;
return res;
}
- Debug
uint16_t voltage; // interrupt can modify
uint16_t current; // interrupt can modify
uint8_t returnPower() {
return voltage * current;
}
a) Volatile declaration needed for variables
b) Return variable should be uint32_t
- Memory
██████████████████████████████████████
███████████████████████████████
██████████████████████████████████
███████████████████████████
██████████████████
████████████████████████████████
██████████████████████████████
████████████████████████
█████████████████████████████
████████████████████████████████
█████████████████████████
██████████████████████
█████████████████
███████████████
███████████████
█████████████████████████████████████████████████████████████████
█████████████████████████████████████████████████████████████████
██████████████████████████████████████████
█████████████████████████████
████████████████████████████████
██████████████████
███████████████
███████████████
███████████████
███████████████████████████████
████████████████████████
███████████████████████████████████████████
████████████████████████████████
█████████████████████████
█████████████████████
████████████████
███████████████
████████████████
█████████████████
█████████████████
████████████████████████
█████████████████████████████████
███████████████████████████
██████████████████████
███████████████
███████████████
███████████████
████████████████████████
██████████████████████████████
████████████████████████████████
█████████████████████
████████████████
███████████████
███████████████████████████████████████████████████████████████
████████████████████████████████████████
████████████████████████████
████████████████████████████████████
█████████████████████████████████████████████████████████████████
███████████████████████████████
██████████████████████████████████████████████████████
███████████████████████████████████████████████████
███████████████████████████