Copy project

This commit is contained in:
schmelczerandras 2020-04-19 12:48:06 +02:00
commit 26a0fd884f
60 changed files with 157458 additions and 1 deletions

View file

@ -0,0 +1,130 @@
#include "display.h"
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include "bitwise.h"
#include "math.h"
#include "../../hardware_access/hardware_access.h"
static uint8_t const configuration[] PROGMEM = {
0xD5, 0xF0, // set clock frequency
0x8D, 0x14, // enable charge pump
0x20, 0x00, // horizontal addressing mode
0xD6, 0x01, // 2 times vertical zoom
0x22, 0x00, 0x03, // only draw to the top half of the screen
0xAF // display on
};
static struct {
uint8_t compositingBuffer[DISPLAY_WIDTH_IN_PIXELS];
Rectangle compositingWindow;
DrawFunction drawEverything;
bool wasIntersection;
} display;
static inline void commandMode() {
setOutputPin(DISPLAY_DC_OUTPUT_PIN, false);
}
static inline void dataMode() {
setOutputPin(DISPLAY_DC_OUTPUT_PIN, true);
}
void setDisplayContrast(uint8_t value) {
commandMode();
sendByteOnSPI(0x81);
sendByteOnSPI(value);
dataMode();
}
void initializeDisplay(DrawFunction drawEverything) {
setOutputPin(DISPLAY_RESET_OUTPUT_PIN, false);
for (volatile uint8_t i = 0; i != 255; i++)
;
// some time has to elapse before the next line gets called,
// otherwise the display wont turn on
setOutputPin(DISPLAY_RESET_OUTPUT_PIN, true);
for (uint8_t i = 0; i < sizeof(configuration); i++) {
sendByteOnSPI(pgm_read_byte(configuration + i));
}
display.drawEverything = drawEverything;
setDisplayContrast(255);
dataMode();
}
void startIntersectionTest(Rectangle compositingWindow) {
display.wasIntersection = false;
display.compositingWindow = compositingWindow;
}
bool endIntersectionTest() {
for (uint8_t x = 0; x < DISPLAY_WIDTH_IN_PIXELS; x++) {
display.compositingBuffer[x] = 0;
}
return display.wasIntersection;
}
void drawFrame() {
display.compositingWindow = DEFAULT_COMPOSITING_WINDOW;
for (display.compositingWindow.position.y = 0; display.compositingWindow.position.y < DISPLAY_HEIGHT_IN_PIXELS; display.compositingWindow.position.y += 8) {
display.drawEverything(display.compositingWindow);
for (uint8_t x = 0; x < DISPLAY_WIDTH_IN_PIXELS; x++) {
sendByteOnSPI(display.compositingBuffer[x]);
sendByteOnSPI(display.compositingBuffer[x]);
display.compositingBuffer[x] = 0;
}
}
}
static void compositePixelColumn(uint8_t x, uint8_t invertedMask, uint8_t fill) {
if (display.compositingBuffer[x] & fill) {
display.wasIntersection = true;
}
display.compositingBuffer[x] = (display.compositingBuffer[x] & (~invertedMask)) | fill;
}
void drawBitmapFromProgMem(Rectangle boundingBox, uint16_t const bitmap[boundingBox.size.x][(boundingBox.size.y + 7) / 8], bool isMirrored) {
boundingBox.position = substract(boundingBox.position, display.compositingWindow.position);
uint8_t spriteY = max(0, -boundingBox.position.y);
uint8_t spriteYByte = spriteY >> 3;
for (uint8_t x = max(0, -boundingBox.position.x); x < boundingBox.size.x && boundingBox.position.x + x < DISPLAY_WIDTH_IN_PIXELS; x++) {
uint8_t spriteX = isMirrored ? boundingBox.size.x - x - 1 : x;
uint16_t currentPixelColumn = eeprom_read_word(&bitmap[spriteX][spriteYByte]);
uint8_t fill, invertedMask;
if (boundingBox.position.y >= 0) {
fill = (currentPixelColumn & 0x00FF) << boundingBox.position.y;
invertedMask = currentPixelColumn >> 8 << boundingBox.position.y;
} else {
uint16_t lowerPixelColumn = spriteYByte + 1 < (boundingBox.size.y + 7) / 8 ? eeprom_read_word(&bitmap[spriteX][spriteYByte + 1]) : 0;
uint8_t shift = spriteY % 8;
uint8_t inverseShift = 8 - shift;
fill = ((currentPixelColumn & 0x00FF) >> shift) | ((lowerPixelColumn & 0x0FF) << inverseShift);
invertedMask = (currentPixelColumn >> 8 >> shift) | (lowerPixelColumn >> 8 << inverseShift);
}
compositePixelColumn(boundingBox.position.x + x, invertedMask, fill);
}
}
void drawFilledRectangle(Rectangle box, uint8_t invertedMask, uint8_t fill) {
box.position = substract(box.position, display.compositingWindow.position);
uint8_t upperGapHeight = min(8, max(0, box.position.y));
uint8_t lowerGapHeight = min(8, max(0, 8 - (box.position.y + box.size.y)));
uint8_t actualFill = (fill >> lowerGapHeight) & (fill << upperGapHeight);
uint8_t actualInvertedMask = (invertedMask >> lowerGapHeight) & (invertedMask << upperGapHeight);
for (uint8_t x = max(0, box.position.x); x < box.position.x + box.size.x && x < DISPLAY_WIDTH_IN_PIXELS; x++) {
compositePixelColumn(x, actualInvertedMask, actualFill);
}
}

View file

@ -0,0 +1,35 @@
#ifndef DISPLAY_H
#define DISPLAY_H
#include <stdbool.h>
#include <avr/io.h>
#include "../../util/rectangle/rectangle.h"
#define DISPLAY_RESET_OUTPUT_PIN PB0
#define DISPLAY_DC_OUTPUT_PIN PB4
#define DISPLAY_WIDTH_IN_PIXELS 64
#define DISPLAY_HEIGHT_IN_PIXELS 32
#define WINDOW ((Rectangle){(Vec2){0, 0}, (Vec2){DISPLAY_WIDTH_IN_PIXELS, DISPLAY_HEIGHT_IN_PIXELS}})
#define DEFAULT_COMPOSITING_WINDOW ((Rectangle){(Vec2){0, 0}, (Vec2){DISPLAY_WIDTH_IN_PIXELS, 8}})
typedef void (*DrawFunction)(Rectangle);
void initializeDisplay(DrawFunction drawEverything);
void setDisplayContrast(uint8_t value);
void startIntersectionTest();
bool endIntersectionTest();
void drawFrame();
void drawBitmapFromProgMem(Rectangle boundingBox, uint16_t const bitmap[boundingBox.size.x][(boundingBox.size.y + 7) / 8], bool isMirrored);
// for white rectangle: invertedMask: don't care, fill: 0xFF
// for black rectangle: invertedMask: 0xFF, fill: 0x00
void drawFilledRectangle(Rectangle box, uint8_t invertedMask, uint8_t fill);
#endif

View file

@ -0,0 +1,146 @@
#include "infra.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include "bitwise.h"
#include "../../hardware_access/hardware_access.h"
#include "../uart/receive.h"
// (0.5625 + (0.5625 + 1.6875) / 2) / 1000 / timer interval
#define MEAN_OF_0_1_BIT_TIMES 53
// 9 / 2 / 1000 / timer interval
#define MAYBE_ONE_CHECK_TIME 141
// some large value
#define TIMEOUT 254
typedef enum {
idle, maybeLeadingOne, leadingOneConfirmed, activeFirstBit, active, timingOut
} ProtocolState;
typedef enum {
noMatch, foundStartingByte, significantByte, waitingForEndOfCommand, waitingForRepeat
} CommandState;
static struct {
uint8_t current;
uint8_t bitPosition;
ProtocolState protocolState;
CommandState commandState;
OnCommandReceived onCommandReceived;
} infra;
static void saveCurrentByte() {
uint8_t byte = infra.current;
infra.current = 0;
infra.bitPosition = 0;
if (byte == INFRA_ADDRESS) {
infra.commandState = foundStartingByte;
return;
}
switch (infra.commandState) {
case foundStartingByte:
infra.commandState = significantByte;
break;
case significantByte:
infra.onCommandReceived(byte);
infra.commandState = waitingForEndOfCommand;
break;
case waitingForEndOfCommand:
if (byte == 0) {
infra.commandState = waitingForRepeat;
}
break;
case waitingForRepeat:
if (byte == 0) {
infra.onCommandReceived(REPEAT_CODE);
}
break;
default:
break;
}
}
static void saveBit(uint8_t bit) {
infra.current <<= 1;
infra.current |= bit;
if (++infra.bitPosition == 8) {
saveCurrentByte();
}
}
static inline uint8_t isIrOff() {
return getBit(PINB, IR_PIN);
}
static inline uint8_t isIrOn() {
return !isIrOff();
}
ISR(PCINT0_vect) {
handlePinChangeForUartReceive();
switch (infra.protocolState) {
case idle:
if (isIrOn()) {
enableTimerB(MAYBE_ONE_CHECK_TIME);
infra.protocolState = maybeLeadingOne;
}
break;
case maybeLeadingOne:
infra.protocolState = idle;
break;
case leadingOneConfirmed:
if (isIrOff()) {
infra.protocolState = activeFirstBit;
}
break;
case activeFirstBit:
case timingOut:
if (isIrOn()) {
infra.protocolState = active;
enableTimerB(MEAN_OF_0_1_BIT_TIMES);
}
break;
case active:
if (isIrOn()) {
saveBit(1);
enableTimerB(MEAN_OF_0_1_BIT_TIMES);
}
break;
}
}
ISR(TIM0_COMPB_vect) {
switch (infra.protocolState) {
case maybeLeadingOne:
if (isIrOn()) {
infra.protocolState = leadingOneConfirmed;
}
disableTimerB();
break;
case active:
saveBit(0);
infra.protocolState = timingOut;
enableTimerB(TIMEOUT);
break;
case timingOut:
infra.protocolState = idle;
saveCurrentByte();
disableTimerB();
break;
default:
disableTimerB();
break;
}
}
void initializeInfra(OnCommandReceived onCommandReceived) {
setBit(PORTB, IR_PIN); // enable pull-up
setBit(PCMSK, IR_PIN); // specific pin change interrupt enable
setBit(GIMSK, PCIE); // global on pin change interrupt enable;
infra.onCommandReceived = onCommandReceived;
}

View file

@ -0,0 +1,16 @@
#ifndef INFRA_H
#define INFRA_H
#include <stdbool.h>
#include <avr/io.h>
#define INFRA_ADDRESS 255
#define IR_PIN PB4
#define REPEAT_CODE 1
typedef void (*OnCommandReceived)(uint8_t);
void initializeInfra(OnCommandReceived onCommandReceived);
#endif

View file

@ -0,0 +1,34 @@
#include "sleep.h"
#include <stdbool.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include "bitwise.h"
#include "../../hardware_access/hardware_access.h"
#define TICKS_IN_MILISECOND 31
volatile int8_t milisecondsSinceFrameStart;
void startFrameLoop(FrameFunction function, uint8_t frameLengthInMilliseconds) {
sleep_enable();
uint8_t previousFrameTime = 0;
while (function(previousFrameTime)) {
previousFrameTime = milisecondsSinceFrameStart;
while (milisecondsSinceFrameStart < frameLengthInMilliseconds) {
sleep_cpu();
}
milisecondsSinceFrameStart = 0;
}
}
ISR(TIM0_COMPA_vect) {
milisecondsSinceFrameStart++;
enableTimerA(TICKS_IN_MILISECOND);
}

View file

@ -0,0 +1,13 @@
#ifndef SLEEP_H
#define SLEEP_H
#include <stdbool.h>
#include <avr/io.h>
typedef bool (*FrameFunction)(uint8_t);
// frameFunction gets previousFrameTime (in milliseconds) as argument
void startFrameLoop(FrameFunction function, uint8_t frameLengthInMilliseconds);
#endif

View file

@ -0,0 +1,49 @@
#include <stdbool.h>
#include <avr/interrupt.h>
#include "receive.h"
#include "bitwise.h"
#include "../../hardware_access/hardware_access.h"
static struct {
uint16_t message;
uint8_t bitPosition; // goes from 0 to 9
bool previousValue;
uint8_t previousTimestamp;
OnCommandReceived onCommandReceived;
} uartReceive = {
.previousValue = true
};
void initializeUartReceive(OnCommandReceived onCommandReceived) {
setBit(PORTB, RECEIVE_PIN); // setup pull-up
setBit(PCMSK, RECEIVE_PIN); // enable interrupt on pin
uartReceive.onCommandReceived = onCommandReceived;
}
void handlePinChangeForUartReceive() {
bool currentValue = getBit(PINB, RECEIVE_PIN);
if (uartReceive.previousValue != currentValue) {
uint8_t elapsedBitCount = (getTimeSince(uartReceive.previousTimestamp) + BIT_TIME / 2) / BIT_TIME;
uartReceive.message >>= elapsedBitCount;
uartReceive.bitPosition += elapsedBitCount;
if (uartReceive.bitPosition == 9) {
uartReceive.onCommandReceived((uint8_t)(uartReceive.message));
uartReceive.bitPosition = 0;
}
if (uartReceive.previousValue == true && currentValue == false) {
if (uartReceive.bitPosition != elapsedBitCount) {
uartReceive.message |= (uint8_t)(0xFF << (8 - elapsedBitCount));
} else {
uartReceive.bitPosition = 0;
}
}
uartReceive.previousTimestamp = getTimestampFromFastTimer();
uartReceive.previousValue = currentValue;
}
}

View file

@ -0,0 +1,15 @@
#ifndef RECEIVE_H
#define RECEIVE_H
// 8*10^6 / 256 / 1200
#define BIT_TIME 26
#define RECEIVE_PIN PB3
typedef void (*OnCommandReceived)(uint8_t);
void initializeUartReceive(OnCommandReceived onCommandReceived);
void handlePinChangeForUartReceive();
#endif

View file

@ -0,0 +1,74 @@
#include "transmit.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include "bitwise.h"
#include "../../hardware_access/hardware_access.h"
static struct {
char bytes[ASYNC_BUFFER_SIZE];
uint8_t start;
uint8_t end;
// 0 - start bit, 1-8 word bits, 9 - end bit
uint8_t currentMaskPosition;
} uartTransmitter;
static inline void send0() {
setOutputPin(UART_OUTPUT_PIN, false);
}
static inline void send1() {
setOutputPin(UART_OUTPUT_PIN, true);
}
ISR(TIM1_COMPA_vect) {
enableFastTimerA(BIT_TIME);
switch(uartTransmitter.currentMaskPosition) {
case 0:
send0();
uartTransmitter.currentMaskPosition++;
break;
case 9:
send1();
uartTransmitter.start = (uartTransmitter.start + 1) % ASYNC_BUFFER_SIZE;
if (uartTransmitter.start == uartTransmitter.end) {
disableFastTimerA();
}
uartTransmitter.currentMaskPosition = 0;
break;
default:
uartTransmitter.bytes[uartTransmitter.start] & BV(uartTransmitter.currentMaskPosition - 1) ? send1() : send0();
uartTransmitter.currentMaskPosition++;
}
}
void sendByteOnUartAsync(char byte) {
cli();
uartTransmitter.bytes[uartTransmitter.end] = byte;
if (uartTransmitter.start == uartTransmitter.end) {
enableFastTimerA(BIT_TIME);
}
uartTransmitter.end = (uartTransmitter.end + 1) % ASYNC_BUFFER_SIZE;
sei();
}
void sendTextOnUartAsync(char text[]) {
for (uint8_t i = 0; text[i]; i++) {
sendByteOnUartAsync(text[i]);
}
}
void sendUintOnUartAsync(uint8_t number) {
sendByteOnUartAsync(number >= 100 ? '0' + number / 100 : ' ');
number %= 100;
sendByteOnUartAsync(number >= 10 ? '0' + number / 10 : ' ');
number %= 10;
sendByteOnUartAsync('0' + number);
}
void initializeUartTransmit() {
send1();
}

View file

@ -0,0 +1,18 @@
#ifndef TRANSMIT_H
#define TRANSMIT_H
#include <avr/io.h>
// 8*10^6 / 256 / 1200
#define BIT_TIME 26
#define ASYNC_BUFFER_SIZE 20
#define UART_OUTPUT_PIN 2
void initializeUartTransmit();
void sendByteOnUartAsync(char byte);
void sendTextOnUartAsync(char text[]);
void sendUintOnUartAsync(uint8_t number);
#endif

View file

@ -0,0 +1,15 @@
#ifndef EEPROM_H
#define EEPROM_H
#include <avr/io.h>
#define STORAGE_SIZE 52
typedef void (*OnEEPROMFinished)(uint8_t*);
inline void initializeEEPROM() {
EECR = 0; // atomic write
}
#endif

View file

@ -0,0 +1,12 @@
#include "hardware_access.h"
#include <avr/interrupt.h>
void initializeHardwareAccess() {
sei();
initializePowerSaving();
initializeSPI();
initializeTiming();
initializeOutputPins();
}

View file

@ -0,0 +1,30 @@
#ifndef HARDWARE_ACCESS_H
#define HARDWARE_ACCESS_H
#include <stdbool.h>
#include <avr/io.h>
#include "bitwise.h"
#include "power_saving/power_saving.h"
#include "spi/spi.h"
#include "eeprom/eeprom.h"
#include "timing/timing.h"
#include "output_pins/output_pins.h"
void initializeHardwareAccess();
void enableTimerA(uint8_t triggerInterruptInXTicks);
void enableTimerB(uint8_t triggerInterruptInXTicks);
void disableTimerB();
void enableFastTimerA(uint8_t triggerInterruptInXTicks);
void disableFastTimerA();
uint8_t getTimestampFromFastTimer();
uint8_t getTimeSince(uint8_t timestamp);
void sendByteOnSPI(uint8_t byte);
void setOutputPin(uint8_t id, bool value);
#endif

View file

@ -0,0 +1,19 @@
#include "shift_register_output_pins.h"
#include "bitwise.h"
#include "../../spi/spi.h"
#include <stdbool.h>
static uint8_t currentValue = 0;
void sendCurrentValue() {
clearBit(PORTB, CLK_ST_PIN);
sendByteOnSPI(currentValue);
setBit(PORTB, CLK_ST_PIN);
}
void setOutputPin(uint8_t id, bool value) {
currentValue = (currentValue & ~BV(id)) | (value & 1) << id;
sendCurrentValue();
}

View file

@ -0,0 +1,18 @@
#ifndef SHIFT_REGITER_OUTPUT_PINS_H
#define SHIFT_REGITER_OUTPUT_PINS_H
#include <avr/io.h>
#include "bitwise.h"
#define CLK_ST_PIN PB0
void sendCurrentValue();
inline void initializeOutputPins() {
setBit(DDRB, CLK_ST_PIN);
setBit(PORTB, CLK_ST_PIN);
sendCurrentValue();
}
#endif

View file

@ -0,0 +1,19 @@
#include "output_pins.h"
#include "bitwise.h"
#include "../spi/spi.h"
#include <stdbool.h>
static uint8_t currentValue = 0;
void sendCurrentValue() {
clearBit(PORTB, CLK_ST_PIN);
sendByteOnSPI(currentValue);
setBit(PORTB, CLK_ST_PIN);
}
void setOutputPin(uint8_t id, bool value) {
currentValue = (currentValue & ~BV(id)) | (value & 1) << id;
sendCurrentValue();
}

View file

@ -0,0 +1,18 @@
#ifndef SHIFT_REGITER_OUTPUT_PINS_H
#define SHIFT_REGITER_OUTPUT_PINS_H
#include <avr/io.h>
#include "bitwise.h"
#define CLK_ST_PIN PB0
void sendCurrentValue();
inline void initializeOutputPins() {
setBit(DDRB, CLK_ST_PIN);
setBit(PORTB, CLK_ST_PIN);
sendCurrentValue();
}
#endif

View file

@ -0,0 +1,13 @@
#ifndef POWER_SAVING_H
#define POWER_SAVING_H
#include <avr/io.h>
#include "bitwise.h"
inline void initializePowerSaving() {
setBit(ACSR, ACD); // disable ADC to save power
PRR = /*BV(PRTIM1) |*/ BV(PRADC); // disable power of timer1 and ADC
}
#endif

View file

@ -0,0 +1,12 @@
#include "spi.h"
#include <avr/interrupt.h>
void sendByteOnSPI(uint8_t byte) {
cli();
USIDR = byte;
for (uint8_t i = 16; i != 0; i--) {
USICR = BV(USIWM0) | BV(USICLK) | BV(USITC) | BV(USICS1);
}
sei();
}

View file

@ -0,0 +1,17 @@
#ifndef SPI_H
#define SPI_H
#include <avr/io.h>
#include "bitwise.h"
#define DO_PIN PB1
#define USCK_PIN PB2
inline void initializeSPI() {
DDRB |= BV(DO_PIN) | BV(USCK_PIN); // set pin directions for MOSI and SCK
}
void sendByteOnSPI(uint8_t byte);
#endif

Some files were not shown because too many files have changed in this diff Show more