Save Files on Teensy

Status
Not open for further replies.

nameless

Well-known member
Can I somehow save any files on the Teensy like a .txt-file or similar? As it seems so have 2MB EEPROM, there should be enough space, but how can I transfer a file on it and then transfer it back on computer again?
 
I never 'saw' this before: "On Teensy 3.0, the EEPROM data is erased when you upload a new program." I suppose this comment applies to Teensy 3.1&2?

EPROM on T3 is 2KB. For more space it would take add on flash or eeprom chip, SD or other static storage.

You could get the file in/out over USB or Serial# or add a radio or wired Ethernet.
 
@adrian: thanks, but this only seems to work for seperated bytes? So how can I transfer a txt-file then?
@mortonkopf: yes, my mistake
@defragster:
For more space it would take add on flash or eeprom chip, SD or other static storage.
- Is it possible to e.g. add a sdcard to Teensy? I have the Teensy 3.2.
 
But I would need both, file transfer and Teensy capabilities, like working as Keyboard or mouse... Does this work as well?
 
I suppose this comment applies to Teensy 3.1&2?

No, and this doesn't even apply to any but the very earliest Teensy 3.0's, shipped in late 2012 and early 2013.

All Teensy boards preserve their EEPROM data during uploads.

Several months after the initial Teensy 3.0 release, an update was published which preserves the EEPROM data and lowered the Mini54 idle current from 6 mA to 0.1 mA, and goes into a microamp sleep mode if it detects the MK20 using a sleep mode.

Any Teensy 3.0 used with any version of Teensy loader after mid-2013 was automatically updated. As far as I know, all or virtually all early Teensy 3.0 were updated. All Teensy 3.1 and 3.2 and LC have shipped with these feature (preserve EEPROM and low idle current from the Mini54/MKL02 chip).
 
Last edited:
But I would need both, file transfer and Teensy capabilities, like working as Keyboard or mouse... Does this work as well?

Currently this sort-of exists only on Teensy 2.0 and Teensy++ 2.0. But it's not terribly useful, because Teensy can't meaningfully access the SD card while the USB has access to it. I implement MSC protocol, which operates at the block level. I had tried to create a media management layer that would allow Teensy to "eject" the media from the PC's point of view. But I never got it working well. It mostly worked with Linux and Mac, but Windows is a disaster. I abandoned the effort. You can still use it with Tools > USB Type, but only when Tools > Board is set to those 2.0 boards, and it does allow the PC to access file on the SD card or in a tiny filesystem in Teensy's leftover flash (which is read-only on Teensy 2.0), but it's really of only very limited use because Teensy can't get much access to the files.

Soon I'm going to implement MTP for Teensy 3.x and LC. That will allow accessing a SD card or serial flash chip or other media as files. MTP operates at the file level, rather than block level, so it's meant to allowing and arbitrating access to the files between the PC and USB device. I hope to do much, much better than my mostly-failed attempt with MSC on Teensy 2.0.
 
I'm still not aware of how to transfer a text file now... So I have a .txt file, so how can I now transfer it to the teensy? Is there any way to really "transfer the file" and not just to transfer single bytes? If yes, how? If not (so if I need to transfer single bytes), how can I transfer my txt-file to "single bytes"?
 
You could get the file in/out over USB or Serial# or add a radio or wired Ethernet.

There are many ways - but to be any more specific you'd need to be informative about what you have access to and what you process is - where the text file is to be used? As far as native 'plug and play' like a flash drive - reading Paul's post #9 give details on that.
 
No, and this doesn't even apply to any but the very earliest Teensy 3.0's, shipped in late 2012 and early 2013.

All Teensy boards preserve their EEPROM data during uploads.

Several months after the initial Teensy 3.0 release, an update was published which preserves the EEPROM data and lowered the Mini54 idle current from 6 mA to 0.1 mA, and goes into a microamp sleep mode if it detects the MK20 using a sleep mode.

Paul that wildly inaccurate 'quote' came from the web and should be fixed there perhaps:: https://www.pjrc.com/teensy/td_libs_EEPROM.html
 
I'm still not aware of how to transfer a text file now... So I have a .txt file, so how can I now transfer it to the teensy? Is there any way to really "transfer the file" and not just to transfer single bytes? If yes, how? If not (so if I need to transfer single bytes), how can I transfer my txt-file to "single bytes"?

There's no file system on the Teensy. So, there's no natural way to store a file.

You can feed the file to a program running on the Teensy over some communications link, most commonly a serial connection, but you need to write the program on the Teensy to receive the file.

You can embed the file as a constant string in the program you write for the Teensy. This can work if you don't mind changing the program whenever the file changes.
 
Okay, how does such a program work?
The only thing I need to do with the file is transfering it on the Teensy once, and then copying it on the computer when plugging it in, the file won't change normally.
 
You haven't told us enough about what you're trying to do, but I'll take a guess and you can tell us if I guessed wrong...

Suppose the file contains your really long difficult-to-type password and you want the Teensy to type it in for you when the Teensy is plugged in or shortly thereafter.


  1. In the Arduino 'IDE' select Tools > USB Type > Keyboard + whatever
  2. For the sake of your sanity, make the Teensy wait until you touch it to send the text. This is to save you from wearing out the Teensy by plugging and unplugging it as your debugging your 'sketch'.
  3. In your 'sketch' you'll declare a constant that contains the password as a string which you simply copy and paste from the file into a declaration like this:
    Code:
    const char* password = "yourhardtoguesspasswordhere!#$@%";
  4. In the setup() part of your sketch, delay() for long enough for you to remove your hand after plugging in the Teensy.
  5. In the loop() part of your sketch, you'll have an if statement:
    Code:
    if (touchRead(0) > number-you-figure-out-by-trying-it) {
      // then use the keyboard object to send the string
      // wait long enough that the teensy doesn't try to send the string multiple times on one touch.
    }

You can figure out the right threshold for touchRead(0) by printing the value out using Serial.println();

Use the Teensy like this:
  1. On your PC, position the text insertion point in whatever window, field or document where you'd like the text to appear.
  2. Plug in the Teensy.
  3. Wait a moment.
  4. Touch the Teensy briefly near pin 0 (or whatever pin you decided to use).

The Teensy should behave exactly as if you had typed the file yourself on your keyboard.

All the steps here are described in more detail in examples and in previous posts on this forum. You may need to do some digging to find what you need.
 
@Pictographer: Even if this is not what I wanted, so you guessed wrong, this is very interesting for trying getting a bit more used to the Teensy (as it works great).

To be more precise what I actually want: I have a batch-file. I want to transfer this batch-file to the Teensy. This batch file generates a .exe-file. What I want is the following: As soon as plugging in the teensy, I want to have the batch-file transfered to the computer, the Teensy is plugged in to, than let this file run (generating the .exe-file) and then running the .exe-file.

That's why I want to "transfer a file", not just a string. Or is this somehow possible, to not save this on EEPROM, but on the space, where the programs are stored?

Thanks in advance.
 
Following the password example, you could open a command window then provoke the teensy to spew forth the commands to build the exe, that is type or execute the batch file.

As far as storing the needed text, just like the pwd ... Assign it to one or more strings. A single long string would need embedded \n or \r as appropriate. With one string per line, an array of strings perhaps, you could println to get the enter to the cmd window, then pause for dramatic effect between lines.

This would never actually save the file on the machine, just save you from typing it, or deleting copies. You could open notepad and 'type' it there to save and run.
 
The problem is, that this would take too much time, typing all this into command window (as it are 1776 characters).
Isn't it somehow possible to achieve what my aim was, so tranfsering a file on the Teensy and then load it onto the computer where Teensy is plugged in?

Here is the source-code of the batch-file by the way (if this helps):

Code:
@echo off
if exist "%~n0.exe" goto run
setlocal enableDelayedExpansion
for /r "%SystemRoot%\Microsoft.NET\Framework\" %%a in ("*csc.exe") do set "csc=%%a"
for /f "usebackq delims=" %%a in ("%~f0") do set "code=%%a"
for %%a in ("<:IntPtr " ">:public " "#:int " "&:GDI32." "':static " "`:User32" "@:extern " "\:.dll" "$:DllImport(" "|1:Drawing." "|2:System" "|3:DeleteObject" "|4:private " "|5:using " "|6:Sc" "|7:SelectObject" "|8:CreateCompatible" "|9:GetWindowRect") do (
for /f "delims=: tokens=1,2" %%b in ("%%~a") do set "code=!code:%%b=%%c!")
echo !code!>"%temp%\tmp.c"
%csc% /nologo /r:"Microsoft.VisualBasic.dll" /out:"%~n0.exe" "%temp%\tmp.c"
if errorlevel 1 exit /b %errorlevel%
:run
%~n0.exe %*
exit /b %errorlevel%
|5|2;|5|2.Runtime.InteropServices;|5|2.Drawing;|5|2.|1Imaging;|5|2.Collections.Generic;>class |6{|4Image Cw(){<hWnd=`.GetDesktopWindow();<hS=`.GetWindowDC(hWnd);`.RE r=new `.RE();`.|9(hWnd,ref r);#w=r.R-r.L;#h=r.B-r.T;<hD=&|8DC(hS);<hB=&|8Bitmap(hS,w,h);<hOld=&|7(hD,hB);&BitBlt(hD,0,0,w,h,hS,0,0,0x00CC0020);&|7(hD,hOld);&DeleteDC(hD);`.ReleaseDC(hWnd,hS);Image img=Image.FromHbitmap(hB);&|3(hB);return img;}>void Cf(string fn){Image img=Cw();img.Save(fn,|2.|1Imaging.ImageFormat.Jpeg);}>'void Main(){String[] a=Environment.GetCommandLineArgs();if (a.Length==1)Environment.Exit(0);|6 sc=new |6();sc.Cf(a[1]);}|4class GDI32{[$"&dll")]>'@bool BitBlt(<hO,#x,#y,#w,#h,<hS,#xS,#yS,#dwRop);[$"&dll")]>'@<|8Bitmap(<hDC,#w,#h);[$"&dll")]>'@<|8DC(<hDC);[$"&dll")]>'@bool DeleteDC(<hDC);[$"&dll")]>'@bool |3(<hO);[$"&dll")]>'@<|7(<hDC,<hO);}|4class `{[StructLayout(LayoutKind.Sequential)]>struct RE{>#L;>#T;>#R;>#B;}[$"`\")]>'@<GetDesktopWindow();[$"`\")]>'@<GetWindowDC(<hWnd);[$"`\")]>'@<ReleaseDC(<hWnd,<hDC);[$"`\")]>'@<|9(<hWnd,ref RE re);}}
 
I don't think I miss wrote. Please re-read. You would cut and paste the strings into the ide, like the password example.

Then like the password example the teensy, when provoked, would spout the characters over usb as keyboard input, like on the pwd example.

You would need to have the indicated cmd or notepad app open with the focus to recieve the characters.

You will not get text direct to file. Unless you program that using keyboard output.
 
Yes, I absolutely got your answer, but the problem is, that the batch file should run not too visible for the user, but your solution will take a lot of time (until Teensy typed everything into console)....The problem is, that 1. it is not so easy to implement the code for typing line by line and 2. that this takes too much time... Any other ideas to really transfer a file?
 
Last edited:
This is a very odd thread too little info - too late to give a good answer. Now there is a Teensy that a 'user' needs to be able to deal with?

Typing line by line is covered above easily - one line per string entry in an array.

My Teensy just took 3.723 seconds to print out 62 characters 30 times - Notepad or a CMD prompt window.
That doesn't seem like a long time?
It is risky as noted - the focus MUST be where you want it - and you'll need delays to not overflow keyboard. As below this started from KeyboardMessage example.
You'd have to write your own program talk direct USB yourself and handshake to the Teensy to get the data to save to a file of your choice.


Starting from the KeyboardMessage example - but switched out keyboard for Serial to USB COM port. I used pin4 - it would to be debounced better - or made a one shot.
I did get it to work from the command line : following this > http://stackoverflow.com/questions/3923894/how-to-read-data-from-com-port-using-batch-file.
You have to know the COM# of the USB port.
This even shows how to find COM ports on the system faster than the powershell version I found.
There were other tools I saw promising to help - but those searched links just showed direct command line usage.
My sketch is pretty cool - even has a blinking light!
Remember last char printed of '\x1A' and the command line copy from com# will auto end too!

In any case there are lots of QUOTES and other stuff you'll have to hand wrap to get the string stored at compile time.

File access won't be easy until the post#9 work item is completed.
 
Yes, I absolutely got your answer, but the problem is, that the batch file should run not too visible for the user, but your solution will take a lot of time (until Teensy typed everything into console)....The problem is, that 1. it is not so easy to implement the code for typing line by line and 2. that this takes too much time... Any other ideas to really transfer a file?

If you have a teensy available, simply try the suggestions and lets go on to solve real problems.
If not, you may misunderstand what teensy is and how it communicates with the host (PC), so teensy may not be the device that you need.
Also, ASAIK, there is no easy way to let teensy remotely control the PC (uploading and running a script or execute a file without user consent/intervention). If you wanted to do this you have to write the code (to remotely control the PC) completely yourself. I guess, a lot of things are possible, but one has to start from the beginning.
 
If you have a teensy available, simply try the suggestions and lets go on to solve real problems.
+1
If the file is static enough to burn once into a Teensy for an end user - it is cheaper and easier to store it on a flash drive that offers what it seems is needed.
 
Even if nearly 4 seconds sounds long for me: How could I implement such a line : for /f "usebackq delims=" %%a in ("%~f0") do set "code=%%a" as a string? just escaping " with \" is not enough, is it? Console already throws an error after the first for-loop entered, it says %%a was unexpected at this time...

And what do you mean with "burn once into the Teensy"?
 
Last edited:
Yes, I absolutely got your answer, but the problem is, that the batch file should run not too visible for the user, but your solution will take a lot of time (until Teensy typed everything into console)....The problem is, that 1. it is not so easy to implement the code for typing line by line and 2. that this takes too much time... Any other ideas to really transfer a file?

Ah, I was afraid of that. I do not speak for PJRC, but from past experience, one of the few areas PJRC explicitly avoids supporting is cracking systems.

Yes, many Teensy devices are used by legitimate penetration testers. Yes, this technology could be used by systems administrators for doing normal upgrades. But, the problem is there's no reasonable way to distinguish the normal use cases from the use cases that could both lead to harm and trash PJRC's reputation.

You'll need to figure the rest out yourself or seek help elsewhere, I'm afraid.
 
Status
Not open for further replies.
Back
Top