int main() { int n; short vref = *(short*)((void*)0x1FFFF7BA); REG_L(RCC_BASE, RCC_AHBENR) |= (1 << 17); // port A clock REG_L(RCC_BASE, RCC_AHB2ENR) |= (1 << 9); // ADC clock REG_L(GPIOA_BASE, GPIO_MODER) |= 1; uartEnable(); adcEnable(); REG_L(ADC_BASE, ADC_CHSELR) |= (1 << 5); // ADC from PA5 REG_L(ADC_BASE, ADC_CHSELR) |= (1 << 17); // ADC from vrefint while(1) { REG_L(GPIOA_BASE, GPIO_BSRR) |= (1 << 0); sends("on\n"); adcRead(2); sends("adc=0x"); sendHex(adc[0], 3); n = (intDiv(3300 * (int) adc[0], adc[1]) * vref) >> 12; sends(", V="); sendDec(n); sends("mv, T="); sendDec(intDiv(((n - 2980) + 5), 10) + 25); sends("\n"); n=250000; while(--n); REG_L(GPIOA_BASE, GPIO_BSRR) |= (1 << 16); sends("off\n"); n=1000000; while(--n); } }
void editorUpdate(GameState& game_state) { const InputButtons& input = game_state.input; editor_state.mouse_coords = game_state.camera.inverse_transform(game_state.input.mouse_pos); if (editor_state.mode == EditorState::MODE_TILES) { int scroll_speed = 4; if (input.held[InputButtons::EDITOR_SCROLL_LEFT]) { game_state.camera.pos.x -= scroll_speed; } if (input.held[InputButtons::EDITOR_SCROLL_RIGHT]) { game_state.camera.pos.x += scroll_speed; } if (input.held[InputButtons::EDITOR_SCROLL_UP]) { game_state.camera.pos.y -= scroll_speed; } if (input.held[InputButtons::EDITOR_SCROLL_DOWN]) { game_state.camera.pos.y += scroll_speed; } if (input.pressed[InputButtons::EDITOR_NEXT_LAYER]) { editor_state.current_layer = GameState::LevelLayer((editor_state.current_layer + 1) % GameState::LAYER_MAX); } BackgroundLayer& layer = game_state.level_layers[editor_state.current_layer]; if (input.pressed[InputButtons::EDITOR_CHOOSE_TILE]) { editor_state.saved_mouse_pos = game_state.input.mouse_pos; ivec2 new_mouse; intDiv(editor_state.current_tile_id, layer.tiles_per_row, new_mouse.y, new_mouse.x); new_mouse = new_mouse * layer.tile_size + (layer.tile_size / 2); game_state.input.warpMouse(new_mouse); editor_state.mode = EditorState::MODE_TILECHOOSER; } else if (input.pressed[InputButtons::EDITOR_PICK_TILE]) { editor_state.current_tile_id = layer.getTileAt(editor_state.mouse_coords); } else if (input.held[InputButtons::EDITOR_PLACE_TILE]) { ivec2 map_coord = layer.getTilePosAt(editor_state.mouse_coords); layer.map.set(map_coord.x, map_coord.y, editor_state.current_tile_id); } } else if (editor_state.mode == EditorState::MODE_TILECHOOSER) { const BackgroundLayer& layer = game_state.level_layers[editor_state.current_layer]; ivec2 selected_tile = game_state.input.mouse_pos / layer.tile_size; selected_tile.x = clamp(0, selected_tile.x, layer.tiles_per_row - 1); uint16_t new_tile_id = selected_tile.y * layer.tiles_per_row + selected_tile.x; if (new_tile_id >= layer.num_tiles) { new_tile_id = layer.num_tiles - 1; } editor_state.current_tile_id = new_tile_id; if (input.released[InputButtons::EDITOR_CHOOSE_TILE]) { game_state.input.warpMouse(editor_state.saved_mouse_pos); editor_state.mode = EditorState::MODE_TILES; } } }
void sendDec(int x) { static char s[10]; int i, x1; i = 0; while (x > 0) { x1 = intDiv(x, 10); s[i++] = x - x1 * 10; x = x1; } if (i == 0) { s[i++] = 0; } while (i > 0) { send('0' + s[--i]); } }
extern "C" Box* intDivmod(BoxedInt* lhs, Box* rhs) { if (!isSubclass(lhs->cls, int_cls)) raiseExcHelper(TypeError, "descriptor '__divmod__' requires a 'int' object but received a '%s'", getTypeName(lhs)); Box* divResult = intDiv(lhs, rhs); if (divResult == NotImplemented) { return NotImplemented; } Box* modResult = intMod(lhs, rhs); if (modResult == NotImplemented) { return NotImplemented; } Box* arg[2] = { divResult, modResult }; return createTuple(2, arg); }