2

I'm trying to read a small file off the SD card installed in a Yun. Here's the sketch:

#include <FastLED.h>
#include <FileIO.h>

#define WIDTH 32
#define HEIGHT 1
#define NUM_LEDS (WIDTH * HEIGHT)

#define DATA_PIN 9

CRGB leds[NUM_LEDS];

void setup() {
  // put your setup code here, to run once:
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  Bridge.begin();
  Serial.begin(115200);
  FileSystem.begin();
}

void loop() {
  File f = FileSystem.open("/mnt/sd/single-line-testpattern.ppm", FILE_READ);
  f.seek(29); // seek past the header bytes
  Serial.print("position = "); Serial.println(f.position());
  for (int i = 0; i < NUM_LEDS; i++) {
    uint8_t r = f.read();
    uint8_t g = f.read();
    uint8_t b = f.read();
    Serial.print("r = "); Serial.print(r, HEX);
    Serial.print(", g = "); Serial.print(g, HEX);
    Serial.print(", b = "); Serial.println(b, HEX);    
    leds[i] = CRGB(r, g, b);
  }
  f.close();
  FastLED.show();
}

Here's the contents of the PPM file:

50 36 0A 23 20 3A 20 62 79 20 54 6F 79 56 69 65 77 65 72 0A 33 32 20 31 20 32 35 35 0A FF 00 00 FF 00 00 FF 00 00 FF 00 00 FF 00 00 FF 00 00 00 FF 00 00 FF 00 00 FF 00 00 FF 00 00 FF 00 00 FF 00 00 FF 00 00 FF 00 00 FF 00 00 FF 00 00 FF 00 00 00 FF 00 00 FF 00 00 FF 00 00 FF 00 00 FF 00 00 FF 00 00 FF 00 00 FF 00 00 FF 00 00 FF 00 00 FF 00 00 FF 00 00 FF 00 00 FF 00 00 FF

This looks pretty straightforward to me, but the output I'm getting is absolutely baffling:

position = 5888
r = 6, g = A9, b = A
r = B9, g = 0, b = 5
r = 0, g = E9, b = 5
r = 0, g = 0, b = 4
r = 8, g = 21, b = 0
r = 0, g = 0, b = 0
r = 0, g = E5, b = A
r = E0, g = A, b = 0
r = 0, g = 0, b = 0
r = A, g = DA, b = 0
r = 0, g = 0, b = 0
r = 0, g = 0, b = A
r = E5, g = 0, b = 4
r = 2, g = 71, b = 5
r = 1, g = 32, b = 80
r = 9, g = 12, b = 58
r = 58, g = 31, b = 30
r = 30, g = 58, b = 58
r = 58, g = 58, b = 58

WAT?! Where are those numbers even coming from? What does file.read() think a byte is? How come it's trying to read from 5888 bytes into a 125 byte file?

Note this is the FileIO library from the Yun Bridge package to read from the SD card built into the Atheron side of the Yun. The "normal" Arduino SD card library isn't relevant here.

2
  • 1
    You might try first reading and printing out the header as a consistency check, then you could try a version that seeks a few bytes into the header. How would you expect this library to respond to a failure, such as the file not being found? And do you really want to close and reopen it in loop()? Is your output from the first cycle of loop() or a subsequent one? Feb 8 '15 at 17:27
  • 1
    You are not testing if the FileSystem.open succeeded. If it did not, subsequent calls using f would possibly give undefined results.
    – Nick Gammon
    Sep 19 '15 at 0:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.