matsstaff / stc1000p

Programmable thermostat firmware and arduino based uploader for the STC-1000 thermostat
GNU General Public License v3.0
267 stars 47 forks source link

optimize handle_com by refactoring state and 16/8 bit data #87

Closed seanmcveigh closed 8 years ago

seanmcveigh commented 8 years ago

I refactored the 16-bit data into a word/bytes union so shifting isn't necessary. Also refactored the state enum into a bitvector.. it is cheaper to check/set bits than to compare against literals. As part of the state refactor, I re-arranged the order in which the if/else is executed because a further optimization is to allow a multi-bit state to exist. Basically, check for the most evolved state first. eg. for a write, the states progress as: 0
com_recv_addr com_recv_addr | com_recv_data1 com_recv_addr | com_recv_data1 | com_recv_data2 com_recv_addr | com_recv_data1 | com_recv_data2 | com_recv_checksum 0

please sanity check.. I have not tried running it yet :) (sorry about the 2 hex files that snuck through)

seanmcveigh commented 8 years ago

please be careful if you work on this code again... the order of operations was pretty important to get the 46 instruction savings. (back-to-back variable accesses, etc.) :)

matsstaff commented 8 years ago

Just had a quick look. Very nice! I'll try it out as soon as I can. I can't promise to be careful, as I lack your 'touch' at optimizing, but hopefully the com code shouldn't need much change (maybe timing could be more relaxed to avoid communication errors, I checked the jitter with my scope, and sort of made it as fast as I dared.... In fact! With your optimizations, maybe it will even run better now :) ). Great job! I really like the union stuff for 16-bit, and the bitfield bit twiddling. That is pure gold!