Software is also somewhat protected because unlike a physical device reverse engineering is far more difficult. I can open pretty much anything and have a CAD model in less time than the original engineer took to design it.
Patents are a contract, the state gives you monopoly, you give the state the knowledge(with plans, drawings and explanations) of how your thing works. Without showing the code, you should not be granted a patent.