SHIELD-Malb41k1/slave/SPISlave.c

192 lines
3.9 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <avr/io.h>
#include "SPISlave.h"
#include "head_oled_i2c.h"
static int index = 0;
static int arIndex = 0;
static char data[255];
void SPI_SlaveInit(void)
{
DDR_SPI = (1 << DD_MISO);
SPCR = (1 << SPE) | (1 << SPIE);
}
void SetCommand(char *data2, int length){
char command = data2[0];
// Отрезать 1 элемент от оставшихся
switch (command){
case 1:
AllClearCommand(&data2[1], length-1);
break;
case 2:
SetPageCommand(&data2[1], length-1);
break;
case 3:
AddSymbolCommand(&data2[1], length-1);
break;
case 4:
DelSymbolCommand(&data2[1], length-1);
break;
case 5:
DrawPixelCommand(&data2[1], length-1);
break;
case 6:
DrawLineCommand(&data2[1], length-1);
break;
case 7:
DrawCircleCommand(&data2[1], length-1);
break;
case 8:
DrawRectangleCommand(&data2[1], length-1);
break;
case 9:
DrawCharCommand(&data2[1], length-1);
break;
}
}
// command 1
void AllClearCommand(char *symbols, int lenght){
int param = symbols[0];
if (param == 0){
Fill(0);
update();
}
else{
Fill(255);
update();
}
ClearPages();
}
// command 2
void SetPageCommand(char *symbols, int lenght){
SetPage(symbols[0]);
update();
}
// command 3
void AddSymbolCommand(char *symbols, int lenght) {
for (int i = 0; i < lenght - 1; i++) {
AddSymbol(symbols[i]);
}
update();
}
// command 4
void DelSymbolCommand(char *symbols, int lenght) {
for (int i = 0; i < symbols[0]; i++) {
DelSymbol();
}
update();
}
// command 5
void DrawPixelCommand(char *symbols, int lenght){
int x = symbols[0];
int y = symbols[1];
int color = symbols[2];
DrawPixel(x, y, color);
update();
}
// command 6
void DrawLineCommand(char *symbols, int lenght){
int x = symbols[0];
int y = symbols[1];
int x1 = symbols[2];
int y1 = symbols[3];
int color = symbols[4];
DrawLine(x, y, x1, y1, color);
update();
}
// command 7
void DrawCircleCommand(char *symbols, int lenght){
int x = symbols[0];
int y = symbols[1];
int r = symbols[2];
// white on black color=1, fill=0
int color = symbols[3];
int fill = symbols[4];
DrawCircle(x, y, r, color, fill);
update();
}
// command 8
void DrawRectangleCommand(char *symbols, int lenght){
int x = symbols[0];
int y = symbols[1];
int width = symbols[2];
int height = symbols[3];
// white on black color=1, fill=0
int color = symbols[4];
int fill = symbols[5];
DrawRect(x, y, width, height, color, fill);
update();
}
// command 9
void DrawCharCommand(char *symbols, int lenght){
int x = symbols[0];
int y = symbols[1];
int charIndex = symbols[2];
int fill = symbols[3];
// white on black color=1, fill=0
DrawChar(x, y, charIndex, fill);
update();
}
// Функция для вычисления контрольной суммы XOR
char CRC8(char *data, int length) {
char crc = 0x00;
char poly = 0x07; // полином для CRC8
for (int i = 0; i < length; i++) {
crc ^= data[i]; // XOR текущего байта с crc
for (int j = 0; j < length; j++) {
if (crc & 0x80) { // если старший бит crc равен 1
crc = (crc << 1) ^ poly; // сдвигаем crc на 1 бит влево и XOR с полиномом
} else {
crc <<= 1; // иначе просто сдвигаем на 1 бит влево
}
}
}
return crc;
}
char crc8(char *data, int len) {
char crc = 0x00;
while (len--) {
crc ^= *data++;
for (int i = 0; i < 8; i++) {
if (crc & 0x80) {
crc = (crc << 1) ^ 0x07;
} else {
crc <<= 1;
}
}
}
return crc;
}
// Проверка массива на ноль
char checkArray(char *arr, int size) {
for (int i = 0; i < size; i++) {
if (arr[i] != 0) {
return 1;
}
}
return 0;
}