Text section exceeds available space in board

pls how can I streamline him? [XD
usb debug

#include <Keyboard.h>
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 32, &Wire);

File myFile;
boolean first = true;
String fileName="payload.txt";
int SD_mode = 0;
Sd2Card card;
SdVolume volume;
SdFile root;


String bufferStr = "";
String last = "";
int defaultDelay = 0;


int key1 = 8;
int key2 = 6;
int key3 = 12 ;
int key4 = 4;
int LedA = 9;
int LedB = 10;
int enablestorage = 7;
int keystatus = 0;
int delaytime = 60;


int SW2 = A5;
int JoystickPush = A4;
int JoystickRight = 5;
int JoystickLeft = 11;
int Buzzer = 13;
const int chipSelect = PIN_SPI_SS;



int modeSelect()
{
  display.println("Payload Select");
  display.println("1.se_mode");
  display.println("2.duck_mode");
  display.println("3.wifi_mode");
  display.display();
  int mode = 2;
  while (!digitalRead(SW2))
  {
    if (digitalRead(JoystickLeft))
    {
      delay(10);
      if (digitalRead(JoystickRight))
      {
       mode = 1;
      }
      else{}
    }
    else if (digitalRead(JoystickRight))
    {
      delay(10);
      if (digitalRead(JoystickRight))
      {
        mode= 3;
      }
      else{}
    }
    else if(digitalRead(JoystickPush))
    {
     delay(10);
      if (digitalRead(JoystickPush))
      {
        mode= 2;
      }
      else{}
    }  
    display.clearDisplay();
    display.println("Payload Select");
    if (mode == 1)
    {
      display.println("1.SE_MODE");
    }
    else
    {
      display.println("1.se_mode");
    }
    if (mode == 2)
    {
      display.println("2.DUCK_MODE");
    }
    else
    {
      display.println("2.duck_mode");
    }
    if (mode == 3)
    {
      display.println("3.WIFI_MODE");
    }
    else
    {
      display.println("3.wifi_mode");
    }
    display.display();
  }
  return mode;
}


void buzzer(int times)
{
  for (int i = 1; i <= times; i++)
  {
    digitalWrite(Buzzer, HIGH);
    delay(100);
    digitalWrite(Buzzer, LOW);
    delay(200);
  }
}
void led(bool led1, bool led2)
{
  digitalWrite(LedA, led1);
  digitalWrite(LedB, led2);
}
void breakpoint()
{
  while(!digitalRead(JoystickPush))
  {
    delay(30);
  }
}

void runPayload(int mode,int Payload_X)
{
    if (mode == 3)
    {
      wifiduck();
    }
    else  if(mode==2)
    {
      const char x[]={Payload_X};
      fileName.replace("payload",x);
      fileName.replace(fileName,".txt");
      rubberducky(fileName);
    }
  {
    switch(Payload_X)
  {
    case 0:
    {
      //payload0
      break;
    }
    case 1:
    {
      //payload1
      break;
    }
    case 2:
    {
      //payload2
      break;
    }
    case 3:
    {
      //payload3
      break;
    }
    case 4:
    {
      //payload4
      break;
    }
    case 5:
    {
      //payload5
      break;
    }
    case 6:
    {
      //payload6
      break;
    }
    case 7:
    {
      //payload7
      break;
    }
    case 8:
    {
      //payload8
      break;
    }
    case 9:
    {
      //payload9
      break;
    }
    case 10:
    {
      //payload10
      break;
    }
    case 11:
    {
      //payload11
      break;
    }
    case 12:
    {
      //payload12
      break;
    }
    case 13:
    {
      //payload13
      break;
    }
    case 14:
    {
      //payload14
      break;
    }
    case 15:
    {
      //payload15
      break;
    }
  }
  }
}



void rubberducky(String duckyScript)
{
  
  if (!SD.begin(PIN_SPI_SS)) {
    return;
  }

  myFile = SD.open(duckyScript);
  if (myFile) {
    Keyboard.begin();

    String line = "";
    while (myFile.available()) {
      char m = myFile.read();
      if (m == '\n') {
        Line(line);
        line = "";
      }
      else if ((int) m != 13)
      {
        line += m;
      }
    }
    Line(line);

    myFile.close();
  } else {

  }

  Keyboard.end();
}

void wifiduck()
{
  Keyboard.begin();
  while (1)
  {
    if(Serial1.available()) {
    bufferStr = Serial1.readStringUntil("END");
  }
  
  if(bufferStr.length() > 0){
    
    bufferStr.replace("\r","\n");
    bufferStr.replace("\n\n","\n");
    
    while(bufferStr.length() > 0){
      int latest_return = bufferStr.indexOf("\n");
      if(latest_return == -1){
        Line(bufferStr);
        bufferStr = "";
      } else{
        Line(bufferStr.substring(0, latest_return));
        last=bufferStr.substring(0, latest_return);
        bufferStr = bufferStr.substring(latest_return + 1);
      }
    }
    
    bufferStr = "";
    Serial1.write(0x99);
  }
  }
}

void Line(String l)
{
  int space_1 = l.indexOf(" ");
  if (space_1 == -1)
  {
    Press(l);
  }
  else if (l.substring(0, space_1) == "STRING")
  {
    Keyboard.print(l.substring(space_1 + 1));
  }
  else if (l.substring(0, space_1) == "DELAY")
  {
    int delaytime = l.substring(space_1 + 1).toInt();
    delay(delaytime);
  }
  else if (l.substring(0, space_1) == "REM") {}
  else
  {
    String remain = l;

    while (remain.length() > 0)
    {
      int latest_space = remain.indexOf(" ");
      if (latest_space == -1)
      {
        Press(remain);
        remain = "";
      }
      else
      {
        Press(remain.substring(0, latest_space));
        remain = remain.substring(latest_space + 1);
      }
      delay(5);
    }
  }

  Keyboard.releaseAll();
}


void Press(String b)
{
  if(b.length() == 1) Keyboard.press(char(b[0]));
  else if (b.equals("ENTER")) Keyboard.press(KEY_RETURN);
  else if (b.equals("CTRL")) Keyboard.press(KEY_LEFT_CTRL);
  else if (b.equals("SHIFT")) Keyboard.press(KEY_LEFT_SHIFT);
  else if (b.equals("ALT")) Keyboard.press(KEY_LEFT_ALT);
  else if (b.equals("GUI")) Keyboard.press(KEY_LEFT_GUI);
  else if (b.equals("UP") || b.equals("UPARROW")) Keyboard.press(KEY_UP_ARROW);
  else if (b.equals("DOWN") || b.equals("DOWNARROW")) Keyboard.press(KEY_DOWN_ARROW);
  else if (b.equals("LEFT") || b.equals("LEFTARROW")) Keyboard.press(KEY_LEFT_ARROW);
  else if (b.equals("RIGHT") || b.equals("RIGHTARROW")) Keyboard.press(KEY_RIGHT_ARROW);
  else if (b.equals("DELETE")) Keyboard.press(KEY_DELETE);
  else if (b.equals("PAGEUP")) Keyboard.press(KEY_PAGE_UP);
  else if (b.equals("PAGEDOWN")) Keyboard.press(KEY_PAGE_DOWN);
  else if (b.equals("HOME")) Keyboard.press(KEY_HOME);
  else if (b.equals("ESC")) Keyboard.press(KEY_ESC);
  else if (b.equals("INSERT")) Keyboard.press(KEY_INSERT);
  else if (b.equals("TAB")) Keyboard.press(KEY_TAB);
  else if (b.equals("END")) Keyboard.press(KEY_END);
  else if (b.equals("CAPSLOCK")) Keyboard.press(KEY_CAPS_LOCK);
  else if (b.equals("F1")) Keyboard.press(KEY_F1);
  else if (b.equals("F2")) Keyboard.press(KEY_F2);
  else if (b.equals("F3")) Keyboard.press(KEY_F3);
  else if (b.equals("F4")) Keyboard.press(KEY_F4);
  else if (b.equals("F5")) Keyboard.press(KEY_F5);
  else if (b.equals("F6")) Keyboard.press(KEY_F6);
  else if (b.equals("F7")) Keyboard.press(KEY_F7);
  else if (b.equals("F8")) Keyboard.press(KEY_F8);
  else if (b.equals("F9")) Keyboard.press(KEY_F9);
  else if (b.equals("F10")) Keyboard.press(KEY_F10);
  else if (b.equals("F11")) Keyboard.press(KEY_F11);
  else if (b.equals("F12")) Keyboard.press(KEY_F12);
  else if (b.equals("SPACE")) Keyboard.press(' ');
}

void setup()
{
  pinMode(key1, INPUT);
  pinMode(key2, INPUT);
  pinMode(key3, INPUT);
  pinMode(key4, INPUT);
  pinMode(LedA, OUTPUT);
  pinMode(LedB, OUTPUT);
  pinMode(enablestorage, OUTPUT);
  pinMode(SW2, INPUT);
  pinMode(JoystickPush, INPUT);
  pinMode(JoystickRight, INPUT);
  pinMode(JoystickLeft, INPUT);
  pinMode(Buzzer, OUTPUT);
  digitalWrite(enablestorage, HIGH); //
  digitalWrite(LedA, LOW); //A
  digitalWrite(LedB, LOW); //B
  Serial1.begin(57200);
  delay(100);
  display.begin(SSD1306_SWITCHCAPVCC,0x3C);
  display.display();
  delay(1000);
  display.clearDisplay();
  display.display();
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setCursor(0,0);
   
  int mode=modeSelect();
  int k1 = digitalRead(key1);
  int k2 = digitalRead(key2);
  int k3 = digitalRead(key3);
  int k4 = digitalRead(key4);
  int payload = k1 * 1 + k2 * 2 + k3 * 4 + k4 * 8;
  runPayload(mode,payload);

   //attachInterrupt(digitalPinToInterrupt(SW2), breakpoint, HIGH);
}

void loop()
{
}

i suggest 2 things

  • i believe you can move strings to FLASH using the F() macro, see PROGMEM
  • the above which looks like it maps a String to a key-code could be captured in a table with a small piece of code that searches the table and returns the code
  • the above could be
    switch(Payload_X)   {
    case 0:
    case 1:
    case 2:
      break;
1 Like

Please post the error message. Text section means too much code, i.e. your libraries are too big.

1 Like
void runPayload(int mode, int Payload_X)
{
    const char x[] = {Payload_X};
    fileName.replace("payload", x);
    fileName.replace(fileName, ".txt");

These lines don't make sense. Did you want String filename; to be the text representation of the number 'Payload_X' followed by ".txt"? To do that, try:
fileName = String(Payload_X) + ".txt";
or

    filename = Payload_X;
    filename += ".txt";

You can't specify a string for the terminator of readStringUntil(). It only takes one character.
bufferStr = Serial1.readStringUntil("END");

I think you want:
bufferStr = Serial1.readString();

To get an idea of where all the space went, here are the object file sizes of my compile:

190412	Adafruit_GFX.cpp.o
114020	Adafruit_SPITFT.cpp.o
109700	Adafruit_SSD1306.cpp.o
107132	SdFile.cpp.o
60312	Sd2Card.cpp.o
50264	SD.cpp.o
40148	Adafruit_GrayOLED.cpp.o
36868	Adafruit_SPIDevice.cpp.o
35284	Wire.cpp.o
32712	Adafruit_BusIO_Register.cpp.o
30600	SdVolume.cpp.o
29820	twi.c.o
27316	File.cpp.o
22904	Keyboard.cpp.o
21660	HID.cpp.o
20104	Adafruit_I2CDevice.cpp.o
11544	SPI.cpp.o
4964	KeyboardLayout_fr_FR.cpp.o
4960	KeyboardLayout_en_US.cpp.o
4948	KeyboardLayout_de_DE.cpp.o
4936	KeyboardLayout_es_ES.cpp.o
4932	KeyboardLayout_it_IT.cpp.o
1864	glcdfont.c.o

1 Like

See Insufficient program storage space