Go Down

Topic: Obfuscation, got a good one? (Read 810 times) previous topic - next topic

avr_fred

Having just read this thread, it occurred to me that we don't see much in the way of obfuscated code. I encountered some the other night, in demo code for the ClickEncoder library. While it wasn't difficult figure out, it was totally unnecessary, especially for a demo program. The silliness begins with the #define on line 72, as found on Github, I did not reformat.

Code: [Select]
#define WITH_LCD 1

#include <ClickEncoder.h>
#include <TimerOne.h>

#ifdef WITH_LCD
#include <LiquidCrystal.h>

#define LCD_RS       8
#define LCD_RW       9
#define LCD_EN      10
#define LCD_D4       4
#define LCD_D5       5
#define LCD_D6       6
#define LCD_D7       7

#define LCD_CHARS   20
#define LCD_LINES    4

LiquidCrystal lcd(LCD_RS, LCD_RW, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
#endif

ClickEncoder *encoder;
int16_t last, value;

void timerIsr() {
  encoder->service();
}

#ifdef WITH_LCD
void displayAccelerationStatus() {
  lcd.setCursor(0, 1); 
  lcd.print("Acceleration ");
  lcd.print(encoder->getAccelerationEnabled() ? "on " : "off");
}
#endif

void setup() {
  Serial.begin(9600);
  encoder = new ClickEncoder(A1, A0, A2);

#ifdef WITH_LCD
  lcd.begin(LCD_CHARS, LCD_LINES);
  lcd.clear();
  displayAccelerationStatus();
#endif

  Timer1.initialize(1000);
  Timer1.attachInterrupt(timerIsr);
 
  last = -1;
}

void loop() { 
  value += encoder->getValue();
 
  if (value != last) {
    last = value;
    Serial.print("Encoder Value: ");
    Serial.println(value);
#ifdef WITH_LCD
    lcd.setCursor(0, 0);
    lcd.print("         ");
    lcd.setCursor(0, 0);
    lcd.print(value);
#endif
  }
 
  ClickEncoder::Button b = encoder->getButton();
  if (b != ClickEncoder::Open) {
    Serial.print("Button: ");
    #define VERBOSECASE(label) case label: Serial.println(#label); break;
    switch (b) {
      VERBOSECASE(ClickEncoder::Pressed);
      VERBOSECASE(ClickEncoder::Held)
      VERBOSECASE(ClickEncoder::Released)
      VERBOSECASE(ClickEncoder::Clicked)
      case ClickEncoder::DoubleClicked:
          Serial.println("ClickEncoder::DoubleClicked");
          encoder->setAccelerationEnabled(!encoder->getAccelerationEnabled());
          Serial.print("  Acceleration is ");
          Serial.println((encoder->getAccelerationEnabled()) ? "enabled" : "disabled");
#ifdef WITH_LCD
          displayAccelerationStatus();
#endif
        break;
    }
  }   
}


If you have a good unintentional example, post away. Let's get keep this simple, Arduino only, no obfuscation contest material please.

AWOL

I can't see any obfuscation in that.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

allanhurst

#2
Aug 23, 2017, 11:01 pm Last Edit: Aug 23, 2017, 11:10 pm by allanhurst
My only grumble might be.....
Code: [Select]
#ifdef WITH_LCD
    lcd.setCursor(0, 0);
    lcd.print("         ");
    lcd.setCursor(0, 0);
    lcd.print(value);
#endif


The ensuing code doesn't know the display size, so doesn't know how many spaces to overwrite .....
perhaps it could be

Code: [Select]


#ifdef WITH_LCD
    lcd.clear;
    lcd.setCursor(0, 0);
    lcd.print(value);
#endif


Depends on the context.

Allan

AWOL

My main grumble would then be
Code: [Select]
    lcd.clear;
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

travis_farmer

My main grumble would then be
Code: [Select]
    lcd.clear;
forgive me from being a bit out of the loop() with my other projects occupying my time, but what is wrong with "lcd.clear;"? other than it tends to cause a flicker on the screen.

~Travis
Current Obsession: Photography, astronomy, server rack cooler.
Check out my website, i have my own very under-used forum on my hobby server.

AWOL

It only causes flicker if you call it.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

allanhurst

Yes - but only a small one.

If completely re-writing a screen as when swapping states in a FSM it's useful.

Allan

AWOL

Yes - but only a small one.
Yes, but only if you call it [hint]
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

MAS3

I wouldn't spend too many words on it, AWOL.

But for it's function, it's got the right name.
So no obfuscation there, it's a good demo sketch doing what it claims to be.
Have a look at "blink without delay".
Did you connect the grounds ?
Je kunt hier ook in het Nederlands terecht: http://arduino.cc/forum/index.php/board,77.0.html

Delta_G

Yes, but only if you call it [hint]
I see what you're getting at. 

There's no problem with using the clear function.  But that's not what the line of code AWOL cut out does.  It's missing something important. 
If at first you don't succeed, up - home - sudo - enter.

MAS3

Tsss.

I've been looking at the code in the 1st. post, to which AWOL's comment "only if you call it" is also valid.
Have a look at "blink without delay".
Did you connect the grounds ?
Je kunt hier ook in het Nederlands terecht: http://arduino.cc/forum/index.php/board,77.0.html

Go Up