From efcb735940cd6cfbb17dd9314608cb52e7b8ab0a Mon Sep 17 00:00:00 2001 From: Luke Irvine Date: Thu, 26 Nov 2020 02:39:00 -0800 Subject: [PATCH 1/2] added wrapper class for SD --- .arduino-ci.yml | 22 +++++++++++++++- Gemfile | 2 +- src/Devices/SD_TC.cpp | 54 +++++++++++++++++++++++++++++++++++++++ src/Devices/SD_TC.h | 26 +++++++++++++++++++ src/TankControllerLib.cpp | 1 + src/TankControllerLib.h | 1 + test/SD.cpp | 13 ++++++++++ 7 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 src/Devices/SD_TC.cpp create mode 100644 src/Devices/SD_TC.h create mode 100644 test/SD.cpp diff --git a/.arduino-ci.yml b/.arduino-ci.yml index 319d7302..1e394c06 100644 --- a/.arduino-ci.yml +++ b/.arduino-ci.yml @@ -1,4 +1,22 @@ - +platforms: + mega2560: + board: arduino:avr:mega:cpu=atmega2560 + package: arduino:avr + gcc: + features: + defines: + - __AVR__ + - __AVR_ATmega2560__ + - ARDUINO_ARCH_AVR + - ARDUINO_AVR_MEGA2560 + # Why aren't these already provided elsewhere? + - SS=1 + - MOSI=2 + - MISO=3 + - SCK=4 + warnings: + flags: + unittest: platforms: - mega2560 @@ -8,6 +26,7 @@ unittest: - "Ethernet" - "LiquidCrystal" - "RTClib" + - "SD" compile: platforms: @@ -18,3 +37,4 @@ compile: - "Ethernet" - "LiquidCrystal" - "RTClib" + - "SD" diff --git a/Gemfile b/Gemfile index 80468b55..fb422d12 100644 --- a/Gemfile +++ b/Gemfile @@ -1,2 +1,2 @@ source 'https://rubygems.org' -gem 'arduino_ci', git: 'https://github.com/Arduino-CI/arduino_ci.git', branch: 'master' +gem 'arduino_ci' diff --git a/src/Devices/SD_TC.cpp b/src/Devices/SD_TC.cpp new file mode 100644 index 00000000..0672efee --- /dev/null +++ b/src/Devices/SD_TC.cpp @@ -0,0 +1,54 @@ +#include "Devices/SD_TC.h" + +// class variables +SDClass_TC* SDClass_TC::_instance = nullptr; + +// class methods +/** + * accessor for singleton + */ +SDClass_TC* SDClass_TC::instance() { + if (!_instance) { + _instance = new SDClass_TC(); + } + return _instance; +} + +// instance methods +/** + * Constructor sets pins, dimensions, and shows splash screen + */ +SDClass_TC::SDClass_TC() : SDClass() { + pinMode(10, OUTPUT); + digitalWrite(10, HIGH); + // TODO: Need to re-enable this for the physical device + // begin(4); +} + +void SDClass_TC::printRootDirectory() { + File root = open("/"); + printDirectory(root, 0); +} + +void SDClass_TC::printDirectory(File dir, int numTabs) { + while (true) { + File entry = dir.openNextFile(); + if (!entry) { + // no more files + break; + } + for (uint8_t i = 0; i < numTabs; i++) { + Serial.print('\t'); + } + Serial.print(entry.name()); + if (entry.isDirectory()) { + Serial.println(F("/")); + printDirectory(entry, numTabs + 1); + } else { + // files have sizes, directories do not + Serial.print(F("\t\t")); + Serial.println(entry.size(), DEC); + } + entry.close(); + } +} diff --git a/src/Devices/SD_TC.h b/src/Devices/SD_TC.h new file mode 100644 index 00000000..453f47f4 --- /dev/null +++ b/src/Devices/SD_TC.h @@ -0,0 +1,26 @@ +#pragma once + +#include +#ifdef MOCK_PINS_COUNT +#include +#else +#include +#endif + +class SDClass_TC : public SDClass { +public: + // class methods + static SDClass_TC* instance(); + // instance methods + void printRootDirectory(); + // bool begin(uint8_t csPin = SD_CHIP_SELECT_PIN); + // bool begin(uint32_t clock, uint8_t csPin); + +private: + // class variables + static SDClass_TC* _instance; + + // instance methods + SDClass_TC(); + void printDirectory(File dir, int numTabs); +}; diff --git a/src/TankControllerLib.cpp b/src/TankControllerLib.cpp index c138ede6..b1cdc519 100644 --- a/src/TankControllerLib.cpp +++ b/src/TankControllerLib.cpp @@ -37,6 +37,7 @@ TankControllerLib::TankControllerLib() { lcd = LiquidCrystal_TC::instance(); log = Serial_TC::instance(); log->print(F("TankControllerLib::TankControllerLib() - version "), TANK_CONTROLLER_VERSION); + SDClass_TC::instance()->printRootDirectory(); } /** diff --git a/src/TankControllerLib.h b/src/TankControllerLib.h index fecb7098..636f2cf8 100644 --- a/src/TankControllerLib.h +++ b/src/TankControllerLib.h @@ -2,6 +2,7 @@ #include #include "Devices/LiquidCrystal_TC.h" +#include "Devices/SD_TC.h" #include "Devices/Serial_TC.h" const char TANK_CONTROLLER_VERSION[] = "0.3.0"; diff --git a/test/SD.cpp b/test/SD.cpp new file mode 100644 index 00000000..8efa8971 --- /dev/null +++ b/test/SD.cpp @@ -0,0 +1,13 @@ +#include +#include + +#include "TankControllerLib.h" + +unittest(test) { + TankControllerLib* tank = TankControllerLib::instance(); + SDClass_TC* sd = SDClass_TC::instance(); + assertTrue(tank != nullptr); + assertTrue(sd != nullptr); +} + +unittest_main() From 2f3b194ac57a055f750d384fa3eb133ee17bb9bb Mon Sep 17 00:00:00 2001 From: James Foster Date: Tue, 9 Mar 2021 23:33:00 -0800 Subject: [PATCH 2/2] Cleanup: remove commented-out code, remove unused code, add comments. --- src/Devices/SD_TC.cpp | 12 ++++-------- src/Devices/SD_TC.h | 4 +--- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/src/Devices/SD_TC.cpp b/src/Devices/SD_TC.cpp index 0672efee..17eadf46 100644 --- a/src/Devices/SD_TC.cpp +++ b/src/Devices/SD_TC.cpp @@ -16,20 +16,16 @@ SDClass_TC* SDClass_TC::instance() { // instance methods /** - * Constructor sets pins, dimensions, and shows splash screen + * print the root directory and all subdirectories */ -SDClass_TC::SDClass_TC() : SDClass() { - pinMode(10, OUTPUT); - digitalWrite(10, HIGH); - // TODO: Need to re-enable this for the physical device - // begin(4); -} - void SDClass_TC::printRootDirectory() { File root = open("/"); printDirectory(root, 0); } +/** + * print a specified directory with an indent + */ void SDClass_TC::printDirectory(File dir, int numTabs) { while (true) { File entry = dir.openNextFile(); diff --git a/src/Devices/SD_TC.h b/src/Devices/SD_TC.h index 453f47f4..585cd4d3 100644 --- a/src/Devices/SD_TC.h +++ b/src/Devices/SD_TC.h @@ -11,16 +11,14 @@ class SDClass_TC : public SDClass { public: // class methods static SDClass_TC* instance(); + // instance methods void printRootDirectory(); - // bool begin(uint8_t csPin = SD_CHIP_SELECT_PIN); - // bool begin(uint32_t clock, uint8_t csPin); private: // class variables static SDClass_TC* _instance; // instance methods - SDClass_TC(); void printDirectory(File dir, int numTabs); };