Skip to content

Commit e8e30ca

Browse files
author
Adam Ford
committed
Add caching to getRestaurant
1 parent f1e41e3 commit e8e30ca

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

restaurant.cpp

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
#include <SD.h>
2-
#include "Restaurant.h"
2+
#include "restaurant.h"
33

44
#define BLOCK_LEN 512
55
#define RESTAURANT_START_BLOCK 4000000
66

7+
uint32_t cachedBlockNumber;
8+
Restaurant buffer[BLOCK_LEN/sizeof(Restaurant)];
9+
710
void getRestaurant(int i, Restaurant *r, Sd2Card *card) {
8-
Restaurant buffer[BLOCK_LEN/sizeof(Restaurant)];
9-
uint32_t block = RESTAURANT_START_BLOCK + i/8;
11+
uint32_t blockNumber = RESTAURANT_START_BLOCK + i/8;
12+
13+
if(blockNumber != cachedBlockNumber) {
14+
card->readBlock(blockNumber, (uint8_t *) buffer);
15+
cachedBlockNumber = blockNumber;
16+
}
17+
1018
uint32_t index = i % 8;
1119

12-
card->readBlock(block, (uint8_t *) buffer);
1320
(*r) = buffer[index];
1421
}
22+

0 commit comments

Comments
 (0)