Saturday, October 18, 2014

AMD Radeon official driver experience

For the Radeon 8330 GPU inside Lenovo g405 powered by AMD A4-5000 APU, I tried out the official driver on Ubuntu 14.04.
The situation is somewhat obscure. The official driver listed on AMD support page doesn't support Ubuntu 14.04. So I had to drop it.
There is one beta driver that supports Ubuntu 14.04 though. So I downloaded it and thought to try it. Took me some efforts to install it though, it just would not install in GUI Mode, problem shown as missing language packs.
Anyway I went ahead with text mode install. After rebooting though, no desktop. Only background with mouse cursor.
So I reverted to open source driver. Later I had another idea, so I went ahead and tried out the official driver available in jocky (proprietary drivers) tool. And after installing that one, I did get a desktop, but the brightness handles were gone.
After searching for a while and not finding anything anywhere, I again reverted to open source driver.
Thankfully that one works okay with my brightness handling script which ensures proper brightness value from Lenovo backlight class is passed to radeon-bl0 backlight class.

Saturday, October 11, 2014

Script for maintaining brightness on Lenovo G405

I observed that the this AMD A4 powered laptop has two backlight classes names idepad and radeon-bl0. Pressing the brightness keys changes brightness values in idepad class while values in radeon-bl0 needs to be changed.
So I wrote a script which will keep the two files in sync and help me get the brightness working.

# Script to sync brightness from sys/classes/idepad to /sys/classes/radeon-bl0
# Task: run in a loop executed every second,  modify radeon  brightness acccording to ideapad brightness
# ideapad brightness from 1 to 16 in steps of 1;
# Radeon brightness 1 to 255 in steps of 1;
while [ 1 -eq 1 ]
IBRIGHTNESS=`cat /sys/class/backlight/ideapad/brightness`
if [ $IBRIGHTNESS -le 1 ]
echo "Ideapad Brightness: $IBRIGHTNESS"
echo "Radeon Brightness to be set: $RBRIGHTNESS"
echo $RBRIGHTNESS>/sys/class/backlight/radeon_bl0/brightness
sleep 1

Tuesday, October 7, 2014

Enum to String and vice versa - Enum.Parse alternative for C++

Original Article Link:

Update: Link to Source.


While adding Serialization support to my project, I realized that I would require some way to convert a string to an enumerator and vice versa. So I did a Google search for the same, and found a lot of information; different ways in which people implemented this functionality.
However, all the solutions I found suffered from one or more of the following:
  • No support for enumerators with non-contiguous values
  • No support (not even partial support) for enumerators with duplicate values
  • No support for existing enumerations (without modifying their source code)
  • Requires one or more extra files per enumeration
  • Requires source-code to be pre-processed (by a custom binary) before compilation
  • Difficult to use or maintain
  • Highly susceptible to typos
  • Is platform/compiler specific (not portable)
So (the rip-off that I am), I borrowed the good ideas from all the solutions I found, added a few of my own and mixed-and-matched to create the code which accompanies this article. I don't claim this to be the best solution for every case, just that it solved my problem nicely and that it could be of use to someone else as well.

How To Use the Code

Using the code is quite easy. All we need to do is add one file: EnumString.h (see the source code accompanying this article) to our project.
Let's say we wanted to create an enum to represent one of the Furious Five Masters (Kung Fu Panda (2008) anyone?). So we go ahead and declare it as usual:
Collapse | Copy Code
// Furious Five Master  enum Master  {      Tigress = 5,      Viper = 3,      Monkey = 4,      Mantis = 1,      Crane = 2  };
Don't worry about the values assigned, they are simply my rough idea of what the 'mass' of each master is, in some imaginary units. Now to add stringizing support, we need to declare the enum again, but in a different format (uses helper macros):
Collapse | Copy Code
// String support for Furious Five Master  Begin_Enum_String( Master )  {      Enum_String( Tigress );      Enum_String( Viper );      Enum_String( Monkey );      Enum_String( Mantis );      Enum_String( Crane );  }  End_Enum_String;
And we're done! Note that since this second declaration lies in the same header/source file as the actual enum definition (probably declared just below it), it's not that difficult to update it whenever we modify the actual enumeration.
Now we can convert from a string to a Master enumerator and vice versa, quite easily. The following code shows how to do that:
Collapse | Copy Code
// Convert from a Master enumerator to a string  const std::string &masterStr = EnumString<Master>::From( Monkey );  assert( "Monkey" ) == 0 );    // Convert from a string to a Master enumerator  Master master = Tigress;  const bool bResult = EnumString<Master>::To( master, masterStr );  assert( bResult == true );  assert( master == Monkey );

Using the Code with Existing Enumerations

Suppose we want to add stringizing support to an existing enumeration from a library, which is namespaced. Imagine that the enum is declared like this in the library:
Collapse | Copy Code
namespace SomeLibrary  {      enum WeekEnd      {          Sunday = 1,          Saturday = 7      };  }
Say that we can't modify the library files (which is anyway not a good practice). So we create a separate header file in our project, in which we will declare stringizing support for the required library enum. For the declaration, we have 3 options:

Option 1 - Fully Qualify All the Names

Collapse | Copy Code
Begin_Enum_String( SomeLibrary::WeekEnd )  {      Enum_String( SomeLibrary::Sunday );      Enum_String( SomeLibrary::Saturday );  }  End_Enum_String;
The consequence of this is that the stringized enums will also be fully qualified names. So, the statement EnumString<WeekEnd>::From( SomeLibrary::Saturday ) will yield "SomeLibrary::Saturday", and not just "Saturday".

Option 2 - Use the 'using namespace' Directive

Collapse | Copy Code
using namespace SomeLibrary;    Begin_Enum_String( WeekEnd )  {      Enum_String( Sunday );      Enum_String( Saturday );  }  End_Enum_String;

Option 3 - Register the Enumerators Yourself

Without using the 'Enum_String' helper macro (see the next section 'How does it actually work?', for an explanation of this):
Collapse | Copy Code
Begin_Enum_String( SomeLibrary::WeekEnd )  {      RegisterEnumerator( SomeLibrary::Sunday, "Sunday" );      RegisterEnumerator( SomeLibrary::Saturday, "Saturday" );  }  End_Enum_String;

How Does It Actually Work?

It's not required to know how it works in order to use it, so those who are not really interested can skip this section. Also, beginners might have to brush up on their C++ before reading this.
If you look at the declarations of the helper macros, you'll see that the definition of the string support for FuriousFiveMaster, works out to the following:
Collapse | Copy Code
template <> struct EnumString<Master> :      public EnumStringBase< EnumString<Master>, Master >  {      static void RegisterEnumerators()      {          RegisterEnumerator( Tigress, "Tigress" );          RegisterEnumerator( Viper,   "Viper" );          RegisterEnumerator( Monkey,  "Monkey" );          RegisterEnumerator( Mantis,  "Mantis" );          RegisterEnumerator( Crane,   "Crane" );      }  }
You might have already realized that the above is a specialization of the EnumString template class. It defines the RegisterEnumerators function which is used by its base class EnumStringBase, via CRTP. Now the workings of the usage become clear. When you use the functions EnumString<Master>::From or EnumString<Master>::To, you're using the version of the EnumString template class which is specialized with the Master enumeration.


As with most things, the code does have some drawbacks. The two most important ones are:
  • Doesn't support conversion of enumerators with duplicate values to strings (although vice versa works just fine). An attempt to convert such an enumerator will yield an empty string (which can be tested for).
  • Conversion performance might be a bottleneck for some applications. A single std::map is used internally for storing the relationship of enumerators to their string representations. So lookups during conversions are not in constant time. A conversion from an enumerator to a string will be in linear time, although a vice versa conversion should be very fast.


Since the code makes extensive use of templates, it may not work with older compilers. The code has been tested with the following compilers:
  • Microsoft Visual C++ 2005/2008 
  • GCC 4.4.0 
There is much potential for improvement of the code. But since the current code is good enough for my needs, I'll leave that for someone else to do. If you make changes to the code, improve it, or simply have an entirely different and better way to solve this, please share it with me also; I might just dump my code and use yours instead! Smile | :)


  5. C___enums_to_strings.aspx

Friday, October 3, 2014

The death of my Laptop

Sometimes things are going as usual and all of a sudden something fails. And as it happens usually the failure is unanticipated. And then starts thee scrambling phase where you start looking for alternatives. And ways to overcome this loss. And maybe if you are like me, you start to analyse the failure and try to understand where you failed.
So to start with, my laptop died last week. It was working fine and as it happens with all electronics, it just stopped. It wouldn't start. So I sent it to a nearby repair guy, who told me that the power circuit is dead. He will try replacing the burnt IC's.
Few days later I rang him up for updates and he said that the main board is dead. The replacement IC's also burned up. And so my laptop was gone to death, since I don't think there's any way to get a replacement board since the system was four year old.
Anyway, coming back to the analysis, I assumed that the system will continue to work. So I never made any plans for failure. Also I assumed the HDD would probably fail. So I had backup of all important data. But I didn't assume that I'll be without a system - ever. Which meant whatever projects I have running parallel to my job, they all are stuck for a fortnight. Thankfully I haven't taken any external work recently which meant I wasn't needed to really scramble.
So now I don't have a computer. I'm making do with my backup android phone. But its limited. And my projects which included learning a new technology and some not-so-critical development have stalled. And worst I don't have a budget allocated for a new computer. So I'm totally caught in a limbo here.
Well, let's see. I'm going to see if I can leave things as they are - means continuing with the android phone as a drop-in replacement and carry on. That sucks a bit. But you got choices and then payments, as they say!!!