Hi ciuffoly,
rern has developed some add-ons, one of these is called GPIO, see
#5370 GPIO from rern is designed to do something different from what you require, but examples all the functionality you need seems to be available. If you make the solution generic enough maybe rern will distribute it with his other add-ons.
janui