pleasedontcode

EEPROM Manager rev_01

Oct 9th, 2025
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: EEPROM Manager
  13.     - Source Code NOT compiled for: Arduino Pro Mini 3.3V
  14.     - Source Code created on: 2025-10-09 16:32:12
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Build a highly customizable project management */
  21.     /* system for Chaseone Technologies.  using PHP, */
  22.     /* MySQL AND JavaScript. The UI should be an */
  23.     /* attractive, mobile responsive, bootstrap based */
  24.     /* interface. The system will be used to manage */
  25.     /* organization projects t */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28.  
  29. /* START CODE */
  30.  
  31. /****** DEFINITION OF LIBRARIES *****/
  32. #include <EEPROM.h>
  33. #include <string.h> // for memset, strncpy, strcmp
  34.  
  35. /****** PROJECT CONFIGURATION *****/
  36. #define MAX_PROJECTS 6
  37. #define NAME_LEN 16
  38. #define PROJECT_SIZE sizeof(Project)
  39.  
  40. /****** PROJECT STRUCTURE AND STORAGE HANDLING *****/
  41. typedef struct {
  42.   char name[NAME_LEN];
  43.   uint8_t status;
  44.   uint32_t due; // due date in YYYYMMDD
  45.   uint8_t used; // 1 if slot contains a project
  46. } Project;
  47.  
  48. /****** PRIVATE PROTOTYPES ******/
  49. void processCommand(const char* cmd);
  50. void addProject(const char* name, uint8_t status, uint32_t due);
  51. bool readProject(uint8_t idx, Project &p);
  52. void writeProject(uint8_t idx, const Project &p);
  53. void listProjects(void);
  54. void clearAllProjects(void);
  55. void clearProjectAt(uint8_t idx);
  56.  
  57. void setup(void);
  58. void loop(void);
  59.  
  60. /****** IMPLEMENTATION ******/
  61. void setup(void) {
  62.   Serial.begin(9600);
  63.   delay(1000); // allow serial connection to establish (if using USB-serial adapter)
  64.   Serial.println(F("Project Manager Ready (Arduino Pro Mini)"));
  65.   Serial.println(F("Type HELP for available commands"));
  66.   // Optional: Initialize EEPROM slots on first run (left as-is to preserve data)
  67. }
  68.  
  69. void loop(void) {
  70.   static const uint16_t LINE_LEN = 64;
  71.   static char line[LINE_LEN];
  72.   static uint16_t len = 0;
  73.  
  74.   while (Serial.available()) {
  75.     int c = Serial.read();
  76.     if (c < 0) break;
  77.     if (c == '\r') continue;
  78.     if (c == '\n') {
  79.       line[len] = '\0';
  80.       if (len > 0) processCommand(line);
  81.       len = 0;
  82.     } else {
  83.       if (len < LINE_LEN - 1) line[len++] = (char)c;
  84.     }
  85.   }
  86. }
  87.  
  88. void processCommand(const char* cmd) {
  89.   // skip leading spaces
  90.   while (*cmd == ' ') cmd++;
  91.  
  92.   if (strcmp(cmd, "LIST") == 0) {
  93.     listProjects();
  94.   } else if (strncmp(cmd, "ADD ", 4) == 0) {
  95.     const char* rest = cmd + 4;
  96.     char name[NAME_LEN];
  97.     uint32_t due = 0;
  98.     uint8_t status = 0;
  99.     if (sscanf(rest, "%15s %u %hhu", name, &due, &status) == 3) {
  100.       if (addProject(name, status, due)) {
  101.         Serial.print(F("Added: "));
  102.         Serial.println(name);
  103.       } else {
  104.         Serial.println(F("No free project slots available."));
  105.       }
  106.     } else {
  107.       Serial.println(F("Invalid ADD format. Use: ADD <name> <YYYYMMDD> <status>"));
  108.     }
  109.   } else if (strncmp(cmd, "ERASE ", 6) == 0) {
  110.     unsigned int idx;
  111.     if (sscanf(cmd + 6, "%u", &idx) == 1) {
  112.       if (idx < MAX_PROJECTS) {
  113.         clearProjectAt((uint8_t)idx);
  114.         Serial.print(F("Erased slot "));
  115.         Serial.println(idx);
  116.       } else {
  117.         Serial.println(F("Invalid index"));
  118.       }
  119.     }
  120.   } else if (strcmp(cmd, "CLEAR") == 0) {
  121.     clearAllProjects();
  122.     Serial.println(F("All slots cleared"));
  123.   } else if (strcmp(cmd, "HELP") == 0) {
  124.     Serial.println(F("Commands: LIST | ADD <name> <YYYYMMDD> <status> | ERASE <idx> | CLEAR | HELP"));
  125.   } else {
  126.     Serial.print(F("Unknown command: "));
  127.     Serial.println(cmd);
  128.   }
  129. }
  130.  
  131. // Storage helpers
  132. bool readProject(uint8_t idx, Project &p) {
  133.   if (idx >= MAX_PROJECTS) return false;
  134.   EEPROM.get(idx * PROJECT_SIZE, p);
  135.   return p.used != 0;
  136. }
  137. void writeProject(uint8_t idx, const Project &p) {
  138.   if (idx >= MAX_PROJECTS) return;
  139.   EEPROM.put(idx * PROJECT_SIZE, p);
  140. }
  141. bool addProject(const char* name, uint8_t status, uint32_t due) {
  142.   for (uint8_t i = 0; i < MAX_PROJECTS; i++) {
  143.     Project p;
  144.     EEPROM.get(i * PROJECT_SIZE, p);
  145.     if (!p.used) {
  146.       Project np;
  147.       memset(&np, 0, sizeof(Project));
  148.       strncpy(np.name, name, NAME_LEN - 1);
  149.       np.name[NAME_LEN - 1] = '\0';
  150.       np.status = status;
  151.       np.due = due;
  152.       np.used = 1;
  153.       EEPROM.put(i * PROJECT_SIZE, np);
  154.       return true;
  155.     }
  156.   }
  157.   return false;
  158. }
  159. void listProjects(void) {
  160.   Serial.println(F("Projects:"));
  161.   for (uint8_t i = 0; i < MAX_PROJECTS; i++) {
  162.     Project p;
  163.     EEPROM.get(i * PROJECT_SIZE, p);
  164.     if (p.used) {
  165.       Serial.print(i);
  166.       Serial.print(": ");
  167.       Serial.print(p.name);
  168.       Serial.print("  Status=");
  169.       Serial.print(p.status);
  170.       Serial.print("  Due=");
  171.       Serial.println(p.due);
  172.     }
  173.   }
  174. }
  175. void clearProjectAt(uint8_t idx) {
  176.   if (idx >= MAX_PROJECTS) return;
  177.   Project p;
  178.   memset(&p, 0, sizeof(Project));
  179.   p.used = 0;
  180.   EEPROM.put(idx * PROJECT_SIZE, p);
  181. }
  182. void clearAllProjects(void) {
  183.   for (uint8_t i = 0; i < MAX_PROJECTS; i++) clearProjectAt(i);
  184. }
  185.  
  186. /* END CODE */
  187.  
Advertisement
Add Comment
Please, Sign In to add comment