Project 9.03 Section 9 ⚡ Embedded Relevance: Core Streams Data Pipeline EEPROM Circular Logging Transformation

9.03 Input-to-Output Stream Transformations & Circular Ring Logging in EEPROM

Executive Summary: Building read-transform-write file pipelines. We analyze streaming mathematical transformation of files and compare file-based streams with circular logging queues stored in non-volatile I2C/SPI EEPROM memory.

💻 1. Annotated Source Code

#include <iostream>
#include <fstream>
using namespace std;

int main() {
	ifstream infile("input.txt");
	ofstream outfile("output.txt");

	if (!infile) {
		cerr << "Error opening input file" << endl;
		return 1;
	}

	int tempNum;

	while (infile >> tempNum) {
		outfile << (tempNum * 2) << endl;
	}

	infile.close();
	outfile.close();

	cout << "Doubled values written to output.txt!" << endl;

	return 0;
}
10
12
14
22
51
35
555
32
15
182
22
20
24
28
44
102
70
1110
64
30
364
44

📐 2. Architecture & UML Class Model

📐 File-to-File Stream Transformation Pipeline Model
+ Public - Private # Protected
<<compilation-unit>> TwiceFileProcessor Stream Pipeline
-inFilePath : const char*
-outFilePath : const char*
+processDoubling(inFile: const char*, outFile: const char*) : bool

📚 3. Core C++ Concepts Deep-Dive

1. Streaming Data Pipelines

Reading elements sequentially from an input stream, applying a transformation function, and writing directly to an output stream in $O(1)$ memory space.

⚡ 4. Embedded Systems & Hardware Reality

1. Non-Volatile EEPROM Circular Buffers

In industrial sensors, small I2C EEPROMs (e.g. 24LC256 - 32KB) store error event logs. Because EEPROM allows byte-level writes with 1,000,000+ erase endurance, circular ring structures log telemetry indefinitely.

💡 5. Production-Ready Embedded Refactoring

Streaming data transformer:

💡 Production-Ready Refactor
#include <cstdint>
#include <cstddef>

// In-place streaming sensor sample scaler
void scaleSensorStream(const int16_t* in_samples, int16_t* out_samples, size_t count, int16_t multiplier) noexcept {
    for (size_t i = 0; i < count; ++i) {
        out_samples[i] = in_samples[i] * multiplier;
    }
}

📝 Knowledge Verification Quiz

Test your understanding of the C++ concepts and embedded microcontroller trade-offs covered in this guide. Click any option for instant feedback.

Q1. How does I2C/SPI EEPROM differ from NOR Flash memory in embedded hardware?
A EEPROM allows true byte-level overwriting without requiring full sector erases, and provides higher endurance (1,000,000+ cycles)
B EEPROM is read-only
C EEPROM requires 64-bit microcontrollers
D Flash memory is faster for single byte writes
Detailed Explanation: EEPROM allows erasing and writing individual bytes independently, making it ideal for parameter storage and high-frequency event logging.
Q2. What is the memory complexity of a streaming data transformation pipeline that processes items one at a time?
A O(1) constant auxiliary RAM space
B O(N) memory space
C O(N^2) memory space
D O(log N) memory space
Detailed Explanation: Processing items one by one in a stream pipeline requires only a single element buffer, operating in $O(1)$ RAM.
Q3. What happens if a stream extraction 'in_file >> val' reaches End-Of-File (EOF)?
A The stream sets its eofbit flag and the extraction expression evaluates to false in a boolean context
B The CPU reboots
C A HardFault is generated
D The file is deleted
Detailed Explanation: Upon reaching EOF, eofbit is set, causing while (in_file >> val) loops to terminate cleanly.
Q4. Why is closing file streams explicitly (or via RAII scope exit) critical before reading the destination file?
A To ensure all pending data buffered in memory is flushed and written to disk before the subsequent reader accesses it
B To reduce RAM clock speed
C To encrypt the file
D To prevent compiler syntax errors
Detailed Explanation: Closing a file flushes all remaining cached buffer data to physical media, ensuring readers see complete files.