int math_intLength(int intNumber) { int inv = 0, count = 0, originalInt = intNumber; if(intNumber < 0) { intNumber = math_abs(intNumber); } while (intNumber > 0) { count = count + 1; //~ intNumber = intNumber / 10; inv = inv * 10 + (intNumber % 10); intNumber = intNumber / 10; //since reverseNumber is int, dividing by ten also floors integer } if(originalInt != 0) { return count; }else if(originalInt == 0)//if the original in is 0, above will not enter while loop, so count will equal 0 { //and will return a number length of 0, so here i will brute return a length of 1 return 1; } }
void set_spin_angle(int32_t compass_heading){ if(!s_spinning) { return; } // Allocate a static output buffer static char s_buffer[32]; static int32_t diff, deg; diff = math_abs(TRIGANGLE_TO_DEG(prev_compass_heading - compass_heading)); if (TESTING){ APP_LOG(APP_LOG_LEVEL_DEBUG, "prev compass heading: %d", (int)prev_compass_heading); APP_LOG(APP_LOG_LEVEL_DEBUG, "compass heading: %d", (int)compass_heading); APP_LOG(APP_LOG_LEVEL_DEBUG, "diff: %d", (int)diff); } if (prev_compass_heading > compass_heading && diff > 5) { angle -= 10 * TRIG_MAX_ANGLE / 360; if(TESTING) APP_LOG(APP_LOG_LEVEL_DEBUG, "clockwise"); } else if(diff > 5) { angle += 10 * TRIG_MAX_ANGLE / 360; if(TESTING) APP_LOG(APP_LOG_LEVEL_DEBUG, "counterclockwise"); } prev_compass_heading = compass_heading; // Set the number of spins completed deg = TRIGANGLE_TO_DEG(angle); spins = (int16_t)((math_abs(deg) + 20) / 180); // Turn off alarm if (spins == MAX_SPINS) { set_alarm_on(false); } APP_LOG(APP_LOG_LEVEL_DEBUG, "deg: %d", (int)deg); APP_LOG(APP_LOG_LEVEL_DEBUG, "spins: %d", (int)deg); // Set spin text snprintf(s_buffer, sizeof(s_buffer), "%d", MAX_SPINS - spins); text_layer_set_text(s_spin_spins_text_layer, s_buffer); layer_mark_dirty(s_spin_triangle_canvas_layer); }
void ui_cut_begin_cmdl(t_event e, void *data) { extern t_command_line g_cmdl; int pos_1; int pos_2; int size; (void) e; (void) data; ui_init_str_line(g_cmdl.copy); pos_1 = 0; pos_2 = g_cmdl.cursor; size = math_abs(pos_2 - pos_1); ui_copy_zone_cmdl(g_cmdl.str + pos_1, size, g_cmdl.copy, 0); ui_delete_zone_str(g_cmdl.str + pos_1, size); g_cmdl.cursor = pos_1; g_cmdl.size += -size; }
void ClydeDev::calibrateEye() { // check if it is time to calibrate or not if(millis()-last_sampled < TIME_THRESH) return; // let's read the sensor now previous_ir_raw = current_ir_raw; current_ir_raw = analogRead(eye_ir_pin); if(LOG_LEVEL <= DEBUG) *debug_stream << "current ir raw: " << current_ir_raw << " previous ir raw: " << previous_ir_raw << endl; // check if the eye is pressed -- don't calibrate if it is! if(isEyePressed() && press_thresh > 0) return; // check what the delta is- if it is not stable, we will try again next time uint16_t raw_delta = 0; if(previous_ir_raw != current_ir_raw) raw_delta = math_abs(previous_ir_raw, current_ir_raw); if(raw_delta >= DELTA_THRESH) { if(LOG_LEVEL <= DEBUG) *debug_stream << "too much delta in raw vals: " << raw_delta << endl; return; } // capture the min and max vals if(sample_count == 0) { ir_min_raw = current_ir_raw; ir_max_raw = current_ir_raw; } if(current_ir_raw < ir_min_raw) ir_min_raw = current_ir_raw; if(current_ir_raw > ir_max_raw) ir_max_raw = current_ir_raw; // increment ir_total += current_ir_raw; sample_count++; if(LOG_LEVEL <= DEBUG) *debug_stream << "sample count: " << sample_count << endl; // now it is time to calculate the average val if(sample_count >= SAMPLE_SIZE) { // calculate average previous_ir_val = current_ir_val; float temp = (float)( (float)(ir_total) / SAMPLE_SIZE); current_ir_val = (uint16_t)temp; float temp2 = (float)((float)(ir_min_raw + ir_max_raw) / 2 ); ir_range_avg = temp2; if(LOG_LEVEL <= DEBUG) *debug_stream << "current ir val: " << current_ir_val << endl; if(LOG_LEVEL <= DEBUG) *debug_stream << "ir min: " << ir_min_raw << " ir max: " << ir_max_raw << endl; // reset! ir_total = 0; sample_count = 0; // check what the range is- if it is not stable, we will try again next sample // commented out as we do not need it -- but we left it in case you need it /* uint16_t range_delta = math_abs(ir_min_raw, ir_max_raw); if(range_delta >= RANGE_THRESH) { if(LOG_LEVEL <= DEBUG) *debug_stream << "range is not stable: " << range_delta << endl; return; } */ // check what the delta is- if it is not stable, we will try again next sample // commented out as we do not need it -- but we left it in case you need it! /* uint16_t avg_delta = 0; if(current_ir_val != previous_ir_val) avg_delta = math_abs(current_ir_val, previous_ir_val); if(avg_delta >= DELTA_THRESH) { if(LOG_LEVEL <= DEBUG) *debug_stream << "avg delta is not stable: " << avg_delta << endl; return; } */ // calculate the new press theshold press_thresh = current_ir_val + PRESS_SENSITIVITY; // we are done now done_calibration = true; last_calibrated = millis(); } last_sampled = millis(); }
void VGA_put_line(int x0, int y0, int x1, int y1, char color) { /* http://en.wikipedia.org/wiki/Bresenham's_line_algorithm function line(x0, y0, x1, y1) boolean steep := abs(y1 - y0) > abs(x1 - x0) if steep then swap(x0, y0) swap(x1, y1) if x0 > x1 then swap(x0, x1) swap(y0, y1) int deltax := x1 - x0 int deltay := abs(y1 - y0) int error := deltax / 2 int ystep int y := y0 if y0 < y1 then ystep := 1 else ystep := -1 for x from x0 to x1 if steep then plot(y,x) else plot(x,y) error := error - deltay if error < 0 then y := y + ystep error := error + deltax */ bool steep = math_abs(y1-y0) > math_abs(x1-x0); int tmp = 0; if(steep) { tmp = x0; x0 = y0; y0 = tmp; tmp = x1; x1 = y1; y1 = tmp; } if(x0 > x1) { tmp = x0; x0 = x1; x1 = tmp; tmp = y0; y0 = y1; y1 = tmp; } int deltax = x1-x0; int deltay = math_abs(y1-y0); int error = deltax / 2; int ystep; int y = y0; if(y0 < y1) ystep = 1; else ystep = -1; for(int x = x0; x < x1;x++) { if(steep) mode.putpixel(y, x, color); else mode.putpixel(x,y,color); error = error - deltay; if(error < 0) { y = y + ystep; error = error +deltax; } } }
DECLARE_TEST( math, utility ) { int i; EXPECT_REALONE( math_abs( REAL_ONE ) ); EXPECT_REALONE( math_abs( -REAL_ONE ) ); EXPECT_REALZERO( math_abs( REAL_ZERO ) ); EXPECT_REALEQ( math_abs( REAL_MAX ), REAL_MAX ); EXPECT_REALEQ( math_abs( -REAL_MAX ), REAL_MAX ); EXPECT_REALEQ( math_abs( REAL_MIN ), REAL_MIN ); EXPECT_REALEQ( math_abs( -REAL_MIN ), REAL_MIN ); EXPECT_REALZERO( math_mod( REAL_ZERO, REAL_ONE ) ); EXPECT_REALZERO( math_mod( REAL_ONE, REAL_ONE ) ); EXPECT_REALZERO( math_mod( REAL_MAX, REAL_ONE ) ); EXPECT_REALONE( math_mod( REAL_THREE, REAL_TWO ) ); EXPECT_REALONE( -math_mod( -REAL_THREE, -REAL_TWO ) ); EXPECT_EQ( math_floor( REAL_ZERO ), 0 ); EXPECT_EQ( math_floor( REAL_C( 0.999 ) ), 0 ); EXPECT_EQ( math_floor( REAL_C( -0.1 ) ), -1 ); EXPECT_EQ( math_floor( REAL_C( 42.5 ) ), 42 ); EXPECT_EQ( math_ceil( REAL_ZERO ), 0 ); EXPECT_EQ( math_ceil( REAL_C( 0.999 ) ), 1 ); EXPECT_EQ( math_ceil( REAL_C( -0.1 ) ), 0 ); EXPECT_EQ( math_ceil( REAL_C( 42.5 ) ), 43 ); EXPECT_EQ( math_ceil( REAL_C( 42.45 ) ), 43 ); EXPECT_EQ( math_floor64( REAL_ZERO ), 0 ); EXPECT_EQ( math_floor64( REAL_C( 0.999 ) ), 0 ); EXPECT_EQ( math_floor64( REAL_C( -0.1 ) ), -1 ); EXPECT_EQ( math_floor64( REAL_C( 42.5 ) ), 42 ); EXPECT_EQ( math_ceil64( REAL_ZERO ), 0 ); EXPECT_EQ( math_ceil64( REAL_C( 0.999 ) ), 1 ); EXPECT_EQ( math_ceil64( REAL_C( -0.1 ) ), 0 ); EXPECT_EQ( math_ceil64( REAL_C( 42.5 ) ), 43 ); EXPECT_EQ( math_ceil64( REAL_C( 42.45 ) ), 43 ); EXPECT_EQ( math_round( REAL_ZERO ), 0 ); EXPECT_EQ( math_round( REAL_C( 0.999 ) ), 1 ); EXPECT_EQ( math_round( REAL_C( -0.1 ) ), 0 ); EXPECT_EQ( math_round( REAL_C( 42.5 ) ), 43 ); EXPECT_EQ( math_round( REAL_C( 42.45 ) ), 42 ); EXPECT_EQ( math_trunc( REAL_ZERO ), 0 ); EXPECT_EQ( math_trunc( REAL_C( 0.999 ) ), 0 ); EXPECT_EQ( math_trunc( REAL_C( -0.1 ) ), 0 ); EXPECT_EQ( math_trunc( REAL_C( 42.5 ) ), 42 ); EXPECT_EQ( math_align_poweroftwo( 2 ), 2 ); EXPECT_EQ( math_align_poweroftwo( 3 ), 4 ); EXPECT_EQ( math_align_poweroftwo( 4 ), 4 ); EXPECT_EQ( math_align_poweroftwo( 33 ), 64 ); EXPECT_EQ( math_align_poweroftwo( 134217729 ), 268435456 ); for( i = 1; i < 31; ++i ) { EXPECT_TRUE( math_is_poweroftwo( math_align_poweroftwo( ( 2 << i ) - 1 ) ) ); EXPECT_TRUE( math_is_poweroftwo( math_align_poweroftwo( ( 2 << i ) ) ) ); EXPECT_TRUE( math_is_poweroftwo( math_align_poweroftwo( ( 2 << i ) + 1 ) ) ); EXPECT_FALSE( math_is_poweroftwo( ( 2 << i ) - 1 ) ); EXPECT_TRUE( math_is_poweroftwo( ( 2 << i ) ) ); EXPECT_FALSE( math_is_poweroftwo( ( 2 << i ) + 1 ) ); } EXPECT_EQ( math_align_up( 1, 1 ), 1 ); EXPECT_EQ( math_align_up( 1, 2 ), 2 ); EXPECT_EQ( math_align_up( 17, 2 ), 18 ); EXPECT_EQ( math_align_up( 43, 42 ), 84 ); EXPECT_REALZERO( math_smoothstep( REAL_ZERO ) ); EXPECT_REALONE( math_smoothstep( REAL_ONE ) ); EXPECT_REALEQ( math_smoothstep( REAL_HALF ), REAL_HALF ); EXPECT_REALZERO( math_smootherstep( REAL_ZERO ) ); EXPECT_REALONE( math_smootherstep( REAL_ONE ) ); EXPECT_REALEQ( math_smootherstep( REAL_HALF ), REAL_HALF ); EXPECT_REALZERO( math_lerp( REAL_ZERO, REAL_ZERO, REAL_ONE ) ); EXPECT_REALZERO( math_lerp( REAL_ONE, REAL_ONE, REAL_ZERO ) ); EXPECT_REALONE( math_lerp( REAL_ONE, REAL_ZERO, REAL_ONE ) ); EXPECT_REALONE( math_lerp( REAL_ZERO, REAL_ONE, REAL_ZERO ) ); EXPECT_REALEQ( math_lerp( REAL_HALF, REAL_ZERO, REAL_ONE ), REAL_HALF ); EXPECT_REALEQ( math_lerp( REAL_HALF, REAL_ZERO, REAL_ONE ), REAL_HALF ); EXPECT_REALZERO( math_unlerp( REAL_ZERO, REAL_ZERO, REAL_ONE ) ); EXPECT_REALZERO( math_unlerp( REAL_ONE, REAL_ONE, REAL_ZERO ) ); EXPECT_REALONE( math_unlerp( REAL_ONE, REAL_ZERO, REAL_ONE ) ); EXPECT_REALONE( math_unlerp( REAL_ZERO, REAL_ONE, REAL_ZERO ) ); EXPECT_REALEQ( math_unlerp( REAL_HALF, REAL_ZERO, REAL_ONE ), REAL_HALF ); EXPECT_REALEQ( math_unlerp( REAL_HALF, REAL_ZERO, REAL_ONE ), REAL_HALF ); EXPECT_REALONE( math_linear_remap( REAL_C( 150.0 ), REAL_C( 100.0 ), REAL_C( 200.0 ), REAL_ZERO, REAL_TWO ) ); EXPECT_REALZERO( math_clamp( -REAL_ONE, REAL_ZERO, REAL_ONE ) ); EXPECT_REALZERO( math_clamp( REAL_ZERO, REAL_ZERO, REAL_ONE ) ); EXPECT_REALEQ( math_clamp( REAL_HALF, REAL_ZERO, REAL_ONE ), REAL_HALF ); EXPECT_REALONE( math_clamp( REAL_ONE, REAL_ZERO, REAL_ONE ) ); EXPECT_REALONE( math_clamp( REAL_TWO, REAL_ZERO, REAL_ONE ) ); return 0; }