File I/O (Storage extension)

Microsoft's MakeCode platform for easy blocks-style programming

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
flounder
 
Posts: 494
Joined: Wed Sep 18, 2013 9:10 pm

File I/O (Storage extension)

Post by flounder »

The mechanism of Read File is not explained. What does it read? A line? A value from a line? What?
There is no apparent way to check EOF when reading a file.

Here;s what I want to do: I want a file called "calibration.txt" which contains, ideally, lines of the following form

Code: Select all

1, 1200
2, 1700
7, 900
I am making a moisture sensor that senses up to 7 flowerpots. I set up the default value at the suggested 1500, but this may not be suitable for all flowers. I want the end user to be able to set the calibration for each flowerpot by putting a file called calibration.txt on the CPX. Then I do (in MakeCode)

Code: Select all

if fileExists("calibration.txt")
           ...magically read data
and I have no idea how to write the loop that magically reads data. The documentation is remarkably silent on how this mechanism would work. I am starting to experiment, but it would be better if I had a real piece of documentation that showed how to read a file.

I want to also, if there is an error, do something like blink the lights all red on and off for several seconds. So assume I want to do input validation. If the data is

Code: Select all

9, 1000
1000, 7
7 1000
A7, 1000
or similar kinds of whatever I declare as nonsense, I want to indicate an error. Since there can be at most 7 lines in the file, I can blink all the lights red except one, which I will blink blue, and it will indicate what line the error is on.

But without knowing what read file does or how to use it, it is impossible to write this code.

User avatar
mikeysklar
 
Posts: 13824
Joined: Mon Aug 01, 2016 8:10 pm

Re: File I/O (Storage extension)

Post by mikeysklar »

@flounder,

I see what you mean about lack of documentation around what readfile isdoing.

I looked through a few github archives and eventually stumbled onto the pxt-common repository which does have the TS and C++ code for the storage library. It looks like read called readAsBuffer() which is sucking in the entire file as one long string.

https://github.com/microsoft/pxt-common ... storage.ts

Code: Select all

    /** 
    * Read contents of file as a string. 
    * @param filename name of the file, eg: "log.txt"
    */
    //% parts="storage"
    //% blockId="storage_read" block="read file $filename"
    export function read(filename: string) {
        const buf = readAsBuffer(filename);
        if (!buf)
            return null;
        return buf.toString();
    }
}
©
https://github.com/microsoft/pxt-common ... torage.cpp

Code: Select all

/** 
* Read contents of file as a buffer. 
* @param filename name of the file, eg: "log.txt"
*/
//% parts="storage"
Buffer readAsBuffer(String filename) {
    auto f = getFile(filename);
    if (NULL == f) 
        return NULL;
    auto sz = f->size();
    if (sz > 0xffff)
        return NULL;
    auto res = mkBuffer(NULL, sz);
    registerGCObj(res);
    f->seek(0);
    f->read(res->data, res->length);
    unregisterGCObj(res);
    return res;
}

User avatar
flounder
 
Posts: 494
Joined: Wed Sep 18, 2013 9:10 pm

Re: File I/O (Storage extension)

Post by flounder »

Thank you. If I can ever get my CPX to talk to my desktop, this may help. I now have to figure out how to parse the entire file using MakeCode.

User avatar
mikeysklar
 
Posts: 13824
Joined: Mon Aug 01, 2016 8:10 pm

Re: File I/O (Storage extension)

Post by mikeysklar »

What is the issue with you CPX talking to your machine? Does it not appear as a drive?

What OS are you connecting the CPX to and is this a known good USB cable?

I did find some Text functions to manipulate strings. They seem limited, but maybe enough to get you started.

https://makecode.microbit.org/reference/text

User avatar
flounder
 
Posts: 494
Joined: Wed Sep 18, 2013 9:10 pm

Re: File I/O (Storage extension)

Post by flounder »

The CPX does not appear as a drive
It does appear in the Device Manager as a disk drive
It does appear in the Disk Manager as a disk drive
It does not get a drive letter or appear in the "My Computer" as a drive
Yes, I asked the window to refresh
Windows 10
The cable is known to be good

User avatar
mikeysklar
 
Posts: 13824
Joined: Mon Aug 01, 2016 8:10 pm

Re: File I/O (Storage extension)

Post by mikeysklar »

This sounds like another program is blocking the drive from being mounted since the CPX device is being seen and the USB cable appears to be working. Do you have anti-virus software installed such as Kapersky or Norton? Win10 does not need drivers, but sometimes users accidentally install the Adafruit Windows drivers which can cause issues. More on both issues here:

https://learn.adafruit.com/adafruit-cir ... leshooting

Are you plugging this into a USB port on the machine or a powered hub? Just wanted to make sure you are not trying to use a keyboard or other low powered port,.

User avatar
flounder
 
Posts: 494
Joined: Wed Sep 18, 2013 9:10 pm

Re: File I/O (Storage extension)

Post by flounder »

As I pointed out in an earlier post, I have used both a hub and direct connection, with no change in behavior.
I also tried a couple different cables.
I do not have any third-party antimalware software.
What is bizarre was that it had been working a couple months ago, same boards, same cables, same hubs. I could be the victim of a drive-by update. That's what I'm trying to determine.

User avatar
mikeysklar
 
Posts: 13824
Joined: Mon Aug 01, 2016 8:10 pm

Re: File I/O (Storage extension)

Post by mikeysklar »

@flounder,

Which bootloader are you running on your CPX? Have you updated that?

I've had to test all my good DATA USB cables to make sure they also had low resistance to get reliable drive mounting. That mean wiring in breakout USB connectors and using an ohm meter to measure the cables resistance. I also find only certain USB ports on powered hubs and desktops provide adequit power.

Just curious when you do the 'reset' into bootloader mode does the BOOTdevice get mounted successfully?

Locked
Please be positive and constructive with your questions and comments.

Return to “MakeCode”