ready to alpha

подготовка к альфа-тестированию
This commit is contained in:
khristolyubov committed 2024-08-19 22:44:04 +07:00
commit e36b851783
164 files changed
+27352

No files matched your search

+122
View File
@@ -0,0 +1,122 @@
#include "Wire.h"
#include "time.h"
// 7-bit RTC address on the I2C bus, specified in the datasheet
#define RTC_ADDR 0x68
#define RTC_DATA_SIZE 7
// Specify 1 to set a new time at initialization
#define RTC_SET_NEW_TIME 0
uint8_t rxBytes[15];
struct tm newTime;
char str[32];
void setup()
{
Serial.begin(115200);
Serial.println("Start I2C rtc");
// Initializing the device as a master on the I2C bus
Wire.begin();
// Set new time if necessary
if (RTC_SET_NEW_TIME)
writeTimeToRtc(); // setting time is written inside the function
}
void loop()
{
// Read and display the current time
readTimeFromRtc();
delay(2000);
}
// ------------------- rtc ------------------- //
// Function to write new time to rtc
void writeTimeToRtc(void)
{
/* The data to be written is located in the common buffer.
First, the register address is specified - writing starts from register 0x00.
Next, the values ​​of seconds, minutes, etc. are listed in accordance with the
order of the corresponding registers, specified in the datasheet for ds1307.
The values ​​are pre-translated into binary-decimal format
*/
// sec min hour wday mday month year
uint8_t buf[] = {0x00, dec2bcd(00), dec2bcd(01), dec2bcd(12), dec2bcd(2), dec2bcd(1), dec2bcd(1), dec2bcd(24)};
Wire.beginTransmission(RTC_ADDR); // Start of transaction
Wire.write(buf, sizeof(buf)); // Writing data for a transaction
uint8_t result = Wire.endTransmission(); // Completing a transaction
// Show results
if (result == 0)
Serial.println("New time successfully set");
else
Serial.println("New time set failed");
}
// Function to read time from rtc
void readTimeFromRtc(void)
{
/* Send a request to receive data from rtc. When reading from rtc, you must
first specify from which register to start reading. To do this, use the extended
version of the requestFrom() function. In the arguments specify:
- device address,
- the number of bytes we want to read,
- the address of the starting register,
- the size of the address of the starting register in bytes,
- 1 - generate a stop condition after the request, 0 - do not generate
*/
Wire.requestFrom(RTC_ADDR, RTC_DATA_SIZE, 0x00, 1, 1);
// Get data
uint8_t i = 0;
while(Wire.available())
rxBytes[i++] = Wire.read();
// Show new time in the port upon successful reading
if (i == RTC_DATA_SIZE)
parseAndShowTime();
else
Serial.println("New time read failed");
}
// Functions for converting binary-decimal code to decimal and back
uint8_t bcd2dec(uint8_t val)
{
return (val >> 4) * 10 + (val & 0x0f);
}
uint8_t dec2bcd(uint8_t val)
{
return ((val / 10) << 4) + (val % 10);
}
// Function for format and output the current time to the port
void parseAndShowTime(void)
{
/* take the values ​​for time output from the received data buffer.
Convert the values ​​into decimal format and adjust them in accordance with
the datasheet for ds1307
*/
newTime.tm_sec = bcd2dec(rxBytes[0] & 0x7f);
newTime.tm_min = bcd2dec(rxBytes[1]);
if (rxBytes[2] & (1 << 6))
{
// RTC in 12 hour mode
newTime.tm_hour = bcd2dec(rxBytes[2] & 0x1f) - 1;
if (rxBytes[2] & (1 << 5))
newTime.tm_hour += 12;
}
else
newTime.tm_hour = bcd2dec(rxBytes[2] & 0x3f);
newTime.tm_wday = bcd2dec(rxBytes[3])-1;
newTime.tm_mday = bcd2dec(rxBytes[4]);
newTime.tm_mon = bcd2dec(rxBytes[5])-1;
// Subtract 1900 so that the year is correctly displayed in the struct tm type structure
newTime.tm_year = bcd2dec(rxBytes[6])+2000-1900;
// The received time is output to the port in the format Sun Aug 19 02:56:02 2012
strftime(str, sizeof(str), "%c", &newTime);
Serial.println(str);
}
@@ -0,0 +1,92 @@
// I2C master
#include <Wire.h>
// Slave address
uint8_t SLAVE_ADDRESS = 0x33;
// Buffers for sending and receiving
uint8_t masterTxData[3] = {0x30, 0x41, 0x51};
uint8_t masterRxData[4] = {0};
// Function for sending data via I2C bus
uint8_t masterSendData(uint8_t* data, uint8_t dataLen)
{
// Initiating work with the bus
Wire.beginTransmission(SLAVE_ADDRESS);
// Send data
Wire.write(data, dataLen);
// Finish working with the bus and return the result
return Wire.endTransmission();
}
// Function to receive a specified number of bytes from the I2C bus
uint8_t masterGetData(uint8_t *buf, uint8_t dataSize)
{
// Request data
Wire.requestFrom(SLAVE_ADDRESS, dataSize);
// If there are bytes available for reading, read them in a loop
uint8_t n = 0;
while (Wire.available())
{
// reading from the bus - from the slave device
buf[n] = Wire.read();
// Increasing the number of received bytes
n++;
}
// Return the number of received bytes
return n;
}
// -------------------------------------------- //
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println("I2C master start");
// Initialize the device on the I2C bus as a master
Wire.begin();
Wire.setClock(400000);
}
void loop()
{
// Send data to slave
Serial.println("------------------------");
Serial.println("Master | Sending 3 bytes");
uint8_t result = masterSendData(masterTxData, sizeof(masterTxData));
// Show result
if (result == 0)
Serial.println("Master | Bytes sended");
else
{
Serial.print("Master | Send ERROR! Code: ");
Serial.println(result);
}
// Change the first byte for variety
masterTxData[0] += 1;
// Delay before requesting data
delay(2000);
// Request data from the slave
Serial.println("------------------------");
Serial.println("Master | Receiving 4 bytes");
uint8_t bytesQty = masterGetData(masterRxData, sizeof(masterRxData));
// Show result
if (bytesQty == 0)
Serial.println("Master | Receive ERROR!");
else
{
for (uint8_t i = 0; i < bytesQty; i++)
{
Serial.print("Master | Received byte: 0x");
Serial.println(masterRxData[i], HEX);
}
}
// Delay before sending data
delay(2000);
}
@@ -0,0 +1,48 @@
// i2c_scanner
#include <Wire.h>
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println("I2C scanner start");
Wire.begin();
}
void loop()
{
int devicesQty = 0;
Serial.println("Scanning...");
for (byte address = 1; address < 127; address++)
{
// transmitting address to I2C bus
Wire.beginTransmission(address);
byte error = Wire.endTransmission();
// Wire.endTransmission() will return 0 only if an ACK is received
// from the subscriber with the current address
if (error == 0)
{
Serial.print("Device found at address 0x");
if (address < 16)
Serial.print("0");
Serial.println(address, HEX);
devicesQty++;
}
delayMicroseconds(1);
}
if (devicesQty == 0)
Serial.println("No device found");
else
{
Serial.print(devicesQty);
Serial.println(" devices found");
}
Serial.println();
delay(5000);
}
@@ -0,0 +1,91 @@
// I2C slave
#include <Wire.h>
// The address on the I2C bus that we will assign to this device
constexpr int MY_ADDRESS = 0x33;
// Buffers for sending and receiving
uint8_t slaveTxData[4] = {0x00, 0x10, 0x22, 0x84};
uint8_t slaveRxData[8] = {0};
// Function for receiving data from the I2C bus
uint8_t slaveGetData(uint8_t *buf, uint8_t bufSize)
{
// If there are bytes available for reading, read them in a loop
uint8_t n = 0;
while (Wire.available())
{
// Read from the bus - from the master device
buf[n] = Wire.read();
// If the entire buffer is filled, stop reading
if (++n == bufSize)
break;
}
// Return the number of received bytes
return n;
}
// Function for sending data via I2C bus
uint8_t slaveSendData(uint8_t* data, uint8_t dataLen)
{
return Wire.write(data, dataLen);
}
// A function that will be called if the master requests data
void requestEvent()
{
Serial.println("------------------------");
Serial.println("Slave | Request event");
// Send data
uint8_t txBytesQty = slaveSendData(slaveTxData, sizeof(slaveTxData));
// Show result
if (txBytesQty == sizeof(slaveTxData))
Serial.print("Slave | Bytes quantity sent: ");
else
Serial.print("Slave | Send ERROR! Bytes quantity sent: ");
Serial.println(txBytesQty);
slaveTxData[0] += 1; // Change the first byte for variety
}
// Function that will be called if the master sends data
void receiveEvent(int bytesNum)
{
Serial.println("------------------------");
Serial.println("Slave | Receive event");
// Get new data
uint8_t rxBytesQty = slaveGetData(slaveRxData, sizeof(slaveRxData));
// Show result
if (rxBytesQty == 0)
Serial.println("Slave | Receive ERROR!");
else
{
for (uint8_t i = 0; i < rxBytesQty; i++)
{
Serial.print("Slave | Received byte: 0x");
Serial.println(slaveRxData[i], HEX);
}
}
}
// ----------------------------------------- //
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println("I2C slave start");
// Start working with I2C as a slave device with the address MY_ADDRESS
Wire.begin(MY_ADDRESS);
// Register an event handler that will be called when data is requested by the master device
Wire.onRequest(requestEvent);
// Register an event handler that will be called after receiving data from the master device
Wire.onReceive(receiveEvent);
}
void loop()
{
// Don't do anything in the loop, everything is implemented in the registered event handlers
}