initial commit

This commit is contained in:
Jonas Leder 2023-06-27 22:39:11 +02:00
commit a0109db75e
7 changed files with 348 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
.pio
.vscode/

39
include/README Normal file
View file

@ -0,0 +1,39 @@
This directory is intended for project header files.
A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.
```src/main.c
#include "header.h"
int main (void)
{
...
}
```
Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.
In C, the usual convention is to give header files names that end with `.h'.
It is most portable to use only letters, digits, dashes, and underscores in
header file names, and at most one dot.
Read more about using header files in official GCC documentation:
* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html

46
lib/README Normal file
View file

@ -0,0 +1,46 @@
This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into executable file.
The source code of each library should be placed in a an own separate directory
("lib/your_library_name/[here are source files]").
For example, see a structure of the following two libraries `Foo` and `Bar`:
|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c
and a contents of `src/main.c`:
```
#include <Foo.h>
#include <Bar.h>
int main (void)
{
...
}
```
PlatformIO Library Dependency Finder will find automatically dependent
libraries scanning project source files.
More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html

22
platformio.ini Normal file
View file

@ -0,0 +1,22 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:rpipico]
board = pico
framework = arduino
upload_protocol = cmsis-dap
debug_tool = cmsis-dap
platform = https://github.com/maxgerhardt/platform-raspberrypi.git
board_build.core = earlephilhower
lib_deps =
https://github.com/adafruit/Adafruit_Protomatter
https://github.com/toblum/TetrisAnimation
https://github.com/ropg/ezTime
mikalhart/TinyGPSPlus@^1.0.3

34
src/configuration.h Normal file
View file

@ -0,0 +1,34 @@
#ifndef configuration_h
#define configuration_h
#define TIMEZONE "CET-1CEST,M3.5.0,M10.5.0/3" // in posix format
#define PIN_LED_R1 6
#define PIN_LED_G1 7
#define PIN_LED_B1 8
#define PIN_LED_R2 9
#define PIN_LED_G2 10
#define PIN_LED_B2 11
#define PIN_LED_A 12
#define PIN_LED_B 13
#define PIN_LED_C 14
#define PIN_LED_D 15
#define PIN_LED_STB 16
#define PIN_LED_OE 17
#define PIN_LED_CLK 18
#define PIN_GPS_RX 1
#define PIN_GPS_TX 0
#define BRIGHTNESS_PIN 26
#define RTC_SDA 20
#define RTC_SCL 21
#define DS1307_ADDRESS 0x68
#define ANIMATION_SPEED 100
#define BRIGHTNESS_FACTOR 1
#define BRIGHTNESS_OFFSET -300
#endif

194
src/main.cpp Normal file
View file

@ -0,0 +1,194 @@
#include <Arduino.h>
#include <configuration.h>
#include <Adafruit_Protomatter.h>
#include <TetrisMatrixDraw.h>
#include <ezTime.h>
#include "pico/time.h"
#include <TinyGPS++.h>
#include <Wire.h>
uint8_t rgbPins[] = {PIN_LED_R1, PIN_LED_G1, PIN_LED_B1,
PIN_LED_R2, PIN_LED_G2, PIN_LED_B2};
uint8_t addrPins[] = {PIN_LED_A, PIN_LED_B, PIN_LED_C, PIN_LED_D};
uint8_t clockPin = PIN_LED_CLK;
uint8_t latchPin = PIN_LED_STB;
uint8_t oePin = PIN_LED_OE;
byte Second, Minute, Hour, Day, Month;
int Year;
unsigned long lastUpdate = 0;
unsigned long lastUpdateAnimation = 0;
unsigned long lastColonUpdate = 0;
bool colonOn = false;
bool validTime = false;
bool validDate = false;
String lastDisplayedTime;
Adafruit_Protomatter matrix(
64, // Matrix width in pixels
6, // Bit depth -- 6 here provides maximum color options
1, rgbPins, // # of matrix chains, array of 6 RGB pins for each
4, addrPins, // # of address pins (height is inferred), array of pins
clockPin, latchPin, oePin, // Other matrix control pins
true); // HERE IS THE MAGIC FOR DOUBLE-BUFFERING!
TetrisMatrixDraw tetris(matrix);
Timezone displayTime;
TinyGPSPlus gps;
void getTimeFromRTC();
int decToBcd(int value);
int bcdToDec(int value);
void setup()
{
analogReadResolution(12);
Serial1.setRX(PIN_GPS_RX);
Serial1.setTX(PIN_GPS_TX);
Wire.setSDA(RTC_SDA);
Wire.setSCL(RTC_SCL);
pinMode(BRIGHTNESS_PIN, INPUT);
Serial1.begin(9600);
Serial.begin(115200);
Wire.begin();
// Initialize matrix...
ProtomatterStatus status = matrix.begin();
if (status != PROTOMATTER_OK)
{
while (1)
{
Serial.print("Display begin failed! Status: ");
Serial.println(status);
delay(1000);
}
}
matrix.fillScreen(0);
matrix.show();
displayTime.setPosix(TIMEZONE);
getTimeFromRTC();
tetris.scale = 2;
}
void loop()
{
if (millis() - lastUpdate > 1000)
{
lastUpdate = millis();
String timeString = displayTime.dateTime("H:i");
if (timeString != lastDisplayedTime)
{
lastDisplayedTime = timeString;
tetris.setTime(timeString);
}
}
if (millis() - lastUpdateAnimation > ANIMATION_SPEED)
{
lastUpdateAnimation = millis();
matrix.fillScreen(0);
tetris.drawNumbers(2, 26, false);
int y = 26 - (TETRIS_Y_DROP_DEFAULT * tetris.scale);
tetris.drawColon(2, y, colonOn ? tetris.tetrisWHITE : tetris.tetrisBLACK);
int brightness = analogRead(BRIGHTNESS_PIN) * BRIGHTNESS_FACTOR + BRIGHTNESS_OFFSET;
if(brightness > 4095) brightness = 4095;
if(brightness < 0) brightness = 0;
for (int x = 0; x < 64; x++)
{
for (int y = 0; y < 32; y++)
{
// get the pixel color
uint16_t color = matrix.getPixel(x, y);
// get the red, green and blue components
uint8_t r = (color & 0xF800) >> 11;
uint8_t g = (color & 0x07E0) >> 5;
uint8_t b = (color & 0x001F);
// adjust the brightness
r = r * brightness / 4096;
g = g * brightness / 4096;
b = b * brightness / 4096;
// set the pixel color
color = (r << 11) | (g << 5) | b;
matrix.drawPixel(x, y, color);
}
}
matrix.show();
}
if (millis() - lastColonUpdate > 1000)
{
lastColonUpdate = millis();
colonOn = !colonOn;
}
while (Serial1.available() > 0)
{
int c = Serial1.read();
Serial.write(c);
Serial.flush();
if (gps.encode(c))
{
if (gps.time.isValid() && gps.time.hour() != 0 && gps.time.minute())
{
Hour = gps.time.hour();
Minute = gps.time.minute();
Second = gps.time.second();
validTime = true;
}
if (gps.date.isValid() && gps.date.day() != 0 && gps.date.month() != 0 && gps.date.year() != 2000)
{
Day = gps.date.day();
Month = gps.date.month();
Year = gps.date.year();
validDate = true;
}
}
}
if (validDate && validTime)
{
// check if GPS time is ahead of RTC time
if (UTC.hour() != Hour || UTC.minute() != Minute || UTC.second() + 2 < Second || UTC.second() - 2 > Second)
{
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(0);
Wire.write(decToBcd(Second));
Wire.write(decToBcd(Minute));
Wire.write(decToBcd(Hour));
Wire.write(decToBcd(Day));
Wire.write(decToBcd(Month));
Wire.write(decToBcd(Year - 2000));
Wire.endTransmission();
}
validTime = false;
validDate = false;
getTimeFromRTC();
}
}
int decToBcd(int value)
{
return ((value / 10 * 16) + (value % 10));
}
int bcdToDec(int value)
{
return ((value / 16 * 10) + (value % 16));
}
void getTimeFromRTC()
{
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
Second = bcdToDec(Wire.read());
Minute = bcdToDec(Wire.read());
Hour = bcdToDec(Wire.read() & 0b111111); // 24 hour time
Day = bcdToDec(Wire.read());
Month = bcdToDec(Wire.read());
Year = bcdToDec(Wire.read()) + 2000;
UTC.setTime(Hour, Minute, Second, Day, Month, Year);
}

11
test/README Normal file
View file

@ -0,0 +1,11 @@
This directory is intended for PlatformIO Test Runner and project tests.
Unit Testing is a software testing method by which individual units of
source code, sets of one or more MCU program modules together with associated
control data, usage procedures, and operating procedures, are tested to
determine whether they are fit for use. Unit testing finds problems early
in the development cycle.
More information about PlatformIO Unit Testing:
- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html