1

Dependencies:   mbed

Committer:
shaorui
Date:
Mon Jan 25 08:36:48 2021 +0000
Revision:
0:571a1835428e
1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shaorui 0:571a1835428e 1 #include "PreferenceWriter.h"
shaorui 0:571a1835428e 2 #include "FlashWriter.h"
shaorui 0:571a1835428e 3 #include "mbed.h"
shaorui 0:571a1835428e 4 #include "user_config.h"
shaorui 0:571a1835428e 5 PreferenceWriter::PreferenceWriter(uint32_t sector) {
shaorui 0:571a1835428e 6 writer = new FlashWriter(sector);
shaorui 0:571a1835428e 7 __sector = sector;
shaorui 0:571a1835428e 8 __ready = false;
shaorui 0:571a1835428e 9 }
shaorui 0:571a1835428e 10
shaorui 0:571a1835428e 11 void PreferenceWriter::open() {
shaorui 0:571a1835428e 12 writer->open();
shaorui 0:571a1835428e 13 __ready = true;
shaorui 0:571a1835428e 14 }
shaorui 0:571a1835428e 15
shaorui 0:571a1835428e 16 bool PreferenceWriter::ready() {
shaorui 0:571a1835428e 17 return __ready;
shaorui 0:571a1835428e 18 }
shaorui 0:571a1835428e 19
shaorui 0:571a1835428e 20 void PreferenceWriter::write(int x, int index) {
shaorui 0:571a1835428e 21 __int_reg[index] = x;
shaorui 0:571a1835428e 22 }
shaorui 0:571a1835428e 23
shaorui 0:571a1835428e 24 void PreferenceWriter::write(float x, int index) {
shaorui 0:571a1835428e 25 __float_reg[index] = x;
shaorui 0:571a1835428e 26 }
shaorui 0:571a1835428e 27
shaorui 0:571a1835428e 28 void PreferenceWriter::flush() {
shaorui 0:571a1835428e 29 int offs;
shaorui 0:571a1835428e 30 for (offs = 0; offs < 256; offs++) {
shaorui 0:571a1835428e 31 writer->write(offs, __int_reg[offs]);
shaorui 0:571a1835428e 32 }
shaorui 0:571a1835428e 33 for (; offs < 320; offs++) {
shaorui 0:571a1835428e 34 writer->write(offs, __float_reg[offs - 256]);
shaorui 0:571a1835428e 35 }
shaorui 0:571a1835428e 36 __ready = false;
shaorui 0:571a1835428e 37 }
shaorui 0:571a1835428e 38
shaorui 0:571a1835428e 39 void PreferenceWriter::load() {
shaorui 0:571a1835428e 40 int offs;
shaorui 0:571a1835428e 41 for (offs = 0; offs < 256; offs++) {
shaorui 0:571a1835428e 42 __int_reg[offs] = flashReadInt(__sector, offs);
shaorui 0:571a1835428e 43 }
shaorui 0:571a1835428e 44 for(; offs < 320; offs++) {
shaorui 0:571a1835428e 45 __float_reg[offs - 256] = flashReadFloat(__sector, offs);
shaorui 0:571a1835428e 46 }
shaorui 0:571a1835428e 47 }
shaorui 0:571a1835428e 48
shaorui 0:571a1835428e 49 void PreferenceWriter::close() {
shaorui 0:571a1835428e 50 __ready = false;
shaorui 0:571a1835428e 51 writer->close();
shaorui 0:571a1835428e 52 }