I have read about variadics. I'm not sure if that's just not the done thing though. Or is that what I should be doing?
I want to do this:
SSD2828_Send_DCS(0xFF, 3, {0x01, 0x81, 0x98});
where FF is the cmd/register, 3 is the data size, and the array is the data.
I realise the normal way would be to store the 3 bytes in an array and pass that, but then it's two lines for each instruction, and I was hoping to have something that looked like a simple list just like the initial-code I've been given by the LCD mfr.
I can think of another couple of options:
1. Overloaded functions. The data is always between 0 and 3 bytes from what I can see. But then I'd be writing the same function 4 times and having to alter/correct 4 separate bits of essentially the same function.
2. Fixing my datasize at 32 bits, and, I was going to say casting down to an 8 bit int if the value is <0xFF. Up until this very moment I thought this was a none-starter as I'd have to write a 1 byte 0x01 as 0x00000001, but that's not true is it. I can just pass 0x01.
My completely untested Send_DCS command, which probably won't work because I've probably screwed up the array pointer passing stuff, looks like this:
void SSD2828_SPI_WriteCmd(uint8_t cmd) {
HAL_GPIO_WritePin(GPIOH, GPIO_PIN_3, GPIO_PIN_RESET);
HAL_SPI_Transmit(&hspi5, &cmd, 1, 100);
}
void SSD2828_SPI_WriteData(uint8_t data) {
HAL_GPIO_WritePin(GPIOH, GPIO_PIN_3, GPIO_PIN_SET);
HAL_SPI_Transmit(&hspi5, &data, 1, 100);
}
void SSD2828_Send_DCS(uint8_t cmd, uint8_t dataSize, uint8_t* data){
SSD2828_SPI_WriteCmd(0xBC);
SSD2828_SPI_WriteData(dataSize);
SSD2828_SPI_WriteData(0x00);
SSD2828_SPI_WriteCmd(0xBF);
SSD2828_SPI_WriteData(cmd);
while (dataSize >0) {
SSD2828_SPI_WriteCmd(0xBF);
SSD2828_SPI_WriteData(data[dataSize-1]);
dataSize--;
}
}
The SPI_WriteCmd and SPI_WriteData do work and the SSD2828 responds to an ID request so that's all good.
I think I'm going to go with the 'uint32_t' data.. that should work, I think.. :-/ then deal with it byte by byte in the send function. Data is never more than 4 bytes.
Update: So I'm sort of thinking like this. Untested. I tried testing in Python but it's tricky.. doesn't do/show bitshifts the same.
void SSD2828_Send_DCS(uint8_t cmd, uint8_t dataSize, uint32_t data){
SSD2828_SPI_WriteCmd(0xBC);
SSD2828_SPI_WriteData(dataSize);
SSD2828_SPI_WriteData(0x00);
SSD2828_SPI_WriteCmd(0xBF);
SSD2828_SPI_WriteData(cmd);
for (int i = 0; i < dataSize; i++) {
SSD2828_SPI_WriteCmd(0xBF);
SSD2828_SPI_WriteData((uint8_t) (data >> 8 * i));
}
}