static inline bool is_ws(int c) {
    return is_between(c, 1, 32);
}
Exemple #2
0
int ErrorControl::is_waiting_ack(char ecPlayerId, char frameId)
{
	// true if frameId is between send_head (inclusive) and send_tail (non-inclusive)
	return is_between( send_head, frameId, send_tail );
}
static inline bool is_digit(int c) {
    return is_between(c, '0', '9');
}
static inline bool is_lower(int c) {
    return is_between(c, 'a', 'z');
}
int interpolate_pc(struct pc_temp_ocv_lut *pc_temp_ocv,
				int batt_temp_degc, int ocv)
{
	int i, j, pcj, pcj_minus_one, pc;
	int rows = pc_temp_ocv->rows;
	int cols = pc_temp_ocv->cols;

	if (batt_temp_degc < pc_temp_ocv->temp[0]) {
		pr_debug("batt_temp %d < known temp range\n", batt_temp_degc);
		batt_temp_degc = pc_temp_ocv->temp[0];
	}

	if (batt_temp_degc > pc_temp_ocv->temp[cols - 1]) {
		pr_debug("batt_temp %d > known temp range\n", batt_temp_degc);
		batt_temp_degc = pc_temp_ocv->temp[cols - 1];
	}

	for (j = 0; j < cols; j++)
		if (batt_temp_degc <= pc_temp_ocv->temp[j])
			break;
	if (batt_temp_degc == pc_temp_ocv->temp[j]) {
		/* found an exact match for temp in the table */
		if (ocv >= pc_temp_ocv->ocv[0][j])
			return pc_temp_ocv->percent[0];
		if (ocv <= pc_temp_ocv->ocv[rows - 1][j])
			return pc_temp_ocv->percent[rows - 1];
		for (i = 0; i < rows; i++) {
			if (ocv >= pc_temp_ocv->ocv[i][j]) {
				if (ocv == pc_temp_ocv->ocv[i][j])
					return pc_temp_ocv->percent[i];
				pc = linear_interpolate(
					pc_temp_ocv->percent[i],
					pc_temp_ocv->ocv[i][j],
					pc_temp_ocv->percent[i - 1],
					pc_temp_ocv->ocv[i - 1][j],
					ocv);
				return pc;
			}
		}
	}

	/*
	 * batt_temp_degc is within temperature for
	 * column j-1 and j
	 */
	if (ocv >= pc_temp_ocv->ocv[0][j])
		return pc_temp_ocv->percent[0];
	if (ocv <= pc_temp_ocv->ocv[rows - 1][j - 1])
		return pc_temp_ocv->percent[rows - 1];

	pcj_minus_one = 0;
	pcj = 0;
	for (i = 0; i < rows-1; i++) {
		if (pcj == 0
			&& is_between(pc_temp_ocv->ocv[i][j],
				pc_temp_ocv->ocv[i+1][j], ocv)) {
			pcj = linear_interpolate(
				pc_temp_ocv->percent[i],
				pc_temp_ocv->ocv[i][j],
				pc_temp_ocv->percent[i + 1],
				pc_temp_ocv->ocv[i+1][j],
				ocv);
		}

		if (pcj_minus_one == 0
			&& is_between(pc_temp_ocv->ocv[i][j-1],
				pc_temp_ocv->ocv[i+1][j-1], ocv)) {
			pcj_minus_one = linear_interpolate(
				pc_temp_ocv->percent[i],
				pc_temp_ocv->ocv[i][j-1],
				pc_temp_ocv->percent[i + 1],
				pc_temp_ocv->ocv[i+1][j-1],
				ocv);
		}

		if (pcj && pcj_minus_one) {
			pc = linear_interpolate(
				pcj_minus_one,
				pc_temp_ocv->temp[j-1],
				pcj,
				pc_temp_ocv->temp[j],
				batt_temp_degc);
			return pc;
		}
	}

	if (pcj)
		return pcj;

	if (pcj_minus_one)
		return pcj_minus_one;

	pr_debug("%d ocv wasn't found for temp %d in the LUT returning 100%%\n",
							ocv, batt_temp_degc);
	return 100;
}
Exemple #6
0
void draw_grid()
{
	int i,j;
	long x0 = 0, y0 = 0, x = 0, y = 0, dx, popravka;
	DWORD text_color;
	//int lx, ly;

//	sprintf(debuf, "%U,%U", scroll_x, scroll_y);
//	rtlDebugOutString(debuf);

	nx=ny=0;

	// очистить область около выделенной ячейки
	if (sel_moved)
	{
		clear_cell_slow(sel_x, sel_y);
		clear_cell_slow(prev_x, prev_y);
	}
	else
	{
		// очистить всю область ячеек
		//kos_DrawBar(col_width[0]+1, row_height[0]+1, wi - SCROLL_BAR_WIDTH-col_width[0]-1, he - SCROLL_BAR_HEIGHT-row_height[0]-1, 0xffffff);
	}

	col_left[0] = 0;
	// ячейки - заголовки столбцов + вертикальные линии
	x = col_width[0]; 
	nx = 1;
	for (i = 1; i < col_count; i++)
	{
		col_left[i] = -1;
		if (i >= scroll_x)
		{
			{				
				if (!sel_moved || is_x_changed(i)) 
					kos_DrawLine(x-x0, 0, x-x0, row_height[0], GRID_COLOR, 0);
			// и заголовок ячейки по х
				text_color = TEXT_COLOR;
				dx = (col_width[i]-6)/2;
				int dy = (row_height[0] - 8) / 2 + 1;
				int cur_width = col_width[i] - 1;
				if (cur_width + x - x0 > wi - SCROLL_BAR_WIDTH)
					cur_width = wi - SCROLL_BAR_WIDTH - x + x0;
				if (!sel_moved || (is_x_changed(i))) 
					if (is_between(i,sel_x,sel_end_x))	
					{
						kos_DrawBar(x - x0 + 1,0,cur_width,row_height[0],SEL_FIXED_CELL_COLOR); //0x0000CC
						text_color = TEXT_SEL_FIXED_COLOR;
					}
					else
					{
						kos_DrawBar(x - x0 + 1,0,cur_width,row_height[0],FIXED_CELL_COLOR);
						text_color = TEXT_COLOR;
					}
				if (!sel_moved || (is_x_changed(i))) kos_WriteTextToWindow(x-x0+2+dx,dy,0,text_color,cells[i][0],strlen(cells[i][0]));

				// есть кнопка стоблца и еще кнопка изменения ширины 
				if (x - x0 + col_width[i] <= wi - col_width[0])
				{
					//kos_DeleteButton(COL_HEAD_BUTTON+i);
					kos_DefineButton(x-x0+5,0,cur_width - 10,row_height[0]-1,0x60000000+COL_HEAD_BUTTON+i,0);
				}
				//kos_DefineButton(x-x0+col_width[i]-10,0,15,row_height[0]-1,0x60000000+COL_SIZE_BUTTON+i,0);
				col_left[i] = x - x0;
			}
			if (x - x0 > wi - col_width[0])
			{
				x += col_width[i];
				nx++;
				break;
			}
		}
		else
		{
			x0 += col_width[i];
		}
		x += col_width[i];
		nx++;
	}

	//kos_DefineButton(0,0,0,0,0x80000000+COL_HEAD_BUTTON+i,0);

	for (j = i + 1; j < col_count; j++) 
		col_left[j] = wi;
	//if (!sel_moved || (is_x_changed(nx))) kos_DrawLine(x - x0, 0, x - x0, he, GRID_COLOR, 0);

	// ячейки - заголовки строк + горизонт. линии
	y = row_height[0];
	ny = 1;
	row_top[0] = 0;
	for (i = 1; i < row_count && y - y0 < he - 10; i++)
	{
		row_top[i] = -1;
		if (i >= scroll_y)
		{
			{
				if (!sel_moved || (is_y_changed(i))) 
					kos_DrawLine(0, y - y0, wi - SCROLL_BAR_WIDTH, y - y0, GRID_COLOR, 0);
				// и заголовок ячейки по y
				text_color = TEXT_COLOR;
				dx = (col_width[0]-6 * strlen(cells[0][i]))/2;	// optimize this, change strlen
				int dy = (row_height[i] - 8) / 2 + 1;
				if (!sel_moved || (is_y_changed(i)))
					if (is_between(i,sel_y,sel_end_y))
					{
						kos_DrawBar(0,y-y0+1,col_width[0],row_height[i] - 1,SEL_FIXED_CELL_COLOR);
						text_color = TEXT_SEL_FIXED_COLOR;
					}
					else
					{
						kos_DrawBar(0,y-y0+1,col_width[0],row_height[i] - 1,FIXED_CELL_COLOR);
						text_color = TEXT_COLOR;
					}

				if (!sel_moved || (is_y_changed(i)))
					kos_WriteTextToWindow(2+dx,y-y0+dy,0,text_color,cells[0][i],strlen(cells[0][i]));

				
				kos_DefineButton(0,y-y0+5,col_width[0]-1,row_height[i]-6,0x60000000+ROW_HEAD_BUTTON+i,0);
				//kos_DefineButton(0,y-y0+row_height[i]-5,col_width[0]-1,10,0x60000000+ROW_SIZE_BUTTON+i,0);
				row_top[i] = y - y0;
			}
		}
		else
		{
			y0 += row_height[i];
		}
		y += row_height[i];
		ny++;
	}
	
	kos_DefineButton(0,0,0,0,0x80000000+ROW_HEAD_BUTTON+ny-1,0);

	for (j = i + 1; j < row_count; j++)
		row_top[j] = he;
	if (!sel_moved || (is_y_changed(ny))) 
		kos_DrawLine(0, y - y0, wi - SCROLL_BAR_WIDTH, y - y0, GRID_COLOR, 0);

	if (!sel_moved || (is_x_changed(0) && is_y_changed(0)))
		kos_DrawBar(0,0,col_width[0],row_height[0],FIXED_CELL_COLOR); 
	// ЛВ ячейка

	//sprintf(debuf, "%U, %U; %U, %U", x0, y0, nx, ny);
	//rtlDebugOutString(debuf);

//	popravka = (y - y0 < he - 10);
	//sprintf(debuf, "%U, %U", scroll_y, ny);
	//rtlDebugOutString(debuf);

	
	// сами ячейки

	y = row_height[0];
	for (i = scroll_y; i < ny; i++)
	{
		x = col_width[0];
		if (!sel_moved)
			kos_DrawBar(col_width[0]+1, y+1, wi - SCROLL_BAR_WIDTH-col_width[0]-1, row_height[i]-1, 0xffffff);
		for (j = scroll_x; j < nx-1; j++)
		{
			if (!sel_moved || is_x_changed(j) || is_y_changed(i)) 
				kos_DrawLine(col_left[j], row_top[i], col_left[j], row_height[i], GRID_COLOR, 0);

			// заголовки уже нарисованы - пропускаем их
			if (i && j)	
			{
				//kos_DrawBar(x+1, y+1, col_width[i]-1, row_height[i]-1, 0xffffff);

				//rtlDebugOutString(cap);
				//if (j >= sel_x && j <= sel_end_x && i >= sel_y && i <= sel_end_y)
				if (is_between(j,sel_x,sel_end_x) && is_between(i, sel_y, sel_end_y)	// (j,i) - выделена
					&& ((!sel_moved) || (is_x_changed(j) && is_y_changed(i))))			// и ее нужно нарисовать
				{
					if (i == sel_y && j == sel_x)		// рамка
					{
						kos_DrawBar(x,y,col_width[j],2,TEXT_COLOR);	// up
						kos_DrawBar(x,y,2,row_height[i],TEXT_COLOR);	// left
						kos_DrawBar(x,y+row_height[i]-2,col_width[j]-2-3,2,TEXT_COLOR);				// bottom
						kos_DrawBar(x+col_width[j]-2,y, 2,row_height[i]-2-3,TEXT_COLOR);				// right

						kos_DrawBar(x+col_width[j]-4,y+row_height[i]-4,4,4,TEXT_COLOR);
						//kos_DefineButton(x+col_width[j]-2,y+row_height[i]-2,4,4,0x60000000+DRAG_BUTTON,0x000000);
						drag_x = x + col_width[j] - 4;
						drag_y = y + row_height[i] - 4;
					}
					else
						kos_DrawBar(x + 1,y + 1,col_width[j] - 2,row_height[i] - 2,SEL_CELL_COLOR);	//	выделена но не основная(серая)

				}
				//kos_DefineButton(x,y,col_width[j]-1,row_height[i]-1,0x60000000+CELL_BUTTON+((i << 8) + j),0);

				char *text;
				if (values[j][i] && values[j][i][0] == '#')
				{
					text = cells[j][i];
					kos_DrawRegion(x+1, y+1, col_width[j]-1, row_height[i]-1, 0xff0000, 0);
				}
				else
					text = (values[j][i] && !display_formulas ? values[j][i] : cells[j][i]);

				int dy = (row_height[i] - 8) / 2 + 1;

				if (text)
					if (strlen(text) < col_width[j]/6)
						kos_WriteTextToWindow(x+2,y+dy,0,text_color,text,strlen(text));
					else
						kos_WriteTextToWindow(x+2,y+dy,0,text_color,text,col_width[j]/6);

			}
			if (!sel_moved || is_x_changed(j) || is_y_changed(i))  
				kos_DrawLine(col_left[j]+col_width[j], row_top[i], col_left[j]+col_width[j], row_height[i], GRID_COLOR, 0);
			x += col_width[j];
		} 
		y += row_height[i];
	}

	// Scrolls: 
	// horizontal

	//if (!sel_moved) kos_DrawBar(0, he - SCROLL_BAR_HEIGHT, wi - SCROLL_BAR_WIDTH, SCROLL_BAR_HEIGHT, FIXED_CELL_COLOR);
	//if (!sel_moved) kos_DrawBar(scroll_x * wi / col_count, he - SCROLL_BAR_HEIGHT, wi / col_count, SCROLL_BAR_HEIGHT, SEL_FIXED_CELL_COLOR);
	if (!sel_moved)
	{
		// горизонталь
		kos_DrawBar(17, he - SCROLL_BAR_HEIGHT, wi - SCROLL_BAR_WIDTH - 32, SCROLL_BAR_HEIGHT, 0xced0d0);
		// синие линии
		kos_DrawRegion(0, he - SCROLL_BAR_HEIGHT, wi - SCROLL_BAR_WIDTH, SCROLL_BAR_HEIGHT+1, 0x94aece, 0);
		// левая кнопка
		draw_custom_button(1, he - SCROLL_BAR_HEIGHT + 1, 14, 14, 1);
		kos_WriteTextToWindow(6, he - SCROLL_BAR_HEIGHT + 5, 0, 0, "\x1B", 1);
		// правая
		draw_custom_button(wi - SCROLL_BAR_WIDTH * 2 + 1, he - SCROLL_BAR_HEIGHT + 1, 14, 14, 1);
		kos_WriteTextToWindow(wi - SCROLL_BAR_WIDTH * 2 + 6, he - SCROLL_BAR_HEIGHT + 5, 0, 0, "\x1A", 1);
		// ползунок
		int tmp_w = (nx - scroll_x) * (wi - SCROLL_BAR_WIDTH - 2 * 14 - 14) / (col_count + 1);
		if (tmp_w < 16)
			tmp_w = 16;
		draw_custom_button(17 + (scroll_x - 1) * (wi - SCROLL_BAR_WIDTH - 2 * 14 - 14) / (col_count + 1), he - SCROLL_BAR_HEIGHT + 1, 
			tmp_w, 14, 1);

#define sw(x,y) y,x
// не пинайте меня за это, было лень переставлять руками...

		// вертикаль
		kos_DrawBar(sw(17, wi - SCROLL_BAR_WIDTH), sw(he - SCROLL_BAR_HEIGHT - 33, SCROLL_BAR_WIDTH), 0xced0d0);
		// синие линии
		kos_DrawRegion(sw(0, wi - SCROLL_BAR_WIDTH), sw(he - SCROLL_BAR_HEIGHT, SCROLL_BAR_WIDTH+1), 0x94aece, 0); // up

		// верхняя кнопка
		draw_custom_button(sw(1, wi - SCROLL_BAR_WIDTH + 1), 14, 14, 1);
		kos_WriteTextToWindow(sw(5, wi - SCROLL_BAR_WIDTH + 6), 0, 0, "\x18", 1);
		// нижняя
		draw_custom_button(sw(he - SCROLL_BAR_HEIGHT * 2 + 1, wi - SCROLL_BAR_WIDTH + 1), 14, 14, 1);
		//draw_custom_button(sw(he - SCROLL_BAR_HEIGHT * 2 + 1, wi - SCROLL_BAR_WIDTH + 1), 14, 14, 1);
		kos_WriteTextToWindow(sw(he - SCROLL_BAR_HEIGHT * 2 + 5, wi - SCROLL_BAR_WIDTH + 6), 0, 0, "\x19", 1);
		// ползунок
		int tmp_h = (ny - scroll_y) * (he - SCROLL_BAR_HEIGHT - 2 * 14 - 14) / (row_count + 1);
		if (tmp_h < 16)
			tmp_h = 16;
		draw_custom_button(sw(17 + (scroll_y - 1) * (he - SCROLL_BAR_HEIGHT - 2 * 14 - 14) / (row_count + 1), wi - SCROLL_BAR_WIDTH + 1), 
			sw(tmp_h, 14), 1);
	}
#define NO_DRAW 0x60000000
	kos_DefineButton(1, he - SCROLL_BAR_HEIGHT + 1, 14, 14, NO_DRAW + SCROLL_LEFT_BUTTON,0);
	kos_DefineButton(wi - SCROLL_BAR_WIDTH * 2 + 2, he - SCROLL_BAR_HEIGHT + 1, 14, 14, NO_DRAW + SCROLL_RIGHT_BUTTON,0);
	kos_DefineButton(17, he - SCROLL_BAR_HEIGHT + 1,  (wi - SCROLL_BAR_WIDTH - 2 * 14), 14, NO_DRAW + SCROLL_WIDTH,0);

	kos_DefineButton(sw(1, wi - SCROLL_BAR_WIDTH + 1), 14, 14, NO_DRAW + SCROLL_UP_BUTTON,0);
	kos_DefineButton(sw(he - SCROLL_BAR_HEIGHT * 2 + 2, wi - SCROLL_BAR_WIDTH + 1), 14, 14, NO_DRAW + SCROLL_DOWN_BUTTON,0);
	kos_DefineButton(sw(17, wi - SCROLL_BAR_WIDTH + 1),  sw((he - SCROLL_BAR_HEIGHT - 2 * 14), 14), NO_DRAW + SCROLL_HEIGHT,0);

}
Exemple #7
0
#include "util/dates.hpp" // Include the date and time funcitons
#include "util/collision.hpp" // Include the collision checking functions
#include "util/sound.hpp" // Include the sound effect functions
#include "util/messagebox.hpp" // Include the message box functions
#include "util/files.hpp" // Include the file handling functions
#include "util/network.hpp" // Include the networking functions

#ifndef NDEBUG // Allow the NDEBUG to disable debug support at compile time

#define DOCTEST_CONFIG_IMPLEMENT
#include "doctest.h" // Include the required unit testing library which is a git submodule in lib/doctest

// Real number function assertions
TEST_CASE("real/random") {
	REQUIRE(random(20) < (unsigned int)20);
	REQUIRE(is_between((int)random_range(20, 40), 20, 40));
	REQUIRE(random_set_seed(5) == (unsigned int)5);
	REQUIRE(random_reset_seed() != (unsigned int)DEFAULT_RANDOM_SEED);
	REQUIRE(randomize() != (unsigned int)DEFAULT_RANDOM_SEED);
}
TEST_CASE("real/math") {
	REQUIRE(sign(5) == 1);
	REQUIRE(sign(0) == 0);
	REQUIRE(sign(-5) == -1);
	REQUIRE(sign(5.0) == 1);
	REQUIRE(sign(0.0) == 0);
	REQUIRE(sign(-5.0) == -1);
	REQUIRE(sqr(5) == 25);
	REQUIRE(sqr(5.0) == 25.0);
	REQUIRE(logn(5.0, 1.0) == 0.0);
	REQUIRE(logn(5.0, 5.0) == 1.0);
void is_between_test_cases(void)
{
 checkit_int(is_between(4, 3, 6), 1);
 checkit_int(is_between(5, 1, 6), 1);
}
void patch::_mousePressed(ofMouseEventArgs &e) {
    
    ofVec3f mouse_transformed = ofVec3f(e.x, e.y, 0.0)*this->getGlobalTransformMatrix();
    
    if (bEditMode){
        
        // check is mouse is pressing over the inspector
        if (inspector != NULL and inspector->isVisible() and inspector->isHit(mouse_transformed.x, mouse_transformed.y)) {
            gui->setOtherSelected(true);
            return;
        }
    }
    
    ofxPatch::_mousePressed(e);
    
    // Is over link dot ?
    if (bEditMode){
        
        bool overDot = false;
        for (int i = 0; i < outPut.size() and !overDot; i++){
            
            for (int j = 0; j < outPut[i].path_coorners.size(); j++){
                
                if ( ofDist(mouse_transformed.x, mouse_transformed.y, outPut[i].path_coorners[j].x, outPut[i].path_coorners[j].y) <= 10 ){
                    if ((e.button == 2) || (glutGetModifiers() == GLUT_ACTIVE_CTRL)) {
                        outPut[i].path_coorners.erase(outPut[i].path_coorners.begin()+j);
                    }
                    else {
                        selectedLinkPath = j;
                        selectedLink = i;
                    }
                    overDot = true;
                    ofxPatch::setLinkHit(true);
                }
            }
            
            if (!overDot and linkType == PATH_LINKS and outPut.size() > 0){
                vector<ofPoint> link_vertices = outPut[i].path_line.getVertices();
                
                if (link_vertices.size()){
                    int addNew = -1;
                    int tolerance = 3;
                    
                    for (int j = 0; j < link_vertices.size()-1; j++){
                        int next = (j+1)%link_vertices.size();
                        
                        if (is_between (mouse_transformed.x, link_vertices[j].x, link_vertices[j+1].x, tolerance) && is_between (mouse_transformed.y, link_vertices[j].y, link_vertices[j+1].y, tolerance))
                        {
                            if ((link_vertices[j+1].y - link_vertices[j].y) <= tolerance) // Horizontal line.
                            {
                                addNew = j;
                            }
                            
                            const float M = (link_vertices[j+1].y - link_vertices[j].y) / (link_vertices[j+1].x - link_vertices[j].x); // Slope
                            const float C = -(M * link_vertices[j].x) + link_vertices[j].y; // Y intercept
                            
                            // Checking if (x, y) is on the line passing through the end points.
                            if(std::fabs (mouse_transformed.y - (M * mouse_transformed.x + C)) <= tolerance) {
                                addNew = j;
                            }
                        }
                    }
    
                    if (addNew >= 0) {
                        ofxPatch::setLinkHit(true);
                        if (outPut[i].path_coorners.size() == 0)
                            outPut[i].path_coorners.push_back(ofVec3f(mouse_transformed.x, mouse_transformed.y, 0.0));
                        else if (addNew == 0)
                            outPut[i].path_coorners.insert(outPut[i].path_coorners.begin(), ofVec3f(mouse_transformed.x, mouse_transformed.y, 0.0));
                        else
                            outPut[i].path_coorners.insert(outPut[i].path_coorners.begin()+addNew, ofVec3f(mouse_transformed.x, mouse_transformed.y, 0.0));
                    }
                }
            }
        }
    }
}