Since motion detection is in several builds I'll post this here.
The wiki docs say md_get_cell_diff will return a value between 0 and 255. But it can return negative values sometimes. The testing for motion (and threshold) is don't like so:
if(motion_detector.points[idx]>0){
motion_detector.prev[idx] = (motion_detector.curr[idx]-motion_detector.prev[idx])/motion_detector.points[idx];
tmp2 = ( motion_detector.prev[idx] < 0 ) ? -motion_detector.prev[idx] : motion_detector.prev[idx] ;
}
if( tmp2 > motion_detector.threshold ){
if (motion_detector.start_time+motion_detector.msecs_before_trigger < tick){
motion_detector.detected_cells++;
}
See how motion_detector.prev[] is updated to the cell's difference, but this can be negative if the previous cell's value was brighter than the current cell.
Then the tmp2 variable is used for the threshold test, but it's first made positive (absolute) for the test.
But md_get_cell_diff returns:
return motion_detector.prev[ MD_XY2IDX(column-1,row-1) ];
In other words it returns the "raw" difference, which may be negative.
I guess the best way to fix this is to remove the tmp2 variable and do the threshold test on motion_detector.prev[] ?
Then again, thinking about it, it's actually useful the way it is since a script can check which way brightness has changed for an individual cell. For instance checking for a dark object moving across a brighter background, or visa versa. It just means the script will need a couple more lines if the abs difference is required.