feat(ex01): done
This commit is contained in:
parent
0c3e25f3bb
commit
2c474aff2d
29 changed files with 1704 additions and 0 deletions
87
ex01/src/lib/aht20.c
Normal file
87
ex01/src/lib/aht20.c
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
#include <avr/io.h>
|
||||
#include <util/delay.h>
|
||||
|
||||
#include "lib/aht20.h"
|
||||
#include "lib/i2c.h"
|
||||
#include "lib/uart.h"
|
||||
|
||||
void aht20_print_status(uint8_t status) {
|
||||
uart_sendstring("AHT20: ");
|
||||
uart_sendstring((status & BV(3)) ? "Calibrated" : "Not Calibrated");
|
||||
uart_sendstring(";");
|
||||
uart_sendstring((status & BV(7)) ? "Busy" : "Not Busy");
|
||||
uart_sendstring(";\r\n");
|
||||
}
|
||||
|
||||
// https://datasheet4u.com/pdf/1551700/AHT20.pdf
|
||||
|
||||
t_error aht20_init(void) {
|
||||
ERR_RET(i2c_start());
|
||||
ERR_RET(i2c_write(I2C_ADDR(AHT20, TW_WRITE)));
|
||||
ERR_RET(i2c_write(0xBE));
|
||||
ERR_RET(i2c_stop());
|
||||
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
t_error aht20_trigger(void) {
|
||||
ERR_RET(i2c_start());
|
||||
ERR_RET(i2c_write(I2C_ADDR(AHT20, TW_WRITE)));
|
||||
// trigger measurement command (7.4)
|
||||
ERR_RET(i2c_write(0xAC));
|
||||
ERR_RET(i2c_write(0x33));
|
||||
ERR_RET(i2c_write(0x00));
|
||||
ERR_RET(i2c_stop());
|
||||
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
t_error aht20_read_measure(aht20_reading* out) {
|
||||
uint8_t data[6];
|
||||
|
||||
ERR_RET(i2c_start());
|
||||
ERR_RET(i2c_write(I2C_ADDR(AHT20, TW_READ)));
|
||||
|
||||
for (uint8_t i = 0; i < 5; i++)
|
||||
ERR_RET(i2c_read_ack(&data[i]));
|
||||
|
||||
ERR_RET(i2c_read_nack(&data[5]));
|
||||
// we dont read the checksum
|
||||
|
||||
i2c_stop();
|
||||
|
||||
uint32_t raw_humi = 0;
|
||||
uint32_t raw_temp = 0;
|
||||
|
||||
struct aht20_reading val;
|
||||
|
||||
raw_humi = data[1];
|
||||
raw_humi <<= 8;
|
||||
raw_humi += data[2];
|
||||
raw_humi <<= 4;
|
||||
raw_humi += data[3] >> 4;
|
||||
|
||||
val.humidity = (float)raw_humi / 1048576.0;
|
||||
|
||||
raw_temp = data[3] & 0x0f;
|
||||
raw_temp <<= 8;
|
||||
raw_temp += data[4];
|
||||
raw_temp <<= 8;
|
||||
raw_temp += data[5];
|
||||
|
||||
val.temperature = (float)raw_temp / 1048576.0 * 200.0 - 50.0;
|
||||
|
||||
*out = val;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
t_error aht20_status(uint8_t* out) {
|
||||
uint8_t status;
|
||||
ERR_RET(i2c_start());
|
||||
ERR_RET(i2c_write(I2C_ADDR(AHT20, TW_READ)));
|
||||
ERR_RET(i2c_read_nack(&status));
|
||||
ERR_RET(i2c_stop());
|
||||
|
||||
*out = status & (BV(7) | BV(3));
|
||||
return NO_ERROR;
|
||||
}
|
||||
64
ex01/src/lib/i2c.c
Normal file
64
ex01/src/lib/i2c.c
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
#include "lib/mystd.h"
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <util/twi.h>
|
||||
|
||||
#include "lib/i2c.h"
|
||||
|
||||
static inline t_error i2c_wait(void) {
|
||||
uint16_t o = 0;
|
||||
// wait until done
|
||||
while (!(TWCR & BV(TWINT)) && o < 32768)
|
||||
o++;
|
||||
if (o >= 32768)
|
||||
return ERROR;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
void i2c_init(void) {
|
||||
// clear the status registers
|
||||
TWSR = 0x00;
|
||||
// set the i2c clock speed
|
||||
TWBR = ((F_CPU / I2C_CLOCK) - 16) / 2;
|
||||
}
|
||||
|
||||
t_error i2c_start(void) {
|
||||
// CLR INT | SET MASTER | ENABLE ;
|
||||
TWCR = BV(TWINT) | BV(TWSTA) | BV(TWEN);
|
||||
ERR_RET(i2c_wait());
|
||||
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
t_error i2c_stop(void) {
|
||||
// CLR INT | ENABLE | STOP ;
|
||||
TWCR = BV(TWINT) | BV(TWEN) | BV(TWSTO);
|
||||
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
t_error i2c_write(uint8_t data) {
|
||||
// set data to write
|
||||
TWDR = data;
|
||||
// CLR INT | ENABLE ;
|
||||
TWCR = BV(TWINT) | BV(TWEN);
|
||||
ERR_RET(i2c_wait());
|
||||
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
t_error i2c_read_ack(uint8_t* out) {
|
||||
TWCR = BV(TWINT) | BV(TWEN) | BV(TWEA);
|
||||
ERR_RET(i2c_wait());
|
||||
|
||||
*out = TWDR;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
t_error i2c_read_nack(uint8_t* out) {
|
||||
TWCR = BV(TWINT) | BV(TWEN);
|
||||
ERR_RET(i2c_wait());
|
||||
|
||||
*out = TWDR;
|
||||
return NO_ERROR;
|
||||
}
|
||||
36
ex01/src/lib/milis.c
Normal file
36
ex01/src/lib/milis.c
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#include <avr/interrupt.h>
|
||||
#include <avr/io.h>
|
||||
|
||||
#include "lib/interupt.h"
|
||||
#include "lib/milis.h"
|
||||
#include "lib/mystd.h"
|
||||
#include "lib/timer1.h"
|
||||
#include "lib/timer_global.h"
|
||||
|
||||
static volatile milis_t TIMER_MS = 0;
|
||||
|
||||
void __attribute__((signal, used)) TIMER1_COMPA_vect(void) {
|
||||
TIMER_MS++;
|
||||
}
|
||||
|
||||
// this function will setup timer1 to increment the global variable TIMER_MS every miliseconds
|
||||
// this function will enable interupts
|
||||
void milis_init(void) {
|
||||
my_sei();
|
||||
t1_init_ctc_4(PRESCALER_1024);
|
||||
t1_set_out_mode(TO_A, TOM_00); // do not touch ORC1A
|
||||
t1_set_ocr(TO_A, (F_CPU / PRESCALER_1024 / 1000) - 1);
|
||||
t1_interrupt(TO_A, true);
|
||||
}
|
||||
|
||||
milis_t milis(void) {
|
||||
return TIMER_MS;
|
||||
}
|
||||
|
||||
bool timer_wait(timer_t* timer, milis_t ms) {
|
||||
if (timer->until == 0) {
|
||||
timer->until = milis() + ms;
|
||||
}
|
||||
|
||||
return milis() > timer->until;
|
||||
}
|
||||
52
ex01/src/lib/pca9555.c
Normal file
52
ex01/src/lib/pca9555.c
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
#include "lib/pca9555.h"
|
||||
#include "lib/i2c.h"
|
||||
#include "lib/mystd.h"
|
||||
|
||||
uint8_t ADDR_BITS = 0;
|
||||
|
||||
t_error pca9555_detect_addr(void) {
|
||||
uint8_t val;
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
ADDR_BITS = i;
|
||||
if (!pca9555_read(PCA9555_OUTPUT_PORT0, &val)) {
|
||||
return NO_ERROR;
|
||||
}
|
||||
}
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
t_error pca9555_write(uint8_t reg, uint8_t value) {
|
||||
ERR_RET(i2c_start());
|
||||
ERR_RET(i2c_write(I2C_ADDR(pca9555_addr(), TW_WRITE))); // write mode
|
||||
ERR_RET(i2c_write(reg));
|
||||
ERR_RET(i2c_write(value));
|
||||
ERR_RET(i2c_stop());
|
||||
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
t_error pca9555_read(uint8_t reg, uint8_t* out) {
|
||||
uint8_t val;
|
||||
|
||||
ERR_RET(i2c_start());
|
||||
ERR_RET(i2c_write(I2C_ADDR(pca9555_addr(), TW_WRITE)));
|
||||
ERR_RET(i2c_write(reg));
|
||||
|
||||
ERR_RET(i2c_start()); // repeated start
|
||||
ERR_RET(i2c_write(I2C_ADDR(pca9555_addr(), TW_READ)));
|
||||
ERR_RET(i2c_read_nack(&val));
|
||||
|
||||
ERR_RET(i2c_stop());
|
||||
|
||||
*out = val;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
t_error pca9555_write_masked(uint8_t reg, uint8_t value, uint8_t mask) {
|
||||
uint8_t val;
|
||||
|
||||
ERR_RET(pca9555_read(reg, &val));
|
||||
ERR_RET(pca9555_write(reg, (val & ~mask) | (value & mask)));
|
||||
|
||||
return NO_ERROR;
|
||||
}
|
||||
87
ex01/src/lib/pcf8563.c
Normal file
87
ex01/src/lib/pcf8563.c
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
// https://tronixstuff.com/2013/08/13/tutorial-arduino-and-pcf8563-real-time-clock-ic/
|
||||
// https://www.nxp.com/docs/en/data-sheet/PCF8563.pdf
|
||||
|
||||
#include "lib/pcf8563.h"
|
||||
#include "lib/i2c.h"
|
||||
#include "lib/uart.h"
|
||||
#include "lib/utils.h"
|
||||
|
||||
t_error pcf8563_read_date(pcf8563_date* out) {
|
||||
uint8_t bytes[7] = {0};
|
||||
pcf8563_date ret;
|
||||
ERR_RET(i2c_start());
|
||||
ERR_RET(i2c_write(I2C_ADDR(PCF8563address, TW_WRITE)));
|
||||
ERR_RET(i2c_write(0x2));
|
||||
ERR_RET(i2c_stop());
|
||||
|
||||
ERR_RET(i2c_start());
|
||||
ERR_RET(i2c_write(I2C_ADDR(PCF8563address, TW_READ)));
|
||||
|
||||
for (uint8_t i = 0; i < 6; i++)
|
||||
ERR_RET(i2c_read_ack(&bytes[i]));
|
||||
ERR_RET(i2c_read_nack(&bytes[6]));
|
||||
|
||||
ret.second = bcdToDec(bytes[0] & 0b01111111);
|
||||
ret.minute = bcdToDec(bytes[1] & 0b01111111);
|
||||
ret.hour = bcdToDec(bytes[2] & 0b00111111);
|
||||
ret.dayOfMonth = bcdToDec(bytes[3] & 0b00111111);
|
||||
ret.dayOfWeek = bcdToDec(bytes[4] & 0b00000111);
|
||||
ret.month = bcdToDec(bytes[5] & 0b00011111);
|
||||
ret.year = bcdToDec(bytes[6]) + ((bytes[5] & 0b10000000) ? 1900 : 2000);
|
||||
|
||||
*out = ret;
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
t_error pcf8563_set_date(pcf8563_date date) {
|
||||
ERR_RET(i2c_start());
|
||||
ERR_RET(i2c_write(I2C_ADDR(PCF8563address, TW_WRITE)));
|
||||
ERR_RET(i2c_write(0x2));
|
||||
ERR_RET(i2c_write(decToBcd(date.second)));
|
||||
ERR_RET(i2c_write(decToBcd(date.minute)));
|
||||
ERR_RET(i2c_write(decToBcd(date.hour)));
|
||||
ERR_RET(i2c_write(decToBcd(date.dayOfMonth)));
|
||||
ERR_RET(i2c_write(decToBcd(date.dayOfWeek)));
|
||||
uint8_t month = decToBcd(date.month);
|
||||
|
||||
if (date.year < 2000)
|
||||
month |= 0b10000000; // set century bit for 1900s
|
||||
else
|
||||
month &= 0b01111111; // clear for 2000s
|
||||
|
||||
ERR_RET(i2c_write(month));
|
||||
ERR_RET(i2c_write(decToBcd(date.year % 100)) /*- ((date.year > 1999) ? 2000 : 1900)))*/);
|
||||
ERR_RET(i2c_stop());
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
void pcf8563_print_date(pcf8563_date* date) {
|
||||
if (date->dayOfMonth < 10)
|
||||
uart_tx('0');
|
||||
uart_send_u8(date->dayOfMonth);
|
||||
|
||||
uart_tx('/');
|
||||
|
||||
if (date->month < 10)
|
||||
uart_tx('0');
|
||||
uart_send_u8(date->month);
|
||||
|
||||
uart_tx('/');
|
||||
uart_send_u16(date->year);
|
||||
|
||||
uart_tx(' ');
|
||||
if (date->hour < 10)
|
||||
uart_tx('0');
|
||||
uart_send_u8(date->hour);
|
||||
uart_tx(':');
|
||||
|
||||
if (date->minute < 10)
|
||||
uart_tx('0');
|
||||
uart_send_u8(date->minute);
|
||||
uart_tx(':');
|
||||
if (date->second < 10)
|
||||
uart_tx('0');
|
||||
uart_send_u8(date->second);
|
||||
|
||||
uart_sendstring("\r\n");
|
||||
}
|
||||
45
ex01/src/lib/rgb.c
Normal file
45
ex01/src/lib/rgb.c
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
#include <avr/io.h>
|
||||
#include "lib/mystd.h"
|
||||
#include "lib/timer0.h"
|
||||
#include "lib/timer2.h"
|
||||
|
||||
#define D5_R PD5
|
||||
#define D5_G PD6
|
||||
#define D5_B PD3
|
||||
|
||||
#define RGB_MASK (BV(D5_R) | BV(D5_G) | BV(D5_B))
|
||||
|
||||
void init_rgb(void) {
|
||||
t0_init_fpwm_3(PRESCALER_64);
|
||||
t2_init_fpwm_3(PRESCALER_64);
|
||||
|
||||
t0_set_out_mode(TO_A | TO_B, TOM_00);
|
||||
t2_set_out_mode(TO_B, TOM_00);
|
||||
DDRD |= RGB_MASK;
|
||||
}
|
||||
|
||||
void set_rgb(uint8_t r, uint8_t g, uint8_t b) {
|
||||
if (r == 0x00) {
|
||||
t0_set_out_mode(TO_B, TOM_00);
|
||||
PORTD = (PORTD & ~BV(D5_R));
|
||||
} else {
|
||||
t0_set_out_mode(TO_B, TOM_10);
|
||||
t0_set_ocr(TO_B, r);
|
||||
}
|
||||
|
||||
if (g == 0x00) {
|
||||
t0_set_out_mode(TO_A, TOM_00);
|
||||
PORTD = (PORTD & ~BV(D5_G));
|
||||
} else {
|
||||
t0_set_out_mode(TO_A, TOM_10);
|
||||
t0_set_ocr(TO_A, g);
|
||||
}
|
||||
|
||||
if (b == 0x00) {
|
||||
t2_set_out_mode(TO_B, TOM_00);
|
||||
PORTD = (PORTD & ~BV(D5_B));
|
||||
} else {
|
||||
t2_set_out_mode(TO_B, TOM_10);
|
||||
t2_set_ocr(TO_B, b);
|
||||
}
|
||||
}
|
||||
31
ex01/src/lib/spi.c
Normal file
31
ex01/src/lib/spi.c
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
#include <avr/io.h>
|
||||
|
||||
#include "lib/mystd.h"
|
||||
#include "lib/spi.h"
|
||||
|
||||
void spi_init(void) {
|
||||
// set the correct pins as output
|
||||
SPI_DDR |= BV(CS) | BV(MOSI) | BV(SCK);
|
||||
|
||||
// enable SPI, set as master, and clock to fosc/128
|
||||
SPCR = BV(SPE) | BV(MSTR) | BV(SPR1) | BV(SPR0);
|
||||
}
|
||||
|
||||
void spi_uninit(void) {
|
||||
SPCR = 0;
|
||||
}
|
||||
|
||||
t_error spi_write(uint8_t data) {
|
||||
uint16_t o = 0;
|
||||
|
||||
// load data into register
|
||||
SPDR = data;
|
||||
|
||||
// Wait for transmission complete
|
||||
while (!(SPSR & BV(SPIF)) && o < 32768)
|
||||
o++;
|
||||
if (o >= 32768)
|
||||
return ERROR;
|
||||
|
||||
return NO_ERROR;
|
||||
}
|
||||
139
ex01/src/lib/uart.c
Normal file
139
ex01/src/lib/uart.c
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
#include "lib/uart.h"
|
||||
#include <avr/io.h>
|
||||
#include "lib/mystd.h"
|
||||
#include "lib/utils.h"
|
||||
|
||||
#define BAUD_RATE 115200
|
||||
|
||||
#define UBRR_VALUE ((F_CPU / (8UL * BAUD_RATE)) - 1)
|
||||
|
||||
// uart is 115200 baud rate, 8 bits per word, no parrity and 1 stop bit
|
||||
// 115200 8N1
|
||||
void uart_init(void) {
|
||||
// Set baud rate
|
||||
UBRR0H = (uint8_t)(UBRR_VALUE >> 8);
|
||||
UBRR0L = (uint8_t)(UBRR_VALUE);
|
||||
|
||||
UCSR0A |= BV(U2X0);
|
||||
// Enable transmitter
|
||||
UCSR0B = BV(TXEN0) | BV(RXEN0);
|
||||
|
||||
// Set frame format: 8 data bits, no parity, 1 stop bit
|
||||
UCSR0C = BV(UCSZ01) | BV(UCSZ00);
|
||||
|
||||
// Set TX (PD1) as output
|
||||
DDRD |= BV(PD1);
|
||||
}
|
||||
|
||||
void uart_tx(char data) {
|
||||
// wait for transmit buffer to be empty
|
||||
while (!(UCSR0A & BV(UDRE0)))
|
||||
;
|
||||
// load data into transmit register
|
||||
UDR0 = data;
|
||||
}
|
||||
|
||||
char uart_rx(void) {
|
||||
while (!(UCSR0A & BV(RXC0)))
|
||||
;
|
||||
return UDR0;
|
||||
}
|
||||
|
||||
void uart_sendstring(const char* str) {
|
||||
if (!str)
|
||||
return;
|
||||
while (*str) {
|
||||
uart_tx(*str);
|
||||
str++;
|
||||
}
|
||||
}
|
||||
void uart_send_u8(uint8_t val) {
|
||||
if (val == 0)
|
||||
return uart_tx('0');
|
||||
char buf[4] = {0, 0, 0, 0};
|
||||
uint8_t idx = 0;
|
||||
bool print = false;
|
||||
|
||||
uint8_t modulus = 100;
|
||||
while (modulus) {
|
||||
uint8_t digit = val / modulus;
|
||||
if (print || digit != 0) {
|
||||
print = true;
|
||||
buf[idx++] = '0' + digit;
|
||||
}
|
||||
val %= modulus;
|
||||
modulus /= 10;
|
||||
}
|
||||
|
||||
uart_sendstring(buf);
|
||||
}
|
||||
|
||||
void uart_send_u16(uint16_t val) {
|
||||
if (val == 0)
|
||||
return uart_tx('0');
|
||||
char buf[6] = {0, 0, 0, 0, 0, 0};
|
||||
uint8_t idx = 0;
|
||||
bool print = false;
|
||||
|
||||
uint16_t modulus = 10000;
|
||||
while (modulus) {
|
||||
uint8_t digit = val / modulus;
|
||||
if (print || digit != 0) {
|
||||
print = true;
|
||||
buf[idx++] = '0' + digit;
|
||||
}
|
||||
val %= modulus;
|
||||
modulus /= 10;
|
||||
}
|
||||
uart_sendstring(buf);
|
||||
}
|
||||
|
||||
void uart_send_i16(int16_t val) {
|
||||
if (val == 0)
|
||||
return uart_tx('0');
|
||||
if (val == -32768)
|
||||
return uart_sendstring("-32768");
|
||||
if (val < 0) {
|
||||
uart_tx('-');
|
||||
val = -val;
|
||||
}
|
||||
uart_send_u16(val);
|
||||
}
|
||||
|
||||
void uart_send_u32(uint32_t val) {
|
||||
if (val == 0)
|
||||
return uart_tx('0');
|
||||
char buf[11] = {};
|
||||
uint8_t idx = 0;
|
||||
bool print = false;
|
||||
|
||||
uint32_t modulus = 1000000000;
|
||||
while (modulus) {
|
||||
uint8_t digit = val / modulus;
|
||||
if (print || digit != 0) {
|
||||
print = true;
|
||||
buf[idx++] = '0' + digit;
|
||||
}
|
||||
val %= modulus;
|
||||
modulus /= 10;
|
||||
}
|
||||
uart_sendstring(buf);
|
||||
}
|
||||
|
||||
void uart_send_u8_hex(uint8_t val) {
|
||||
char buf[3] = {0, 0, 0};
|
||||
buf[0] = "0123456789abcdef"[(val >> 4) & 0x0F];
|
||||
buf[1] = "0123456789abcdef"[(val >> 0) & 0x0F];
|
||||
|
||||
uart_sendstring(buf);
|
||||
}
|
||||
|
||||
void uart_send_u16_hex(uint16_t val) {
|
||||
char buf[5] = {0, 0, 0, 0, 0};
|
||||
|
||||
buf[0] = "0123456789abcdef"[(val >> 12) & 0x0F];
|
||||
buf[1] = "0123456789abcdef"[(val >> 8) & 0x0F];
|
||||
buf[2] = "0123456789abcdef"[(val >> 4) & 0x0F];
|
||||
buf[3] = "0123456789abcdef"[(val >> 0) & 0x0F];
|
||||
uart_sendstring(buf);
|
||||
}
|
||||
125
ex01/src/lib/utils.c
Normal file
125
ex01/src/lib/utils.c
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
#include "lib/utils.h"
|
||||
#include "lib/mystd.h"
|
||||
#include "lib/uart.h"
|
||||
|
||||
// this just burns cycles.
|
||||
// the volatile is important, it means that the cpu can't optimize any
|
||||
// read/writes for the value
|
||||
static inline void spin_loop(volatile uint16_t counts) {
|
||||
while (counts)
|
||||
counts--;
|
||||
}
|
||||
|
||||
void delay_ms(uint16_t ms) {
|
||||
while (ms) {
|
||||
// this value was taken using a delay of 500ms, and just recording the led
|
||||
// blinking. it seems to be high enough such that each loop of delay_loop
|
||||
// takes 1ms :D
|
||||
spin_loop((F_CPU) / 5000);
|
||||
ms--;
|
||||
}
|
||||
}
|
||||
|
||||
void ft_bzero(void* data, uint16_t size) {
|
||||
char* d = data;
|
||||
while (size) {
|
||||
*d = 0;
|
||||
d++;
|
||||
size--;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t ft_stridx(const char* str, char chr) {
|
||||
if (!str)
|
||||
return -1;
|
||||
for (uint8_t i = 0; str[i]; i++) {
|
||||
if (str[i] == chr)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static float ft_pow(float base, float count) {
|
||||
float out = 1;
|
||||
|
||||
while (count--)
|
||||
out *= base;
|
||||
return out;
|
||||
}
|
||||
|
||||
// Reverses a string 'str' of length 'len'
|
||||
static void reverse(char* str, uint16_t len) {
|
||||
uint16_t i = 0, j = len - 1, temp;
|
||||
while (i < j) {
|
||||
temp = str[i];
|
||||
str[i] = str[j];
|
||||
str[j] = temp;
|
||||
i++;
|
||||
j--;
|
||||
}
|
||||
}
|
||||
|
||||
static uint16_t ft_itoa(uint32_t value, char* out, uint16_t width) {
|
||||
uint16_t i = 0;
|
||||
if (value == 0) {
|
||||
while (i < width)
|
||||
out[i++] = '0';
|
||||
return i;
|
||||
}
|
||||
while (value) {
|
||||
out[i++] = (value % 10) + '0';
|
||||
value = value / 10;
|
||||
}
|
||||
|
||||
// If number of digits required is more, then
|
||||
// add 0s at the beginning
|
||||
while (i < width)
|
||||
out[i++] = '0';
|
||||
|
||||
// we need to reverse the data
|
||||
reverse(out, i);
|
||||
out[i] = 0;
|
||||
|
||||
return i;
|
||||
}
|
||||
// 0.3250351
|
||||
// Converts a floating-point/double number to a string.
|
||||
uint8_t ft_ftoa(float val, char* out, uint16_t precision) {
|
||||
uint32_t ipart = (uint32_t)val;
|
||||
float fpart = val - (float)ipart;
|
||||
|
||||
// convert integer part to string
|
||||
uint8_t i = ft_itoa(ipart, out, 1);
|
||||
|
||||
if (precision != 0) {
|
||||
out[i] = '.';
|
||||
fpart = fpart * ft_pow(10.f, precision);
|
||||
i += ft_itoa((uint32_t)fpart, &out[i + 1], precision);
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
void* ft_memcpy(void* dest, void* src, uint16_t len) {
|
||||
for (uint16_t i = 0; i < len; i++)
|
||||
((uint8_t*)dest)[i] = ((uint8_t*)src)[i];
|
||||
return dest;
|
||||
}
|
||||
|
||||
bool ft_memcmp(void* s1, void* s2, uint16_t len) {
|
||||
for (uint16_t i = 0; i < len; i++) {
|
||||
if (((uint8_t*)s1)[i] != ((uint8_t*)s2)[i])
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t is_hex_digit(char chr) {
|
||||
uint8_t i;
|
||||
if ((i = ft_stridx("0123456789", chr)) != 255)
|
||||
return i;
|
||||
if ((i = ft_stridx("abcdef", chr)) != 255)
|
||||
return 10 + i;
|
||||
if ((i = ft_stridx("ABCDEF", chr)) != 255)
|
||||
return 10 + i;
|
||||
return 255;
|
||||
}
|
||||
55
ex01/src/main.c
Normal file
55
ex01/src/main.c
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
#include <avr/io.h>
|
||||
#include <util/delay.h>
|
||||
|
||||
#include "lib/adc.h"
|
||||
#include "lib/aht20.h"
|
||||
#include "lib/apa102.h"
|
||||
#include "lib/eeprom.h"
|
||||
#include "lib/i2c.h"
|
||||
#include "lib/milis.h"
|
||||
#include "lib/mystd.h"
|
||||
#include "lib/pca9555.h"
|
||||
#include "lib/pcf8563.h"
|
||||
#include "lib/rgb.h"
|
||||
#include "lib/spi.h"
|
||||
#include "lib/uart.h"
|
||||
#include "lib/utils.h"
|
||||
|
||||
#define tabsize(tab) (sizeof(tab) / sizeof(tab[0]))
|
||||
|
||||
int main(void) {
|
||||
uart_init();
|
||||
i2c_init();
|
||||
pca9555_detect_addr();
|
||||
milis_init();
|
||||
|
||||
timer_t addr_timer = timer_reset();
|
||||
timer_t button_timer = timer_reset();
|
||||
|
||||
uint8_t counter = 0;
|
||||
uint8_t button_state = false;
|
||||
|
||||
pca9555_write(PCA9555_CONFIG_PORT0, 0b00000001);
|
||||
pca9555_write(PCA9555_OUTPUT_PORT1, 0b00001110);
|
||||
|
||||
while (1) {
|
||||
if (timer_wait(&addr_timer, 100)) {
|
||||
addr_timer = timer_reset();
|
||||
pca9555_detect_addr();
|
||||
}
|
||||
|
||||
if (timer_wait(&button_timer, 20)) {
|
||||
button_timer = timer_reset();
|
||||
uint8_t current = true;
|
||||
if (pca9555_read(PCA9555_INPUT_PORT0, (uint8_t*)¤t))
|
||||
uart_sendstring("Failed to read the button (did it change address ?)\r\n");
|
||||
current = !(current & BV(0));
|
||||
|
||||
if (!button_state && current) {
|
||||
counter++;
|
||||
pca9555_write(PCA9555_OUTPUT_PORT0, (((~counter) & 0b00000111) << 1));
|
||||
}
|
||||
button_state = current;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue