Skip to content

Commit

Permalink
sd card
Browse files Browse the repository at this point in the history
  • Loading branch information
bearums committed Dec 15, 2023
1 parent 2abfc8d commit 440bb12
Show file tree
Hide file tree
Showing 18 changed files with 433 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Basic reading and writing to an SD card
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"version": 1,
"author": "Uri Shaked",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 71.32, "left": -100.01, "attrs": {} },
{
"type": "wokwi-microsd-card",
"id": "sd1",
"top": -33.33,
"left": 14.67,
"rotate": 90,
"attrs": {}
},
{
"type": "wokwi-ntc-temperature-sensor",
"id": "ntc1",
"top": 319.4,
"left": 162.6,
"attrs": {}
}
],
"connections": [
[ "sd1:SCK", "uno:13", "green", [ "v18", "h-31" ] ],
[ "sd1:GND", "uno:GND.1", "black", [ "v21", "h-48" ] ],
[ "sd1:DO", "uno:12", "green", [ "v24", "h-42" ] ],
[ "sd1:DI", "uno:11", "green", [ "v14", "h6" ] ],
[ "sd1:CS", "uno:10", "green", [ "v7", "h21" ] ],
[ "uno:5V", "sd1:VCC", "red", [ "v11", "h-89", "v-220", "h79" ] ],
[ "ntc1:GND", "uno:GND.3", "black", [ "h0" ] ],
[ "ntc1:OUT", "uno:A0", "green", [ "h38.4", "v-76.9", "h-230.4" ] ],
[ "ntc1:VCC", "uno:VIN", "red", [ "h9.6", "v-38.4", "h-220.8" ] ]
],
"dependencies": {}
}
Original file line number Diff line number Diff line change
@@ -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 changes: 46 additions & 0 deletions workshops/arduino-programming/emulator-examples/SD_card/lib/README
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
; 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:uno]
platform = atmelavr
board = uno
framework = arduino
lib_deps =
arduino-libraries/SD@^1.2.4
100 changes: 100 additions & 0 deletions workshops/arduino-programming/emulator-examples/SD_card/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include <Arduino.h>
#include <SD.h>

#define CS_PIN 10

File root;
File myFile;


void 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("/");
printDirectory(entry, numTabs + 1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}

void setup() {
Serial.begin(115200);

Serial.print("Initializing SD card... ");

if (!SD.begin(CS_PIN)) {
Serial.println("Card initialization failed!");
while (true);
}

Serial.println("initialization done.");

Serial.println("Files in the card:");
root = SD.open("/");
printDirectory(root, 0);
Serial.println("");

// Example of reading file from the card:
//File textFile = SD.open("wokwi.txt");
//if (textFile) {
// Serial.print("wokwi.txt: ");
// while (textFile.available()) {
// Serial.write(textFile.read());
// }
// textFile.close();
//} else {
// Serial.println("error opening wokwi.txt!");
//}
}

void loop() {
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);

// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}

// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");

// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
delay(10000);
}



Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[wokwi]
version=1
firmware='.pio/build/uno/firmware.hex'
elf='.pio/build/uno/firmware.elf'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
You just bought your Arduino and opened the box!

This is a template for other projects
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"version": 1,
"author": "Anonymous maker",
"editor": "wokwi",
"parts": [
{ "type": "wokwi-arduino-uno", "id": "uno", "top": 0, "left": 0, "attrs": {} },
{
"type": "wokwi-ntc-temperature-sensor",
"id": "ntc1",
"top": -130.63,
"left": 11.15,
"attrs": { "beta": "3950", "temperature": "33" }
}
],
"connections": [
[ "ntc1:OUT", "uno:A0", "green", [ "h178", "v319", "h-81" ] ],
[ "ntc1:VCC", "uno:VIN", "red", [ "h206", "v351", "h-137" ] ],
[ "ntc1:GND", "uno:GND.1", "black", [ "h26", "v78", "h-58" ] ]
],
"dependencies": {}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit 440bb12

Please sign in to comment.