KravitzLabDevices / FED3_library

GNU General Public License v3.0
6 stars 17 forks source link

Hot swap SD cards #20

Closed KravitzLab closed 3 years ago

KravitzLab commented 3 years ago

GPIO 7 (not broken out to a pin on the feather) is set up as an SD card detector. Could this be used to detect the removal/replacement of a card and automatically start a new file? image

KravitzLab commented 3 years ago

https://learn.adafruit.com/adafruit-feather-m0-adalogger/pinouts?view=all&gclid=Cj0KCQiAoab_BRCxARIsANMx4S6oBMcRrCBFGluH_NyFs_wCMEvQeYewdekKXpsjzwAi3doaUTjVTWUaAs1PEALw_wcB#using-the-sd-card

KravitzLab commented 3 years ago

As a stop-gap, I could make a visual indicator showing when an SD card is inserted/removed, using Pin 7 and putting a small SD icon next to the battery icon

KravitzLab commented 3 years ago

Function for drawing a small SD card icon (and turning on the green led when card is out):

void FED3::DisplaySDicon() {
if (digitalRead(7) == HIGH){
    //draw icon
    display.drawRect (100, 2, 11, 16, BLACK);
    display.drawRect (99, 6, 2, 12, BLACK);
    display.fillRect (100, 7, 4, 10, WHITE);
    display.drawRect (102, 4, 1, 3, BLACK);
    display.drawRect (104, 4, 1, 3, BLACK);
    display.drawRect (106, 4, 1, 3, BLACK);
    display.drawRect (108, 4, 1, 3, BLACK);
    digitalWrite(8, LOW);
 }
 else {
    //Remove icon
    display.fillRect (98, 1, 13, 19, WHITE);
    digitalWrite(8, HIGH);
  }
}

Also requires:

pinMode(7, INPUT_PULLUP);
KravitzLab commented 3 years ago

I played with this some but found that Pin 7 was not entirely reliable for identifying the SD card on all Feather boards. Unfortunately I don't think this approach will work. Perhaps SD errors can be used to denote a problem and put an icon up on the screen.

KravitzLab commented 3 years ago

Done!

This was easier than I thought after figuring it out, but it took me a while to get it.

Short solution: the logdata() function now initializes the SD card each time it is called and re-opens the logfile. This means that you can pop the card out and in again and it doesn't matter. Behavior that occurs while the card is out will be lost, but the FED3 will start logging events again when the card is back in. (You can even put a different card in, and it will create a new logfile with the current filename on that card and start writing to it.)

I also made a new on-screen icon to show when card is not detected and data is being lost: image